文章目录
article
文件接口
AI文章摘要
qwen-turbo-latest
加载中...
1. sys/stat.h
1.1 数据结构
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* Inode number */
mode_t st_mode; /* File type and mode */
nlink_t st_nlink; /* Number of 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; /* Total size, in bytes */
blksize_t st_blksize; /* Block size for filesystem I/O */
blkcnt_t st_blocks; /* Number of 512B blocks allocated */
struct timespec st_atim; /* Time of last access (read) */
struct timespec st_mtim; /* Time of last modification (write) */
struct timespec st_ctim; /* Time of last status change (property change) */
};
//st_mode是一个16位的整形数, 1-3表示其他人权限4-6表示组权限7-9表示拥有者权限10-12表示特殊权限13-16表示文件类型
struct timespec {
__kernel_time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
}
//常用配合函数
struct passwd getpwuid(uid_t uid) //
struct group getgrgid(gid_t gid) //
struct tm* localtime(const time_t* timep) //可来处理st_atim.tv_sec
1.2 函数
int stat( //成功返回0, 失败返回-1设置errno, stat参数是一个传出参数
const char *pathname,
struct stat *statbuf).
int chmod(
const char *path,
mode_t mode)
int access(
const char *path ,
int mode) //R_OK(判断读权限)\W_OK(判断写权限)\X_OK(判断执行权限)\F_OK(判断文件存在)
int mkfifo( //创建fifo管道文件.针对fifo管道, 使用open打开时若只读或只写打开会阻塞, 直到另一个进程或线程打开对应的写或读
const char *pathname,
mode_t mode)
2. fcntl.h
2.1 数据结构
struct flock {
...
short l_type; /* Type of lock: F_RDLCK, F_WRLCK, F_UNLCK */
short l_whence; /* How to interpret l_start: SEEK_SET, SEEK_CUR, SEEK_END */
off_t l_start; /* Starting offset for lock */
off_t l_len; /* Number of bytes to lock */
pid_t l_pid; /* PID of process blocking our lock(set by F_GETLK and F_OFD_GETLK) */
};
2.2 函数
int open(//打开一个文件, 返回一个最小可用的文件描述符
const char* path,
int flag): //是一个int型位图, 其中O_RDONLY、O_WRONLY和O_RDWD必选其一
//- O_RDONLY: 只读打开
//- O_WRONLY: 只写打开
//- O_RDWD: 读写打开
//- O_APPEND: 打开时文件指针指向文件末尾
//- O_CREAT: 打开时文件不存在则创建, O_EXCL补充打开时文件存在, 返回-1并设置errno
//- NO_BLOCK: 非阻塞文件描述符
int fcntl(//获取或设置文件描述符的标志位(即open函数中使用的那个)
int fd,
int cmd,...): //文件描述符
//- F_GETFLAG
//- F_SETFLAG
//- F_SETLG
3. sys/dirent.h
3.1 数据结构
struct dirent {
ino_t d_ino; /* Inode number 目录项ino编号*/
off_t d_off; /* Not an offset; see below 目录项偏移*/
unsigned short d_reclen; /* Length of this record 目录项的大小*/
unsigned char d_type; /* Type of file; not supported by all filesystem types */
char d_name[256]; /* Null-terminated filename 目录项名称*/
};
3.2 函数
int mkdir( //创建目录, mkdir命令默认给的是0777&~umask权限
const char *pathname,
mode_t mode)
DIR *opendir(const char *name) //
struct dirent *readdir(DIR *dirp): //返回一个目录项结构体指针, 目录结尾返回NULL, 或错误返回NULL并设置errno
int closedir(DIR *dirp): //关闭目录流指针
rewindir(DIR *dirp): //目录流指针指向开头
telldir(DIR *dirp): //返回目录流指针位置
seekdir(DIR *dirp): //指定目录流指针