Top Banner
ch05: Files and Directories Ju, Hong Taek Computer Network Lab. Keimyung University [email protected] Rm: 1228, Tel: 580-5234
25

ch05: Files and Directories

Feb 11, 2016

Download

Documents

walden

ch05: Files and Directories. Ju, Hong Taek Computer Network Lab. Keimyung University [email protected] Rm: 1228, Tel: 580-5234 . Objectives. Learn about file systems and directories Experiment with directory traversal Explore UNIX i-node implementation - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ch05: Files and Directories

ch05: Files and Directories

Ju, Hong TaekComputer Network Lab.

Keimyung [email protected]

Rm: 1228, Tel: 580-5234

Page 2: ch05: Files and Directories

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

Page 3: ch05: Files and Directories

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

Page 4: ch05: Files and Directories

File system, directory entry, absolute path name

Page 5: ch05: Files and Directories

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

Page 6: ch05: Files and Directories
Page 7: ch05: Files and Directories

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

Page 8: ch05: Files and Directories
Page 9: ch05: Files and Directories

5.2 Directory Access Directory require specialized functions whose correspondin

g 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);

Page 10: ch05: Files and Directories
Page 11: ch05: Files and Directories

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

Page 12: ch05: Files and Directories

The contents of the struct statdev_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 */

Page 13: ch05: Files and Directories
Page 14: ch05: Files and Directories
Page 15: ch05: Files and Directories

Macros to test the st_mode fieldfor the file type

Page 16: ch05: Files and Directories

5.3 Unix File System Implementation Structure of a typical UNIX file system

Page 17: ch05: Files and Directories

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.

Page 18: ch05: Files and Directories

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 file

names to i-nodes Each i-node contains a count of the number of hard links to the i-n

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

or 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 sys

tem call These decrement the link count

The i-node and associated disk space are freed when the count is decremented to 0.

Page 19: ch05: Files and Directories

A directory entry, i-node, and data block for a simple file.

Page 20: ch05: Files and Directories

Two hard links to the same file for the previous figure

Page 21: ch05: Files and Directories

Copy

1. open("/dirA/name1");2. read3. close4. modify memory image file file5. rename("/dirA/name1","dirA/name1.bak");6. open("/dirA/name1");7. write8. close

Page 22: ch05: Files and Directories
Page 23: ch05: Files and Directories

Symbolic links A symbolic link is a special type of file that contai

ns the name of another file A reference to the name of a symbolic link cause

s the operating system to use the name stored in the file, rather than the name itself

Symbolic lines are created with the commandln -s newname oldname

Symbolic links do not affect the link count in the i-node.

Unlink hard links, symbolic links can span filesystems

Page 24: ch05: Files and Directories
Page 25: ch05: Files and Directories