Course: CompSci 337 - System Programming
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: ..
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
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
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
Standard I/O Functions do not permit directory navigation
Using System Calls for Directory Navigation
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
Definition: Mechanism for user mode programs to request services from the kernel.
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()
.
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()
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.
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);
}