No matching definitions.

tur/io

stdlib/io.tur

FileSystem capability implementation using libc functions.

Since: Phase 16

defopaque

DirListing

(DirListing)
defopaque

FileSystem

(FileSystem)
defopaque

FileStream

(FileStream)
defn

file-stream-open

(file-stream-open [path : cstr mode : cstr] :)

open a file as a non-linear FileStream handle.

pathfilesystem path to open (:cstr).
modefopen 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

defn

file-stream-ok?

(file-stream-ok? [f : FileStream] :)

return true if the FileStream wraps a real FILE*.

fa 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

defn

file-stream-close

(file-stream-close [f : FileStream] :)

close a FileStream and release the underlying FILE*.

fa FileStream to close.

0 on success, non-zero on error (mirrors fclose).

(file-stream-close s)  ; => 0

Since: I/O sweep

defn

file-size

(file-size [f : FileStream] :)

return the byte size of an already-open FileStream.

fa FileStream (from file-stream-open).

The file size in bytes as an int.

(file-size my-stream)  ; => 1024
defn

Real-FileSystem

(Real-FileSystem :)
defn

Real-FileSystem-free

(Real-FileSystem-free [fs : FileSystem] :)

release a FileSystem capability created by Real-FileSystem.

fsthe FileSystem pointer returned by Real-FileSystem.

Since: Phase 16

defn

read-file

(read-file [path : cstr] :)

read an entire file into a newly allocated buffer.

paththe 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

defn

write-file

(write-file [path : cstr data ptr<void> len : int] :)

write a byte buffer to a file, overwriting any existing content.

paththe destination filesystem path (:cstr).
datapointer to the data to write.
lennumber of bytes to write.

0 on success, -1 on error.

(write-file "/tmp/out.bin" buf 256)  ; => 0

Since: Phase 16

defn

file-exists?

(file-exists? [path : cstr] :)

test whether a file is accessible for reading.

paththe filesystem path to check (:cstr).

1 if the file can be opened for reading, 0 otherwise.

(file-exists? "/etc/hosts")  ; => 1

Since: Phase 16

defn

list-dir

(list-dir [path : cstr] :)

list all non-hidden entries in a directory.

paththe 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

defn

free-dir-listing

(free-dir-listing [entries : DirListing] :)

free a directory listing returned by list-dir.

entriesthe NULL-terminated string array returned by list-dir.

Since: Phase 16

defstruct linear

FileHandle

(defstruct FileHandle :linear [ptr ptr<void>])

a linear (exactly-once) wrapper around a FILE* pointer.

Since: LT4

defn

file-open

(file-open [path : cstr mode : cstr] :)

open a file and return a move-only FileHandle.

pathfilesystem path to open (:cstr).
modefopen 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

defn

file-handle-ok?

(file-handle-ok? [^borrow fh : FileHandle] :)

return true if the FileHandle is valid (non-NULL).

fha 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

defn

file-read

(file-read [^borrow fh : FileHandle buf ptr<void> len : int] :)

read up to len bytes from fh into buf.

fhan open FileHandle (by value; does not consume the handle).
bufdestination buffer as ptr<void>.
lenmaximum number of bytes to read.

Number of bytes actually read (int).

(file-read fh buf 1024)  ; => 512

Since: LT4

defn

file-close

(file-close [fh : FileHandle] :)

close a FileHandle and release the underlying FILE*.

fha 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