RegularFile

Trait RegularFile 

Source
pub trait RegularFile
where Self: Send + Sync,
{ // Required methods fn ino(&self) -> InodeNumber; fn size(&self) -> usize; fn read( &self, fba: FileBlockNumber, buf: &mut [u8; 4096], ) -> Result<bool, KernelError>; fn write( &self, fba: FileBlockNumber, buf: &[u8; 4096], min_size: usize, ) -> Result<(), KernelError>; fn writeback(&self) -> Result<(), KernelError>; // Provided method fn mmap(&self, fba: FileBlockNumber) -> Result<Page, KernelError> { ... } }
Expand description

Trait representing a regular file in the filesystem.

A regular file contains user data and supports basic read and write operations.

Required Methods§

Source

fn ino(&self) -> InodeNumber

Returns the inode number of the file.

Source

fn size(&self) -> usize

Returns the size of the file in bytes.

Source

fn read( &self, fba: FileBlockNumber, buf: &mut [u8; 4096], ) -> Result<bool, KernelError>

Reads data from the file into the provided buffer.

§Parameters
  • fba: The FileBlockNumber which to read.
  • buf: A mutable array where the file content will be stored.
§Returns
  • Ok(true): If the read success.
  • Ok(false): If the read success.
  • Err(Error): An error occured while the read operation.
Source

fn write( &self, fba: FileBlockNumber, buf: &[u8; 4096], min_size: usize, ) -> Result<(), KernelError>

Writes a 4096-byte page of data into the specified file block.

This method writes the contents of buf to the file block indicated by fba. If the target block lies beyond the current end of the file, the file may be extended up to new_size bytes to accommodate the write.

However, if the target block lies beyond the current file size and min_size is insufficient to reach it, the write will fail.

§Parameters
  • fba: The FileBlockNumber indicating the block to write to.
  • buf: A buffer containing exactly 4096 bytes of data to write.
  • min_size: The desired minimum file size (in bytes) after the write. If this value is less than or equal to the current file size, no growth occurs.
§Returns
  • Ok(()) if the write is successful.
  • Err(KernelError) if the operation fails (e.g., out-of-bounds write, I/O error).
Source

fn writeback(&self) -> Result<(), KernelError>

Write back the file to disk.

Provided Methods§

Source

fn mmap(&self, fba: FileBlockNumber) -> Result<Page, KernelError>

Maps a file block into memory.

This method retrieves the contents of the file at the specified file block number (fba) and returns it as a Page containing the contents of the file block.

The memory-mapped page reflects the current contents of the file at the requested block, and it can be used for reading or modifying file data at page granularity.

§Parameters
  • fba: The file block number to map into memory. This is a logical offset into the file, measured in fixed-size blocks (not bytes).
§Returns
  • Ok(Page): A reference-counted, in-memory page containing the file block’s data.
  • Err(KernelError): If the file block cannot be found or loaded (e.g., out-of-bounds access).

Implementors§