tur/fs
extended file-system operations (stat, readdir, fnmatch, walk).
Since: Phase B1
fs/stat
(fs/stat [path :cstr] :int)
return file metadata for a path.
| path | NUL-terminated path cstr |
An opaque pointer (as :int) to a { size mtime mode } triple, or 0 if the path is inaccessible. Inspect with fs/stat-size, fs/stat-mtime, fs/stat-mode. Free with fs/stat-free.
(let [s (fs/stat "/etc/hosts")] (fs/stat-size s))
Since: Phase B2
fs/stat-free
(fs/stat-free [s :int] :void)
free the stat result returned by fs/stat.
| s | stat pointer from fs/stat (may be 0) |
(fs/stat-free s)
Since: Phase B2
fs/stat-size
(fs/stat-size [s :int] :int)
extract file size from a stat result.
| s | stat pointer from fs/stat |
File size in bytes as :int.
(fs/stat-size (fs/stat "/etc/hosts")) ; => 221
Since: Phase B2
fs/stat-mtime
(fs/stat-mtime [s :int] :int)
extract modification time from a stat result.
| s | stat pointer from fs/stat |
Modification time as a Unix timestamp (:int).
(fs/stat-mtime (fs/stat "/etc/hosts"))
Since: Phase B2
fs/stat-mode
(fs/stat-mode [s :int] :int)
extract file mode bits from a stat result.
| s | stat pointer from fs/stat |
File mode bits as :int (same as st_mode from stat(2)).
(fs/stat-mode (fs/stat "/etc/hosts")) ; => 33188
Since: Phase B2
fs/exists?
(fs/exists? [path :cstr] :bool)
test whether a path exists.
| path | NUL-terminated path cstr |
true if stat(2) succeeds on the path, false otherwise.
(fs/exists? "/etc/hosts") ; => true
Since: Phase B2
fs/file?
(fs/file? [path :cstr] :bool)
test whether a path is a regular file.
| path | NUL-terminated path cstr |
true if path is a regular file.
(fs/file? "/etc/hosts") ; => true
Since: Phase B2
fs/dir?
(fs/dir? [path :cstr] :bool)
test whether a path is a directory.
| path | NUL-terminated path cstr |
true if path is a directory.
(fs/dir? "/tmp") ; => true
Since: Phase B2
fs/mkdir
(fs/mkdir [path :cstr] :int)
create a directory with mode 0755.
| path | NUL-terminated path cstr |
0 on success, -1 on error (errno set).
(fs/mkdir "/tmp/mydir") ; => 0
Since: Phase B2
fs/mkdirp
(fs/mkdirp [path :cstr] :int)
create a directory and all missing parent directories.
| path | NUL-terminated path cstr |
0 on success, -1 if any mkdir call fails for a reason other than the directory already existing.
(fs/mkdirp "/tmp/a/b/c") ; => 0
Since: Phase B2
fs/rmdir
(fs/rmdir [path :cstr] :int)
remove an empty directory.
| path | NUL-terminated path cstr |
0 on success, -1 on error.
(fs/rmdir "/tmp/mydir") ; => 0
Since: Phase B2
fs/rm
(fs/rm [path :cstr] :int)
remove a regular file.
| path | NUL-terminated path cstr |
0 on success, -1 on error.
(fs/rm "/tmp/tmpfile.txt") ; => 0
Since: Phase B2
fs/rename
(fs/rename [src :cstr dst :cstr] :int)
rename or move a file.
| src | source path cstr | |
| dst | destination path cstr |
0 on success, -1 on error.
(fs/rename "/tmp/a.txt" "/tmp/b.txt") ; => 0
Since: Phase B2
fs/copy
(fs/copy [src :cstr dst :cstr] :int)
copy a file from src to dst using a read/write loop.
| src | source path cstr | |
| dst | destination path cstr |
0 on success, -1 on error.
(fs/copy "/etc/hosts" "/tmp/hosts.bak") ; => 0
Since: Phase B2
fs/glob
(fs/glob [dir :cstr pattern :cstr] :int)
list directory entries matching a fnmatch(3) pattern.
| dir | directory to search (e.g., "/tmp") | |
| pattern | fnmatch pattern applied to entry names (e.g., "*.txt") |
A cons list of heap-allocated full-path cstrs for matching entries. Returns 0 (empty list) if the directory cannot be opened. Free with fs/glob-free.
(fs/glob "/tmp" "*.txt") ; => cons list of paths
Since: Phase B2
fs/glob-free
(fs/glob-free [lst :int] :void)
free a cons list of paths returned by fs/glob.
| lst | cons list returned by fs/glob |
(fs/glob-free my-list)
Since: Phase B2
fs/tmpfile
(fs/tmpfile :int)
create a unique temporary file via mkstemp(3).
An opaque pointer (as :int) to a pair { path-cstr, fd }. The path is heap-allocated; the fd is the open descriptor. Free with fs/tmpfile-free. Returns 0 on mkstemp failure.
(let [t (fs/tmpfile)] (fs/tmpfile-path t))
Since: Phase B2
fs/tmpfile-path
(fs/tmpfile-path [t :int] :cstr)
extract the path from an fs/tmpfile pair.
| t | tmpfile pair from fs/tmpfile |
The path cstr (owned by the pair; do not free separately).
(fs/tmpfile-path my-tmp) ; => "/tmp/tur_aB3xYz"
Since: Phase B2
fs/tmpfile-fd
(fs/tmpfile-fd [t :int] :int)
extract the file descriptor from an fs/tmpfile pair.
| t | tmpfile pair from fs/tmpfile |
The open file descriptor as :int.
(fs/tmpfile-fd my-tmp) ; => 5
Since: Phase B2
fs/tmpfile-free
(fs/tmpfile-free [t :int] :void)
close the fd and free the tmpfile pair.
| t | tmpfile pair from fs/tmpfile (may be 0) |
(fs/tmpfile-free my-tmp)
Since: Phase B2
fs/read-text
(fs/read-text [path :cstr] :cstr)
read an entire file into a NUL-terminated heap buffer.
| path | NUL-terminated path cstr |
A heap-allocated NUL-terminated cstr of the file contents, or NULL on error. The caller must free the returned buffer.
(fs/read-text "/etc/hostname") ; => "myhost\n"
Since: Phase B2
fs/write-text
(fs/write-text [path :cstr content :cstr] :int)
write a NUL-terminated string to a file.
| path | NUL-terminated destination path cstr | |
| content | NUL-terminated string to write |
0 on success, -1 on error.
(fs/write-text "/tmp/out.txt" "hello\n") ; => 0
Since: Phase B2