Professional Documents
Culture Documents
File Management 1 1
File Management 1 1
© Ravi Subramanian
File Types
© Ravi Subramanian
Overview
❑ Building block of every Operating System
❑ All the operations involve files
Finish execution
© Ravi Subramanian 3
Unix and POSIX systems
❑ Wide range of file types
❑ Common set of interfaces to files
❑ Ease of application development
© Ravi Subramanian 4
File Types
/
/home /dev
Regular files
© Ravi Subramanian 5
Regular Files
❑ Text file, Binary files
❑ No distinction
❑ Text files - created/modified by
editors,
Directory files ❑ Binary files - created by
projectfiles compilers/other programs
❑ Exeutable files - execution rights
file1.c file2.c a.out are set
Regular files $ls -l a.out
_rwxrwxrwx a.out
$
© Ravi Subramanian 6
Directory Files
❑ File folder that contains other
files
❑ Created through mkdir
$mkdir dirname
Directory files
projectfiles
❑ Listed through ls command
$ls dirname
file1.c file2.c a.out ❑ Removed using rmdir command
Regular files rmdir dirname
© Ravi Subramanian 7
Character and Block Device Files
© Ravi Subramanian
Block Device Files
❑ Written/read a block at a
time
❑ Harddisk drives, pen drives,
solid state drives
❑ Block devices are accessed as
block of bytes
❑ ls -l /dev/sda0
``
❑ brw-rw-rw- /dev/
sda0
© Ravi Subramanian 9
Character Device Files
❑ Written/read one character at a time
❑ Console terminals, line printers, Serial ports
❑ /dev/tty<x> device file associated with the terminal
$ls -l /dev/tty1 #include <stdio.h> //print1.c
crw-rw-rw /dev/tty1 main() {
$cc print1.c -o print1 printf(“Hello, world on tty\
$print1 n”);
Hello, world on tty }
$ tty #include <stdio.h> //print2.c
/dev/tty1 main`(`) {
$cc print2.c -o print2 FILE *fp =
$print2 fopen(“dev/tty1”, “w”);
Hello, world on tty fprintf(fp, “Hello, world on
$ tty\n”);
}
© Ravi Subramanian 10
Character Device Files
/dev/tty1 /dev/tty2
$ tty $ tty
/dev/tty1 /dev/tty2
$cc print3.c -o print3 $ Hello, world on tty1
$print3 <print output goes to tty2> message to tty2
$ echo message to tty2 >/dev/tty2
© Ravi Subramanian 11
Creating a Character Device File
❑ mknod command - device file creation
❑ mknod /dev/dname c <major dev number> <minor dev number>
❑ mknod /dev/tty5 c 4 5
© Ravi Subramanian 12
Character Device Files
/dev/tty1
$ ls -l /dev/tty*
crw-rw-rw 4, 0 tty0
crw-rw-rw 4, 1 tty1
crw-rw-rw 4, 2 tty2
$ ls -l /dev/sda*
crw-rw-rw 7, 0 sda0
crw-rw-rw 7, 1 sda1
crw-rw-rw 7, 2 sda2
© Ravi Subramanian 13
Creating a Block Device File
❑ mknod /dev/dname b <major dev number> <minor dev number>
❑ mknod /dev/bda4 b 8 4
© Ravi Subramanian 14
FIFO Files
© Ravi Subramanian
FIFO File
❑ Temporary buffer for two or more processes
❑ Processes communicating writing and reading data into the file
❑ Data read cannot be read again
Program 1
FIFO File Program 3
Program 2
© Ravi Subramanian 16
FIFO File creation
❑ mkfifo command used to create a FIFO file
/dev/tty1
$ mkfifo fifo1
$ ls -l fifo1
prw-rw-rw- 1 user user 0 fifo1
❑ mkfifo call used to create a FIFO file in a program
❑ mkfifo(char *filename, int permissions)
❑ 0666 for read and write for everyone
#include <stdio.h> //mymkfifo.c /dev/tty1
main() { $ cc mymkfifo.c -o mymkfifo
mkfifo(“fifo2”, 0666); $ mymkfifo
} $ ls -l fifo2
prw-rw-rw- 1 user user 0 fifo1
© Ravi Subramanian 17
FIFO usage example
/dev/tty1 /dev/tty2
© Ravi Subramanian 18
Links
© Ravi Subramanian
Hard Links
❑ Reference to the local or remote pathname
❑ Commands will deference names to original
❑ File is deleted only when all links are removed
/dev/tty1
$ ln file1 file2
$ ls -l file1 file2
-rw-rw-rw- 1 user user 294 Nov 25 10:24 file1
-rw-rw-rw- 1 user user 8 Dec 8 20:59
$ cat file2 file2
<print contents of file1>
$rm file1 # Does remove file1.
$cat file2
<print contents of file1>
© Ravi Subramanian 20
Hard Links
© Ravi Subramanian 21
Symbolic (soft) Links
❑ Reference to the local or remote pathname
❑ Commands will deference symbolic names to original
❑ Removing original file removes the links as well
/dev/tty1
$ ln -s file1 file2
$ ls -l file1 file2
-rw-rw-rw- 1 user user 294 Nov 25 10:24 file1
lrw-rw-rw- 1 user user 8 Dec 8 20:59 file2 -> file1
$ cat file2
<print contents of file1>
$rm file1 # remove file1. This removes reference of file2
$cat file2
file2 : No such file
© Ravi Subramanian 22
Symbolic Links
Directory
File System
Hard Link
File1 File1
Symbolic Link
File2
© Ravi Subramanian 23
File System Hierarchy
© Ravi Subramanian
File Path Names
❑ Absolute path name : starts from root
❑ /bin/cat
❑ Current working direcory : /home/user1/pers
❑ test1.c in /home/user1 can be named as
❑ Relative path name : ../test1.c
❑ Full path name : /home/user1/test.c
❑ NAME_MAX : Max number of characters in File name
❑ PATH_MAX : Max number of characters in path name
© Ravi Subramanian 25
File System Hierarchy
/
cat ps /user1
test,c pers
root
directory
leaf node
© Ravi Subramanian 26
Important directories and files
❑ /etc : Admin files and programs
❑ /etc/password : User information
❑ /etc/shadow : User passwords
❑ /etc/group : Group information
❑ /bin : System programs
❑ /dev : Character and block device files
❑ /usr/include : Standard header files
❑ /usr/lib : standard libraries
❑ /tmp : Temporary files used by applications
© Ravi Subramanian 27
File Attributes
© Ravi Subramanian
File Types
Last modify time Time when file was modified for any of access permission, UID,
GID or hard link count
Inode number System inode number of the file
© Ravi Subramanian 29
File Attribute Listing
/dev/tty1
$ ls -al /dev/tty
crw-rw---- 1 ravi tty 4, 1 Dec 8 20:25 /dev/tty1 #character device
$ ls -al /dev/sda
brw-rw-rw- 5, 1 /dev/sda0 #block device major 5, minor
1 ls -l fifofile
$
prw-rw-rw- 1 user user 0 fifofile #FIFO File
$ls -l f1
-rw-rw-rw- 1 ravi ravi 107 Dec 8 23:37 x1 # Link count 1
$link f1 f2 # Create a new hard link
$ls -al f1 f2
-rw-rw-rw- 2 ravi ravi
`` 107 Dec 8 23:37 x1 # Hard link count = 2
rw-rw-rw- 2 ravi ravi 107 Dec 8 23:37 x2
$link f1 f3
$ls -al f1 f2 f3
-rw-rw-rw- 3 ravi ravi 107 Dec 8 23:37 x1 # Hard link count = 3
-rw-rw-rw- 3 ravi ravi 107 Dec 8 23:37 x2
-lrw-rw-rw- 3 ravi ravi 107 Dec 8 23:37 x3
© Ravi Subramanian 30
File Attribute - Access Permissions
3 Bits for read, write, execute <r><w><x>
❑ One set of 3 bits for user, group and others
❑ <user rwx><group rwx><others rwx>
❑ 000 - No Read, No Write, No execute
❑ 100 (4) - Only read
❑ 110 (6) - Only read and write
❑ 111 (7) - Read, write and execute
❑ 777 - Owner, group and others can read, write and execute
❑ 666 - Owner, group and others can read and write
© Ravi Subramanian 31
Access Permissions
$ ls -l file1
-rw-rw-rw- 1 user user 294 Nov 25 10:24 file1 Disk
#all can read and write
Memory
$ ls -l file2
-rwxr-xr-x 1 user user 294 Nov 25 10:24 file2
Owner can read, write and execute, group can read and execute, ohers can
read and execute
$ ls -l file3
-rw-rw-r-- 1 user user 294 Nov 25 10:24 file3
Owner can read and write, group can read and write , ohers can read
$ chmod 777 file3 #change mode to 777
$ ls -l file3
-rwxrwxrwx 1 user user 294 Nov 25 10:24 file3
All can read, write and execute
© Ravi Subramanian 32
File Attributes that don't change
File type : regular, directory, block, character,
pipe
❑ File inode number
❑ File system ID
❑ Major and Minor device numbers
© Ravi Subramanian 33
Changing File Attributes
Command System Call Attributes Changed
© Ravi Subramanian 34
INODES
© Ravi Subramanian
Inodes
Each file will have an Inode
Directory contains
File names and associated inode numbers
© Ravi Subramanian 36
Inode
Inode Table
File1 23598 23598
© Ravi Subramanian 37
inode listing
$ ls -i /usr
3940649674189555 bin Disk Memory
3377699720769213 lib
5348024557776511 share
1688849860505250 games
7318349394750941 local
12947848929298260 src
2251799813926563 include
4222124650933744 sbin
$
© Ravi Subramanian 38
File Access Using Inode
Steps applied when user tries to access /usr/krish/myfile.c
© Ravi Subramanian 39
Inode
File System
/ 78532 Inode Table
78532
/ Directory 12321
usr 12321 42423
etc 42423 45333
/krish
myfile.c 78787
test1,c 77767
© Ravi Subramanian 40
Application Programming
Interface
© Ravi Subramanian
File System APIs
Unix and Posix systems provide a set of Application Programming
Interfaces to access files
Type of Files Programs System Calls
Regular Files cat, vi, rm, cp, mv open, create, read, write, unlink, lseek,
fcntl, fstat, stat
Directory Files mkdir mkdir, mknod, rmdir
© Ravi Subramanian 42
Common File APIs
File open : This can be done using open or creat API
© Ravi Subramanian 43
stat structure
Contains key information about file provide by stat or fstat call
File system id
File inode
Hard link count
User id of the owner
Group id of the owner
Major and minor device numbers
Size of the file
Access time
Modification time
Status change time
© Ravi Subramanian 44
Unix Kernel Support For Files
© Ravi Subramanian
Kernel Structures
File table - keeps track of all the open files
© Ravi Subramanian 46
Inode
File System
/ 78532 Inode Table
78532
/ Directory 12321
usr 12321 42423
etc 42423 45333
/krish
myfile.c 78787
test1,c 77767
© Ravi Subramanian 47
Inode
File Descriptor
File Table
Table FIniloedSeyT File System
4 file2, r
Process 2 File
Descriptor 5, file4, w
Table
© Ravi Subramanian 48
File Open
Steps executed when a process opens a file
Kernel connects the file descriptor table, kernel file table and
the inode table
© Ravi Subramanian 49
File Open
Create an entry in the process file descriptor table
Update the current pointer entry in the kernel file table
Set the reference count to 1. This will be incremented if one more
process accesses the file
Increment the reference count of the inode entry in the inode
table. This indicates how many file table entries are pointing to the
inode.
© Ravi Subramanian 50
File Operations
read and write calls update the file pointer
Checks if the file can be accessed in the desired way based on file
permissions
© Ravi Subramanian 51
File Operations - lseek
lseek is performed on a file descriptor
Moves the file pointer to desired location
Kernel checks if the file is a block device
character, FIFO devices cannot use lseek operation
If the lseek is compatible, kernel updates the file pointer in the file
table entry.
© Ravi Subramanian 52
File Operations - close
Sets the file descriptor entry as unused
Decrements reference count in file table entry. If reference count
is 0, the entry is marked as unused
Decrements reference count entry in the inode table entry
If hardlink count in the inode is 0, all physical space occupied by
the file is freed up.
© Ravi Subramanian 53
Directory Files
© Ravi Subramanian
Directory File
File of records - each record is an entry for a file
Record structure
struct dirent in Unix system V and POSIX
struct direct in BSD UNIX
Implementation dependent
© Ravi Subramanian 55
Directory Functions
Directory files can be accessed using open/read/lseek functions
Use the structure defined in dirent.h
Not portable as dirent structure is system dependent
© Ravi Subramanian 56
Directory Listing Code
#include <stdio.h>
#include <dirent.h>
int main(void)
{
struct dirent *dentry; // Pointer for directory entry
// opendir() returns a pointer of DIR type.
DIR *dr = opendir(".");
© Ravi Subramanian 57
Directory Search for a File Name
#include <stdio.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
struct dirent *dentry; // Pointer for directory entry
// opendir() returns a pointer of DIR type.
DIR *dr = opendir(".");
while ((detry = readdir(dr)) != NULL)
{
if (strcmp(dentry->d_name, argv[1]) == 0)
{
printf("%s is found in current directy\n", argv[1]);
break;
}
}
closedir(dr);
}
© Ravi Subramanian 58
Hard Links and Symbolic Links
© Ravi Subramanian
Hard Link
Link : Path name for a file
Link in the directory to the inode
Typically, only one link for a given file
© Ravi Subramanian 60
Hard Link
Same inode for the linked file
$ ln test1.c test2.c
$ ls -i test1.c test2.c
12345 test1.c
12345 test2.c
Inode Table
inode 12345
test1.c 12345
test2.c 12345
abcd 34457
a.out 87765 File
File System
Directory Contents
© Ravi Subramanian 61
Difference between cp and ln
cp creates new file in the system
$ cp test1.c test3.c
$ ls -i test1.c test2.c test3.c
12345 test1.c
12345 test2.c
Inode Table
34459 test3.c inode 12345
inode 34459
test1.c 12345
File
test2.c 12345
test3.c 34459
abcd 34458 File
a.out 87765
File System
Directory Contents
© Ravi Subramanian 62
Symbolic Link
ln -s creates a symbolic link. It is a new file that contains the name
of the linked file
$ ln -s test1.c test4.c
$ ls -i test1.c test4.c
12345 test1.c
Inode Table
78524 test4.c inode 12345
inode 78524
test1.c 12345
File
test2.c 12345
test4.c 78524
abcd 34458 File
a.out 87765
File System
Directory Contents
© Ravi Subramanian 63
Constraints
No hardlinks for directories. Prevents cyclic
links
ln /user/krish/source/cfiles /user/krish
© Ravi Subramanian 64
Differences between Hard and Symbolic Links
Cannot link across file systems Can link across file systems
Increases the hardlink count of the No change in the hard link count of the
inode inode
© Ravi Subramanian 65
Thank You
© Ravi Subramanian