1 / 26

Navigating File Systems

Navigating File Systems. CS 241 Discussion Section Week 8 10/29/07 – 11/2/07. Outline. UNIX file systems inodes directories UNIX File Operations Directory File Status Links. UNIX File Systems. inode: per-file data structure Advantage Efficient for small files

waylon
Download Presentation

Navigating File Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Navigating File Systems • CS 241 Discussion Section • Week 8 • 10/29/07 – 11/2/07

  2. Outline UNIX file systems inodes directories UNIX File Operations Directory File Status Links

  3. UNIX File Systems inode: per-file data structure Advantage Efficient for small files Flexible if the size changes Disadvantage File must fit in a single disk partition

  4. UNIX File Systems inode (continued) Storing Large Files

  5. Directories are files too! • Directories, like files, have inodes with attributes and pointers to disk blocks • Each directory contains the name and i-node for each file in the directory.

  6. Directory functions #include <unistd.h> Change the directory int chdir(const char *path); Get the current working directory char *getcwd(char *buf, size_t size); Get the maximum path length long fpathconf(int fildes, int name); long pathconf(const char *path, int name); long sysconf(int name);

  7. Directory reading functions #include <dirent.h> Open the directory DIR *opendir(const char *dirname); Close the directory int closedir(DIR *dirp); Read the directory struct dirent *readdir(DIR *dirp);

  8. What’s in a directory entry? struct dirent Member Fields char d_name[] Null-terminated file name ino_t d_fileno inode number unsigned char d_namlen Length of file name unsigned char d_type Type of file DT_REG, DT_DIR, DT_FIFO, DT_SOCK, DT_CHR, DT_BLK, DT_UNKNOWN

  9. More Directory Functions #include <dirent.h> Set the position of next readdir void seekdir(DIR *dir, off_t offset); Set the position back to the start of the directory void rewinddir(DIR *dirp); Get the current location of directory stream off_t telldir (DIR *dir);

  10. File information: stat Use the stat functions to view the file’s inode’s attributes. #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> For a file: int stat(const char *restrict path, struct stat *restrict buf); For a link: int lstat(const char *restrict path, struct stat *restrict buf); For a file descriptor: int fstat(int fildes, struct stat *buf);

  11. Useful fields and macros in struct stat • stat.st_size • File size • stat.st_mode • File type • User permissions • Etc. • stat.st_mtime • Time of last modification • S_ISDIR(stat.st_mode) • Is this a directory?

  12. Links Hard Link Directory Entry e.g. all regular files Symbolic Link Also called a Soft Link A special file that serves as a reference to another file

  13. Link Functions #include <unistd.h> To create a new link: int link(const char *oldpath, const char *newpath); Same as ln To removes an entry from the directory: int unlink(const char *path); Same as rm Returns 0 if successful, -1 with errno set if unsuccessful

  14. Hard Link Example Command Line ln /dirA/name1 /dirB/name2 C Code Segments if (link("/dirA/name1", "/dirB/name2") == -1) perror("Failed to make a new link in /dirB");

  15. Hard Link Example (contd) Q: What happens if /dirA/name1 is deleted and recreated?

  16. Hard Link Example (contd) A:/dirA/name1 and /dirB/name2 are now two distinct files.

  17. Symbolic Link Function #include <unistd.h> To create a symbolic link: int symlink(const char *oldpath, const char *newpath); Same function as command ln –s Returns 0 if successful, -1 with errno set if unsuccessful

  18. Soft Link Example Command Line ln –s /dirA/name1 /dirB/name2 C Code Segments if (symlink("/dirA/name1", "/dirB/name2") == -1) perror("Failed to create a symbolic link in /dirB");

  19. Soft Link Example (contd) Q: What happens if /dirA/name1 to is deleted and recreated?

  20. Soft Link Example (contd) A:/dirA/name1 has a different inode, but /dir/name2 still links to it.

  21. Link number • The link number (the st_nlink field in stat) tells how many directory entries link to this inode. The link number is: • Set to 1 when a file is created • Incremented when link is called • Decremented when unlink is called • The link number appears in the second column of the output of ls –l. Try it! • The link number only counts hard links, not soft links.

  22. Exercise What happens in the following figure if rm /dirA/name1 is executed?

  23. Problem 1:read a directory • Use opendir and readdir to print the names of all the files in the current directory. • You will use this program in the next two problems.

  24. Problem 2: find subdirectories • Modify your program from Problem 1 to use the member fields of struct dirent to display the inode for each file, as well as whether the file is a directory or a regular file. if (entry.d_type == DT_DIR) // File is a directory

  25. Problem 3: file information Now modify your program to also give file information about each file. • How large is each file? • Which files are world-readable? • Which files have been modified in the last 24 hours? Hint: man 2 stat

  26. Summary UNIX File Systems i-nodes directories File operations Directory opendir, readdir, closedir File Status stat, fstat, lstat Links link, symlink, unlink

More Related