FileStruct

Struct FileStruct 

Source
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: Directory

The current working directory of the process.

§files: BTreeMap<FileDescriptor, File>

The file descriptor table of the process.

Implementations§

Source§

impl FileStruct

Source

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.

Source

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::TooManyOpenFile if the process already has more than 1024 open files, meaning no additional descriptors are available.
Source

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
§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.

Source

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
§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.

Source

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
§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

Source

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
§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.

Source

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
§Syscall API
off_t tell(int fd);
  • fd: File descriptor of the file.

Returns the position of the file descriptor.

Source

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
§Syscall API
int close(int fd);
  • fd: File descriptor to close.

Returns 0 if success.

Source

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, where pipefd[0] is for reading and pipefd[1] is for writing.

Returns 0 if success.

Trait Implementations§

Source§

impl Clone for FileStruct

Source§

fn clone(&self) -> FileStruct

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for FileStruct

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto 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 T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.