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

Implementation of path resolution utilities for BSFS. More...

Functions

int resolve_path (FILE *disk, superblock_t *sb, const char *path, dir_t *cwd, dir_t *parent_out, char *final_name)
 Resolves a path to its parent directory and the final component name.
int lookup_file (FILE *disk, superblock_t *sb, const char *path, dir_t *cwd, inode_t *out_inode)
 Looks up a file or directory by path and loads its inode.
int resolve_parent_dir (FILE *disk, superblock_t *sb, const char *path, dir_t *cwd, dir_t *out_parent, char *out_basename, size_t basename_cap)
 Resolve the parent directory and basename from a path.

Detailed Description

Implementation of path resolution utilities for BSFS.

This file provides the core logic for resolving filesystem paths, including functions for navigating directories, extracting final components, and retrieving inodes based on path strings.

Function Documentation

◆ lookup_file()

int lookup_file ( FILE * disk,
superblock_t * sb,
const char * path,
dir_t * cwd,
inode_t * out_inode )

Looks up a file or directory by path and loads its inode.

This function resolves the given path and attempts to locate the corresponding inode (for either a file or directory). It internally uses resolve_path and lookup_dir_entry.

Parameters
diskPointer to the filesystem image.
sbPointer to the superblock.
pathInput path string (absolute or relative).
cwdPointer to current working directory (used for relative paths).
out_inodeOutput parameter to receive the found inode.
Returns
0 on success, -1 if the file or directory is not found.

◆ resolve_parent_dir()

int resolve_parent_dir ( FILE * disk,
superblock_t * sb,
const char * path,
dir_t * cwd,
dir_t * out_parent,
char * out_basename,
size_t basename_cap )

Resolve the parent directory and basename from a path.

This function takes an absolute or relative path, normalizes it, validates it, splits it into parent directory path and basename, and resolves the parent directory into a dir_t structure.

Workflow:

  1. Normalize the input path using normalize_path().
  2. Reject paths ending with '/' (files must have a basename).
  3. Split the normalized path into parent and basename using split_parent_basename().
  4. Resolve the parent path into a directory inode using lookup_file().
  5. Verify that the resolved parent is a directory.
  6. Return the parent directory in out_parent and the basename in out_basename.

Error handling:

  • Returns -1 if arguments are invalid.
  • Returns -1 if normalization or splitting fails.
  • Returns -1 if the parent path cannot be resolved or is not a directory.
  • Returns 0 on success.

Memory:

  • Internally allocates a temporary normalized path; this is freed before return.
Parameters
diskPointer to the open filesystem image.
sbPointer to the loaded superblock.
pathAbsolute or relative path of the file.
cwdCurrent working directory for relative path resolution.
out_parentOutput: resolved parent directory (as dir_t).
out_basenameOutput: buffer that will receive the final component (basename) of the path.
basename_capCapacity of out_basename buffer (in bytes).
Returns
0 on success; -1 on error.

◆ resolve_path()

int resolve_path ( FILE * disk,
superblock_t * sb,
const char * path,
dir_t * cwd,
dir_t * parent_out,
char * final_name )

Resolves a path to its parent directory and the final component name.

This function traverses the file system path starting from either the root (for absolute paths) or the given current working directory (cwd), and returns:

  • The parent directory (dir_t) that contains the final component.
  • The name of the final component (file or directory name).
Parameters
diskPointer to the filesystem image.
sbPointer to the superblock.
pathInput path string (absolute or relative).
cwdPointer to current working directory (used for relative paths).
parent_outOutput parameter to receive the parent directory.
final_nameOutput parameter to receive the final name.
Returns
0 on success, -1 on error (invalid path or not found).