1 files and directories hua lisystems programmingcs2690files and directories

Download 1 Files and Directories Hua LiSystems ProgrammingCS2690Files and Directories

Post on 18-Dec-2015

225 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • Slide 1
  • 1 Files and Directories Hua LiSystems ProgrammingCS2690Files and Directories
  • Slide 2
  • 2 stat, fstat and lstat Functions Hua LiSystems ProgrammingCS2690Files and Directories #include int stat (const char *pathname, struct stat *buf); int fstat (int filedes, struct stat *buf); int lstat (const char *pathname, struct stat *buf); All three return: 0 if OK, -1 on error
  • Slide 3
  • 3 Given a pathname, the stat function returns a structure of information about the named file. The fstat function obtains information about the file that is already open on the descriptor filedes. The lstat function is similar to stat, but when the named file is a symbolic link, lstat returns information about the symbolic link, not the file referenced by the symbolic link. Hua LiSystems ProgrammingCS2690Files and Directories
  • Slide 4
  • 4 The second argument of the stat functions is a pointer to a structure that we must supply. The function fills in the structure pointed to by buf. The structure could look like: struct stat { mode_t st_mode; /* file type & mode (permissions) */ ino_t st_ino; /* i-node number (serial number) */ dev_t st_dev; /* device number (filesystem) */ dev_t st_rdev; /* device number for special files */ nlink_t st_nlink; /* number of links */ Hua LiSystems ProgrammingCS2690Files and Directories
  • Slide 5
  • 5 uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ off_t st_size; /* size in bytes, for regular files */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last file status change */ long st_blksize; /* best I/O block size */ long st_blocks; /* number of 512-byte blocks allocated */ }; Hua LiSystems ProgrammingCS2690Files and Directories
  • Slide 6
  • 6 File Types Regular File It is the most common type of file, which contains data of some form. There is no distinction to the Unix kernel whether this data is text or binary. Any interpretation of the contents of a regular file is left to the application processing the file. Hua LiSystems ProgrammingCS2690Files and Directories
  • Slide 7
  • 7 Directory File It is a file that contains the names of other files and pointers to information on these files. Any process that has read permission for a directory file can read the contents of the directory. Character Special File It is a type of file used for certain types of devices on a system. Hua LiSystems ProgrammingCS2690Files and Directories
  • Slide 8
  • 8 Block Special File It is a type of file typically used for disk devices. All devices on a system are either character special files or block special files. FIFO It is a type of file used for interprocess communication between processes. Hua LiSystems ProgrammingCS2690Files and Directories
  • Slide 9
  • 9 Socket It is a type of file used for network communication between processes. A socket can also be used for nonnetwork communication between processes on a single host. Symbolic Link It is a type of file that points to another file. Hua LiSystems ProgrammingCS2690Files and Directories
  • Slide 10
  • 10 The type of a file is encoded in the st_mode member of the stat structure. The various file type macros in are as follows: Hua LiSystems ProgrammingCS2690Files and Directories MacroType of file S_ISREG ( ) S_ISDIR ( ) S_ISCHR ( ) S_ISBLK ( ) S_ISFIFO ( ) regular file directory file character special file block special file pipe or FIFO
  • Slide 11
  • 11 The argument to each of these macros is the st_mode member from the stat structure. Example The following program takes its command-line arguments and prints the type of file for each command-line argument. Hua LiSystems ProgrammingCS2690Files and Directories MacroType of file S_ISLNK ( ) S_ISSOCK ( ) symbolic link (not in POSIX.1 or SVR4) socket (not in POSIX.1 or SVR4)
  • Slide 12
  • 12 Hua LiSystems ProgrammingCS2690Files and Directories #include #include ourhdr.h int main (int argc, char *argv[]) { int i; struct stat buf; char *ptr;
  • Slide 13
  • 13 Hua LiSystems ProgrammingCS2690Files and Directories for (i = 1; i < argc; i++) { printf (%s: , argv[i]); if (lstat(argv[i], &buf) < 0) { err_ret (lstat error); continue; } if (S_ISREG(buf.st_mode)) ptr = regular; else if (S_ISDIR(buf.st_mode)) ptr = directory; else if (S_ISCHR(buf.st_mode)) ptr = character special;
  • Slide 14
  • 14 Hua LiSystems ProgrammingCS2690Files and Directories else if (S_ISBLK(buf.st_mode)) ptr = block special; else if (S_ISFIFO(buf.st_mode)) ptr = fifo; #ifdef S_ISLNK else if (S_ISLNK(buf.st_mode)) ptr = symbolic link; #endif #ifdef S_ISSOCK else if (S_ISSOCK(buf.st_mode)) ptr = socket; #endif else ptr = ** unknown mode ** printf (%s\n, ptr);
  • Slide 15
  • 15 Hua LiSystems ProgrammingCS2690Files and Directories } exit (0); }
  • Slide 16
  • 16 Set-User-ID and Set-Group-ID Every process has six or more IDs associated with it. They are shown as follows: Hua LiSystems ProgrammingCS2690Files and Directories real user ID who we really are real group ID
  • Slide 17
  • 17 The saved set-user-ID and saved set-group-ID contain copies of the effective user ID and the effective group ID when a program is executed. Hua LiSystems ProgrammingCS2690Files and Directories effective user ID effective group ID used for file access permission checks supplementary group Ids saved set-user-ID saved by exec functions saved set-group-ID
  • Slide 18
  • 18 File Access Permissions The st_mode value also encodes the access permission bits for the file. All the file types (directories, character special files, etc.) have permissions. There are nine permission bits for each file, divided into three categories. These nine file access permission bits, from are as follows: Hua LiSystems ProgrammingCS2690Files and Directories
  • Slide 19
  • 19 Hua LiSystems ProgrammingCS2690Files and Directories st_mode maskMeaning S_IRUSR S_IWUSR S_IXUSR user-read user-write user-execute S_IRGRP S_IWGRP S_IXGRP group-read group-write group-execute S_IROTH S_IWOTH S_IXOTH other-read other-write other-execute
  • Slide 20
  • 20 The chmod command, which is typically used to modify these nine permission bits. Ownership of New Files and Directories The user ID of a new file is set to the effective user ID of the process. The group ID of a new file can be the effective group ID of the process. The group ID of a new file can be the group ID of the directory in which the file is being created. Hua LiSystems ProgrammingCS2690Files and Directories
  • Slide 21
  • 21 In the above case, all the files and directories created in that directory will have the group ID belonging to the directory. This group ownership of files and directories will then propagate down the hierarchy from that point. access Function The access function bases its tests on the real user ID and the real group ID. Hua LiSystems ProgrammingCS2690Files and Directories #include
  • Slide 22
  • 22 The mode is the bitwise OR of any of the constants shown below, for access function, from Hua LiSystems ProgrammingCS2690Files and Directories int access (const char *pathname, int mode); Returns: 0 if OK, -1 on error. modeDescription R_OK W_OK test for read permission test for write permission
  • Slide 23
  • 23 Example The program which is an example of an access function is as follows: Hua LiSystems ProgrammingCS2690Files and Directories modeDescription X_OK F_OK test for execute permission test for existence of file #include
  • Slide 24
  • 24 Hua LiSystems ProgrammingCS2690Files and Directories #include #include ourhdr.h int main (int argc, char *argv[]) { if (argc != 2) err_quit (usage: a.out ); if (access (argv[1], R_OK) < 0) err_ret (access error for %s, argv[1]); else
  • Slide 25
  • 25 chmod and fchmod Functions Hua LiSystems ProgrammingCS2690Files and Directories printf (read access OK\n); if (open (argv[1], O_RDONLY) < 0) err_ret (open error for %s, argv[1]); else printf (open for reading OK\n); exit(0); }
  • Slide 26
  • 26 These two functions allow us to change the file access permissions for an existing file. Hua LiSystems ProgrammingCS2690Files and Directories #include int chmod (const char *pathname, mode_t mode); int fchmod (int filedes, mode_t mode); Both return: 0 if OK, -1 on error
  • Slide 27
  • 27 The chmod function operates on the specified file while the fchmod function operates on a file that has already been opened. To change the permission bits of a file, the effective user ID of the process must equal the owner of the file, or the process must have superuser permissions. The mode is specified as the bitwise OR of the constants shown in the figure below: Hua LiSystems ProgrammingCS2690Files and Directories modeDescription S_ISUIDset-user-ID on execution
  • Slide 28
  • 28 Hua LiSystems ProgrammingCS2690Files and Directories modeDescription S_ISGIDset-group-ID on execution S_IRWXU S_IRUSR S_IWUSR S_IXUSR read, write, and execute by user (owner) read by user (owner) write by user (owner) execute by user (owner) S_IRWXG S_IRGRP read, write, and execute by group read by group
  • Slide 29
  • 29 The mode constants for chmod functions are from Hua LiSystems ProgrammingCS2

Recommended

View more >