读书笔记 – APUE2e Chap4(2)
/////////////////////////////////////////////
stat, fstat, lstat
Specification:
#include<sys/stat.h>
int stat (const char *restrict pathname, struct stat *restrict buf);
int fstat(int filedes, struct stat *buf);
int lstat(const char *restrict pathname, struct stat *buf);
Return:
All three return: 0 if OK, –1 on error.
Comment:
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.
The returned information is filled in the structure pointed by buf. See 读书笔记 – APUE2e Chap4(1) for detail about struct stat.
/////////////////////////////////////////////
access
Specification:
#include<unistd.h>
int access(const char *pathname, int mode);
Return:
0 if OK, –1 on error.
Comment:
When we open a file, the kernel performs its access tests based on the effective user and group IDs. access allows testing the file accessibility based on the real user and group IDs. Similar strategies is used in the testing (Four steps). See 读书笔记 – APUE2e Chap4(1) File Access Test (replace effective with real in these four steps.)
The mode is bitwise OR of any of the following constants:
- R_OK: test for read permission
- W_OK: test for write permission
- X_OK: test for execute permission
- F_OK: test for existence of file
/////////////////////////////////////////////
- umask
Specification:
#include<sys/stat.h>
mode_t umask (mode_t cmask);
Return:
previous file mode creation mask
Comment:
File Mode Creation Mask is associated with every process. It is used whenever the process creates a new file or a new directory. Any bits that are on in the file mode creation mask are turned off in the file’s mode. The cmask is formed as the bitwise OR of the nine constants of File Access Permissions (See 读书笔记 – APUE2e Chap4(1) for detail).
/////////////////////////////////////////////
chmod, fchmod
Specification:
#include<sys/stat.h>
int chmod (const char *pathname, mode_t mode);
int fchmod (int filedes, mode_t mode);
Return:
Both return 0 if OK, –1 on error.
Comment:
This two functions change the file access permissions for an existing file. The chmod function operates on the specified file, whereas the fchmod function operates on a file that is opened on descriptor filedes.
To change the permission bits of a file, the effective user ID of the process must be equal to the owner ID of the file, or the process must have superuser permissions.
The mode is specified as the bitwise OR of the nine constants of File Access Permission (See 读书笔记 – APUE2e Chap4(1) for detail) and additional six constants as shown below:
S_ISUID: set-user-ID on execution
S_ISGID: set-group-ID on execution
S_ISVTX: saved-text (sticky bit)
S_IRWXU: read, write and execute by user (owner)
S_IRWXG: read, write and execute by group
S_IRWXO: read, write and execute by other (world)
/////////////////////////////////////////////
chown, fchown, lchown
Specification:
#include <unistd.h>
int chown(const char *pathname, uid_t owner, gid_t group);
int fchown(int filedes, uid_t owner, gid_t group);
int lchown(const char *pathname, uid_t owner, gid_t group);
Return:
All three return 0 if OK, –1 on error.
Comment:
lchown changes the owners of the symbolic link itself, not the file pointed by the symbolic link.
If either arguments owner or group is –1, the corresponding ID is left unchanged.
If these functions are called by a process other than a superuser process, on successful return, both the set-user-ID and the set-group-ID bits are cleared.
(See APUE2e Page 103 for details about _POSIX_CHOWN_RESTRICTED)
/////////////////////////////////////////////
truncate, ftruncate
Specification:
#include <unistd.h>
int truncate (const char *pathname, off_t length);
int ftruncate (int filedes, off_t length);
Return:
Both returns 0 if OK, –1 on error
Comment:
These two functions truncate an existing file to length bytes.
Emptying a file, which we can do with the O_TRUNC flag to open, is a special case of truncation.
/////////////////////////////////////////////
link, unlink
Specification:
#include <unistd.h>
int link (const char *existingpath, const char *newpath);
int unlink (const char *pathname);
Return:
Both return 0 if OK, –1 on error
Comment:
hard link vs symbolic link.
Every i-node has a link count that contains the number of directory entries that point to the i-node. In the stat structure, the link count is contained in the st_nlink member. This type of links are called hard links.
link function creates a new directory entry, newpath, that reference the existing file existingpath. If the newpath already exists, an error is returned. Only the last component of the newpath is created (The rest of the path must already exist).
The creation of the new directory entry and the increment of the link count must be an atomic operation.
We cannot create a new file in a directory unless we have write permission and execute permission in the directory.
unlink function removes the directory entry and decrements the link count of the file referenced by pathname. If there are other links to the file, the data in the file is still accessible through other links. The file is not changed if an error occurs.
If the file is a symbolic link, unlink removes the symbolic link, not the file referenced by the link.
Only when the link count reaches 0 can the contents of the file be deleted. If some process has the file open, its contents will not be deleted.
To unlink (delete) a file, we must have write and execute permissions in the directory containing the directory entry. If the sticky bit is set in this directory, we must have write permission for the directory and one of the following:
- Own the file
- Own the directory
- Have a superuser priviledges
/////////////////////////////////////////////
remove, rename
Specification:
#include <stdio.h>
int remove (const char *pathname);
int rename (const char *oldname, const char *newname);
Return:
Both returns 0 if OK, –1 on error.
Comment:
remove function: unlink a file or a directory. For a file, remove is identical to unlink. For a directory, remove is identical to rmdir.
rename function; rename a file or a directory. Four conditions:
- If oldname specifies a file that is not a directory, then if newname exist, it cannot refer to a directory. If newname exists and is not a directory, it is removed and oldname is renamed to newname. We must have write permissions for the directories containing newname and oldname.
- If oldname specifies a directory, then if newname exists, it must refer to a directory, and that directory must be empty (only dot and dot-dot entries left). If newname exists and is an empty directory, it is removed and oldname is renamed to newname. newname cannot contain a path prefix that names oldname.
- If either oldname or newname refers to a symbolic link, then the link itself is processed, not the file to which it resolves.
- If oldname and newname refer to the same file, the function returns successfully without changing anything.
/////////////////////////////////////////////
symlink, readlink
Specification:
#include <unistd.h>
int symlink (const char *actualpath, const char *sympath);
ssize_t readlink (const char *restrict pathname, char *restrict buf, size_t bufsize);
Return:
symlink returns 0 if OK, –1 on error.
readlink returns number of bytes read if OK, –1 on error.
Comment:
hard link vs symbolic link.
A symbolic link is an indirect pointer to a file, unlike the hard links which points directly to the i-node of the file. Symbolic links are introduced to get around the limitations of hard links:
- Hard links normally require that the link and the file reside in the same file system
- Only the superuser can create a hard link to a directory (not file).
- There are no file system limitations on a symbolic link and what it points to, and anyone can create a symbolic link to a directory. Symbolic links are typically used to move a file or an entire directory hierarchy to another location on a system.
- When using functions that refer to a file by name, we always need to know whether the function follows a symbolic link. (See APUE2e Figure 4.17 on Page 113 for detail.)
symlink function: a new directory entry, sympath, is created that points to actualpath. It is not required that actualpath exist when the symbolic link is created. Also, actualpath and sympath need not reside in the same file system.
readlink function: because the open function follows a symbolic link, we need a way to open the link itself and read the name in the link. If the function is successful, it returns the number of bytes placed into buf. The contents of the symbolic link that are returned in buf are not null terminated.
/////////////////////////////////////////////
utime
Specification:
#include <utime.h>
int utime (const char * pathname, const struct utimbuf *times);
Return:
0 if OK, –1 on error.
Comment:
utime function: change the access time (st_atime in struct stat) and the modification time (st_mtime in struct stat) of a file.
struct utimbuf{
time_t actime; //access time
time_t modtime; //modification time
}
The two time values in the utimbuf structure are calendar times, which counts seconds since the Epoch.
If times is null, the access time and the modification time are both set to the current time. To do this, either the effective user ID of the process must equal the owner ID of the file, or the process must have write permission for the file.
If times is non-null, the access time and the modification time are set to the values in the times structure. To do this, the effective user ID of the process must equal the owner ID of the file, or the process must be a superuser process. Merely having write permissions for the file is not adequate.
/////////////////////////////////////////////
mkdir, rmdir
Specification:
#include <sys/stat.h>
int mkdir (const char *pathname, mode_t mode);
Return:
0 if OK, –1 on error.
Comment:
mkdir function: Creates a new, empty directory (dot, dot-dot entries are automatically created).
The specified file access permissions, mode, are modified by the file mode creation mask of the process.
The user ID and group ID of the new directory are established according rules in Section 4.6 of APUE2e.
rmdir function: delete an empty directory. If the link count of the directory becomes 0 with this call, and no other processes has the directory open, then the space occupied by the directory is freed.
/////////////////////////////////////////////
openfir, readdir, rewinddir, closedir, telldir, seekdir
Specification:
#include <dirent.h>
DIR *opendir (const char*pathname);
struct dirent *readdir (DIR *dp);
void rewinddir (DIR *dp);
int closedir (DIR *dp);
long telldir (DIR *dp);
void seekdir (DIR *dp, long loc);
Return:
opendir returns pointer to DIR if OK, NULL on error.
readdir returns pointer to dirent if OK, NULL at the end of directory or error
closedir returns 0 if OK, –1 on error.
testdir returns current location in the directory associated with dp.
Comment:
struct dirent {
ino_t d_ino; //i-node number
char d_name[NAME_MAX + 1]; //null-terminated filename
}
The DIR structure is an internal structure used by these six functions to maintain information about the directory being read.
telldir and seekdir are not part of POSIX.1 stardard. They are XSI extensions.
rewinddir function: resets the position of the directory stream to which dp refers to the beginning of the directory.
seekdir function: se the position of next readdir() operation on the directory stream specified by dp to the position specified by loc.
/////////////////////////////////////////////
chdir, fchdir
Specification:
#include <unistd.h>
int chdir (const char *pathname);
int fchdir (int filedes);
Return:
Both return 0 if OK, –1 on error.
Comment:
The new current working directory is specified either as a pathname or through an open file descriptor.
Each process has a current working directory. The current working directory is an attribute of a process; the home directory is an attribute of a login name.
Because it is an attribute of a process, the current working directory cannot affect processes that invoke the process that executes the chdir.
/////////////////////////////////////////////
getcwd
Specification:
#include <unistd.h>
char *getcwd (char *buf, size_t size);
Return:
Returns buf if OK, NULL on error.
Comment:
The kernel doesn’t maintain full pathname of the current working directory. Instead, the kernel keeps information about the directory, such as a pointer to the directory’s v-node.
getcwd function gets the entire absolute pathname of the current working directory which is stored in buf. buf must be large enough to accommodate the absolute pathname plus a terminating null byte, or an error is returned.
/////////////////////////////////////////////
device special file
Every file system is know by its major and minor device numbers. The major device number identifies the device driver and sometimes encodes which peripheral board to communicate with; the minor device number identifies the specific subdevice.
We can access the major and minor device numbers using two macros major and minor.
The st_dev in structure stat value for every filename on a system is the device number of the file system containing that filename and its corresponding i-node.
The st_rdev is only for character special file and block special file. This value contains the device number of the actual device.
Example:
struct stat buf;
major(buf.st_dev);
minor(buf.st_dev);
major(buf.st_rdev);
minor(buf.st_rdev);
完全看不懂……
@moper 完全抄书上的,不继续了,这个太浪费时间,哪怕是抄的。