1 / 24

CS241 System Programming

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

chaeli
Download Presentation

CS241 System Programming

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. CS241 System Programming Discussion Section 8 March 27 – March 30

  2. Outline • UNIX File Systems • Directory • File Status • Links • UNIX File Systems vs. Windows File Systems

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

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

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

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

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

  8. 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); }

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

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

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

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

  13. Exercise (Continued) • What happens to the previous figure after the following sequence of edit operations? (Exercise 5.17)

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

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

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

  17. Exercise (Continued) • What happens to the previous figure after the same sequence of edit operations as Exercise 5.17? (Exercise 5.21)

  18. Exercise • What happens if rm /dirA/name1 is executed to the following figure? (Exercise 5.26)

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

  20. UNIX File Systems • I-node (continued) • Storing Large Files

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

  22. Windows File Systems • NTFS • 64-bit index • MFT: Master File Table • MFT record example • Run: represents one or multiple consecutive blocks

  23. Windows File Systems • NTFS (continued) • Storing Large Files

  24. Summary • UNIX File Systems • Directory • File Status • Links • File Systems • UNIX File Systems • Windows File Systems • FAT, NTFS

More Related