system programming1 advanced programming in the unix environment ch 4. files and directories
TRANSCRIPT
System Programming 1
Advanced Programming in the Unix Advanced Programming in the Unix EnvironmentEnvironment
Ch 4. Files and DirectoriesCh 4. Files and Directories
System Programming 2
ContentsContents File AttributesFile Attributes File TypesFile Types IDs in Unix systems and File Access PermissionsIDs in Unix systems and File Access Permissions Overview of the Unix File SystemOverview of the Unix File System Symbolic linkSymbolic link File TimesFile Times Reading DirectoriesReading Directories Special Device FilesSpecial Device Files FunctionsFunctions
– File access permissions : access, umask, chmod, chownFile access permissions : access, umask, chmod, chown– File system operations: link, unlink, remove, renameFile system operations: link, unlink, remove, rename– Symbolic links: symlink readlinkSymbolic links: symlink readlink– Directory Functions : mkdir, rmdir, chdir, getcwdDirectory Functions : mkdir, rmdir, chdir, getcwd– Sync functions: sync, fsyncSync functions: sync, fsync
System Programming 3
File AttributesFile Attributes
struct stat { mode_t st_mode; /* file type & mode (permissions) */ ino_t st_ino; /* i-node number (serial number)*/ dev_t st_dev; /* device number (filesystem)*/ dev_t st_rdev; /* device number for special files */ nlink_t st_nlink; /* number of links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ off_t st_size; /* size in bytes, for regular files */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last file status change */ long st_blksize; /* best I/O block size */ long st_blocks; /* number of 512-byte blocks allocated */};
Stat structure
System Programming 4
stat, fstat, and lstat functionsstat, fstat, and lstat functions
stat() returns a structure of information on the named filestat() returns a structure of information on the named file fstat() obtains information about the file that is already fstat() obtains information about the file that is already
open on descriptor open on descriptor fildesfildes lstat() is similar to stat, but returns information about the lstat() is similar to stat, but returns information about the
symbolic link.symbolic link. buf buf : a pointer a structure that will be filled by the : a pointer a structure that will be filled by the
functions.functions. lsls commands use the functions commands use the functions
int stat(const char *int stat(const char *pathnamepathname, struct stat *, struct stat *bufbuf););
int fstat(int int fstat(int fildesfildes, struct stat *, struct stat *bufbuf););
int lstat(const char *int lstat(const char *pathnamepathname, struct stat *, struct stat *bufbuf););
all three return: 0 if OK, -1 on errorall three return: 0 if OK, -1 on error
System Programming 5
File typesFile types Regular fileRegular file Directory fileDirectory file
– A file that contains the names of other files and pointers to information on A file that contains the names of other files and pointers to information on these filesthese files
Character special fileCharacter special file– A type of file used for certain types of devicesA type of file used for certain types of devices
Block special fileBlock special file– A type of file typically used for disk devicesA type of file typically used for disk devices
FIFO (Named pipe)FIFO (Named pipe)– A type of file used for interprocess communicationA type of file used for interprocess communication
SocketSocket– A type of file used for network communicationA type of file used for network communication
Symbolic linkSymbolic link– A type of file that points to another fileA type of file that points to another file
System Programming 6
File types (cont’d)File types (cont’d) Encoded in Encoded in st_modest_mode macros defined in <sys/stat.h>macros defined in <sys/stat.h>
S_ISLNK()
regular file
S_ISFIFO()
S_ISBLK()
S_ISCHR()
S_ISDIR()
S_ISREG()
S_ISSOCK()
directory file
character specific file
block specific file
pipe or FIFO
symbolic link
socket
File Type Macroint main(int argc, char *argv[]){int i; char *ptr;struct stat buf;for (i = 1; i < argc; i++) { printf("%s: ", argv[i]); if (lstat(argv[i], &buf) < 0) { err_ret("lstat error"); continue; } if (S_ISREG(buf.st_mode)) ptr = "regular"; else if (S_ISDIR(buf.st_mode)) ptr = "directory"; else if (S_ISCHR(buf.st_mode)) ptr = "character special"; else if (S_ISBLK(buf.st_mode)) ptr = "block special"; else if (S_ISFIFO(buf.st_mode)) ptr = "fifo"; ... else ptr = "** unknown mode **"; printf("%s\n", ptr);} exit(0);}
System Programming 7
IDs in UnixIDs in Unix
IDs associate with every processIDs associate with every process
– real user and group IDreal user and group ID : who we really are : who we really are• from the password file when we log in (by login)from the password file when we log in (by login)• only superuser process can change it, otherwise never change.only superuser process can change it, otherwise never change.
– effective user and group IDeffective user and group ID, , supplementary group IDssupplementary group IDs • determine file access permissionsdetermine file access permissions• usually effective user and group ID are real user and group IDusually effective user and group ID are real user and group ID
– saved set-user-ID, saved set-group-IDsaved set-user-ID, saved set-group-ID : saved by exec : saved by exec functionsfunctions
• contain copies of the effective user and group ID when a program contain copies of the effective user and group ID when a program executedexecuted
System Programming 8
Process ID vs File OwnerProcess ID vs File Owner
Process IDProcess ID File OwnerFile Owner
– Owner Owner st_uidst_uid
– Group owner Group owner st_gidst_gid
Usually when process executesUsually when process executes– Effective (group) ID equals real (group) IDEffective (group) ID equals real (group) ID
ExceptionException– Special flag (in file’s Special flag (in file’s st_modest_mode): set-user-ID & set-group-ID): set-user-ID & set-group-ID
– ““when this file is executed, set the effective user ID of the when this file is executed, set the effective user ID of the process to be the owner of the file”process to be the owner of the file”
– passwd(1) program is a set-user-ID programpasswd(1) program is a set-user-ID program
System Programming 9
Set-User-ID and Set-Group-IDSet-User-ID and Set-Group-ID Encoded in Encoded in st_modest_mode Set-user-ID (S_ISUID)Set-user-ID (S_ISUID)
– regular fileregular file• Set effective user ID on execution to the program file’s owner ID.Set effective user ID on execution to the program file’s owner ID.ex) passwd(1) runs as the rootex) passwd(1) runs as the root-r-s--x--x 1 root root 22312 Sep 26 00:52 passwd-r-s--x--x 1 root root 22312 Sep 26 00:52 passwd
– directory : not useddirectory : not used Figure 4.6Figure 4.6
#define S_IRUSR 00400 /* read permission: owner */#define S_IRUSR 00400 /* read permission: owner */
#define S_IWUSR 00200 /* write permission: owner */#define S_IWUSR 00200 /* write permission: owner */
#define S_IXUSR 00100 /* execute permission: owner */#define S_IXUSR 00100 /* execute permission: owner */
#define S_IRGRP 00040 /* read permission: group */#define S_IRGRP 00040 /* read permission: group */
#define S_IWGRP 00020 /* write permission: group */#define S_IWGRP 00020 /* write permission: group */
#define S_IXGRP 00010 /* execute permission: group */#define S_IXGRP 00010 /* execute permission: group */
#define S_IROTH 00004 /* read permission: other */#define S_IROTH 00004 /* read permission: other */
#define S_IWOTH 00002 /* write permission: other */#define S_IWOTH 00002 /* write permission: other */
#define S_IXOTH 00001 /* execute permission: other */#define S_IXOTH 00001 /* execute permission: other */
System Programming 10
File Access PermissionsFile Access Permissions File access permissionsFile access permissions
– All the file types have permissions (All the file types have permissions (encoded in st_modeencoded in st_mode))
– User/Group/Other – Read/Write/Execute (Figure 4.6)User/Group/Other – Read/Write/Execute (Figure 4.6)– File’s owner and group owner IDs (File’s owner and group owner IDs (specified by st_uid and st_gidspecified by st_uid and st_gid))
ex) ex) -rwxr-xr-x 1 -rwxr-xr-x 1 sikimsikim sal sal 49844 Sep 24 1999 test 49844 Sep 24 1999 test
File access rulesFile access rules– Read permissionRead permission for a file to open the file with O_RDONLY for a file to open the file with O_RDONLY
and O_RDWR.and O_RDWR.– Write permissionWrite permission for a file to open the file with O_WRONLY, for a file to open the file with O_WRONLY,
O_RDWR and O_TRUNC.O_RDWR and O_TRUNC.
– Execution permissionExecution permission for a file to execute the file using exec for a file to execute the file using exec functions (The file also has to be a regular file).functions (The file also has to be a regular file).
System Programming 11
File Access Permissions (cont’d)File Access Permissions (cont’d) Directory access rulesDirectory access rules
– Read permissionRead permission for a directory to obtain a list of all the file for a directory to obtain a list of all the file names in the directory.names in the directory.
– Execution permissionExecution permission for a directory to pass through the for a directory to pass through the directory when it is a component of a pathname to access.directory when it is a component of a pathname to access.
Ex) to access file /usr/dict/words, need execution permissions Ex) to access file /usr/dict/words, need execution permissions for /, /usr, /usr/dictfor /, /usr, /usr/dict
– Write and execution permissionsWrite and execution permissions for a directory to create a new for a directory to create a new file in the directory.file in the directory.
– Write and execution permissionsWrite and execution permissions for a directory to delete a file for a directory to delete a file in the directory (in the directory (butbut no need of read or write permission for no need of read or write permission for the file itself! cf: sticky bit).the file itself! cf: sticky bit).
System Programming 12
File Access TestFile Access Test Done by kernel each time a process opens, creates or deletes a Done by kernel each time a process opens, creates or deletes a
file.file.– Depends on two owner IDs associated with a file and two Depends on two owner IDs associated with a file and two effectiveeffective IDs IDs
with a process.with a process.
The conditions to allow the access of a file.The conditions to allow the access of a file.1.1. The effective user ID of the process is 0 (superuser).The effective user ID of the process is 0 (superuser).
2.2. The effective user ID equals the owner ID of the file and the appropriate The effective user ID equals the owner ID of the file and the appropriate user access permission bit is set, such as user-read bit for read access.user access permission bit is set, such as user-read bit for read access.
3.3. The effective group ID or one of the supplementary group IDs of the The effective group ID or one of the supplementary group IDs of the process equals the group ID of the file and the appropriate group access process equals the group ID of the file and the appropriate group access permission bit is set.permission bit is set.
4.4. The appropriate other access permission bit is set.The appropriate other access permission bit is set. The four steps are tried in sequence.The four steps are tried in sequence. If the access is denied at any step, no further step is tried.If the access is denied at any step, no further step is tried.
System Programming 13
File OwnershipFile Ownership Ownership of new files and directoriesOwnership of new files and directories
– The user ID of a new file (directory) is set to the The user ID of a new file (directory) is set to the effective usereffective user ID of the process.ID of the process.
– The group ID of a new file can be The group ID of a new file can be • the the effective group IDeffective group ID of the process OR of the process OR• the the group ID of the directorygroup ID of the directory where the file is being created where the file is being created
(depending on the system).(depending on the system).
System Programming 14
access functionaccess function
Access test based on the real UID/GID (Not on the effective UID/GID)Access test based on the real UID/GID (Not on the effective UID/GID)
int access(const char *int access(const char *pathnamepathname, int , int modemode););
return: 0 if OK, -1 on errorreturn: 0 if OK, -1 on error
modemode DescriptionDescription
R_OKR_OK
W_OKW_OK
X_OKX_OK
F_OKF_OK
Test for read permissionTest for read permission
Test for write permissionTest for write permission
Test for execute permissionTest for execute permission
Test for existence of fileTest for existence of file
If (access(argv[1], R_OK) < 0) err_ret(“access error for %s”,argv[1]);else printf(“read access OK\n”);
If (open(argv[1], O_RDONLY) < 0) err_ret(“open error for %s”, argv[1]);else printf(“open for reading OK\n”);
$whoamikim$ ls –l a.out -rwsrwxr-x 1 uucp 105216 Jan 18 08:48 a.out$ ls -l /etc/uucp/Systems-rw-r----- 1 uucp 1441 Jul 18 15:05 /etc/uucp/Systems$ a.out /etc/uucp/Systemsaccess error for /etc/uucp/Systems: Permission deniedopen for reading OK
System Programming 15
umask functionumask function
File mode creation mask (umask)File mode creation mask (umask)– associated with every process.associated with every process.
– Any bits that are on in the file mode creation mask are turned off in the Any bits that are on in the file mode creation mask are turned off in the file’s mode of the file being created with open() or creat().file’s mode of the file being created with open() or creat().
– Changing the file mode creation mask of a process doesn’t affect the mask Changing the file mode creation mask of a process doesn’t affect the mask of its parent.of its parent.
– Usually set once, on login by the shell’s start-up file and never changed.Usually set once, on login by the shell’s start-up file and never changed.
umask() sets the file mode creation mask for the process and umask() sets the file mode creation mask for the process and returns the previous value.returns the previous value.
cmaskcmask: the bitwise OR of any of the nine constants from file : the bitwise OR of any of the nine constants from file access permission access permission st_modest_mode mask (Figure 4.4) mask (Figure 4.4)
mode_t umask(mode_t mode_t umask(mode_t cmaskcmask););
returns: previous file mode creation maskreturns: previous file mode creation mask
System Programming 16
umask function (cont’d)umask function (cont’d)
To assure the specific access permission of a file created, we must modify To assure the specific access permission of a file created, we must modify the umask value of the running process (ex: umask(0))the umask value of the running process (ex: umask(0))
main(void){ umask(0); if (creat("foo",S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
S_IROTH | S_IWOTH) < 0) err_sys("creat error for foo");
umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); if (creat("bar", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
S_IROTH | S_IWOTH) < 0) err_sys("creat error for bar"); exit(0);}
$umask 02$ a.out$ ls –l foo bar-rw------- 1 stevens 0 Nov 16 16:23 bar-rw-rw-rw- 1 stevens 0 Nov 16 16:23 foo$umask02
System Programming 17
chmod() and fchmod()chmod() and fchmod()
Change the file access permissions for an existing fileChange the file access permissions for an existing file fchmod operates on a file that has already opened.fchmod operates on a file that has already opened. mode: mode: bitwise OR of the constants shown in Figure 4.6bitwise OR of the constants shown in Figure 4.6 The effective uid of the process must equal the owner of The effective uid of the process must equal the owner of
the file to change the permission bits of the file.the file to change the permission bits of the file.
int chmod(const char *int chmod(const char *pathnamepathname, mode_t , mode_t modemode););
int fchmod(int int fchmod(int filedesfiledes, mode_t , mode_t modemode););
Both return: 0 if OK, -1 on errorBoth return: 0 if OK, -1 on error
System Programming 18
Sticky Bit (Save-text)Sticky Bit (Save-text) Sticky bit for an executable fileSticky bit for an executable file
– Used on earlier version of Unix.Used on earlier version of Unix.– Program’s text was saved in the swap space for faster loading into memory.Program’s text was saved in the swap space for faster loading into memory.– Today’s systems do not need this technique due to virtual memory and Today’s systems do not need this technique due to virtual memory and
faster file systems. faster file systems. Sticky bit for a directorySticky bit for a directory
– A file in the directory can be removed or renamed only if the user has A file in the directory can be removed or renamed only if the user has write write and executionand execution permission for the directory and either permission for the directory and either
• owns the fileowns the file• owns the directoryowns the directory• is the superuseris the superuser
Ex) /tmp – candidate for the sticky bit so that users should not be able to Ex) /tmp – candidate for the sticky bit so that users should not be able to delete or rename files owned by others.delete or rename files owned by others.
drwxrwxrwt 9 root root 4096 Mar 22 16:08 tmpdrwxrwxrwt 9 root root 4096 Mar 22 16:08 tmp
System Programming 19
chown, fchown, and lchownchown, fchown, and lchown
Change the user ID and group ID of a fileChange the user ID and group ID of a file lchown() changes the owners of the symbolic link itself, not the lchown() changes the owners of the symbolic link itself, not the
file pointed to by the symbolic link.file pointed to by the symbolic link.
int chown(const char *int chown(const char *pathnamepathname, uid_t , uid_t ownerowner, git_t , git_t groupgroup););
int fchown(int int fchown(int filedesfiledes, uid_t , uid_t ownerowner, gid_t , gid_t groupgroup););
int lchown(const char *int lchown(const char *pathnamepathname, uid_t , uid_t ownerowner, gid_t , gid_t groupgroup););
All three return: 0 if OK, -1 on errorAll three return: 0 if OK, -1 on error
System Programming 20
File SizeFile Size File size in File size in statstat structure structure
– st_sizest_size : file size in bytes. : file size in bytes.– st_blksizest_blksize : the preferred block size for I/O for the file. : the preferred block size for I/O for the file.– st_blocksst_blocks : the actual number of 512-byte blocks allocated. : the actual number of 512-byte blocks allocated.
Holes in a fileHoles in a file– Created by seeking past the current end of file and writing some dataCreated by seeking past the current end of file and writing some data– Ex) holes in core file Ex) holes in core file
• dudu reports the disk space used by the file is only 139,264 bytes (512x 272) reports the disk space used by the file is only 139,264 bytes (512x 272)• read() returns 0 for any byte positions in holesread() returns 0 for any byte positions in holes• When copying the file, all holes are written out as actual data bytes of 0When copying the file, all holes are written out as actual data bytes of 0
$ls –l core-rw-r—r– 1 stevens 8483248 Nov 18 12:18 core$du –s core272 core$cat core > core.copy$du –s core272 core16592 core.copy
System Programming 21
File TruncationFile Truncation
Truncate an existing file to Truncate an existing file to lengthlength bytes. bytes. If the previous size > If the previous size > lengthlength, ,
the data beyond length is no longer accessible.the data beyond length is no longer accessible. If the previous size < If the previous size < lengthlength, ,
the effect is system dependent.the effect is system dependent.
int truncate(const char *int truncate(const char *pathnamepathname, off_t , off_t lengthlength););
int ftruncate(int int ftruncate(int filedesfiledes, off_t , off_t lengthlength););
Both return: 0 if OK, -1 on errorBoth return: 0 if OK, -1 on error
System Programming 22
Overview of the Unix File SystemsOverview of the Unix File Systems File system : consist of a sequence of File system : consist of a sequence of logicallogical blocks blocks
File system
partitionpartitionpartition
Boot block(s)
Super block
Device drive
… I-nodeI-nodeI-node
Figure 4.7 Device drive, partitions, and a file system
I-list Directory blocks and data blocks
System Programming 23
Overview of the Unix File SystemsOverview of the Unix File Systems Super blockSuper block
– containscontains• The size of the file system, the size of the I-node listThe size of the file system, the size of the I-node list• The number of free blocks/I-nodes in the file systemThe number of free blocks/I-nodes in the file system• A list of free blocks/I-nodes in the file systemA list of free blocks/I-nodes in the file system
I-node : internal representation of a fileI-node : internal representation of a file– containscontains
• description of the disk layout of the file datadescription of the disk layout of the file data• file information (e.g. owner, access permission, access time, a link count…)file information (e.g. owner, access permission, access time, a link count…)
– one-to-one mapping with a file (cf. link())one-to-one mapping with a file (cf. link()) DirectoryDirectory
– file namefile name– I-node number (in the I-node number (in the samesame file system, not in the different file system) file system, not in the different file system)
Data blocksData blocks
System Programming 24
I-nodeI-node
direct ptr’s
singleindirect ptr
doubleindirect prt
tripleindirect ptr
Data B
locks
File owner identifierFile typeFile Access permissionsFile access times# of links to the fileFile size
contain ptr’s to data blockfile information
ptr’s to data blocks
struct stat
System Programming 25
I-listdirectory
block
directory
block
data
block
data
block
data
block
I-node
DirBI-node
I-node
100
I-node
DirA
100 File name = a
I-nodenumber
File name = b
directory blocks and data blocks
third data block
second data block
first data block
File system in more detailFile system in more detail
System Programming 26
I-node1267lc = 3
I-node
0
I-node2549lc= 2
directory
blocki-list
directory
block
directory blocks and data blocks
.1267
2549
..
2549 testdir2549
1267
...I-node
number
Fig4.9 sample filesystem after creating the directory testdir (mkdir testdir)
System Programming 27
link()link()
Creates a new directory entry, Creates a new directory entry, newpath, newpath, that references that references the existing file, the existing file, existingpathexistingpath
– increment the link count of the I-node for the existing file.increment the link count of the I-node for the existing file.
– The creation of the new directory entry and the increment must The creation of the new directory entry and the increment must be an be an atomic operationatomic operation..
– Only a superuser process can create a new link that points to a Only a superuser process can create a new link that points to a directory because this could cause loops in the file system. directory because this could cause loops in the file system.
– Hard links created by link() can’t cross file systems.Hard links created by link() can’t cross file systems.
int link(const char *int link(const char *existingpathexistingpath, const char *, const char *newpathnewpath););
return: 0 if OK, -1 on errorreturn: 0 if OK, -1 on error
link(“/dirA/name1”, “/dirB/name2”);
System Programming 28
What really happens with link()What really happens with link()
12345 name1
I-node name
directory entry in /dirA
I-node12345
...
...
...
st_link=1
23567
block23567
“This is thetext in thefile.”
12345 name2
I-node name
directory entry in /dirB
link(“/dirA/name1”, “/dirB/name2”);
Two hard links to the same file
st_link=2
System Programming 29
unlink()unlink()
Removes the directory entry and decrements the link count of the Removes the directory entry and decrements the link count of the file referenced by file referenced by pathnamepathname– must have write and execute permission in the directory containing the must have write and execute permission in the directory containing the
file/directory.file/directory.
Only when link count reaches 0, the contents of the file can be Only when link count reaches 0, the contents of the file can be deleteddeleted– if some process has the file open, the file will not be deleted.if some process has the file open, the file will not be deleted.
– What kernel does when a file is closedWhat kernel does when a file is closed• checks if the count of the number of processes that have the file open is 0checks if the count of the number of processes that have the file open is 0• checks if the link count is 0checks if the link count is 0• if both are yes, the file’s contents are deleted.if both are yes, the file’s contents are deleted.
int unlink(const char *int unlink(const char *pathnamepathname););
return: 0 if OK, -1 on errorreturn: 0 if OK, -1 on error
System Programming 30
unlink() - continuedunlink() - continued
– A way to assure that a temporary file not left when the program A way to assure that a temporary file not left when the program crashes - call unlink crashes - call unlink immediately afterimmediately after open or create the file. open or create the file.
– If If pathname pathname is a symbolic link, unlink references the symbolic is a symbolic link, unlink references the symbolic link, not the file referenced by the linklink, not the file referenced by the link
– The superuser can call unlink with directory pathname, but use The superuser can call unlink with directory pathname, but use rmdir() to unlink a directoryrmdir() to unlink a directory
– unlink a file or directoryunlink a file or directory• For a file, remove() = unlink()For a file, remove() = unlink()• For a directory remove() = rmdir()For a directory remove() = rmdir()
int remove(const char *int remove(const char *pathnamepathname););
return: 0 if OK, -1 on errorreturn: 0 if OK, -1 on error
System Programming 31
rename()rename()
rename a file or directoryrename a file or directory– if if oldnameoldname is a file and is a file and newnamenewname exists(and is not a directory), exists(and is not a directory),
the the newnamenewname is removed and is removed and oldname oldname is renamed to is renamed to newname.newname.• Need write permission for both directories containing Need write permission for both directories containing oldnameoldname and and
newnamenewname
– if oldname is a directory and newname exists, the newname must if oldname is a directory and newname exists, the newname must refer to a directory and that directory must be empty.refer to a directory and that directory must be empty.
• oldnameoldname is renamed to is renamed to newnamenewname
– Renaming a file without changing file system is done by Renaming a file without changing file system is done by updating directory entries pointing to the file’s i-node without updating directory entries pointing to the file’s i-node without moving the actual contents of the file.moving the actual contents of the file.
int rename(const char *int rename(const char *oldname, const char *newnameoldname, const char *newname););
return: 0 if OK, -1 on errorreturn: 0 if OK, -1 on error
System Programming 32
Symbolic linksSymbolic links
A symbolic link is an indirect pointer to a fileA symbolic link is an indirect pointer to a file
– A function that refer to a file by name may or may not follow a A function that refer to a file by name may or may not follow a symbolic link; i.e. it may operate on symbolic link; i.e. it may operate on the symbolic link itselfthe symbolic link itself or or the file referred tothe file referred to by the symbolic link. (See Figure 4.10) by the symbolic link. (See Figure 4.10)
hard link limitationshard link limitations– normally requires the link and the file in the same file systemnormally requires the link and the file in the same file system
– Only the superuser can create a hard link to a directoryOnly the superuser can create a hard link to a directory
$ ln -s /home/no_file myfile$ ls -l myfilelrwxrwxrwx 1 stevens 13 Dec 6 07:27 myfile -> /home/no_file
System Programming 33
Symbolic linksSymbolic links
12345 name1
I-node name
directory entry in /dirA
I-node12345
...
...
...
1
23567
block23567
“This is thetext in thefile.”
13579 name2
I-node name
directory entry in /dirB
I-node13579
...
...
...1
15123
block15123
“/dirA/name1”
symlink(“/dirA/name1”, “/dirB/name2”);
System Programming 34
symlink()symlink()
symlink()symlink()– create a new directory entry, create a new directory entry, sympath, sympath, whichwhich points topoints to
actualpathactualpath
– actualpath actualpath does not have to existdoes not have to exist
– The permissions of a symbolic link are irrelevantThe permissions of a symbolic link are irrelevant• The ownership is ignored when following the linkThe ownership is ignored when following the link• The ownership is checked when The ownership is checked when removal removal oror renaming renaming of the link is of the link is
requested and the link is in a directory with the requested and the link is in a directory with the sticky bitsticky bit set. set.
int symlink(const char *int symlink(const char *actualpath, const char *sympathactualpath, const char *sympath););
returns: 0 if OK, -1 on errorreturns: 0 if OK, -1 on error
System Programming 35
readlink()readlink()
readlink()readlink()– open the symbolic link itself and read the name in the link into open the symbolic link itself and read the name in the link into
the the buf buf and close itand close it. . (open() function follows a symbolic link)(open() function follows a symbolic link)
int readlink(const char *int readlink(const char *pathnamepathname, char *, char *bufbuf, int , int bufsizebufsize););
returns: number of bytes read if OK, -1 on errorreturns: number of bytes read if OK, -1 on error
System Programming 36
File Times (utime function)File Times (utime function)
changes the access and modification time of a filechanges the access and modification time of a file– times = NULL, the access time and the modification time are set to the current timetimes = NULL, the access time and the modification time are set to the current time– time time NULL, the access time and the modification time are set to the values in the NULL, the access time and the modification time are set to the values in the
structure pointed to by structure pointed to by timestimes
st_ctime is automatically updated when utime() is called.st_ctime is automatically updated when utime() is called. The time values are calendar times (see Section 1.10)The time values are calendar times (see Section 1.10)
FieldField DescriptionDescription ExampleExample ls optionls option
st_atimest_atime last access time of file datalast access time of file data readread -lu-lu
st_mtimest_mtime last-modification time of file datalast-modification time of file data writewrite -l-l
st_ctimest_ctime last-change time of I-node statuslast-change time of I-node status chmod, chownchmod, chown -lc-lc
int utime(const char *int utime(const char *pathname, pathname, const struct utimbufconst struct utimbuf *times*times););
returns: 0 if OK, -1 on errorreturns: 0 if OK, -1 on error struct utimbuf { time_t actime; /* access time */ time_t modtime; /* modification time */ }
System Programming 37
mkdir() and rmdir()mkdir() and rmdir()
mkdir()mkdir()– creates a new, empty directorycreates a new, empty directory
– The entries for dot and dot-dot are automatically createdThe entries for dot and dot-dot are automatically created
rmdir()rmdir()– The directory is freed if the link count of the directory The directory is freed if the link count of the directory
becomes 0 and no other process has the directory open.becomes 0 and no other process has the directory open.
int mkdir(const char *int mkdir(const char *pathname, pathname, mode_tmode_t mode mode););
return: 0 if OK, -1 on errorreturn: 0 if OK, -1 on errorint rmdir(const char *int rmdir(const char *pathnamepathname););
return: 0 if OK, -1 on errorreturn: 0 if OK, -1 on error
System Programming 38
Reading DirectoriesReading Directories
Open and read directory entriesOpen and read directory entries– Directories can be read by anyone with read access Directories can be read by anyone with read access
permissions; but only kernel can write to a directory!permissions; but only kernel can write to a directory!
– opendir() returns the pointer to a DIR structure that is used by opendir() returns the pointer to a DIR structure that is used by other three functions; other three functions;
• It initializes things so that the first readdir reads the first entry in the It initializes things so that the first readdir reads the first entry in the directory; The ordering of entries is implementation dependentdirectory; The ordering of entries is implementation dependent
DIR *opendir(const char *DIR *opendir(const char *pathnamepathname))
returns: pointer if OK, NULL on errorreturns: pointer if OK, NULL on errorstruct dirent *readdir(DIR *struct dirent *readdir(DIR *dpdp););
returns: pointer if OK, NULL at end of directory or errorreturns: pointer if OK, NULL at end of directory or errorvoid rewinddir(DIR *void rewinddir(DIR *dpdp););
int closedir(DIR *int closedir(DIR *dpdp););
returns: 0 if OK, -1 on error returns: 0 if OK, -1 on error
struct dirent {
/* I-node number */
ino_t d_ino:
/* null terminated filename*/
char d_name[NAME_MAX + 1];
}
System Programming 39
chdir(), fchdir() and getcwd()chdir(), fchdir() and getcwd()
chdir() and fchdir() chdir() and fchdir() – change the change the current working directorycurrent working directory– Every process has a current working directory; i.e. it is an Every process has a current working directory; i.e. it is an
attribute of a processattribute of a process– chdir() can not affect a parent processchdir() can not affect a parent process
getcwd() getcwd() – copies the absolute pathname of the current working directory copies the absolute pathname of the current working directory
to the array pointed to by to the array pointed to by bufbuf, which is of length , which is of length sizesize..
int int chdir(const char *chdir(const char *pathnamepathname););
intint fchdir(int fchdir(int filedesfiledes););
Both returns: 0 if OK, NULL on errorBoth returns: 0 if OK, NULL on errorcharchar *getcwd(char **getcwd(char *buf, buf, size_tsize_t sizesize););
Returns: Returns: bufbuf if OK, NULL on error if OK, NULL on error
System Programming 40
Special Device FilesSpecial Device Files
– Every filesystem is known by its major and minor device Every filesystem is known by its major and minor device number encoded in the primitive system data type dev_t.number encoded in the primitive system data type dev_t.
– Two macros, major() and minor() to access the major and Two macros, major() and minor() to access the major and minor device numbers, respectively.minor device numbers, respectively.
– The The st_dev st_dev for every filename is the device number for the for every filename is the device number for the filesystem containing that filename and its i-node.filesystem containing that filename and its i-node.
– The The st_rdev st_rdev contains the device number for the actual device, contains the device number for the actual device, character special files or block special filescharacter special files or block special files
$ ls -l /dev/sd0[ah] /dev/tty[ab]brw-r----- 1 root 7, 0 Jan 31 08:23 /dev/sd0abrw-r----- 1 root 7, 7 Jan 31 08:23 /dev/sd0hcrw-rw-rw- 1 root 12, 0 Jan 31 08:23 /dev/ttyacrw-rw-rw- 1 root 12, 1 Jan 31 08:23 /dev/ttyb
System Programming 41
Special Device Files (cont’d)Special Device Files (cont’d)
$ a.out / /home/stevens /dev/tty[ab]/: dev = 7/0/home/stevens: dev = 7/7dev/ttya: dev = 7/0 (character) rdev = 12/0dev/ttyb: dev = 7/0 (character) rdev = 12/1
main(int argc, char *argv[]) { int i; struct stat buf; for (i = 1; i < argc; i++) { printf("%s: ", argv[i]); if (lstat(argv[i], &buf) < 0) { err_ret("lstat error"); continue; } printf("dev = %d/%d", major(buf.st_dev), minor(buf.st_dev) ); if (S_ISCHR(buf.st_mode) || S_ISBLK(buf.st_mode) ) { printf(" (%s) rdev = %d/%d",
(S_ISCHR(buf.st_mode) ) ? "character" : "block", major(buf.st_rdev), minor(buf.st_rdev)); } printf("\n"); } exit(0);}
System Programming 42
Mount/UnmountMount/Unmount
Mount: connecting a file system to an existing hierarchyMount: connecting a file system to an existing hierarchy Unmount: disconnect that which was mountedUnmount: disconnect that which was mounted
Example : two partitions on /dev/hdaExample : two partitions on /dev/hda– ntfs filesystem (MS windows): /dev/hda0ntfs filesystem (MS windows): /dev/hda0
– ext3 filesystem: /dev/hda1 (mounted on / )ext3 filesystem: /dev/hda1 (mounted on / )
– $ mount –t ntfs /dev/hda0 /mnt$ mount –t ntfs /dev/hda0 /mnt//
etcetc usrusrmntmnt varvar
Documents Documents
and Settingsand SettingsProgram FilesProgram Files WINDOWSWINDOWS
System Programming 43
sync() and fsync()sync() and fsync()
Delayed writeDelayed write– writing data to a file normally copies the data into one of writing data to a file normally copies the data into one of
buffers in the kernel (buffer cache) and queues it for I/O at buffers in the kernel (buffer cache) and queues it for I/O at some later timesome later time
– sync() queues all the modified block buffers for writing and sync() queues all the modified block buffers for writing and returns; does not wait for the actual I/O to take placereturns; does not wait for the actual I/O to take place
– sync() is normally called every 30 seconds from a system sync() is normally called every 30 seconds from a system daemondaemon
– fsync() refers only to a single file; waits for the I/O complete fsync() refers only to a single file; waits for the I/O complete before returning; a database application may use itbefore returning; a database application may use it
void void sync(void);sync(void);
intint fsync(int fsync(int filedesfiledes););
Returns: 0 if OK, -1 on errorReturns: 0 if OK, -1 on error