me.raynes.fs
File system utilities in Clojure
*cwd*
dynamic
Current working directory. This cannot be changed in the JVM. Changing this will only change the working directory for functions in this library.
base-name
(base-name path)
(base-name path trim-ext)
Return the base name (final segment/file part) of a path
.
If optional trim-ext
is a string and the path
ends with that string, it is trimmed.
If trim-ext
is true, any extension is trimmed.
child-of?
(child-of? p c)
Takes two paths and checks to see if the first path is a parent of the second.
chmod
(chmod mode path)
Change file permissions. Returns path.
mode
can be a permissions string in octal or symbolic format. Symbolic: any combination of r
(readable) w
(writable) and x
(executable). It should be prefixed with +
to set or -
to unset. And optional prefix of u
causes the permissions to be set for the owner only. Octal: a string of three octal digits representing user, group, and world permissions. The three bits of each digit signify read, write, and execute permissions (in order of significance). Note that group and world permissions must be equal.
Examples:
(chmod "+x" "/tmp/foo") ; Sets executable for everyone
(chmod "u-wx" "/tmp/foo") ; Unsets owner write and executable
copy-dir
(copy-dir from to)
Copy a directory from from
to to
. If to
already exists, copy the directory to a directory with the same name as from
within the to
directory.
copy-dir-into
(copy-dir-into from to)
Copy directory into another directory if destination already exists.
delete-dir
(delete-dir root & link-options)
Delete a directory tree. Optional link-options may be provided to determine whether or not to follow symbolic links.
directory?
(directory? path & link-options)
Return true if path
is a directory, false otherwise. Optional link-options may be provided to determine whether or not to follow symbolic links.
ephemeral-dir
(ephemeral-dir prefix)
(ephemeral-dir prefix suffix)
(ephemeral-dir prefix suffix tries)
Create an ephemeral directory (will be deleted on JVM exit). Returns nil if dir could not be created even after n tries (default 10).
ephemeral-file
(ephemeral-file prefix)
(ephemeral-file prefix suffix)
(ephemeral-file prefix suffix tries)
Create an ephemeral file (will be deleted on JVM exit). Returns nil if file could not be created even after n tries (default 10).
expand-home
(expand-home path)
If path
begins with a tilde (~
), expand the tilde to the value of the user.home
system property. If the path
begins with a tilde immediately followed by some characters, they are assumed to be a username. This is expanded to the path to that user’s home directory. This is (naively) assumed to be a directory with the same name as the user relative to the parent of the current value of user.home
.
file
(file path & paths)
If path
is a period, replaces it with cwd and creates a new File object out of it and paths
. Or, if the resulting File object does not constitute an absolute path, makes it absolutely by creating a new File object out of the paths
and cwd.
home
(home)
(home user)
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
.
iterate-dir
(iterate-dir path)
Return a sequence [root dirs files]
, starting from path
in depth-first order
link
(link new-file existing-file)
Create a “hard” link from path to target. Requires Java version 7 or greater. The arguments are in the opposite order from the link(2) system call.
move
(move source target & copy-options)
Move or rename a file to a target file. Requires Java version 7 or greater. Optional copy-options may be provided.
ns-path
(ns-path n)
Takes a namespace symbol and creates a path to it. Replaces hyphens with underscores. Assumes the path should be relative to cwd.
path-ns
(path-ns path)
Takes a path
to a Clojure file and constructs a namespace symbol out of the path.
read-sym-link
(read-sym-link path)
Return the target of a ‘soft’ link. Requires Java version 7 or greater.
sym-link
(sym-link path target)
Create a “soft” link from path
to target
. Requires Java version 7 or greater.
temp-dir
(temp-dir prefix)
(temp-dir prefix suffix)
(temp-dir prefix suffix tries)
Create a temporary directory. Returns nil if dir could not be created even after n tries (default 10).
temp-file
(temp-file prefix)
(temp-file prefix suffix)
(temp-file prefix suffix tries)
Create a temporary file. Returns nil if file could not be created even after n tries (default 10).
temp-name
(temp-name prefix)
(temp-name prefix suffix)
tmpdir
(tmpdir)
The temporary file directory looked up via the java.io.tmpdir
system property. Does not create a temporary directory.
walk
(walk func path)
Lazily walk depth-first over the directory structure starting at path
calling func
with three arguments [root dirs files]
. Returns a sequence of the results.
with-mutable-cwd
macro
(with-mutable-cwd & body)
Execute the body
in a binding with *cwd*
bound to *cwd*
. This allows you to change *cwd*
with set!
.