libstd/file
Files and file descriptors: open/close, stat, read the whole file, duplicate descriptors, and the flag and mode constants of the Linux ABI. Includes stdio, process, mem and time.
#include <libstd/file>
Includes libstd/stdio, libstd/process, libstd/mem, libstd/time. Same functions and constant names on Linux and Windows: the system-specific part is in wrapper/linux/sysfile and wrapper/windows/sysfile (see Linux and Windows). Links the shared library kernel32.dll, KernelBase.dll (FFI).
Example
#include <libstd/stdio>
#include <libstd/file>
text = readfile("./notes.txt") // the whole file as a string
print(text)
fd = open("/proc/self/status", "r")
status = read_all(fd) // everything up to the end, any kind of fd
close(fd)
fd = open("./out.txt", "w")
fprint(fd, "hello\n")
close(fd)
st = new Stat()
if sys_stat("./out.txt", st) == 0:
print("size: ", st.st_size, "\n")
Constants
| Name | Linux | Windows | Meaning |
|---|---|---|---|
O_CREAT | 0o100 | 0o100 | ottale = 64 |
O_TRUNC | 0o1000 | 0o1000 | ottale = 512 |
O_EXCL | 0o200 | 0o200 | con O_CREAT: fallisce se il file esiste |
O_NOCTTY | 0o400 | 0o400 | non diventare il terminale di controllo |
O_APPEND | 0o2000 | 0o2000 | ottale = 1024 |
O_RDONLY | 0 | 0 | sola lettura |
O_WRONLY | 1 | 1 | sola scrittura |
O_RDWR | 2 | 2 | lettura e scrittura |
O_ACCMODE | 3 | 3 | mask per i 3 sopra |
CLASSIS | 420 | 420 | lettura / scrittura file di testo classici |
O_OWNER | 384 | 384 | solo owner |
CLASSIC | 420 | 420 | lettura / scrittura file di testo classici |
BINARY | 493 | 493 | eseguibile pubblico |
S_IXUSR | 64 | 64 | execute by owner |
S_IWUSR | 128 | 128 | write by owner |
S_IRUSR | 256 | 256 | read by owner |
S_IXGRP | 8 | 8 | execute by group |
S_IWGRP | 16 | 16 | write by group |
S_IRGRP | 32 | 32 | read by group |
S_IXOTH | 1 | 1 | execute by others |
S_IWOTH | 2 | 2 | write by others |
S_IROTH | 4 | 4 | read by others |
S_IFIFO | 4096 | 4096 | pipe |
S_IFCHR | 8192 | 8192 | dispositivo a caratteri |
S_IFDIR | 16384 | 16384 | directory |
S_IFREG | 32768 | 32768 | file normale |
S_IFMT | 61440 | 61440 | mask per i tipi sopra |
O_NONBLOCK | 2048 | 2048 | I/O non bloccante |
O_DSYNC | 4096 | 4096 | scritture sincrone (data) |
FASYNC | 8192 | 8192 | notifica asincrona (obsolete) |
O_DIRECT | 65536 | 16384 | |
O_LARGEFILE | 131072 | 32768 | |
O_DIRECTORY | 16384 | 65536 | |
O_NOFOLLOW | 32768 | 131072 | |
O_CLOEXEC | 524288 | 524288 | close-on-exec (FD_CLOEXEC) |
SHUT_RD | 0 | 0 | |
SHUT_WR | 1 | 1 | |
SHUT_RDWR | 2 | 2 | |
F_OK | 0 | 0 | |
R_OK | 4 | 4 | |
W_OK | 2 | 2 | |
FIONREAD | 21531 | 21531 |
Classes
The struct stat of the x86-64 Linux ABI, field by field, filled by sys_stat/sys_fstat.
st_dev long
st_ino long
st_nlink long
st_mode int
st_uid int
st_gid int
pad0 int
st_rdev long
st_size long
st_blksize long
st_blocks long
st_atime long
st_atime_nsec long
st_mtime long
st_mtime_nsec long
st_ctime long
st_ctime_nsec long
reserved0 long
reserved1 long
reserved2 longFunctions
Size in bytes of an open file, via fstat.
1 if path is a directory that exists, 0 otherwise (a missing path, a file, ""), like Python's os.path.isdir. Symbolic links are followed: a link to a directory counts as a directory. It uses stat on Linux and the file attributes on Windows.
Reads a whole file into a new string, through try_read_all: files in /proc and /sys, which fstat reports as empty or with a made-up size, are read in full. Exits with an error if the file cannot be opened or read.
Reads everything from an open file descriptor up to the end and returns it as a string; the buffer is allocated for you. After a read error it returns what it read up to that point; use try_read_all to know whether that happened.
err, data = try_read_all(fd): like read_all, with err 0 or the negative error of sys_read (for example -9 for a closed descriptor). For a regular file the size comes from fstat and exactly that much is read, with no extra call. For a pipe, a socket or a /proc file the buffer starts at READ_ALL_CHUNK (16 KiB), doubles as needed and is shrunk at the end if more than a quarter is unused. The length is kept in the string header, so binary content with zero bytes comes back whole and strlen is O(1). On a non-blocking socket it stops at EAGAIN, with that error.
Opens a file with a C-style mode string: "r" read, "w" write (create/truncate), "a" append. Returns the descriptor or a negative value.
Closes a descriptor only if it is valid (≥ 0); safe on a failed open.
Bytes available for reading right now on a descriptor (e.g. data already arrived on a socket), via FIONREAD.
Linux only.
(no description yet)Linux only.
(no description yet)Linux only.
(no description yet)Raw ioctl(2) with an integer argument passed by address (e.g. FIONREAD).
Raw access(2): 0 if the file can be accessed with mode (X_OK etc.), negative otherwise.
Raw open(2) with numeric flags (O_RDONLY, O_WRONLY | O_APPEND, ...) and mode. Returns a descriptor or a negative errno.
Raw close(2). On Windows it also closes a socket, with closesocket.
Fills a Stat for a path. Returns 0 on success.
Fills a Stat for an open descriptor. Returns 0 on success.
Linux only.
Raw shutdown(2) on a socket (SHUT_RD/SHUT_WR/SHUT_RDWR).
Reads count bytes at offset without moving the file position.
Duplicates a descriptor (dup(2)); both share offset and flags.
Duplicates onto a chosen descriptor number, closing it first if open. On Windows the number can only be 0, 1 or 2 (stdin, stdout, stderr): a descriptor there is a handle, whose value cannot be chosen, and any other number gives -EINVAL.
Like sys_dup2 with flags (O_CLOEXEC), atomically; on Windows with the same limit.