1 / 28

Navigating File Systems

Navigating File Systems. CS 241 Discussion Section Week 8 3/26/07 – 3/30/07 Stephen Kloder. 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

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 • 3/26/07 – 3/30/07 • Stephen Kloder

  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. Example 1: ds8-p1.c • Use opendir and readdir to print all the filenames in the current directory: #include <dirent.h> … DIR *dir; struct dirent *entry; dir = opendir(“.”); while(entry = readdir(dir)) printf(“%s\n”,entry->d_name); closedir(dir); • Remember to include error checking!!

  9. 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

  10. Example 2: ds8-p2.c • Modify ds8-p1.c 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

  11. 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);

  12. Warning! Warning! opendir and readdir are NOT thread-safe. DO NOT open two directories at the same time!

  13. How to recursively traverse a directory tree • Open the directory (opendir) • Read each entry (readdir) • If the file is a directory (d_type), store it (e.g. in an array of strings). • Close the directory (closedir) • Traverse each saved subdirectory, EXCEPT'.' and '..'

  14. 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);

  15. Example 3: ds8-p3.c Modify ds8-p2.c 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

  16. 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?

  17. 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

  18. 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

  19. 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");

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

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

  22. 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

  23. 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");

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

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

  26. 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.

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

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

More Related