240 likes | 363 Views
CS241 System Programming. Discussion Section 8 March 27 – March 30 . Outline. UNIX File Systems Directory File Status Links UNIX File Systems vs. Windows File Systems. Review. #include <unistd.h> Change the directory int chdir(const char *path); Get the current working directory
E N D
CS241 System Programming Discussion Section 8 March 27 – March 30
Outline • UNIX File Systems • Directory • File Status • Links • UNIX File Systems vs. Windows File Systems
Review #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);
Review #include <dirent.h> • Open the directory DIR *opendir(const char *dirname); • Close the directory int closedir(DIR *dirp); void rewinddir(DIR *dirp); • Read the directory struct dirent *readdir(DIR *dirp);
Directory Entry • Struct dirent • Member Fields • char d_name[] • Null-terminated File Name • ino_t d_fileno • File Serial Number • unsigned char d_namlen • Length of a File Name • unsigned char d_type • Type of the 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); • Get the current location of directory stream off_t telldir (DIR *dir);
Review #include <sys/stat.h> • Get the status of a file int lstat(const char *restrict path, struct stat *restrict buf); int stat(const char *restrict path, struct stat *restrict buf); int fstat(int fildes, struct stat *buf); • st_mode • Access permission • Type
Example • Write a function isdirectory that returns nonzero if path is a directory, 0 otherwise #include <stdio.h> #include <time.h> #include <sys/stat.h> int isdirectory(char *path) { struct stat statbuf; if (stat(path, &statbuf) == -1) return 0; else return S_ISDIR(statbuf.st_mode); }
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> • Creates additional links int link(const char *path1, const char *path2); • Removes the directory entry int unlink(const char *path); • Same function as commands ln and rm, respectively • Returns 0 if successful, -1 with errno set if unsuccessful
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");
Exercise • What happens to the previous figure after the following sequence of edit operations? (Exercise 5.17) • Open the file /dirA/name1. • Read the entire file into memory. • Close /dirA/name1. • Modify the memory image of the file. • Unlink /dirA/name1. • Open the file /dirA/name1 (create and write flags). • Write the contents of memory to the file. • Close /dirA/name1.
Exercise (Continued) • What happens to the previous figure after the following sequence of edit operations? (Exercise 5.17)
Symbolic Link Function #include <unistd.h> • Creates a symbolic link int symlink(const char *path1, const char *path2); • Same function as commands ln -s • Returns 0 if successful, -1 with errno set if unsuccessful
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");
Exercise • What happens to the previous figure after the same sequence of edit operations as Exercise 5.17? (Exercise 5.21) • Open the file /dirA/name1. • Read the entire file into memory. • Close /dirA/name1. • Modify the memory image of the file. • Unlink /dirA/name1. • Open the file /dirA/name1 (create and write flags). • Write the contents of memory to the file. • Close /dirA/name1.
Exercise (Continued) • What happens to the previous figure after the same sequence of edit operations as Exercise 5.17? (Exercise 5.21)
Exercise • What happens if rm /dirA/name1 is executed to the following figure? (Exercise 5.26)
UNIX File Systems • I-node: 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 • I-node (continued) • Storing Large Files
Windows File Systems • FAT: File Allocation Table • Advantage • Random access is faster • Disadvantage • FAT should be in memory • FAT16, FAT32 • Number of bits to identify blocks on a disk
Windows File Systems • NTFS • 64-bit index • MFT: Master File Table • MFT record example • Run: represents one or multiple consecutive blocks
Windows File Systems • NTFS (continued) • Storing Large Files
Summary • UNIX File Systems • Directory • File Status • Links • File Systems • UNIX File Systems • Windows File Systems • FAT, NTFS