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

NameLinuxWindowsMeaning
O_CREAT0o1000o100ottale = 64
O_TRUNC0o10000o1000ottale = 512
O_EXCL0o2000o200con O_CREAT: fallisce se il file esiste
O_NOCTTY0o4000o400non diventare il terminale di controllo
O_APPEND0o20000o2000ottale = 1024
O_RDONLY00sola lettura
O_WRONLY11sola scrittura
O_RDWR22lettura e scrittura
O_ACCMODE33mask per i 3 sopra
CLASSIS420420lettura / scrittura file di testo classici
O_OWNER384384solo owner
CLASSIC420420lettura / scrittura file di testo classici
BINARY493493eseguibile pubblico
S_IXUSR6464execute by owner
S_IWUSR128128write by owner
S_IRUSR256256read by owner
S_IXGRP88execute by group
S_IWGRP1616write by group
S_IRGRP3232read by group
S_IXOTH11execute by others
S_IWOTH22write by others
S_IROTH44read by others
S_IFIFO40964096pipe
S_IFCHR81928192dispositivo a caratteri
S_IFDIR1638416384directory
S_IFREG3276832768file normale
S_IFMT6144061440mask per i tipi sopra
O_NONBLOCK20482048I/O non bloccante
O_DSYNC40964096scritture sincrone (data)
FASYNC81928192notifica asincrona (obsolete)
O_DIRECT6553616384
O_LARGEFILE13107232768
O_DIRECTORY1638465536
O_NOFOLLOW32768131072
O_CLOEXEC524288524288close-on-exec (FD_CLOEXEC)
SHUT_RD00
SHUT_WR11
SHUT_RDWR22
F_OK00
R_OK44
W_OK22
FIONREAD2153121531

Classes

class
Stat

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 long

Functions

def get_size_file(fd: int) -> long

Size in bytes of an open file, via fstat.

def is_dir(path: str) -> int

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.

def readfile(path: str) -> str

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.

def read_all(fd: int) -> str

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.

def try_read_all(fd: int) -> (int, str)

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.

def open(path: str, mode: str) -> int

Opens a file with a C-style mode string: "r" read, "w" write (create/truncate), "a" append. Returns the descriptor or a negative value.

def close(fd: int) -> None

Closes a descriptor only if it is valid (≥ 0); safe on a failed open.

def get_available_size(fd: int) -> int

Bytes available for reading right now on a descriptor (e.g. data already arrived on a socket), via FIONREAD.

def sys_newfstatat(dirfd: int, path: str, raw: str, flags: int) -> int

Linux only.

(no description yet)
def sys_fstat_raw(fd: int, raw: str) -> int

Linux only.

(no description yet)
def a64_stat_copy(raw: str, statbuf: Stat) -> None

Linux only.

(no description yet)
def sys_ioctl(fd: int, request: int, argp: &int) -> int

Raw ioctl(2) with an integer argument passed by address (e.g. FIONREAD).

def sys_access(path_file: str, mode: int) -> int

Raw access(2): 0 if the file can be accessed with mode (X_OK etc.), negative otherwise.

def sys_open(path: str, flags: int, mode: int) -> int

Raw open(2) with numeric flags (O_RDONLY, O_WRONLY | O_APPEND, ...) and mode. Returns a descriptor or a negative errno.

def sys_close(fd: int) -> None

Raw close(2). On Windows it also closes a socket, with closesocket.

def sys_stat(path: str, statbuf: Stat) -> int

Fills a Stat for a path. Returns 0 on success.

def sys_fstat(fd: int, statbuf: Stat) -> int

Fills a Stat for an open descriptor. Returns 0 on success.

def sys_shutdown(fd: int, how: int) -> None

Linux only.

Raw shutdown(2) on a socket (SHUT_RD/SHUT_WR/SHUT_RDWR).

def sys_pread64(fd: int, buf: str, count: int, offset: int) -> int

Reads count bytes at offset without moving the file position.

def sys_dup(fd: int) -> int

Duplicates a descriptor (dup(2)); both share offset and flags.

def sys_dup2(old_fd: int, new_fd: int) -> int

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.

def sys_dup3(old_fd: int, new_fd: int, flag: int) -> int

Like sys_dup2 with flags (O_CLOEXEC), atomically; on Windows with the same limit.