点击(此处)折叠或打开
- #include <sys/stat.h>
- int stat(const char *pathname, struct stat *statbuf);
- int lstat(const char *pathname, struct stat *statbuf);//若为软链接,不follow
- int fstat(int fd, struct stat *statbuf);
- /*all return 0 on success, or -1 on error*/
- struct stat{
- dev_t st_dev;// 文件所驻留的设备
- ino_t st_ino;// 文件的i节点号
- mode_t st_mode;// 标识文件类型和指定文件权限的双重作用
- nlink_t st_nlink;// 硬链接数
- uid_t st_uid;
- gid_t st_gid;
- dev_t st_rdev;// 如果是针对设备的i节点,该字段包含设备的主、辅ID
- off_t st_size;// 文件的字节数
- blksize_t st_blksize;// 针对文件系统上进行I/O操作时的最优块大小(以字节为单位)
- blkcnt_t st_blocks;// 分配给文件的总块数, 块大小为512字节
- time_t st_atime;
- time_t st_mtime;// 上次修改时间
- time_t st_ctime;// 文件状态发生改变的上次时间
- };
2. 文件时间戳
点击(此处)折叠或打开
- #include <utime.h>
- int utime(const char *pathname, const struct utimbuf *buf);
- /*returns 0 on success, or -1 on error*/
- struct utimbuf{
- time_t actime;
- time_t modtime;
- };
- #include <sys/time.h>
- int utimes(const char *pathname, const struct timeval tv[2]);
- int futimes(int fd, const struct timeval tv[2]);
- int lutimes(const char *pathname, const struct timeval tv[2]);
- /*both return 0 on success, or -1 on error*/
文件创建时,其用户id取自进程的有效用户id,而新建文件的组id则取自进程的有效组id(systemV),或父目录的组id(bsd)
点击(此处)折叠或打开
- #include <unistd.h>
- int chown(const char *pathname, uid_t owner, gid_t group);
- #define _XOPEN_SOURCE 500
- #include <unistd.h>
- int lchown(const char *pathname, uid_t owner, gid_t group);
- int fchown(int fd, uid_t owner, gid_t group);
- /*all return 0 on success, or -1 on error0*/
4. 文件权限
点击(此处)折叠或打开
- #include <unistd.h>
- int access(const char *pathname, int mode);
- /*returns 0 if all permissions are granted, otherwise -1*/
umask是一种进程属性,当进程新建文件或目录时,该属性用于指明哪些权限位。进程的umask通常继承自其父shell,用户可以使用shell的内置命令umask来改变shell进程的umask,从而控制在shell下运行程序的umask
点击(此处)折叠或打开
- #include <sys/stat.h>
- mode_t umask(mode_t mask);
- /*always successfully returns the previous process umask*/
- #include <sys/stat.h>
- int chmod(const char *pathname, mode_t mode);
- #define _XOPEN_SOURCE 500 /*or: #define _BSD_SOURCE*/
- #include <sys/stat.h>
- int fchmod(int fd, mode_t mode);
- /*both return 0 on success, or -1 on error*/