C/C++C/C++#include <sys/stat.h>
寻觅~流光#include <sys/stat.h>
<sys/stat.h>
是 Unix/Linux 系统编程中用于 文件元数据操作 的核心头文件,提供了 文件属性检查、权限管理、文件创建 等功能。
1. 核心功能
功能 | 关键函数/宏 | 用途 |
---|
文件信息获取 | stat() , fstat() , lstat() | 获取文件元数据 |
文件权限检查 | S_ISDIR() , S_ISREG() 等宏 | 判断文件类型 |
权限位操作 | S_IRUSR , S_IWGRP 等宏 | 设置/检查文件权限 |
文件创建 | mkdir() , mkfifo() | 创建目录或命名管道 |
文件模式修改 | chmod() , fchmod() | 修改文件权限 |
2. 详细函数解析
2.1 文件信息获取
(1) stat()
- 通过路径获取文件信息
1
| int stat(const char *pathname, struct stat *statbuf);
|
功能:获取文件元数据(如大小、权限、时间戳)。
示例:
1 2 3 4 5 6 7 8 9 10
| #include <sys/stat.h> #include <stdio.h>
int main() { struct stat file_info; if (stat("test.txt", &file_info) == 0) { printf("File size: %ld bytes\n", file_info.st_size); } return 0; }
|
(2) fstat()
- 通过文件描述符获取信息
1
| int fstat(int fd, struct stat *statbuf);
|
- 适用场景:已打开的文件(如通过
open()
获取的 fd
)。
(3) lstat()
- 不解析符号链接
1
| int lstat(const char *pathname, struct stat *statbuf);
|
- 与
stat()
的区别:对符号链接文件返回链接本身的信息,而非目标文件。
2.2 文件类型检查宏
宏 | 含义 |
---|
S_ISREG() | 普通文件 |
S_ISDIR() | 目录 |
S_ISCHR() | 字符设备 |
S_ISBLK() | 块设备 |
S_ISFIFO() | 命名管道(FIFO) |
S_ISLNK() | 符号链接 |
S_ISSOCK() | 套接字 |
示例:
1 2 3
| if (S_ISDIR(file_info.st_mode)) { printf("It's a directory\n"); }
|
2.3 权限位宏
宏 | 含义 | 八进制值 |
---|
S_IRUSR | 用户读权限 | 0400 |
S_IWUSR | 用户写权限 | 0200 |
S_IXUSR | 用户执行权限 | 0100 |
S_IRGRP | 组读权限 | 0040 |
S_IWGRP | 组写权限 | 0020 |
S_IXGRP | 组执行权限 | 0010 |
S_IROTH | 其他用户读权限 | 0004 |
S_IWOTH | 其他用户写权限 | 0002 |
S_IXOTH | 其他用户执行权限 | 0001 |
组合权限示例:
1
| mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
|
2.4 文件/目录创建
(1) mkdir()
- 创建目录
1
| int mkdir(const char *pathname, mode_t mode);
|
(2) mkfifo()
- 创建命名管道(FIFO)
1
| int mkfifo(const char *pathname, mode_t mode);
|
2.5 文件模式修改
(1) chmod()
- 通过路径修改权限
1
| int chmod(const char *pathname, mode_t mode);
|
示例:
1
| chmod("test.txt", 0644);
|
(2) fchmod()
- 通过文件描述符修改权限
1
| int fchmod(int fd, mode_t mode);
|
3. struct stat
结构体详解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| struct stat { dev_t st_dev; ino_t st_ino; mode_t st_mode; nlink_t st_nlink; uid_t st_uid; gid_t st_gid; dev_t st_rdev; off_t st_size; blksize_t st_blksize; blkcnt_t st_blocks; time_t st_atime; time_t st_mtime; time_t st_ctime; };
|
4. 完整示例
检查文件类型和权限
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #include <sys/stat.h> #include <stdio.h>
void check_file(const char *path) { struct stat info; if (stat(path, &info) == -1) { perror("stat"); return; }
printf("File: %s\n", path); printf("Size: %ld bytes\n", info.st_size);
if (S_ISREG(info.st_mode)) printf("Type: Regular file\n"); else if (S_ISDIR(info.st_mode)) printf("Type: Directory\n"); else if (S_ISLNK(info.st_mode)) printf("Type: Symbolic link\n");
printf("Permissions: %o\n", info.st_mode & 0777); }
int main() { check_file("test.txt"); return 0; }
|
5. 注意事项
- 符号链接处理:
- 用
lstat()
检查链接本身,stat()
会跟踪链接。
- 权限位兼容性:
- 直接使用
0777
等八进制数可能降低可读性,建议用宏(如 S_IRUSR
)。
- 时间戳精度:
st_atime
/st_mtime
的精度取决于文件系统(可能为秒或纳秒)。
- 错误处理:
- 检查函数返回值(如
stat()
返回 -1
时表示失败)。
6. 总结
<sys/stat.h>
的核心用途:
- 文件元数据获取:
stat()
, fstat()
, lstat()
+ struct stat
。
- 文件类型判断:
- 权限管理:
- 文件系统操作:
适用于 文件管理工具、备份系统、权限监控 等场景。