Outline CSCI 4061 Introduction to Operating Systems Instructor: Abhishek Chandra File Systems Directories File and directory operations Inodes and metadata Links 2 File Systems An organized collection of data Think of your book rack or music collection Think of how libraries organize books/magazines Why do we need file systems? Proper data organization Easy to find data Interface to underlying I/O devices and disks Efficient data storage File System Structure A file system typically consists of: Files: Data objects Directories or folders: Collection of files Other objects specific to how data is organized in the file system Different file systems implement these entities in different ways 3 4 1
cp Unix File System / bin dev etc home lib usr tmp var ls chandra bin include lib Unix File System Structure Files: Data objects Regular files, device files, FIFOs Directories: Collections of files Contain information about files Links: Pointers to other files Pseudonyms/nicknames bin cc which libc.so libm.so myprog 5 6 Directories Directory Structure Directory is a special file A directory is a list of entries about files and directories that it contains Each entry contains: Name of file and file type Pointer to file (metadata) filename no. file1 12345 file2 98765 12345 98765 7 8 2
Directory Operations Different operations from the ones used for regular files. Why? Cannot use: open, read, write, close Instead use: opendir, readdir, closedir Opening a Directory DIR *opendir(char *dirname); Returns a directory stream of type DIR Contains an ordered sequence of directory entries Order need not be alphabetical Order is implementation-dependent 9 10 Reading Directory Entries struct dirent *readdir(dir *dirp); Returns the next directory entry NULL if end of directory or error Directory entry contains information about a file or subdirectory Filename and file type Pointer to file s Pointer to next directory entry Closing and Repositioning a Directory int closedir(dir *dirp); Closes an open directory void rewinddir(dir *dirp); Repositions the directory to the beginning 11 12 3
Modifying Directory Entries Cannot write to a directory entry directly Instead directory entries change through operations on files: File creation/deletion: creat, remove, open Filenames, symbolic links: rename, link, unlink Directory creation and removal: mkdir, rmdir Some of the operations differ based on whether the argument is a file or a directory File System Traversal int chdir(char *path); Change directory to a given path cd command char *getcwd(char *buf, size_t size); Get the current working directory pwd command 13 14 Inodes Inode Structure Inodes are kernel structures that contain file information File properties File data location: pointers to disk blocks containing file information Directory entry contains filename and pointer to file Filename is information provided by file system Actual file is independent of its filename File Properties Direct Data Block pointers Single Indirect ptr Double Indirect ptr Triple Indirect ptr Data Blocks Data Blocks Data Blocks 15 16 4
File Properties File type Size Owner, permissions Times: Last access, last modification, last status change Number of hard links Reading File Properties int stat(char *path, struct stat *buf); Parameters: path: File path buf: structure containing file properties struct stat: Contains file properties and no. Variations: lstat: Distinguishes between links and files fstat: Takes file descriptor instead of path 17 18 File Operations The following change the file properties in the : chmod: Mode change chown: Ownership change The following change the file entry in the file system remove: Delete rename: Move/rename Links Pointers to files Pseudonyms/nicknames Example: ln /home/user/file1 file2 Creates a link (file2) that points to file1 Links provide access to the original files Hard links: Direct access to file Symbolic links: Indirect access to file 19 20 5
Hard Links Pointer to the of a file Each link is equivalent No original filename File is deleted only when all hard links are deleted Symbolic Links Pointer to another directory entry A symbolic link is different from original file If original file is moved or deleted, the link becomes hanging does not keep count of symbolic links file1 12345 file2 12345 file1 12345 file2 13579 12345 12345 file1 13579 21 22 File System Summary File system structure Directory and file operations Inodes and metadata Links 23 6