Skip to content

Commit

Permalink
Fix #97: add owner function (#99)
Browse files Browse the repository at this point in the history
Co-authored-by: Emil Aasa <[email protected]>
  • Loading branch information
borkdude and emilaasa authored May 24, 2023
1 parent 867977a commit f75af13
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ For a list of breaking changes, check [here](#breaking-changes).

Babashka [fs](https://github.com/babashka/fs): file system utility library for Clojure

## v??
- [#97](https://github.com/babashka/fs/issues/97): add `file-owner` function

## v0.4.18 (2023-05-11)

- [#48](https://github.com/babashka/fs/issues/48): support input-stream in `fs/copy`
Expand Down
31 changes: 21 additions & 10 deletions src/babashka/fs.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@
(^Path [f {:keys [:nofollow-links]}]
(.toRealPath (as-path f) (->link-opts nofollow-links))))

(defn owner
"Returns the owner of a file. Call `str` on it to get the owner name
as a string."
([f] (owner f nil))
([f {:keys [:nofollow-links]}]
(Files/getOwner (as-path f) (->link-opts nofollow-links))))

;;;; Predicates

(defn regular-file?
Expand Down Expand Up @@ -462,20 +469,21 @@
"Creates a temporary directory using Files#createDirectories.
`(create-temp-dir)`: creates temp dir with random prefix.
`(create-temp-dir {:keys [:prefix :path :posix-file-permissions]})`:
`(create-temp-dir {:keys [:prefix :dir :posix-file-permissions]})`:
create temp dir in path with prefix. If prefix is not provided, a random one
create temp dir in dir path with prefix. If prefix is not provided, a random one
is generated. If path is not provided, the directory is created as if called with `(create-temp-dir)`. The `:posix-file-permissions` option is a string like `\"rwx------\"`."
([]
(Files/createTempDirectory
(str (java.util.UUID/randomUUID))
(make-array FileAttribute 0)))
([{:keys [:prefix :path :posix-file-permissions]}]
([{:keys [:prefix :dir :posix-file-permissions] :as opts}]
(let [attrs (posix->attrs posix-file-permissions)
prefix (or prefix (str (java.util.UUID/randomUUID)))]
(if path
prefix (or prefix (str (java.util.UUID/randomUUID)))
dir (or dir (:path opts))]
(if dir
(Files/createTempDirectory
(as-path path)
(as-path dir)
prefix
attrs)
(Files/createTempDirectory
Expand All @@ -495,13 +503,16 @@
(str (java.util.UUID/randomUUID))
(str (java.util.UUID/randomUUID))
(make-array FileAttribute 0)))
([{:keys [:path :prefix :suffix :posix-file-permissions]}]
([{:keys [:dir :prefix :suffix :posix-file-permissions] :as opts}]
(let [attrs (posix->attrs posix-file-permissions)
prefix (or prefix (str (java.util.UUID/randomUUID)))
suffix (or suffix (str (java.util.UUID/randomUUID)))]
(if path
suffix (or suffix (str (java.util.UUID/randomUUID)))
dir (or dir
;; backwards compat
(:path opts))]
(if dir
(Files/createTempFile
(as-path path)
(as-path dir)
prefix
suffix
attrs)
Expand Down
7 changes: 7 additions & 0 deletions test/babashka/fs_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -709,3 +709,10 @@
(fs/xdg-state-home)))
(is (= (fs/path default-home "clj-kondo")
(fs/xdg-state-home "clj-kondo")))))

(deftest file-owner-test
(testing "works for files as well"
(let [dir (doto (fs/create-temp-dir)
fs/delete-on-exit)
file-in-dir (fs/create-temp-file {:dir dir})]
(is (= (str (fs/owner dir)) (str (fs/owner file-in-dir)))))))

0 comments on commit f75af13

Please sign in to comment.