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.
FileBlocking 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
sizebytes 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-idis pulled in automatically (imports x/platform/syscall);syscalland (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:
STRING — File path to openANY — Open mode – a number (e.g. 577), one symbol from (File file-modes) (e.g. ‘rdonly), or a list of symbols OR’d together (e.g. (list ‘wronly ‘creat ‘trunc))ANY — Permission bits for a newly created file when the mode includes creat; default 0644. Ignored when the file is not created.Returns: INT — File descriptor, or negative on error
(File close fd)Close a file descriptor.
Parameters:
INT — File descriptor to closeReturns: INT — 0 on success, negative on error
(File read fd buffer size)Read bytes from a file descriptor into a buffer.
Parameters:
INT — File descriptor to read fromSTRING — Buffer to read intoINT — Maximum bytes to readReturns: INT — Bytes read, 0 at EOF, negative on error
(File write fd buffer size)Write bytes from a buffer to a file descriptor.
Parameters:
INT — File descriptor to write toSTRING — Data to writeINT — Number of bytes to writeReturns: INT — Bytes written, or negative on error
(File getc fd)Read a single character from a file descriptor.
Parameters:
INT — File descriptor to read fromReturns: 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:
ANY — Seek origin – symbol or numberReturns: 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:
INT — File descriptorINT — Byte offset, interpreted per whenceANY — Origin – ‘set (absolute, the default), ‘cur (relative to the current offset), ‘end (relative to end of file); or the numeric 0/1/2Returns: 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:
INT — File descriptorReturns: 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:
INT — File descriptor, opened writableINT — New size in bytes; default the current offsetReturns: INT — 0 on success, negative on error
(File %stat-decode buf)Decode a stat64/stat buffer into the public metadata alist.
Parameters:
STRING — A stat buffer a syscall filledReturns: 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:
STRING — Path to statReturns: 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:
STRING — Path to testReturns: 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:
STRING — File to readReturns: 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:
STRING — File to write (created/truncated)STRING — ContentsReturns: 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:
STRING — File to readReturns: 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:
STRING — Directory to listReturns: LIST — Entry-name strings
(File mkdir path . perm)Create a directory (default mode 0755). Raises a tag ‘io Err on failure; returns nil.
Parameters:
STRING — Directory to createINT — Permission bits; default 0755Returns: ANY — nil
(File unlink path)Remove a file (not a directory – see rmdir). Raises a tag ‘io Err on failure; returns nil.
Parameters:
STRING — File to removeReturns: ANY — nil
(File rmdir path)Remove an empty directory. Raises a tag ‘io Err on failure; returns nil.
Parameters:
STRING — Empty directory to removeReturns: ANY — nil
(File rename from to)Rename/move a filesystem entry. Raises a tag ‘io Err on failure; returns nil.
Parameters:
STRING — Existing pathSTRING — New pathReturns: ANY — nil
(File chmod path mode)Set a path’s permission bits (chmod). Raises a tag ‘io Err on failure; returns nil.
Parameters:
STRING — Path whose mode to setINT — Permission bits, e.g. 420 for 0644Returns: 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:
STRING — Path whose owner to setINT — Owning user id, or -1 to leave itINT — Owning group id, or -1 to leave itReturns: ANY — nil
(File link target path)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:
STRING — Existing pathSTRING — New name for itReturns: ANY — nil
(File symlink target path)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:
STRING — What the link should point atSTRING — The link to createReturns: ANY — nil
(File readlink path)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:
STRING — Symbolic link to readReturns: 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:
STRING — Path to stampReturns: 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:
STRING — FIFO to createINT — Permission bits; default 0644Returns: 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:
STRING — Any path on the filesystem to measureReturns: 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:
STRING — Path to stat, symlinks NOT followedReturns: 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:
STRING — Source fileSTRING — Destination (created/truncated, mode 0644)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:
STRING — Path prefix for the new file; default “/tmp/x-“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:
STRING — Directory to walkReturns: LIST — Relative path strings