Basic Solution File System - BSFS
Loading...
Searching...
No Matches
dir_entry_operations_bsfs.c File Reference

Directory entry B-tree operations for BSFS. More...

Functions

dir_topen_dir (FILE *disk, btree_handle_t *inode_tree, uint32_t inode_number)
 Opens a directory given its inode number.
int close_dir (dir_t *dir)
 Closes a directory and frees allocated resources.
int add_dir_entry (FILE *disk, superblock_t *sb, dir_t *dir, dir_entry_t *entry)
 Adds a new entry to a directory.
int scan_dir_tree_find (FILE *disk, btree_handle_t *handle, uint32_t node_block, const char *name, dir_entry_t *out)
 Recursively search a directory B-tree for an exact filename match.
int remove_dir_entry (FILE *disk, superblock_t *sb, dir_t *dir, const char *filename)
 Collect all directory entries in-order into a dynamic array.
int update_dir_entry (FILE *disk, superblock_t *sb, dir_t *dir, dir_entry_t *entry)
 Updates an existing directory entry in the B-tree.
int lookup_dir_entry (FILE *disk, superblock_t *sb, dir_t *dir, const char *name, dir_entry_t *result)
 Looks up a directory entry by name in a given directory.
int read_dir_entry (FILE *disk, superblock_t *sb, dir_t *dir, btree_handle_t *handle, dir_entry_t *out)
 Reads the next directory entry from a directory.

Detailed Description

Directory entry B-tree operations for BSFS.

Provides the in-memory directory descriptor (dir_t) and the set of operations for creating, removing, updating, looking up, and iterating directory entries backed by a B-tree stored in the filesystem image.

Function Documentation

◆ add_dir_entry()

int add_dir_entry ( FILE * disk,
superblock_t * sb,
dir_t * dir,
dir_entry_t * entry )

Adds a new entry to a directory.

Inserts a new dir_entry_t into the directory's B-tree. If this is the first insertion in the directory (i.e., the directory had no B-tree root), the function persists the newly created root into the directory inode.

Parameters
diskPointer to the filesystem image file.
sbPointer to the loaded superblock.
dirPointer to the open directory structure.
entryPointer to the directory entry to insert.
Returns
0 on success, -1 if an entry with the same name already exists or on write error.

◆ close_dir()

int close_dir ( dir_t * dir)

Closes a directory and frees allocated resources.

This function deallocates the memory used by the dir_t structure and its associated B-tree handle.

Parameters
dirPointer to the open directory to be closed.
Returns
0 on success.

◆ lookup_dir_entry()

int lookup_dir_entry ( FILE * disk,
superblock_t * sb,
dir_t * dir,
const char * name,
dir_entry_t * result )

Looks up a directory entry by name in a given directory.

Parameters
diskPointer to the disk image.
sbPointer to the superblock.
dirPointer to the open directory (dir_t).
nameName of the entry to look for.
resultOutput parameter: filled with the entry if found.
Returns
0 if found, -1 if not found.

◆ open_dir()

dir_t * open_dir ( FILE * disk,
btree_handle_t * inode_tree,
uint32_t inode_number )

Opens a directory given its inode number.

Loads the directory inode from the inode B-tree, initializes a dir_t structure and prepares the B-tree handle for reading directory entries.

Parameters
diskPointer to the open filesystem image.
inode_treePointer to the B-tree containing inodes.
inode_numberThe inode number of the directory to open.
Returns
Pointer to an allocated dir_t on success, NULL on failure.

◆ read_dir_entry()

int read_dir_entry ( FILE * disk,
superblock_t * sb,
dir_t * dir,
btree_handle_t * handle,
dir_entry_t * out )

Reads the next directory entry from a directory.

This function traverses the B-tree of a directory and returns the next valid entry. It uses the dir_index field to keep track of position across calls.

Parameters
diskPointer to the open disk file.
sbPointer to the superblock.
dirPointer to the open directory structure (dir_t).
handleB-tree handler for the directory entries.
outOutput parameter to hold the directory entry read.
Returns
0 if an entry was read successfully, -1 if end of directory is reached.

◆ remove_dir_entry()

int remove_dir_entry ( FILE * disk,
superblock_t * sb,
dir_t * dir,
const char * filename )

Collect all directory entries in-order into a dynamic array.

Removes an entry from a directory by name.

Removes an entry from a directory by name.

Looks up the entry in the B-tree and removes it if found.

Parameters
diskPointer to the filesystem image file.
sbPointer to the loaded superblock.
dirPointer to the open directory structure.
filenameName of the entry to remove.
Returns
0 on success, -1 if the entry was not found or on write error.

◆ scan_dir_tree_find()

int scan_dir_tree_find ( FILE * disk,
btree_handle_t * handle,
uint32_t node_block,
const char * name,
dir_entry_t * out )

Recursively search a directory B-tree for an exact filename match.

Traverses the directory entries B-tree in depth-first order starting at node_block, comparing each entry's name against name. Copies the first matching entry into out and returns 1 when found.

Parameters
diskPointer to the open disk file.
handleB-tree handle for directory entries.
node_blockRoot block of the subtree to scan.
nameNull-terminated filename to match exactly.
outOutput parameter to receive the matching entry on success.
Returns
1 if found; 0 otherwise.

◆ update_dir_entry()

int update_dir_entry ( FILE * disk,
superblock_t * sb,
dir_t * dir,
dir_entry_t * entry )

Updates an existing directory entry in the B-tree.

Searches for an existing directory entry using the hash of the filename and replaces its contents with entry.

Parameters
diskPointer to the filesystem image.
sbPointer to the superblock (unused).
dirPointer to the open directory structure.
entryPointer to the updated directory entry.
Returns
1 on success; -1 if the entry was not found.