Skip to content

Commit

Permalink
Better fix for copy
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Mar 15, 2021
1 parent b187460 commit d0394a1
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/babashka/fs.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,27 @@
[this other]
(.relativize (as-path this) (as-path other)))

(defn file-name
"Returns the name of the file or directory. E.g. (file-name \"foo/bar/baz\") returns \"baz\"."
[x]
(.getName (as-file x)))

(defn normalize
"Normalizes f via Path#normalize."
[f]
(.normalize (as-path f)))

(defn ^Path canonicalize
"Returns the canonical path via
java.io.File#getCanonicalPath. If :nofollow-links is set, then it
will fall back on absolutize + normalize. This function can be used
as an alternative to real-path which requires files to exist."
([f] (canonicalize f nil))
([f {:keys [:nofollow-links]}]
(if nofollow-links
(-> f absolutize normalize)
(as-path (.getCanonicalPath (as-file f))))))

(defn file-name
"Returns the name of the file or directory. E.g. (file-name \"foo/bar/baz\") returns \"baz\"."
[x]
(.getName (as-file x)))

(def ^:private continue (constantly :continue))

(defn walk-file-tree
Expand Down Expand Up @@ -353,13 +364,12 @@
([src dest] (copy-tree src dest nil))
([src dest {:keys [:replace-existing
:copy-attributes
:nofollow-links
:posix-file-permissions]}]
(create-dirs dest {:posix-file-permissions posix-file-permissions})
:nofollow-links]}]
(let [copy-options (->copy-opts replace-existing copy-attributes false nofollow-links)
link-options (->link-opts nofollow-links)
from (real-path src {:nofollow-links nofollow-links})
to (real-path dest {:nofollow-links nofollow-links})]
;; using canonicalize here because real-path requires the path to exist
to (canonicalize dest {:nofollow-links nofollow-links})]
(walk-file-tree from {:pre-visit-dir (fn [dir _attrs]
(let [rel (relativize from dir)
to-dir (path to rel)]
Expand All @@ -374,7 +384,8 @@
(Files/copy ^Path from-path to-file
^"[Ljava.nio.file.CopyOption;"
copy-options)
:continue))}))))
:continue)
:continue)}))))

(defn temp-dir
"Returns java.io.tmpdir property as path."
Expand Down

0 comments on commit d0394a1

Please sign in to comment.