First, Introduction
The basic functions of performing I/O in the previous chapter (opening files, reading files, and writing files), this chapter describes the other characteristics of the file system and the nature of the files, we will start with the stat function and describe each member of the stat structure individually to understand all the properties of a file. In this procedure, we will explain the various functions that modify these properties.
II. functions Stat, Fstat, Fstatat and Lstat
#include <sys/stat.h>intStatConst Char*restrict Pathname,structStat *restrict buf);intFstat (intFdstructStat *buf);intLstatConst Char*restrict Pathname,structStat *restrict buf);intFstatat (intFdConst Char*restrict Pathname,structStat *restrict buf,intflag);//If successful, returns 0; if failed, returns-1
If pathname gives an absolute path, BUF returns the information of the stat corresponding file; two points to note: Lstat returns information about the symbolic connection, not the file referenced by the symbolic connection; Fstatat when At_symlink_nofollow is set, Does not follow the symbolic join, but, like Lstat, returns information about the symbolic connection itself, and when AT_FDCWD is set, and pathname is a relative path, pathname is calculated from the current path. The basic form of the stat structure is as follows:
struct stat{mode_t st_mode;ino_t st_ino;dev_t st_dev;dev_t st_rdev;nlink_t st_nlink;uid_t st_uid;gid_t st_gid;off_t st_size; struct timespec st_atime; struct timespec st_mtime; struct timespec st_ctime;blksize_t st_blksize;blkcn_t st_blocks; };
Iii. Types of files
1. Common files: Most commonly used file types
2. catalog file: This file contains the names of other files and pointers to information about those files.
3. Block special files: each access in fixed length units
4. Character Special files: This type of file provides access to the device without buffering. Devices in the system are either character special files or block special files
5.FIFO: For interprocess communication, also known as Named pipes
6. Sockets: For inter-process network communication
7. Symbolic connection: This type of file points to another file
#include"apue.h"intMainintargcChar*argv[]) { inti; Char*ptr; structstat buf; for(i =1; i < argc; i++) {printf ("%s:", Argv[i]); if(Lstat (argv[1], &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"; } Else if(S_ISBLK (Buf.st_mode)) {ptr="Block Special"; } Else if(S_isfifo (Buf.st_mode)) {ptr="FIFO"; } Else if(S_islnk (Buf.st_mode)) {ptr="Symbolic Link"; } Else if(S_issock (Buf.st_mode)) {ptr="Socket"; } Else{ptr="Unknown mode"; } printf ("%s\n", PTR); }}
4-3 print file types for each command line argument
Iv. setting the user ID and setting the group ID
The ID associated with a process has 6 or more: The actual user ID and the actual group ID: who are we actually? Valid user ID and valid group ID and satellite group ID: Used for file access check; Saved settings user ID and saved settings group ID: used for the EXEC function save.
Each file has an owner and group owner, specified by St_uid and St_gid.
Five, file access rights
All file types have access rights, and many people think that only ordinary files have access, which is a misconception. The permissions of a file are divided into three parts: users, Groups, and others, composed of read, write, and execute, respectively. The permissions for a file have the following rules:
1. When we open any type of file by name, each directory included in the change name, including the current working directory that he may imply, should have execute permissions, which is why it is known as the search bit for the directory execution permission bit. For a directory: Read permission means a list of all file names in that directory, and Execute permissions means entering the directory.
2. In order to create a new file in the directory, we must have write permission and execute permission for the directory
3. In order to delete a file in the directory, we must have write and execute permissions for the directory, and for that specific file, read and write permissions are not required.
Vi. functions Access and Faccessat
When a file is opened with the open function, the kernel performs a test of its access based on the process's valid user ID and valid group ID. Sometimes processes also want to test their access capabilities by their actual user ID and actual group ID, and access and Faccessat are designed to do this, regardless of the valid user ID and valid group ID, which are tested with the actual user ID and the actual group ID.
#include <unistd.h>int access (constcharint mode); int faccessat (intconstcharint mode); // successful return 0, failure return-1
#include <apue.h>#include<fcntl.h>intMainintargcChar*argv[]) { if(ARGC! =2) {Err_quit ("usage:a.out <pathname>"); } if(Access (argv[1], R_OK) <0) {Err_ret ("access error for%s", argv[1]); } Else{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%s\n", argv[1]); } exit (0);}
4-8 Access Function instances
Apue (4)---files and directories (1)