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 -aInode 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
drohis the current working directory, the relative path forhello.cis./hello.c
Examples of Path Calculation
Finding Absolute Paths
Absolute path for
file01.txtlocated in/home/user1/is/home/user1/file01.txtAbsolute path for
file02.txtlocated in/home/user2/files2.txtis/home/user2/files2.txt
Finding Relative Paths
If current working directory is
user2, relative path forfile02.txtis../user2/file02.txtIf current working directory is
user1, relative path forfile02.txtis../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:**
Invoke
printf()which formats the data.A system call is initiated, transitioning to kernel mode.
The kernel manages data output to the console.
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
DIRstructure.
Reading Directory Contents
readdir():
System call for retrieving directory entries
Utilizes a pointer of type
DIRReturns next entry or
NULLwhen no more entries are found.Entry structure contains:
d_ino: inode numberd_name: name of the file/directory
Example Usage:
struct dirent *entry; while ((entry = readdir(dir)) != NULL) { printf("%s ", entry->d_name); }