250 likes | 395 Views
ch05: Files and Directories. Ju, Hong Taek Computer Network Lab. Keimyung University juht@kmu.ac.kr Rm: 1228, Tel: 580-5234 . Objectives. Learn about file systems and directories Experiment with directory traversal Explore UNIX i-node implementation
E N D
ch05: Files and Directories Ju, Hong Taek Computer Network Lab. Keimyung University juht@kmu.ac.kr Rm: 1228, Tel: 580-5234
Objectives • Learn about file systems and directories • Experiment with directory traversal • Explore UNIX i-node implementation • Use functions for accessing directories • Understand hard links and symbolic links
5.1 Unix File System Navigation • A file system is a collection of file and attributes • Operating systems organizes physical disks into file systems to provide high-level logical access to the actual bytes of files • A directory is a file containing directory entries that associated a file name with the physical location of a file on disk • root directory is at the top of the file system
5.1 The current working directory • Each process has an associated directory, called the current working directory • Path names do not begins with / are called relative path names #include <unistd.h> int chdir(const char *path); char *getcwd(char *buf, size_t size);
A more flexible approach uses the pathconf function to determine the real value for the maximum path length at run time • sysconf: system wide limit • pathconf: take a path name and a limit designator • fpathconf: take a file descriptor and a limit designator #include <unistd.h> long fpathconf(int filedes, int name); long pathconf(const char *path, int name); long sysconf(int name);
5.2 Directory Access • Directory require specialized functions whose corresponding names end with “dir” • opendir, closedir, readdir • They should not be accessed with the ordinary open, close and read functions • The DIR type represents a directory stream which is an ordered sequence of all of the directory entries in a particular directory #include <dirent.h> DIR *opendir(const char *filename); struct dirent *readdir(DIR *dirp); void rewinddir(DIR *dirp); int closedir(DIR *dirp);
5.2.1 Accessing file status information • Retrieving file status information • stat is given the name of a file. • fstat is used for open files. • lstat does the same thing as stat except that if the file is a symbolic link, it gives information about the link, rather than the file it is linked to. #include <sys/stat.h> 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);
The contents of the struct stat dev_t st_dev; /* device ID of device containing file */ ino_t st_ino; /* file serial number */ mode_t st_mode; /* file mode */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of file */ gid_t st_gid; /* group ID of file */ off_t st_size; /* file size in bytes (regular files) */ /* path size (symbolic links) */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last data modification */ time_t st_ctime; /* time of last file status change */
Macros to test the st_mode field for the file type
5.3 Unix File System Implementation • Structure of a typical UNIX file system
5.3.1 Unix file implementation • i-node • directory entry contains only a name and an index into a table giving information about a file. • The table and the index are both referred to as an i-node.
5.4 Hard links and Symbolic links • A link is an association between a filename and an i-node • UNIX has two types of links: hard and symbolic (also called soft) • Directory entries are called hard links because they directly link filenames to i-nodes • Each i-node contains a count of the number of hard links to the i-node. • When a file is created, a new directory entry is created an a new i-node is assigned. • Additional hard links can be created within newname oldnameor with the link system call • A new hard link to an existing file creates a new directory entry but assigns no other additional disk space • A new hard link increments the link count in the i-node • A hard link can be removed with the rm command or the unlink system call • These decrement the link count • The i-node and associated disk space are freed when the count is decremented to 0.
A directory entry, i-node, and data block for a simple file.
Copy • open("/dirA/name1"); • read • close • modify memory image file file • rename("/dirA/name1","dirA/name1.bak"); • open("/dirA/name1"); • write • close
Symbolic links • A symbolic link is a special type of file that contains the name of another file • A reference to the name of a symbolic link causes the operating system to use the name stored in the file, rather than the name itself • Symbolic lines are created with the command ln -s newname oldname • Symbolic links do not affect the link count in the i-node. • Unlink hard links, symbolic links can span filesystems