Skip to content

Commit

Permalink
Fix #88: bbin ls with 0-length files doesn't crash (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude authored Sep 5, 2024
1 parent 3852708 commit 186c793
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- [Fix #88: NPE when using `bbin ls` in dirs with zero-length files](https://github.com/babashka/bbin/issues/88)

## 0.2.3

- [Fix error in compiled script when installing from Homebrew (again)](https://github.com/babashka/bbin/commit/f0a3096a1e57408af77eed35f86a3d71cccccb07)
Expand Down
13 changes: 7 additions & 6 deletions src/babashka/bbin/scripts.clj
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@
edn/read-string)))

(defn- read-header [filename]
(with-open [input-stream (io/input-stream filename)]
(let [buffer (byte-array (* 1024 5))
n (.read input-stream buffer)]
(when (nat-int? n)
(String. buffer 0 n)))))
(or (with-open [input-stream (io/input-stream filename)]
(let [buffer (byte-array (* 1024 5))
n (.read input-stream buffer)]
(when (nat-int? n)
(String. buffer 0 n))))
""))

(defn load-scripts [dir]
(->> (file-seq dir)
(filter #(.isFile %))
(map (fn [x] [(symbol (str (fs/relativize dir x)))
(parse-script (read-header x))]))
(-> (read-header x) (parse-script ))]))
(filter second)
(into {})))

Expand Down

0 comments on commit 186c793

Please sign in to comment.