File System ImplementationContentsUNIX file structure implementationSystem File TableOpening and Closing FilesFle Access ExampleAdditional OflagsAccess Control sys/stat.hCloseSELECTFile Pointers and BufferingFilters and RedirectionDirectoriespathList files in a directoryFile StatusStruct StatPOSIX Macros for mode_tUNIX Special FilesPipesParent writes string to ChildSlide 22Summary01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved1File System ImplementationCS 241 Lecture 23R: Ch 4-6 pp116-186Roy Campbell01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved2ContentsFile System TablesOpen CloseFile Permissions/Access ControlDirectoriesSpecial FilesPipes01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved3UNIX file structure implementationFile positionR/WPointer to inodeFile positionR/WPointer to inodeModeLink CountUIDGIDFile sizeTimesAddress of first 10 disk blocksSingle IndirectDouble IndirectTriple IndirectInode in memoryOpen file descriptionParent File descriptortableChildFile descriptortableUnrelated processFile descriptor tableSystem file table01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved4System File TableOne per system – is in kernelContains file offset, access mode, count of file descriptor entries using itSeveral entries may correspond to one fileThe in-memory inode table has one entry for each active file.01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved5Opening and Closing FilesOpen associates a file descriptor with a file or physical deviceFile descriptor is an index into a table per process and can be inherited to child processes allowing sharingShould close open filesOflag access modes and status01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved6Fle Access Exampleint fd;mode_t fdmode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);If ((fd = open(“info.dat”, O_RDWR | O_CREAT, fdmode)) == -1) perror(“Failed to open info.dat”);Opens a file info.dat in current directory rewriting any existing file dataif present.01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved7Additional OflagsO_APPEND – file offset to EOFO_CREAT – need to give permissionsO_EXCL – use with CREATE detects fileO_NOCTTY – prevents device from becoming controlling terminalO_NONBLOCK – return immediatelyO_TRUNC – file to beginning for write01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved8Access Control sys/stat.hR W X R W X R W Xusergroup othersS_IRUSRS_IWUSRS_IXUSRS_IRWXUS_IRGRPS_IWGRPS_IXGRPS_IRWXGS_IROTHS_IWOTHS_IXOTHS_IRWXOS_ISUID – set user ID on executionS_ISGID – set group ID on execution01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved9Close#include <errno.h>#include <unistd.h>int r_close(int fd) { int retval; while ((retval = close(fd)) == -1 && errno == EINTR) ; return retval;}01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved10SELECTMonitoring multiple files – use a process per file or use theSelect statement – can select input from one of a number of different files01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved11File Pointers and BufferingISO C standard I/O Library uses file pointers not file descriptors (p122)fflush call empties buffer to deviceFiles associated with terminals are line buffered – ie not written out until buffer is full or newline symbol entered01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved12Filters and RedirectionFilters in shellhead, tail, more, sort, grep, awk, catRedirection uses < >#include <unistd.h>int dup2(int fildes, int fildes2); Copies file descriptor entry fildes to entry fildes201/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved13DirectoriesCh 501/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved14path#include <unistd.h>int chdir(const char *path);char * getcwd(char *buf, size_t size);01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved15List files in a directory#include <dirent.h>#include <stdio.h> int main(int argc, char *argv[]) { struct dirent *direntp; DIR *dirp; if (argc != 2) { fprintf(stderr, "Usage: %s directory_name\n", argv[0]); return 1; } if ((dirp = opendir(argv[1])) == NULL) { perror ("Failed to open directory"); return 1; } while ((direntp = readdir(dirp)) != NULL) printf("%s\n", direntp->d_name); while ((closedir(dirp) == -1) && (errno == EINTR)) ; return 0;01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved16File Status#include <sys/stat.h>int stat(const char *restrict path, struct stat *restrict buf);int lstat(const char *restrict path, struct stat *restrict buf);•Lstat returns status info and info about symbolic links•Stat returns status info of files referred to by any links01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved17Struct Stat dev_t st_dev; /* device ID of device containing file*/ ino_t st_ino; /* file inode number */ mode_t st_mode;/* file mode*/ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /*user id of file*/ gid_t st_guid; /* group id of file off_t st_size; /* file size in bytes*/ time_t st_atime; /* time of last access*/ time_t st_mtime;/* time of last data modification*/ time_t st_ctime; /* time of last file status change*/01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved18POSIX Macros for mode_tmacro tests forS_ISBLK(m) block special fileS_ISCHR(m) character special fileS_ISDIR(m) directoryS_ISFIFO(m) pipe or FIFO special fileS_ISLNK(m) symbolic linkS_ISREG(m) regular fileS_ISSOCK(m) socket01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved19UNIX Special FilesCh 601/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved20Pipes#include <unistd.h>int pipe(int fildes[2]);Creates 2 file descriptorse.g.Int fd[2];If (pipe(fd) == -1) perror(“Failed to create the pipe”);01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved21Parent writes string to Child#include <stdio.h>#include <string.h>#include <unistd.h>#include <sys/types.h>#define BUFSIZE 10 int main(void) { char bufin[BUFSIZE] = "empty"; char bufout[] = "hello"; int bytesin; pid_t childpid; int fd[2]; if (pipe(fd) == -1) { perror("Failed to create the pipe"); return 1; }01/13/19 CS241 © 2005 Roy Campbell, All Rights Reserved22Parent writes string to Childbytesin = strlen(bufin); childpid = fork(); if (childpid == -1) { perror("Failed to fork"); return 1; } if (childpid) /* parent code */ write(fd[1], bufout, strlen(bufout)+1); else
View Full Document