|
Basic Solution File System - BSFS
|
Implementation of file-level operations in the BSFS file system. More...
Functions | |
| int | open_file (FILE *disk, btree_handle_t *inode_tree, uint32_t inode_number, file_t *out_file) |
| Opens a file by loading its inode into memory. | |
| int | close_file (FILE *disk, btree_handle_t *inode_tree, file_t *file) |
| Closes an open file and writes back its inode. | |
| int32_t | seek_file (file_t *file, int32_t offset, int whence) |
| Moves the file position indicator to a new location. | |
| int | read_file (FILE *disk, btree_handle_t *handle, file_t *file, uint8_t *buffer, uint32_t size) |
| Reads data from an open file into a buffer. | |
| int | truncate_file (FILE *disk, superblock_t *sb, btree_handle_t *bspan_tree, file_t *f, uint32_t new_size) |
| Truncates a file from a given byte offset, removing its excess data blocks. | |
| int | write_file (FILE *disk, superblock_t *sb, btree_handle_t *bspan_tree, file_t *f, const uint8_t *buffer, uint32_t size, int truncate) |
| Writes data to a file, with optional truncation. | |
Implementation of file-level operations in the BSFS file system.
This file provides functions to manipulate files, including opening, closing, reading, writing, and seeking, by utilizing inodes and B-tree-based address mapping. Files are loaded into memory as file_t structures which manage stateful access.
| int close_file | ( | FILE * | disk, |
| btree_handle_t * | inode_tree, | ||
| file_t * | file ) |
Closes an open file and writes back its inode.
This function persists any changes made to the file's inode (e.g., size updates) by writing it back into the inode B-tree.
| disk | Pointer to the open disk file. |
| inode_tree | Pointer to the B-tree of inodes. |
| file | Pointer to the file_t structure representing the open file. |
| int open_file | ( | FILE * | disk, |
| btree_handle_t * | inode_tree, | ||
| uint32_t | inode_number, | ||
| file_t * | out_file ) |
Opens a file by loading its inode into memory.
This function looks up the specified inode number in the inode B-tree, and populates the file_t structure with its contents.
| disk | Pointer to the open disk file. |
| inode_tree | Pointer to the B-tree of inodes. |
| inode_number | The inode number of the file to open. |
| out_file | Pointer to the file_t structure to populate. |
| int read_file | ( | FILE * | disk, |
| btree_handle_t * | handle, | ||
| file_t * | file, | ||
| uint8_t * | buffer, | ||
| uint32_t | size ) |
Reads data from an open file into a buffer.
Reads up to size bytes from the current position in the file into buffer. The function checks direct bspan_t spans in the inode first, then falls back to the B-tree for additional block mappings. If a block is not mapped, it is treated as zeroed.
| disk | Pointer to the disk image file. |
| handle | B-tree handle used for indirect block mapping. |
| file | Open file structure (with inode and current position). |
| buffer | Buffer to read data into. |
| size | Number of bytes to read. |
| int32_t seek_file | ( | file_t * | file, |
| int32_t | offset, | ||
| int | whence ) |
Moves the file position indicator to a new location.
This function adjusts the pos field of the file_t structure, which determines the current read/write position in the file.
The behavior of the seek operation depends on the whence parameter:
| file | Pointer to the open file structure. |
| offset | Number of bytes to move the position indicator. |
| whence | Starting point for the offset: SEEK_SET, SEEK_CUR, SEEK_END. |
| int truncate_file | ( | FILE * | disk, |
| superblock_t * | sb, | ||
| btree_handle_t * | bspan_tree, | ||
| file_t * | f, | ||
| uint32_t | new_size ) |
Truncates a file from a given byte offset, removing its excess data blocks.
This function removes all bspan mappings beyond the given logical file size, effectively truncating the file and freeing corresponding disk blocks.
It updates the file's inode size and adjusts the read/write position if needed.
| disk | Pointer to the open disk file. |
| sb | Pointer to the superblock structure. |
| bspan_tree | B-tree handle that maps logical file blocks to disk blocks. |
| f | Pointer to the open file structure. |
| new_size | The new desired file size in bytes. |
| int write_file | ( | FILE * | disk, |
| superblock_t * | sb, | ||
| btree_handle_t * | bspan_tree, | ||
| file_t * | f, | ||
| const uint8_t * | buffer, | ||
| uint32_t | size, | ||
| int | truncate ) |
Writes data to a file, with optional truncation.
This function writes size bytes from buffer into the file starting at f->pos. It optionally truncates the file from that position before writing, based on the truncate flag:
| disk | Pointer to the open disk file. |
| sb | Pointer to the superblock (for allocation and deallocation). |
| bspan_tree | B-tree handle mapping logical to physical blocks. |
| f | Pointer to the open file structure. |
| buffer | Pointer to the data to write. |
| size | Number of bytes to write. |
| truncate | 1 to truncate after position before writing, 0 to preserve. |