Unformatted text preview:

Chapter 4: Files and DirectoriesFiles and DirectoriesStat, fstat, lstatStat detailsFile Types IFile Types IIDevice access via the file systemFile Types IIIFile Types IVDetermining file typeFile type frequenciesSet-User-ID and Set-Group-IDFile Access PermissionsRulesFile Access Permission ChecksOwnership of new files and directoriesAccess functionUmask functionChmod and fchmodSticky bitChown, fchown, and lchownFile sizeFile truncationFile systemsLinkunlinkremoveRenameSymbolic Linkssymlink/readlinkFile timesutimeHow file operations affect timesmkdir and rmdirReading directorieschdir, fchdir, and getcwdSpecial device filessync and fsyncChapter 4: Files and DirectoriesCMPS 105: Systems ProgrammingProf. Scott BrandtT Th 2-3:45Soc Sci 2, Rm. 167Files and Directories Chapter 3 covered basic file I/O Chapter 4 covers more details stat File attributes Special files DirectoriesStat, fstat, lstat Sys/types.h, sys/stat.h Int stat (const char *pathname, struct stat *buf) Int fstat(int fildes, struct stat *buf) Int lstat(const char *pathname, struct stat *buf) All three return 0 or -1 (on error) Provide information about the named file Fstat works on open files Lstat is like stat, but provides info about symbolic link on symbolic linksStat detailsstruct stat {mode_t st_mode; /* file type and mode (perms) */ino_t st ino; /* i-node 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 */};File Types I Regular files Most common Contain data (text, binary, etc.) Kernel considers contents to be a stream of bytes (or blocks of bytes) Directory files Contains the names of other files Also contains pointers to other files Read permission = read contents of directory Write permission = create new files in the directory Execute permission = access files in the directoryFile Types II Character special file A type of file used for certain types of devices Character-oriented devices: keyboard, mouse, … Block special file A type of file used for certain types of devices Block-oriented devices: disk, tape, …Device access via the file system Devices need to be accessible to processes Devices need to be nameable by processes Devices are generally read and written File systems provide all of this We use the file system to interface to the devices The read and write calls executed by the OS are specific to the individual devicesFile Types III FIFO A type of file used for interprocesscommunication (IPC) between files Also called a named pipe Socket A type of file used for network communication between processes Can also be used for processes on the same machineFile Types IV Symbolic Link A type of file that points to another file A hard link is a name for a file Different hard links to the same file are really two different names for the file A soft link always contains the name of a file It refers to the file indirectly through the “real” name of the fileDetermining file type File type is encoded in the st_mode member of the stat data structure Macros S_ISREG() /* regular file */ S_ISDIR() /* directory file */ S_ISCHR() /* character special file */ S_ISBLK() /* block special file */ S_ISFIFO() /* pipe or FIFO */ S_ISLNK() /* symbolic link */ S_ISSOCK() /* socket */#include <sys/types.h>#include <sys/stat.h>Int main(int argc, char *argv[]) {int i;struct stat buf;char *ptr;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”;#ifdef S_ISLNKelse if(S_ISLNK(buf.st_mode)) ptr = “symbolic link”;#endif#ifdef S_ISSOCKelse if(S_ISSOCK(buf.st_mode)) ptr = “socket”;#endifelse ptr = “unknown”;printf(“%s\n”, ptr);}exit(0);}File type frequenciesFile Type Count PercentageRegular file 30,369 91.7%Directory 1,901 5.7%Symbolic link 416 1.3%Character special 373 1.1Block special 61 0.2Socket 5 0.0FIFO 1 0.0Set-User-ID and Set-Group-ID Every process has six or more IDs Who we really are Real user ID Real group ID Taken from our entry in the password file Don’t generally change Who we are currently pretending to be Effective user ID Effective group ID Supplementary group IDs Used for file access permission checks Normally the same as the real user and group ID Can be changed via set-uid and set-gid bits in programs Passwd is a set-uid program Saved by exec() functions Saved set-user-ID Saved set-group-ID Copies of effective user ID and effective group ID when a program is executed Only meaningful when running a set-uid or set-gid programFile Access Permissions st_mode also include access permissions for the file All file types have permissions Nine permission bits S_IRUSR /* user-read */ S_IWUSR /* user-write */ S_IXUSR /* user-execute */ S_IRGRP /* group-read */ S_IWGRP /* group-write */ S_IXGRP /* group-execute */ S_IROTH /* other-read */ S_IWOTH /* other-write */ S_IXOTH /* other-execute */Rules To open a file, must have execute permission on the directory Directory read = read names of files Directory execute = access files Read permission for a file determines if we can read a file Write permission for a file determines if we can write the file Also needed for truncation To create a new file, must have write and execute permission for the directory To delete a file, must have write and execute permission for thedirectory Do not need read or write permission for the file itself To execute a file, must have execute permission for the file andexecute permission for the directoryFile Access Permission Checks If effective user ID is zero,


View Full Document

UCSC CMPS 105 - 01 - Files and Directories

Download Files and Directories
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Files and Directories and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Files and Directories 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?