tur/io
FileSystem capability implementation using libc functions.
Since: Phase 16
DirListing
(DirListing)
FileSystem
(FileSystem)
FileStream
(FileStream)
file-stream-open
(file-stream-open [path : cstr mode : cstr] :)
open a file as a non-linear FileStream handle.
| path | filesystem path to open (:cstr). | |
| mode | fopen mode string, e.g. "rb" or "wb" (:cstr). |
A FileStream wrapping the opened FILE*, or a FileStream wrapping NULL on error. Check with file-stream-ok? before using.
(let [s (file-stream-open "/etc/hostname" "rb")] (file-size s))
Since: I/O sweep
file-stream-ok?
(file-stream-ok? [f : FileStream] :)
return true if the FileStream wraps a real FILE*.
| f | a FileStream. |
true if the underlying FILE* is non-NULL, false on open failure.
(file-stream-ok? (file-stream-open "/etc/hostname" "rb")) ; => true
Since: I/O sweep
file-stream-close
(file-stream-close [f : FileStream] :)
close a FileStream and release the underlying FILE*.
| f | a FileStream to close. |
0 on success, non-zero on error (mirrors fclose).
(file-stream-close s) ; => 0
Since: I/O sweep
file-size
(file-size [f : FileStream] :)
return the byte size of an already-open FileStream.
| f | a FileStream (from file-stream-open). |
The file size in bytes as an int.
(file-size my-stream) ; => 1024
Real-FileSystem
(Real-FileSystem :)
Real-FileSystem-free
(Real-FileSystem-free [fs : FileSystem] :)
release a FileSystem capability created by Real-FileSystem.
| fs | the FileSystem pointer returned by Real-FileSystem. |
Since: Phase 16
read-file
(read-file [path : cstr] :)
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 : cstr data ptr<void> len : int] :)
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 : cstr] :)
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 : cstr] :)
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 : DirListing] :)
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 : cstr mode : cstr] :)
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? [^borrow fh : FileHandle] :)
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 [^borrow fh : FileHandle buf ptr<void> 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] :)
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