Files and Directories
Stat functions Given pathname, stat function returns structure of information about file fstat function obtains information about the file that is already open lstat same as stat except returns information about symbolic link when file is referenced by symbolic link fstatat return file statistics about pathname relative to an open directory
buf argument Refers to structure something like
File Types Regular contains data Directory contains names of other files & pointers to information on these files Block special file provides buffered I/O access in fixed-size units to devices Character special file unbuffered I/O access in variable-sized units to devices FIFO used for communication between processes Socket used for network communication between processes Symbolic link points to another files
File Types
Set-User-ID & SetGroup-ID Every process 6 ids
File Access Permissions st_mode encodes access permission bits User owner of file Can be modified by chmod command
Usage of Permission Categories To open a file, must have execute permission if each directory mentioned in file name Read permission can open file for reading Write permission can open file for writing Need execute & write permission in directory to create a new file Need execute & write permission in directory to delete a file Need execute permission to execute a file
File Access Tests Performed by kernel If effective user ID is 0 (superuser) access is allowed If effective user ID is owner of file access specified by permission bits is allowed If effective group ID is group ID of the file, appropriate access is allowed If appropriate other access permission bit is set, access is allowed
New File Ownership User ID of new file set to effective user ID of process 2 options Group ID of new file effective group ID of process Group ID of new file group ID of directory in which file resides
Access Functions Functions used by process to test accessibility int access (const char* pathname, int mode); int faccessat (int fd, const char* pathname, int mode, int flag); mode which access to test
umask function mode_t umask (mode_t, cmask); Sets file mode creation mask & returns previous state
Change Access Functions chmod, fchmod, fchmodat chmod operates on specified file fchmod operates on a file that has already been opened fchmodat operates like chmod except that can specify relative vs. absolute pathname
Sticky Bit S_ISVTX bit Historically If set for an executable program file, first time is was executed, program code saved in swap area Technique not needed today because of paging Today If set for a directory, file in directory can be renamed or removed only if owner has write permission in directory & owns file or owns directory or is su
Changing ID's chown functions allowing changing user ID & group ID chown, fchown, fchownat, lchown Operate similarly except for symbolic links Change ownership of link not file
File Size st_size member of stat structure contains file size in bytes Holes in files Copy of file with holes contains holes
File Truncation truncate & ftruncate functions Truncate file to a specified number of bytes If # of bytes > size of file hole is created at end of file
File Systems Different implementations of file system are being used
File Systems i-nodes fixed length entries contain most of information about file
i-node Every i-node has a counter for number of directory entries pointing to it Symbolic link data blocks store name of file I-node contains all the information about file Called hard links File type Accession permission bits Size of file Pointers to data blocks Directory entry Filename I-node number
i-node Directory entry cannot refer to an i-node in a different file system Renaming a file Add new directory entry pointing to existing i-node Unlink old directory entry
Creating a Directory mkdir testdir
Link Count Number of entries in a directory which are directories Always at least 2:. and..
Link functions link & linkat used to create links to an existing file Create new entires that reference existing fi linkat specifies file descriptors & pathnames Creation of new directory entry & incrementing link count must be atomic
Unlink Functions unlink & unlinkat remove existing directory & decrement link count Must have write permission in directory when link count is 0 contents of file can be deleted unlinkat uses relative pathnames Used to remove temporary files
Rename Functions rename & renameat functions Rename a file or directory int rename (const char* oldname, const char* newname); Applies to symbolic links not files pointed to by links Cannot rename. or..
Symbolic Links Indirect pointer to a file Created to get around limitations of hard links Hard links require link & file are in same file system Only su can create a hard link to a directory Used to move file or directory to another location in system
Symbolic Links
Loops Symbolic links can produce loops Symbolic link in child directory to parent directory
Creating Symbolic Links symlink or symlinkat functions Read symbolic links with readlink or readlinkat
File Times 2008 resolution of time fields changed from seconds to seconds + nanoseconds
Change Access & Modifications Times futimesns & utimensat specify timestamps in terms of nanoseconds Specification of timestamps times argument NULL set to current time times argument pointer to 2 timespec structures & UTIME_NOW set to current time times argument pointer to 2 timespec structures & UTIME_OMIT no change times argument pointer to 2 timespec structures & different flag value set to values in timespec structures Required privileges times argument NULL - su or write permission not NULL & flag is neither UTIME_NOW nor UTIME_OMIT write permission not adequate not NULL & UTIME_OMIT no permission checks
Directory Functions mkdir & mkdirat create new empty directory Normally want at least 1 execute bits enabled allow access to filenames within directory rmdir delete empty directory
Reading Directories Reading just requires read permission Writing only done by kernel Actual format of directory depends upon implementation fdopendir conversion of open file descriptor into a DIR structure dirent structure implementation dependent Defined in <dirent.h> Contains i-node number & filename DIR structure store information about directory Pointer to DIR structure returned by opendir
Directory Functions Working directory search for all relative pathnames starts Initially user home directory chdir or fchdir functions change working directory getcwd function returns current working directory
Device Special Files st_dev & st_rdev fields for devices dev_t type which stores major & minor device numbers st_dev device number of the file system containing that filename & corresponding inode Only character special files & block special files have an st_rdev value