
Agenda • Working with files and directories – lseek – stat, fstat, lstat – access Working with Files and more – umask – link, unlink, rename – symlink, readlink – mkdir, rmdir – chdir, fchdir, getcwd • Bibliography – Stevens chapter 4 – man pages lseek() File offset reposition read/write file offset • Every open file has an associated “current file • off_t lseek(int fildes, off_t offset, int whence) offset” • whence can be • This is a nonnegative integer that measure the – SEEK_SET : the offset is set to offset bytes – SEEK_CUR : the offset is set to its current location plus offset number of bytes from the beginning of the file butes • Read and Write operations normally starts at the – SEEK_END : the offset is set to the size of the file plus offset current file offset and cause the offset to be bytes • It is possible to set the file offset beyond the size of the incremented file. This is referred to as creating a hole in a file. • By default the current file offset is set to 0 when • Any bytes that have not been written are read back as 0 the file is opened (except when the O_APPEND option is used) offset 0 end of file data hole data Files with holes Seek (cont …) char buff1[]="abcdefghij"; char buff2[]="ABCDEFGHIJ"; int main() • It is not possible to perform seek on every file, for { int fd; example on terminals it is not possible to seek if ((fd = open("/tmp/file.hole", O_CREAT | O_TRUNC | O_WRONLY, 0755)) == -1) • The following program test if seek can be done { perror("Error in open\n"); exit(1); int } if (write(fd, buff1, 10) != 10) main() 4 4 4 { abcdefghij 1212131213121 ABCDEFGHIJ 576 8 8:9 ;:< 836 < { 5 perror("Error in write\n"); =2>3? 8 8:9 if(lseek(STDIN_FILENO, 0, SEEK_CUR) == -1) <@6 exit(1); > D2D 82E DF6 82839 perror("Cant seek\n"); ACB ¡ ¢ ¢ £ £ ¤ ¤ ¥ ¥ ¦ ¦ £ £ § § ¨ ¨ © © £ £ § § § § ¦ ¦ ¡ ¡ § § ££ ¦ ¦ ¢ ¢ ¡ ¡ £ £ ¡ ¡ } ¡ ¡ ¡ ¢ ¢ £ £ ¤ ¤ ¥ ¥ ¦ ¦ £ £ § § ¨ ¨ © © £ £ § § § § ¦ ¦ ¡ ¡ § § ££ ¦ ¦ ¢ ¢ ¡ ¡ £ £ ¡ ¡ !! ¢#"%$¢#" $ $ $ $ $ $ $ $ $ $ $ !! ¢#"¢#" $ $ $ $ $ $ $ $ $ $ $ $ if (lseek(fd, 40, SEEK_SET) == -1) else ¨ ¨ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ¨ ¨ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ { >>seek_test < /etc/fstab $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ &'(&'( ) ) * * +#,+#, - - $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ &'(&'( ) ) * * +#,+#, - - perror("Error in lseek()\n"); printf("seek OK\n"); . / / 0 0 . / / 0 0 exit(1); seek OK ¨ ¨ . ¨ ¨ . } return 0; >> if (write(fd, buff2, 10) != 10) { } perror("Error in write\n"); exit(1); } return 0; } stat(), fstat() and lstat() stat() fstat() and lstat() (continued) file information • int stat(const char *file_name, struct stat *buf); • stat() return information about the named file • int fstat(int filedes, struct stat *buf); • int lstat(const char *file_name, struct stat *buf); • fstat() return information about an open file • lstat() return information about the symbolic link, ¡ ¢ £ ¤ ¥ ¢¦¡ ¢ § ¢ ¨ © © © ¡ ¢ ¥ ¢ not the file referenced by the symbolic link © ¢ ¡ ¢ © © ¢ ¡ ¢ £ ¢ ¥ ¢ ¦ ¦ © ¢ ¡ ¢ ¤ £ §£ ¡ © ©! % ¤ ¢ ¡ ¢ ¤ ¤ ¡ £#" $ £ © ©! % & ¢ ¡ ¢ & & £ ¤ " $ £ © © ' © *) © © + ¢ ¡ ¢ £ ¥ ¢ ( ¥ , , -. ¢ ¡ ¢ ¡ ¢ ¢ § ¡ ( ¢ ¡ © , ! / , 0¦ ¤ ¡ & &¦¡ ¢ ¡ ¥ ¡ £ ¡ ( ¡ ¢ ¦" © 1 ¦ © ¤ ¡ & &¦¡ ¢ ¥ ¡ ¤ £ ¥ ¡§ ¥ § ¢ ! ¢ ¢ ¡ ¢ § ¢ ¢ § ¡ ¢§ ¥ ¥ ¡ ¡ ! © ¢ ¢ ¡ ¢ ¢ ¢ § ¡ ¢ ¥ § ¢ ! ¢ ¢ ¡ ¢ ¥ ¢ ¢ § ¡ ¢¥ § & 2 File types File types (cont…) • Regular file : The most common type of file, there is no • The type of a file is encoded in the st_mode distinction to the Unix kernel whether the data is text or member of the stat structure binary • The following macros can be used to determine • Directory file : A file that contains the names of other files the file type: and pointers to information on these files – S_ISREG(m) is it a regular file? • Character special file : A type of file used for certain – S_ISDIR(m) directory? types of devices – S_ISCHR(m) character device? • Block special file : A type of file used for disk devices – S_ISBLK(m) block device? • FIFO : A type of file used for interprocess communication – S_ISFIFO(m) fifo? between processes (also known as named pipe) – S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.) • Socket : A type of file used for network communication – S_ISSOCK(m) socket? (Not in POSIX.1-1996.) between processes • Symbolic link : A type of file that points to another file Set-User-ID and Set-Group-ID Set-User-ID (cont..) • Every process has six IDs associated with it • This feature is useful when we want a user to execute a certain ACBDEA=6/467'8'8FG7C4 6 35467'8:9<;6=4?>@ program in super-user permission D'9'J:>@ 35467'8EHI4 • For example passwd is a set-uid file % © £ Y ¡ Z [ \'] ^ ¢ § ¥ ¡ ¡ § ¡ _ ¡` § ¤ ¡ £ § ¡ ¡ 3/6'K'K6'L!M<N!O<6:9<;6=4?>@ 9<;6!PQKDC4EKN'86:7'LL'6<;; ` £ ` a a ` ` b ` ` bc[d£ ¢e£ ¢ [ f \ g hji ¤ &kglZ m m [ a a 3/6'K'K6'L!M<N!O<6EHI4 D'9'J % © ¤ ¡ £ § ¡ ¡ 3R;!7O<6PG;6!M=S!9<;6=4!S!>@ ;!7O<6PQTF/6U=6LQK9V'LM'ND'V 3R;!7O<6PG;6!M=SWX4 D'9'J'S>!@ • Normally the effective user id equal the real user id • The set-user-id (set-group-id) bit are contained in the st_mode • Every file has an owner, the owner is specified by the st_uid member of the stat structure member • The constant S_ISUID (S_ISGID) can be used to test this bit • There is a capability to set a special flag in the file’s mode word (st_mode) that says “when the file is executed set the effective uid of the process to the owner of the file” File Access Permissions access() • The st_mode also encode the st_mode mask Meaning • int access(const char *pathname, int mode); file permissions bits S_IRUSR User read • from the shell the command • access checks whether the process would be allowed to chmod can be used to change S_IWUSR User write read, write, execute the file or test for existence file permissions S_IXUSR User execute • mode is a mask of one or more of: • whenever we want to open any type of file by name, we must S_IRGRP Group read – R_OK test for read permission have execute permissions on – W_OK test for write permission S_IWGRP Group write the directories in the name – X_OK test for execute permission • we cannot create a new file in S_IXGRP Group execute – F_OK test for existence of file a directory unless we have write and execute permissions S_IROTH Other read • The check is done with the process’s real uid and gid, in the directory S_IWOTH Other write rather then with the effective id’s. This is to allow set-UID • to delete a file we need write programs to easily determine the invoking user authority and execute permission in the S_IXOTH Other execute directory umask() File system structure • mode_t umask(mode_t cmask); disk partition partition partition • Every process has a file mode creation mask drive • The umask() function set the file mode creation mask for the process and return the previous value file • The file mode creation mask is used whenever the i-list directory blocks and data blocks system process creates a new file or a new directory • recall that the open() functions get a mode argument boot block • Any bits that are on in the file mode creation mask are super block turned off in the file’s mode i-node i-node ¡¢¡¢¡ i-node i-node File System structure (cont…) Hard Links i-list data data directory directory block block block block • i-nodes are fixed-length entries that contain most of the information about a file k ck c blo lo ta b da • directory entry contain the i-node number and the file- ta t a irs d f t name rs fi i-node file name • we can see 2 directory entries that point to the same i- i-node i-node i-node i-node number node entry. This is a hard link • from the shell the command “ln” can be used to create hard links i-node file name number • Every i-node has a link-count , which is the number of directory entries that point to this i-node • Only when the link count reach 0 the file can be deleted • What is the initial link-count of a new empty directory?? link(), unlink() and rename() Temporary files • int link(const char *oldpath, const char *newpath); • A process can open a temporary file, unlink it and continue to work • int unlink(const char *pathname); with it. • int rename(const char *oldpath, const char *newpath); • The files data will be removed only when the process exit • This feature can be used by programs to assure that temporary files • link() create a new directory entry that point to the same inode won’t be left around in case the program crashes oldpath is pointing ©¢¡ ) £ £ - 0 ¤ ¤§¦I0 ¨ ¤ © ¦ 0 ¤ ¨- + ¢ $ ¥ i m g • The creation of the directory entry and the increment of the link ¢ ) ©¢¡ ¡ + [ count must be atomic why?? ` © ¤ ¤ 0 ¤ ) £ © j £ + £ £ £ ) ) £ £ +¢¡ ¡ + ¢ ¢ ` [ • unlink() remove a directory entry and decrement the i-node link ¤ © ¤ ¤ 0 ¤ ) £ © £ + £ £ ¤ count £ % #% £ & £ § ¦¥ ¢ ¤ ¢ £ ¢ ¢ • If the link count reach 0 the i-node is removed ¢ • not exactly, only when no process holds the file open that file can be removed Symbolic Links Symbolic Links (cont…) • Hard links have several
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages5 Page
-
File Size-