open (nr 2)
Linux Signature
int open(const char *pathname, int flags, mode_t mode);
Description
Opens a file or directory at pathname and returns a file descriptor.
Current Implementation
- Reads a null-terminated path string from user space (max 4096 bytes). Returns
-EFAULTif the pointer is invalid. - Resolves the path relative to the process’s current working directory (
cwd). Normalises.and..components. - Unless
O_DIRECTORY(0o200000) is set, first attempts to open as a file viadevices::vfs::read_file()(throughosl::blocking::blocking()). On success, the entire file content is loaded into aVfsHandle(buffered in kernel memory) and a new fd is allocated. - If the file open fails with
VfsError::NotFoundorVfsError::NotAFile, orO_DIRECTORYwas requested, falls back to opening as a directory viadevices::vfs::list_dir(). On success, creates aDirHandlewith the directory listing and allocates a new fd. - Returns the new fd number on success, or a negative errno.
The VFS operations use osl::blocking::blocking() which spawns the async VFS call as a kernel task and blocks the calling user thread until it completes.
Flags supported: O_DIRECTORY (to explicitly request directory). O_RDONLY is implied for all opens. Other flags are accepted but ignored.
Source: osl/src/syscalls/fs.rs — sys_open
Errors
| Errno | Condition |
|---|---|
-EFAULT (-14) | Invalid pathname pointer |
-ENOENT (-2) | File or directory not found |
-ENOTDIR (-20) | Path is not a directory (when O_DIRECTORY used) |
-EMFILE (-24) | Per-process fd limit reached (64) |
-EIO (-5) | VFS I/O error |
Future Work
- Support
O_WRONLY,O_CREAT,O_TRUNCfor writable files. - Streaming reads instead of loading entire file into memory at open time.
- Proper
modehandling.