CS241 System Programming - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

CS241 System Programming

Description:

More Directory Functions #include dirent.h Set the position of next readdir ... Write a function isdirectory that returns nonzero if path is a directory, 0 otherwise ... – PowerPoint PPT presentation

Number of Views:95
Avg rating:3.0/5.0
Slides: 27
Provided by: jinta2
Category:

less

Transcript and Presenter's Notes

Title: CS241 System Programming


1
CS241 System Programming
  • File Systems
  • 10/30/06-11/3/06

2
Quiz
  • LMP1-(10/30/06)-QuizA password 1066

3
  • 1 Autograder    50 for total 50
  • 4 Compass Quiz (4 of them for LPM1) 5 each for
    total 20
  • 2 MpQuiz for Discussion Section (2 per week) 10
    each for total 20
  • 1 TA Evaluation 10

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

5
Review
  • include ltunistd.hgt
  • 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)

6
Review
  • include ltdirent.hgt
  • 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)

7
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

8
More Directory Functions
  • include ltdirent.hgt
  • 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)

9
Review
  • include ltsys/stat.hgt
  • 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

10
Example
  • Write a function isdirectory that returns nonzero
    if path is a directory, 0 otherwise
  • include ltstdio.hgt
  • include lttime.hgt
  • include ltsys/stat.hgt
  • int isdirectory(char path)
  • struct stat statbuf
  • if (stat(path, statbuf) -1)
  • return 0
  • else
  • return S_ISDIR(statbuf.st_mode)

11
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

12
Link Functions
  • include ltunistd.hgt
  • 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

13
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")

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

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

16
Symbolic Link Function
  • include ltunistd.hgt
  • 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

17
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")

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

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

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

21
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

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

23
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

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

25
Windows File Systems
  • NTFS (continued)
  • Storing Large Files

26
Summary
  • UNIX File Systems
  • Directory
  • File Status
  • Links
  • File Systems
  • UNIX File Systems
  • Windows File Systems
  • FAT, NTFS
Write a Comment
User Comments (0)
About PowerShow.com