lecture7.1-unix filesystem

UNIX File System Overview

  • Course: CompSci 337 - System Programming

Directory Hierarchy

  • Overview of Linux File System Structure

    • All files are organized in a single directory hierarchy

    • Every file is a descendant of the root directory: /

    • Definitions:

      • Current Directory: .

      • Parent Directory: ..

Directory Fundamentals

  • Structure of Directories

    • Every directory has at least two special entries:

      • .: mapping to the directory’s own inode number

      • ..: mapping to the parent directory’s inode number

    • Command to check entries: ls -a

    • Inode Number: Unique identifier for files and directories

Paths in the Directory Hierarchy

  • Types of Paths

    • Absolute Path: Indicates location from the root node.

      • Example: /home/droh/hello.c

    • Relative Path: Indicates location relative to the current working directory.

      • Example: if droh is the current working directory, the relative path for hello.c is ./hello.c

Examples of Path Calculation

  • Finding Absolute Paths

    • Absolute path for file01.txt located in /home/user1/ is /home/user1/file01.txt

    • Absolute path for file02.txt located in /home/user2/files2.txt is /home/user2/files2.txt

  • Finding Relative Paths

    • If current working directory is user2, relative path for file02.txt is ../user2/file02.txt

    • If current working directory is user1, relative path for file02.txt is ../user2/file02.txt

Navigating Directories in C

  • Standard I/O Functions do not permit directory navigation

  • Using System Calls for Directory Navigation

User Mode vs Kernel Mode

  • Access Levels in Operating Systems

    • User Mode:

      • Limited access to system resources

      • Applications like web browsers and text editors operate here.

    • Kernel Mode:

      • Full access to all system resources

      • Essential OS functions run in kernel mode

System Calls: Interaction with the OS

  • Definition: Mechanism for user mode programs to request services from the kernel.

Example of System Call in Action: printf()

  • Process:**

    1. Invoke printf() which formats the data.

    2. A system call is initiated, transitioning to kernel mode.

    3. The kernel manages data output to the console.

    4. Control returns to the program post-execution of printf().

System Calls in C

  • High-Level I/O Functions:

    • scanf(), printf(), fopen(), fread(), fwrite(), fclose(), fseek()

  • Low-Level System Calls:

    • Allow low-level operations like file access and directory management.

    • Functions include:

      • Directory I/O: opendir(), closedir(), readdir()

      • File I/O: open(), close(), read(), write(), lseek()

Accessing Directories Using System Calls

  • opendir()

    • System call to open a directory given by path (absolute or relative).

    • Returns a pointer to a directory structure:

      const char *path = "./somedir";
      DIR *dir = opendir(path);
      if (dir == NULL) {
      perror(path);
      return;
      }
      closedir(dir);
  • closedir():

    • Closes the directory pointed to by a DIR structure.

Reading Directory Contents

  • readdir():

    • System call for retrieving directory entries

    • Utilizes a pointer of type DIR

    • Returns next entry or NULL when no more entries are found.

    • Entry structure contains:

      • d_ino: inode number

      • d_name: name of the file/directory

    • Example Usage:

      struct dirent *entry;
      while ((entry = readdir(dir)) != NULL) {
      printf("%s ", entry->d_name);
      }

robot