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

Implementation of file creation routines 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

Implementation of file creation routines in the BSFS filesystem.

This module provides the implementation of functions to create new empty files in the BSFS filesystem. A new inode is initialized, inserted into the inode B-tree, and linked into its parent directory via a directory entry. No data blocks are allocated at creation time; they are allocated on demand during writes.

Responsibilities:

  • Resolve parent directory and basename from a path.
  • Ensure that duplicate names are not allowed in the parent directory.
  • Allocate and initialize a fresh inode with zero size and no blocks.
  • Insert the inode into the inode B-tree.
  • Add the directory entry in the parent directory.
  • Update parent directory metadata (timestamps).

This file is part of the higher-level file management interface for BSFS.

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