CSCI 4061 Introduction to Operating Systems
Instructor:
Outline
File Systems
Directories
File and directory operations Inodes and metadata
Links
2
File Systems
An organized collection of data
Think of your book rack or music collection
Think of how libraries organize books/magazines
Discussion: Why do we need file systems?
3
File System Structure
A file system typically consists of:
Files: Data objects
Directories or folders: Collection of files
Other objects specific to how data is organized in
the file system
Different file systems implement these entities in different ways
5
1
Unix File System
/
bin dev etc home lib usr tmp var
cp ls
6
chandra
bin myprog
bin include lib
cc which libc.so libm.so
Unix File System Structure
Files: Data objects
Regular files, device files, FIFOs
Directories: Collections of files Contain information about files
Links: Pointers to other files Pseudonyms/nicknames
7
Directories
Directory is a special file
A directory is a list of entries about files and
directories that it contains Each entry contains:
Name of file and file type
Pointer to file inode (metadata)
8
Directory Structure
filename inode no.
file1
12345
file2
98765
inode 12345
inode 98765
9
2
Directory Operations
Different operations from the ones used for regular files. Why?
Cannot use:
open, read, write, close
Instead use:
opendir, readdir, closedir
10
11
Opening a Directory
Returns a directory stream of type DIR
Contains an ordered sequence of directory entries
Order need not be alphabetical
Order is implementation-dependent
DIR *opendir(char *dirname);
12
Reading Directory Entries
Returns the next directory entry NULL if end of directory or error
Directory entry contains information about a file or subdirectory
Filename
File’s inode number
Optional fields: E.g.: File type
struct dirent *readdir(DIR *dirp);
Closing and Repositioning a Directory
Closes an open directory
Repositions the directory to the beginning
13
int closedir(DIR *dirp);
void rewinddir(DIR *dirp);
3
Modifying Directory Entries
No writedir: Cannot write to a directory entry directly. Why not?
Directory entries changed through operations on files:
File creation/deletion: creat, remove, open Filenames, symbolic links: rename, link,
unlink
Directory creation and removal: mkdir, rmdir
Some of the operations differ based on whether the argument is a file or a directory
14
File System Traversal
Change directory to a given path cd command
Get the current working directory pwd command
15
int chdir(char *path);
char *getcwd(char *buf, size_t size);
Inodes
Inodes are kernel structures that contain file information
File properties
File data location: pointers to disk blocks
containing file information
Directory entry contains filename and pointer to
file inode
Filename is information provided by file system
16
Actual file is independent of its filename
File Properties
File type
Size
Owner, permissions
Times: Last access, last modification, last status change
Number of hard links
17
4
Reading File Properties
Parameters:
path: File path
buf: structure containing file properties
struct stat:
Contains file properties and inode no.
Variations:
lstat: Distinguishes between links and files fstat: Takes file descriptor instead of path
18
int stat(char *path, struct stat *buf);
File Operations
The following change the file properties in the inode:
chmod: Mode change
chown: Ownership change
The following changes the directory entry in the file system
rename: Move/rename
19
Inode Structure
File Properties
Direct Data Block pointers
Single Indirect ptr
Double Indirect ptr
Triple Indirect ptr
Data Blocks Data Blocks
Data Blocks
20
Links
21
Pointers to files
Pseudonyms/nicknames
Example: ln /home/user/file1 file2 Creates a link (file2) that points to file1
Links provide access to the original files Hard links: Direct access to file
Symbolic links: Indirect access to file
5
Hard Links
Pointer to the inode of a file Each link is equivalent
No “original” filename
File is deleted only when all hard links are deleted
inode 12345
file1 12345
file2 12345
22
Symbolic Links
Pointer to another directory entry
A symbolic link is different from original file
If original file is moved or deleted, the link becomes hanging
inode does not keep count of symbolic links
inode “file1” inode 12345 13579
file1 12345
file2 13579
23
Hard Link vs. Symbolic Link
What happens if we delete the original file? What happens if we delete the link?
24
6