io
Package io provides typed file IO primitives for Linux.
The central type is FD — a kernel file descriptor — with read, write, and close methods. open returns owned FD | error: a successful descriptor must be closed exactly once, while an error carries no descriptor. STDIN, STDOUT, and STDERR are non-owning globals.
read and write return (n, err), with nil on success. open uses exclusive result variants so a failed acquisition creates no descriptor obligation.
Index
Types
- pub type io_err values (i64, []byte) { ... }
- message(e io_err) []byte
- format(e io_err, w writer) ?error
- pub type FD i64
- pub fn open(path []byte, flags i64, mode i64) owned FD | error
- read(fd *FD, buf []mut byte) i64, ?error
- write(fd *FD, buf []byte) i64, ?error
- close(fd *owned FD) ?error
- pub type buffered_writer struct { ... }
- new(w writer) owned *mut buffered_writer
- flush(w *mut buffered_writer) i64, ?error
- write(w *mut buffered_writer, var buf []byte) i64, ?error
- close(w owned *mut buffered_writer) ?error
Variables 3
STDIN is the standard input file descriptor (fd 0), pre-opened by the kernel at process start. It is borrowed: do not pass it to close.
STDOUT is the standard output file descriptor (fd 1), pre-opened by the kernel at process start. It is borrowed: do not pass it to close.
STDERR is the standard error file descriptor (fd 2), pre-opened by the kernel at process start. It is borrowed: do not pass it to close.
Functions 1
copy reads from src and writes to dst until src returns EOF. It uses a 1024-byte internal buffer and issues one write per read.
Returns io_err.OK (coerced to the error interface) once src reaches EOF. On any read or write error it returns the failing io_err immediately, with no further attempts to drain src or flush dst. A short write from dst (fewer bytes written than read) is reported as io_err.EIO, since copy has no way to buffer the remainder for a retry.
The return type is the builtin `error` interface, not a specific concrete type, so callers can take the result and pass it onward without forcing io_err on every signature in the chain. Callers that want to match on specific failures can use `==` against io_err cases — the interface equality machinery folds that into a typedesc-and-data-word compare.
Types 3
values (i64, []byte) { UNKNOWN: -1, "unknown I/O error" EPERM: 1, "operation not permitted" ENOENT: 2, "no such file or directory" ESRCH: 3, "no such process" EINTR: 4, "interrupted system call" EIO: 5, "I/O error" ENXIO: 6, "no such device or address" E2BIG: 7, "argument list too long" ENOEXEC: 8, "exec format error" EBADF: 9, "bad file descriptor" ECHILD: 10, "no child processes" EAGAIN: 11, "resource temporarily unavailable" ENOMEM: 12, "out of memory" EACCES: 13, "permission denied" EFAULT: 14, "bad address" ENOTBLK: 15, "block device required" EBUSY: 16, "device or resource busy" EEXIST: 17, "file exists" EXDEV: 18, "cross-device link" ENODEV: 19, "no such device" ENOTDIR: 20, "not a directory" EISDIR: 21, "is a directory" EINVAL: 22, "invalid argument" ENFILE: 23, "file table overflow" EMFILE: 24, "too many open files" ENOTTY: 25, "inappropriate ioctl for device" ETXTBSY: 26, "text file busy" EFBIG: 27, "file too large" ENOSPC: 28, "no space left on device" ESPIPE: 29, "illegal seek" EROFS: 30, "read-only file system" EMLINK: 31, "too many links" EPIPE: 32, "broken pipe" EDOM: 33, "numerical argument out of domain" ERANGE: 34, "numerical result out of range" EDEADLK: 35, "resource deadlock avoided" ENAMETOOLONG: 36, "file name too long" ENOLCK: 37, "no locks available" ENOSYS: 38, "function not implemented" ENOTEMPTY: 39, "directory not empty" ELOOP: 40, "too many levels of symbolic links" ENOMSG: 42, "no message of desired type" EIDRM: 43, "identifier removed" ECHRNG: 44, "channel number out of range" EL2NSYNC: 45, "level 2 not synchronized" EL3HLT: 46, "level 3 halted" EL3RST: 47, "level 3 reset" ELNRNG: 48, "link number out of range" EUNATCH: 49, "protocol driver not attached" ENOCSI: 50, "no CSI structure available" EL2HLT: 51, "level 2 halted" EBADE: 52, "invalid exchange" EBADR: 53, "invalid request descriptor" EXFULL: 54, "exchange full" ENOANO: 55, "no anode" EBADRQC: 56, "invalid request code" EBADSLT: 57, "invalid slot" EBFONT: 59, "bad font file format" ENOSTR: 60, "device not a stream" ENODATA: 61, "no data available" ETIME: 62, "timer expired" ENOSR: 63, "out of streams resources" ENONET: 64, "machine is not on the network" ENOPKG: 65, "package not installed" EREMOTE: 66, "object is remote" ENOLINK: 67, "link has been severed" EADV: 68, "advertise error" ESRMNT: 69, "srmount error" ECOMM: 70, "communication error on send" EPROTO: 71, "protocol error" EMULTIHOP: 72, "multihop attempted" EDOTDOT: 73, "RFS specific error" EBADMSG: 74, "bad message" EOVERFLOW: 75, "value too large for defined data type" ENOTUNIQ: 76, "name not unique on network" EBADFD: 77, "file descriptor in bad state" EREMCHG: 78, "remote address changed" ELIBACC: 79, "cannot access a needed shared library" ELIBBAD: 80, "accessing a corrupted shared library" ELIBSCN: 81, ".lib section in a.out corrupted" ELIBMAX: 82, "attempting to link in too many shared libraries" ELIBEXEC: 83, "cannot exec a shared library directly" EILSEQ: 84, "illegal byte sequence" ERESTART: 85, "interrupted system call should be restarted" ESTRPIPE: 86, "streams pipe error" EUSERS: 87, "too many users" ENOTSOCK: 88, "socket operation on non-socket" EDESTADDRREQ: 89, "destination address required" EMSGSIZE: 90, "message too long" EPROTOTYPE: 91, "protocol wrong type for socket" ENOPROTOOPT: 92, "protocol not available" EPROTONOSUPPORT: 93, "protocol not supported" ESOCKTNOSUPPORT: 94, "socket type not supported" EOPNOTSUPP: 95, "operation not supported" EPFNOSUPPORT: 96, "protocol family not supported" EAFNOSUPPORT: 97, "address family not supported by protocol" EADDRINUSE: 98, "address already in use" EADDRNOTAVAIL: 99, "cannot assign requested address" ENETDOWN: 100, "network is down" ENETUNREACH: 101, "network is unreachable" ENETRESET: 102, "network dropped connection on reset" ECONNABORTED: 103, "software caused connection abort" ECONNRESET: 104, "connection reset by peer" ENOBUFS: 105, "no buffer space available" EISCONN: 106, "transport endpoint is already connected" ENOTCONN: 107, "transport endpoint is not connected" ESHUTDOWN: 108, "cannot send after transport endpoint shutdown" ETOOMANYREFS: 109, "too many references" ETIMEDOUT: 110, "connection timed out" ECONNREFUSED: 111, "connection refused" EHOSTDOWN: 112, "host is down" EHOSTUNREACH: 113, "no route to host" EALREADY: 114, "operation already in progress" EINPROGRESS: 115, "operation now in progress" ESTALE: 116, "stale file handle" EUCLEAN: 117, "structure needs cleaning" ENOTNAM: 118, "not a XENIX named type file" ENAVAIL: 119, "no XENIX semaphores available" EISNAM: 120, "is a named type file" EREMOTEIO: 121, "remote I/O error" EDQUOT: 122, "quota exceeded" ENOMEDIUM: 123, "no medium found" EMEDIUMTYPE: 124, "wrong medium type" ECANCELED: 125, "operation canceled" ENOKEY: 126, "required key not available" EKEYEXPIRED: 127, "key has expired" EKEYREVOKED: 128, "key has been revoked" EKEYREJECTED: 129, "key was rejected by service" EOWNERDEAD: 130, "owner died" ENOTRECOVERABLE: 131, "state not recoverable" ERFKILL: 132, "operation not possible due to RF-kill" EHWPOISON: 133, "memory page has hardware error" }
io_err is the typed error set returned by this package. The cases mirror Linux's asm-generic errno values: kernel syscalls return -EACCES, -ENOENT, etc.; io maps those raw integers to this closed set.
FD is a Linux file descriptor. Every kernel-backed handle in this package is represented as one.
An owned FD carries the obligation to call close exactly once; the compiler tracks this and will reject paths that leak or double-close. Borrowed FDs — the three standard streams above, or any *FD passed by reference into a function — have no close obligation and must not be closed.
FD's underlying representation is i64, so it can be stored directly in struct fields and slice elements.
open opens the file at path with the given open(2) flags and mode.
path is an ordinary Boson byte slice — open copies it into an internal buffer and appends the NUL terminator that the underlying open(2) syscall requires, so callers do not need to embed a trailing \x00 or otherwise concern themselves with C-string representation.
flags is the bitwise OR of the O_* constants defined in this package (O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_EXCL, O_TRUNC, O_APPEND, …). mode is the permission bits applied when O_CREAT creates a new file (e.g. 0o644 = 420).
Returns an owned FD on success or an errno-backed error on failure. The error alternative carries no descriptor and requires no dummy descriptor cleanup. Paths longer than 4095 bytes return io_err.ENAMETOOLONG.
Unpack with an expression switch (the error branch must exit):
fd := switch (r := io.open(path, io.O_RDONLY, 0)) {
case io.error { report(r); return }
case owned io.FD r
}
fd.close()read fills buf with bytes from the descriptor.
Returns (n, err): n is the number of bytes read (0 = EOF) and err is io_err.OK on success or an errno-backed io_err on failure, coerced to the builtin error interface. A partial read (n < len(buf), err == io_err.OK) may be retried.
write writes buf to the descriptor.
Returns (n, err): n is the number of bytes written and err is io_err.OK on success or an errno-backed io_err on failure (coerced to error). A short write (n < len(buf), err == io_err.OK) is not an error; the caller is responsible for re-issuing the remainder.
close releases the descriptor back to the kernel and consumes the owned FD. After close, the FD value is dead — any subsequent use is a compile-time error.
Returns io_err.OK on success or an errno-backed io_err on failure, coerced to error. Some filesystems surface delayed write errors through close.
struct { w writer pos i64 buf owned [4096]byte }
Interfaces 4
reader is the interface satisfied by any value with a read method of the appropriate shape. read fills buf with up to len(buf) bytes and returns (n, err): n bytes read (0 = EOF), err is io_err.OK (or any other error implementation) on success, an errno-backed io_err on failure. The return type is the builtin error interface so a single reader can wrap diverse underlying error types.
writer is the interface satisfied by any value with a write method of the appropriate shape. write attempts to write buf and returns (n, err): n bytes written, err is io_err.OK on success or an errno-backed io_err on failure (coerced to error). A short write (n < len(buf), err == io_err.OK) must be retried by the caller.
error is the interface implemented by types that represent typical error conditions.
message(e *self) []byte from(e)
built_error is the interface implemented by types that represent complicated error conditions. These error conditions contain owned resources such as allocations and require destruction when they're no longer in use.
message(e *mut self) []byte from(e)