Basic Solution File System - BSFS
Loading...
Searching...
No Matches
file_operations_bsfs.h File Reference

Header file for 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 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.

Detailed Description

Header file for file-level operations in the BSFS file system.

This module provides functions for opening, closing, seeking, reading, and writing files using the BSFS file system. File access is performed through a memory-resident file_t structure, which maintains a cursor and an associated inode.

Function Documentation

◆ close_file()

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.

Parameters
diskPointer to the open disk file.
inode_treePointer to the B-tree of inodes.
filePointer to the file_t structure representing the open file.
Returns
0 on success, -1 on failure.

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

Parameters
diskPointer to the open disk file.
inode_treePointer to the B-tree of inodes.
inode_numberThe inode number of the file to open.
out_filePointer to the file_t structure to populate.
Returns
0 on success, -1 on failure (e.g., inode not found).

◆ read_file()

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.

Parameters
diskPointer to the disk image file.
handleB-tree handle used for indirect block mapping.
fileOpen file structure (with inode and current position).
bufferBuffer to read data into.
sizeNumber of bytes to read.
Returns
Number of bytes successfully read.

◆ seek_file()

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:

  • SEEK_SET: position is set to offset
  • SEEK_CUR: position is moved by offset from the current position
  • SEEK_END: position is set to file size + offset
Parameters
filePointer to the open file structure.
offsetNumber of bytes to move the position indicator.
whenceStarting point for the offset: SEEK_SET, SEEK_CUR, SEEK_END.
Returns
The new file position on success, or -1 on failure.

◆ write_file()

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:

  • truncate == 0: overwrite data starting at f->pos, preserve what's after.
  • truncate == 1: remove all data after f->pos before writing.
  • If f->pos == inode.size, this acts as append (with or without truncation).
Parameters
diskPointer to the open disk file.
sbPointer to the superblock (for allocation and deallocation).
bspan_treeB-tree handle mapping logical to physical blocks.
fPointer to the open file structure.
bufferPointer to the data to write.
sizeNumber of bytes to write.
truncate1 to truncate after position before writing, 0 to preserve.
Returns
Number of bytes written, or -1 on error.