读书笔记 – 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