Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions src/me/raynes/fs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,28 @@
:dynamic true}
*cwd* (.getCanonicalFile (io/file ".")))

(let [homedir (io/file (System/getProperty "user.home"))
usersdir (.getParent homedir)]
(defn home
"With no arguments, returns the current value of the `user.home` system
property. If a `user` is passed, returns that user's home directory. It
is naively assumed to be a directory with the same name as the `user`
located relative to the parent of the current value of `user.home`."
([] homedir)
([user] (if (empty? user) homedir (io/file usersdir user)))))
(def ^{:doc "Regular expression to match illegal characters in a username.
Used to prevent shell exploits, as bash is used to get users
home directory"}
invalid-username-regexp #"[^A-Za-z0-9\-\._]+")

(defn home
"With no arguments, returns the current value of the `user.home`
system property. If a `user` is passed, returns that user's home
directory. If user is not present on the system, returns nil. If
shell call fails (it will on windows), returns nil."
([] (-> "user.home" System/getProperty io/file))
([username]
(when-not
(re-find invalid-username-regexp username)
(let [{:keys [out exit]}
(->> username
(str "echo ~")
(sh/sh "sh" "-c"))]
(when (and
(zero? exit)
(not= "~" (subs out 0 1)))
(-> out string/trim io/file))))))

(defn expand-home
"If `path` begins with a tilde (`~`), expand the tilde to the value
Expand Down