280 likes | 457 Views
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
E N D
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 Flexible if the size changes Disadvantage File must fit in a single disk partition
UNIX File Systems inode (continued) Storing Large Files
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.
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);
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);
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!!
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
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
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);
Warning! Warning! opendir and readdir are NOT thread-safe. DO NOT open two directories at the same time!
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 '..'
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);
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
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?
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
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
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");
Hard Link Example (contd) Q: What happens if /dirA/name1 is deleted and recreated?
Hard Link Example (contd) A:/dirA/name1 and /dirB/name2 are now two distinct files.
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
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");
Soft Link Example (contd) Q: What happens if /dirA/name1 to is deleted and recreated?
Soft Link Example (contd) A:/dirA/name1 has a different inode, but /dir/name2 still links to it.
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.
Exercise What happens in the following figure if rm /dirA/name1 is executed?
Summary UNIX File Systems i-nodes directories File operations Directory opendir, readdir, closedir File Status stat, fstat, lstat Links link, symlink, unlink