x-lang

← Index

x/sys/file

File I/O via POSIX syscalls, homed on the File class.

Imports x/platform/syscall for syscall-id; read buffers come from the (str make) core primitive, so File runs under plain x-core. Call (File file-modes) / (File stat-flags) for the symbolic flag tables.

Class File

Blocking file I/O over raw POSIX syscalls (open/close/read/write/seek).

Lifecycle: (File open path mode) -> a file descriptor; thread it through (File read)/(File write)/(File getc)/(File seek); (File close fd) when done.

Return values are the raw syscall results: a negative number is an error (-errno). (File read) returns the byte count, 0 at EOF; (File getc) returns -1 at EOF.

read/write/getc operate on a caller-allocated string buffer – allocate one with (str make N), fetched via (prim-ref ‘str ‘make): read fills it and returns how many bytes landed; write sends size bytes out of it.

(File open)’s mode is flexible: a number passes straight through; a single symbol (rdonly, wronly, …) resolves via (File file-modes); a list of symbols is OR’d together – (list ‘wronly ‘creat ‘trunc) is 577. Call (File file-modes) for the full table, or (File stat-flags) for the stat S_* flags.

syscall-id is pulled in automatically (imports x/platform/syscall); syscall and (str make) are core primitives, so File runs under plain x-core.

(File file-modes)

The file open-mode table: an alist mapping each symbolic O_* flag name to its numeric Linux value. Use a key as (File open)’s mode argument; OR numeric values together for combined flags.

Returns: LIST — Alist of (symbol value) for: accmode rdonly wronly rdwr creat excl noctty trunc append nonblock dsync fasync direct largefile directory nofollow noatime cloexec sync path

(File stat-flags)

The stat mode-flag table: an alist mapping each symbolic S_* name to its numeric Linux value, for decoding a stat result’s st_mode (the ifmt bits select the file type; the rest are permission and set-id/sticky bits).

Returns: LIST — Alist of (symbol value) for: ifmt ifdir ifchr ifblk ifreg ififo iflnk ifsock isuid isgid isvtx iread iwrite iexec

(File open pathname mode . perm)

Open a file, returning a file descriptor.

Parameters:

Returns: INT — File descriptor, or negative on error

(File close fd)

Close a file descriptor.

Parameters:

Returns: INT — 0 on success, negative on error

(File read fd buffer size)

Read bytes from a file descriptor into a buffer.

Parameters:

Returns: INT — Bytes read, 0 at EOF, negative on error

(File write fd buffer size)

Write bytes from a buffer to a file descriptor.

Parameters:

Returns: INT — Bytes written, or negative on error

(File getc fd)

Read a single character from a file descriptor.

Parameters:

Returns: CHAR — Character read, or -1 at EOF

(File %whence whence)

Resolve a seek origin to its POSIX SEEK_* value (identical across Linux/macOS): ‘set -> 0, ‘cur -> 1, ‘end -> 2; a number passes through. Raises a tag ‘type Err on anything else – an unknown origin must not reach the kernel.

Parameters:

Returns: INT — 0, 1, 2, or the number given

(File seek fd offset . whence)

Reposition a file descriptor’s read/write offset (lseek). Seeking past end of file is allowed; a later write there leaves a hole that reads back as zero bytes.

Parameters:

Returns: INT — The new offset from the start of the file, or negative on error

(File tell fd)

The file descriptor’s current offset – (File seek fd 0 ‘cur).

Parameters:

Returns: INT — Current offset from the start of the file, or negative on error

(File truncate fd . size)

Truncate (or extend) the open file to size bytes (ftruncate). The offset does not move – seek explicitly if it now lies past the end.

Parameters:

Returns: INT — 0 on success, negative on error

(File %stat-decode buf)

Decode a stat64/stat buffer into the public metadata alist.

Parameters:

Returns: ALIST — ((size . N) (mode . M) (kind . K) (mtime . T))

(File stat path)

File metadata as an alist: ((size . BYTES) (mode . RAW) (kind . SYM) (mtime . UNIX-SECONDS)). kind is one of ‘file ‘dir ‘link ‘char ‘block ‘fifo ‘socket (from the S_IFMT bits). Raises a tag ‘io Err on failure.

Parameters:

Returns: ALIST — ((size . N) (mode . M) (kind . K) (mtime . T))

(File exists? path)

Does path name an existing filesystem entry? (Any kind – file, directory, link target…)

Deliberately duplicated across tiers with (Sys file-exists?) (#361): that access(2) door is what boot/module.x can reach before this module loads. Post-boot file work belongs here.

Parameters:

Returns: BOOL — True when stat succeeds

(File read-all path)

The whole file as one string (stat for the size, one read). Raises a tag ‘io Err on open/read failure.

Parameters:

Returns: STRING — The file’s bytes

(File write-all path s)

Write s as the entire contents of path (create or truncate, mode 0644). Raises a tag ‘io Err on failure; returns the byte count written.

Parameters:

Returns: INT — Bytes written

(File read-lines path)

The file as a list of lines (split on newline; a trailing final newline yields no empty last line).

Parameters:

Returns: LIST — List of line strings

(File list-dir path)

The directory’s entry names as a list of strings, ‘.’ and ‘..’ excluded. Per-OS dirent decoding over getdents64 (Linux) / getdirentries64 (Darwin). Raises a tag ‘io Err on failure.

Parameters:

Returns: LIST — Entry-name strings

(File mkdir path . perm)

Create a directory (default mode 0755). Raises a tag ‘io Err on failure; returns nil.

Parameters:

Returns: ANY — nil

Remove a file (not a directory – see rmdir). Raises a tag ‘io Err on failure; returns nil.

Parameters:

Returns: ANY — nil

(File rmdir path)

Remove an empty directory. Raises a tag ‘io Err on failure; returns nil.

Parameters:

Returns: ANY — nil

(File rename from to)

Rename/move a filesystem entry. Raises a tag ‘io Err on failure; returns nil.

Parameters:

Returns: ANY — nil

(File chmod path mode)

Set a path’s permission bits (chmod). Raises a tag ‘io Err on failure; returns nil.

Parameters:

Returns: ANY — nil

(File chown path uid gid)

Set a path’s owning user and group (chown). Either id may be -1 to leave that half alone. Raises a tag ‘io Err on failure; returns nil.

Parameters:

Returns: ANY — nil

Create a hard link: a second directory entry for the SAME inode, so both names share the file’s bytes and it survives until the last one goes. Raises a tag ‘io Err on failure; returns nil.

Parameters:

Returns: ANY — nil

Create a symbolic link at path pointing at target. The target is stored as WRITTEN and is never resolved here, so it need not exist. Raises a tag ‘io Err on failure; returns nil.

Parameters:

Returns: ANY — nil

The text a symbolic link holds, exactly as it was written – relative targets come back relative. Raises a tag ‘io Err when the path is not a link.

Parameters:

Returns: STRING — The link’s target

(File utimes path)

Set a path’s access and modification times to the current clock (utimes with a null times pointer) – what touch(1) means for a file that already exists. Raises a tag ‘io Err on failure; returns nil.

Explicit timestamps would want a packed pair of timevals, and this module holds no pointer prims to build one; the clock is the door.

Parameters:

Returns: ANY — nil

(File mkfifo path . perm)

Create a named pipe (mknod with the S_IFIFO bit). Raises a tag ‘io Err on failure; returns nil.

Parameters:

Returns: ANY — nil

(File statfs path)

The filesystem holding path, as an alist: ((bsize . BYTES) (blocks . N) (bfree . N) (bavail . N) (files . N) (ffree . N)). Multiply a block count by bsize for bytes. Raises a tag ‘io Err on failure.

Parameters:

Returns: ALIST — ((bsize . B) (blocks . N) (bfree . N) (bavail . N) (files . N) (ffree . N))

(File lstat path)

File metadata like (File stat), but a symbolic link reports itself (kind ‘link) instead of its target – the door (File walk) uses to avoid following link cycles. Raises a tag ‘io Err on failure.

Parameters:

Returns: ALIST — ((size . N) (mode . M) (kind . K) (mtime . T))

(File copy from to)

Copy a file’s bytes, binary-safe: a 64KB fd-level read/write loop driven by the raw byte counts, never by string length (a string’s observable bytes end at the first NUL, so read-all->write-all corrupts binary). Raises a tag ‘io Err on failure; returns the byte count copied.

Parameters:

Returns: INT — Bytes copied

(File temp . prefix)

Create a fresh temporary file that did not exist before the call: O_CREAT O_EXCL O_RDWR, mode 0600, name = prefix + random suffix (bytes from /dev/urandom), retrying on collision. Returns (fd . path); closing and unlinking are the caller’s.

Parameters:

Returns: PAIR — (open-fd . path)

(File walk path)

Every non-directory entry under path, recursively, as paths RELATIVE to path (files, links, sockets, fifos alike – filter on (File lstat) kind for finer policy). Recursion decisions ride lstat, so a symlinked directory is REPORTED as its link, never followed (no cycle risk). Order follows the directory tables; treat it as unspecified. Raises a tag ‘io Err on failure.

Parameters:

Returns: LIST — Relative path strings