#inlcude <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 *restrict buf);
stat 和 fstat 完全相同,除了前者使用路径名,而后者使用文件描述符。
lstat 和 stat唯一区别是,在操作文件的符号连接时,前者取得的是符号连接本身的信息。
2. stat结构
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number off hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* size in bytes, for regular files */
time_t st_atime; /* time of last acess */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last stat change */
blksize_t st_blksize; /* best IO block size */
blkcnt_t st_blocks; /* number of disk blocks allocated */
}
3. 文件类型
1) regular file : S_ISREG()
2) directory file: S_ISDIR()
3) block special file: S_ISBLK()
4) character special file: S_ISCHR()
5) FIFO: S_ISFIFO()
6) socket: S_ISSOCK()
7) symbolic link: S_ISSOCK()
4. 访问许可
S_IRUSR
S_IWUSR
S_IXUSR
S_IRGRP
S_IWGRP
S_IXGRP
S_IROTH
S_IWOTH
S_IXOTH
目录必须有执行权限才能操作目录下的文件。
5. 新建文件ID
用户ID设置成有效用户ID, 组ID可选: 有效用户ID或者所在目录ID
6. access 函数
int access(const char *pathname, int mode);
R_OK, W_OK, X_OK, F_OK
7, umask 函数
mode_t umask(mode_t cmask);
改变进程文件创建掩码, 并返未改变前文件创建掩码。
8. chmod 和 fchmod函数
改变文件权限, 为了改变权限,进程有效用户ID必须等于文件ower ID或者,有效用户ID是超级用户。