270 likes | 447 Views
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
E N D
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 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);
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
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);
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);
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?
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.
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
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
Summary UNIX File Systems i-nodes directories File operations Directory opendir, readdir, closedir File Status stat, fstat, lstat Links link, symlink, unlink