pub struct FileStruct {
pub cwd: Directory,
pub files: BTreeMap<FileDescriptor, File>,
}Expand description
The FileStruct represents the filesystem state for a specific
process, which corresponding to the Linux kernel’s struct files_struct.
This struct encapsulates information about the current state of the
filesystem for the process, including the management of open
files, their positions, and the operations that can be performed on them.
The FileStruct is responsible for keeping track of file descriptors and
their associated file states, ensuring that file operations (like
reading, writing, seeking, and closing) are executed correctly and
efficiently.
It also provides a mechanism for the operating system to manage and interact with files in a multi-process environment, allowing for process-local filesystem management.
§Filesystem State
The filesystem state refers to the set of files currently open for a given process. This includes managing the file descriptors (unique identifiers for open files), file positions, and ensuring that resources are freed once a file is closed.
Fields§
§cwd: DirectoryThe current working directory of the process.
files: BTreeMap<FileDescriptor, File>The file descriptor table of the process.
Implementations§
Source§impl FileStruct
impl FileStruct
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new instance of FileStruct.
This function initializes a new filesystem state, typically when a process starts or when a fresh file operation is needed.
§Returns
Returns a new FileStruct struct, representing a clean slate for the
filesystem state. The clean state must initialize the STDIN, STDOUT,
STDERR.
Sourcepub fn install_file(
&mut self,
file: File,
) -> Result<FileDescriptor, KernelError>
pub fn install_file( &mut self, file: File, ) -> Result<FileDescriptor, KernelError>
Installs a File into the process’s file descriptor table.
This method assigns the lowest available file descriptor number to
file and returns it as a FileDescriptor.
The descriptor can then be used by the process to perform I/O operations
such as read, write, stat, or close.
§Errors
- Returns
KernelError::TooManyOpenFileif the process already has more than 1024 open files, meaning no additional descriptors are available.
Sourcepub fn open(&mut self, abi: &SyscallAbi<'_>) -> Result<usize, KernelError>
pub fn open(&mut self, abi: &SyscallAbi<'_>) -> Result<usize, KernelError>
Opens a file.
This function handles the system call for opening a file, including
checking if the file exists, and setting up the file’s access mode
(e.g., read, write, or append). It modifies the FileStruct by
associating the file with the current process and prepares the file
for subsequent operations.
§Errors
- Returns
KernelError::InvalidArgumentif unexpected access mode is provided. - Propagates any errors from underlying APIs (e.g.
uaccess).
§Syscall API
int open(const char *pathname, int flags);pathname: Path to the file to be opened.flags: Specifies the access mode. The possible values are:O_RDONLY(0): The file is opened for read only.O_WRONLY(1): The file is opened for write only.O_RDWR(2): The file is opened for both read and write.
Returns the corresponding file descriptor number for the opened file.
Sourcepub fn read(&mut self, abi: &SyscallAbi<'_>) -> Result<usize, KernelError>
pub fn read(&mut self, abi: &SyscallAbi<'_>) -> Result<usize, KernelError>
Reads data from an open file.
This function implements the system call for reading from an open file. It reads up to a specified number of bytes from the file and returns them to the user. The current file position is adjusted accordingly.
§Errors
- Returns
KernelError::IsDirectoryif the specified file is a directory. - Returns
KernelError::BrokenPipeif the specified file is a disconnected interprocesscommunication channel. - Returns
KernelError::BadFileDescriptorif the specified file descriptor is invalid.
§Syscall API
ssize_t read(int fd, void *buf, size_t count);fd: File descriptor of the file to read from.buf: Buffer to store the data read from the file.count: Number of bytes to read.
Returns the actual number of bytes read.
Sourcepub fn write(&mut self, abi: &SyscallAbi<'_>) -> Result<usize, KernelError>
pub fn write(&mut self, abi: &SyscallAbi<'_>) -> Result<usize, KernelError>
Writes data to an open file.
This function implements the system call for writing data to a file. It writes a specified number of bytes to the file, starting from the current file position. The file’s state is updated accordingly.
§Errors
- Returns
KernelError::IsDirectoryif the specified file is a directory. - Returns
KernelError::BrokenPipeif the specified file is a disconnected interprocesscommunication channel. - Returns
KernelError::BadFileDescriptorif the specified file descriptor is invalid. - Propagates any errors from underlying APIs (e.g.
uaccess).
§Syscall API
ssize_t write(int fd, const void *buf, size_t count);fd: File descriptor of the file to write to.buf: Buffer containing the data to be written.count: Number of bytes to write.
Returns the number of bytes written
Sourcepub fn seek(&mut self, abi: &SyscallAbi<'_>) -> Result<usize, KernelError>
pub fn seek(&mut self, abi: &SyscallAbi<'_>) -> Result<usize, KernelError>
Seeks to a new position in the file.
This function implements the system call for moving the file pointer to a specified position within the file. The position can be set relative to the beginning, current position, or end of the file.
§Errors
- Returns
KernelError::InvalidArgumentif the calculated position is invalid. - Returns
KernelError::InvalidArgumentif the specified file is not aFileKind::RegularFile. - Returns
KernelError::BadFileDescriptorif specified file descriptor is invalid. - Propagates any errors from underlying APIs (e.g.
uaccess).
§Syscall API
off_t seek(int fd, off_t offset, int whence);fd: File descriptor of the file to seek in.offset: Number of bytes to move the file pointer.whence: Specifies how the offset is to be interpreted. Common values are:SEEK_SET(0): The offset is relative to the beginning of the file.SEEK_CUR(1): The offset is relative to the current file position.SEEK_END(2): The offset is relative to the end of the file.
Returns the new position of the file descriptor after moving it.
Sourcepub fn tell(&mut self, abi: &SyscallAbi<'_>) -> Result<usize, KernelError>
pub fn tell(&mut self, abi: &SyscallAbi<'_>) -> Result<usize, KernelError>
Tells the current position in the file.
This function implements the system call for retrieving the current file pointer position. It allows the program to know where in the file the next operation will occur.
§Errors
- Returns
KernelError::InvalidArgumentif the specified file is not aFileKind::RegularFile. - Returns
KernelError::BadFileDescriptorif specified file descriptor is invalid.
§Syscall API
off_t tell(int fd);fd: File descriptor of the file.
Returns the position of the file descriptor.
Sourcepub fn close(&mut self, abi: &SyscallAbi<'_>) -> Result<usize, KernelError>
pub fn close(&mut self, abi: &SyscallAbi<'_>) -> Result<usize, KernelError>
Closes an open file.
This function implements the system call for closing an open file.
§Errors
- Returns
KernelError::BadFileDescriptorif specified file descriptor is invalid.
§Syscall API
int close(int fd);fd: File descriptor to close.
Returns 0 if success.
Sourcepub fn pipe(&mut self, abi: &SyscallAbi<'_>) -> Result<usize, KernelError>
pub fn pipe(&mut self, abi: &SyscallAbi<'_>) -> Result<usize, KernelError>
Creates an interprocess communication channel between two file
descriptors.
Pipes are unidirectional communication channels commonly used for IPC.
Data written to pipefd[1] can be read from pipefd[0].
A process that read from pipe must wait if there are no bytes to be read.
§Syscall API
int pipe(int pipefd[2]);pipefd: An array of two file descriptors, wherepipefd[0]is for reading andpipefd[1]is for writing.
Returns 0 if success.
Trait Implementations§
Source§impl Clone for FileStruct
impl Clone for FileStruct
Source§fn clone(&self) -> FileStruct
fn clone(&self) -> FileStruct
1.0.0§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for FileStruct
impl !RefUnwindSafe for FileStruct
impl Send for FileStruct
impl Sync for FileStruct
impl Unpin for FileStruct
impl !UnwindSafe for FileStruct
Blanket Implementations§
§impl<T> Any for Twhere
T: 'static + ?Sized,
impl<T> Any for Twhere
T: 'static + ?Sized,
§impl<T> Borrow<T> for Twhere
T: ?Sized,
impl<T> Borrow<T> for Twhere
T: ?Sized,
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)