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

Interface for file creation operations in the BSFS filesystem. More...

Functions

int create_file (FILE *disk, superblock_t *sb, btree_handle_t *inode_tree, const char *path, dir_t *cwd, uint16_t uid, uint16_t gid, uint16_t permissions)
 Creates an empty regular file at the given path.

Detailed Description

Interface for file creation operations in the BSFS filesystem.

This header exposes functions to create new files in the BSFS filesystem. File creation involves resolving the parent directory, validating uniqueness of the filename, allocating and initializing a new inode, inserting it into the inode B-tree, and linking the inode into the directory hierarchy with a directory entry.

Provided functions:

This header is part of the public API for higher-level file operations.

Function Documentation

◆ create_file()

int create_file ( FILE * disk,
superblock_t * sb,
btree_handle_t * inode_tree,
const char * path,
dir_t * cwd,
uint16_t uid,
uint16_t gid,
uint16_t permissions )

Creates an empty regular file at the given path.

This function initializes a fresh inode_t (size = 0, no data blocks), inserts it into the inode B-tree, and then adds a directory entry in the parent directory resolved from path. It prevents duplicate names in the parent directory and updates the parent's metadata (timestamps).

Workflow: 1) Resolve the parent directory and basename from path (relative to cwd if needed) using resolve_parent_dir(). 2) Allocate a new inode number and initialize a zero-sized regular file inode. 3) Insert the inode into the inode B-tree via insert_inode(). 4) Build a dir_entry_t and insert it into the parent directory using add_dir_entry(). 5) Update the parent directory's timestamps and persist them via update_inode().

Notes:

  • This routine does NOT allocate any data blocks; writes will allocate on demand.
  • On failure to add the directory entry, the newly inserted inode is removed from the inode B-tree (basic rollback).
Parameters
diskOpen filesystem image.
sbLoaded superblock.
inode_treeHandle for the inode B-tree.
pathAbsolute or relative path of the new file.
cwdCurrent working directory for resolving relative paths.
uidOwner user ID for the new inode.
gidOwner group ID for the new inode.
permissionsMode bits (e.g., 0644).
Returns
0 on success; -1 on error (invalid args, path resolution failure, inode insertion failure, duplicate name, or dir entry insertion failure).