tur/io
FileSystem capability implementation using libc functions.
Since: Phase 16
file-size
(file-size [f])
return the byte size of an already-open file.
| f | a FILE* cast to int64_t (as returned by fopen). |
The file size in bytes as an int.
(file-size my-file-ptr) ; => 1024
Real-FileSystem
(Real-FileSystem)
create a FileSystem capability backed by libc.
A heap-allocated FileSystem struct whose function pointers use fopen/fread/fwrite/remove/opendir/readdir for all operations. Free with Real-FileSystem-free when done.
(let [fs (Real-FileSystem)] ...)
Since: Phase 16
Real-FileSystem-free
(Real-FileSystem-free [fs])
release a FileSystem capability created by Real-FileSystem.
| fs | the FileSystem pointer returned by Real-FileSystem. |
Since: Phase 16
read-file
(read-file [path])
read an entire file into a newly allocated buffer.
| path | the filesystem path to open (:cstr). |
A malloc'd buffer containing the file contents with a '\0' terminator, or NULL on any error. The caller is responsible for calling free.
(let [buf (read-file "/etc/hostname")] ...)
Since: Phase 16
write-file
(write-file [path data len])
write a byte buffer to a file, overwriting any existing content.
| path | the destination filesystem path (:cstr). | |
| data | pointer to the data to write. | |
| len | number of bytes to write. |
0 on success, -1 on error.
(write-file "/tmp/out.bin" buf 256) ; => 0
Since: Phase 16
file-exists?
(file-exists? [path])
test whether a file is accessible for reading.
| path | the filesystem path to check (:cstr). |
1 if the file can be opened for reading, 0 otherwise.
(file-exists? "/etc/hosts") ; => 1
Since: Phase 16
list-dir
(list-dir [path])
list all non-hidden entries in a directory.
| path | the directory path to list (:cstr). |
A NULL-terminated array of strdup'd entry name strings, or NULL on error. The caller must free each string and then the array itself (or use free-dir-listing).
(let [entries (list-dir "/tmp")] ...)
Since: Phase 16
free-dir-listing
(free-dir-listing [entries])
free a directory listing returned by list-dir.
| entries | the NULL-terminated string array returned by list-dir. |
Since: Phase 16
FileHandle
(defstruct FileHandle :linear [ptr :ptr<void>])
a linear (exactly-once) wrapper around a FILE* pointer.
Since: LT4
file-open
(file-open [path mode] :FileHandle)
open a file and return a move-only FileHandle.
| path | filesystem path to open (:cstr). | |
| mode | fopen mode string, e.g. "rb" or "wb" (:cstr). |
A FileHandle wrapping the opened FILE*, or a FileHandle with a NULL ptr on error. Check with file-handle-ok? before using.
(let [fh (file-open "/etc/hostname" "rb")] ...)
Since: LT4
file-handle-ok?
(file-handle-ok? [fh : FileHandle] :bool)
return true if the FileHandle is valid (non-NULL).
| fh | a FileHandle (by value). |
true if the underlying FILE* is non-NULL, false on open failure.
(file-handle-ok? (file-open "/etc/hostname" "rb")) ; => true
Since: LT4
file-read
(file-read [fh : FileHandle buf len] :int)
read up to len bytes from fh into buf.
| fh | an open FileHandle (by value; does not consume the handle). | |
| buf | destination buffer as ptr<void>. | |
| len | maximum number of bytes to read. |
Number of bytes actually read (int).
(file-read fh buf 1024) ; => 512
Since: LT4
file-close
(file-close [fh : FileHandle] :int)
close a FileHandle and release the underlying FILE*.
| fh | a FileHandle to close (by value; handle is consumed). |
0 on success, non-zero on error (mirrors fclose return value).
(file-close fh) ; => 0
Since: LT4