tur/fs
extended file-system operations (stat, readdir, fnmatch, walk).
Since: Phase B1
StatInfo
(StatInfo)
TmpFile
(TmpFile)
fs/stat
(fs/stat [path : cstr])
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 : StatInfo] :)
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 : StatInfo] :)
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 : StatInfo] :)
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 : StatInfo] :)
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])
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])
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])
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])
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])
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])
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])
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])
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])
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])
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] :)
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)
create a unique temporary file via mkstemp(3).
A linear TmpFile handle wrapping a pair { path-cstr, fd }. The path is heap-allocated; the fd is the open descriptor. Release exactly once with fs/tmpfile-free. Returns 0 on mkstemp failure. Under -Xlinear the checker requires the handle be consumed exactly once (dropping it is TUR-E0100; freeing it twice is TUR-E0101).
(let [t (fs/tmpfile)] (fs/tmpfile-free t))
Since: Phase B2
fs/tmpfile-path
(fs/tmpfile-path [^borrow t : TmpFile] :)
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 [^borrow t : TmpFile] :)
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 : TmpFile])
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])
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])
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