#include <sys/stat.h>
int stat(const char *restrict pathname, struct stat *restrict
buf);
int fstat(int fd, struct stat *buf);
int lstat(const chat *restrict pathname, struct stat *restrict
buf);
int fstatat(int fd, const char *restrict pathname, struct stat
*restrict buf, int flag);
//所有4个函数的返回值:若成功,返回0;若出错,返回-1
一旦给出pathname,stat函数将返回与此命名文件有关的信息结构。fstat函数获得已在描述符fd上打开文件的有关信息。lstat函数类似于stat,但是当命名函数是一个符号连接时,lstat返回该符号链接的有关信息,而不是由该符号引用的文件的信息。
fstatat函数为一个相对于当前打开目录(有fd参数指向)的路径名返回文件统计信息。flag参数控制着是否跟随着一个符号链接。当AT_SYMLINK_NOFOLLOW标志被设置时,fstatat不会跟随符号链接,而是返回符号链接本身的信息。否则,在默认情况下,返回的是符号链接所指向的实际文件的信息。如果fd参数的值是AT_FDCWD,并且pathname参数是一个相对路径名,fstatat会计算相对于当前目录的pathname参数。如果pathname是一个绝对路径,fd参数就会被忽略。这两种情况下,根据flag的取值,fstatat的作用就跟stat或lstat一样。
struct stat{
mode_t st_mode; /*file type or mode*/
ino_t st_ino; /*i-node number*/
dev_t st_dev; /*device number*/
dev_t st_rdev; /*device number for special files*/
nlink_t st_nlink; /*number of link*/
uid_t st_uid; /*user ID of owner*/
gid_t st_gid; /*group ID of owner*/
off_t st_size; /*size in bytes,for regular files*/
struct timespec st_atime; /*time of last access*/
struct timespec st_mtime; /*time of last modification*/
struct timespec st_ctime; /*time of last file status change*/
blksize_t st_blksize;/*best I/O block size*/
blkcnt_t st_blocks;/*number of disk blocks allocated*/
}
文件类型包括如下几种:
1. 普通文件(regular file)。
2. 目录文件(directory file)。
3. 块特殊文件(block special file)。这种类型的文件提供对设备带缓冲的访问,每次访问以固定长度为单位。
4. 字符特殊文件(character special file)。这种类型的文件提供对设备不带缓冲的访问,每次访问长度可变。系统中的所有设备要么是字符特殊文件,要么是块特殊文件。
5. FIFO。这种类型的文件用于进程间通信,有时也称为命名管道。
6. 套接字(socket)
7. 符号链接(symbolic link)。这种类型的文件指向另一个文件。
与一个进程相关联的ID有6个或更多
实际用户ID 实际组ID | 我们实际上是谁 |
---|---|
有效用户ID 有效组ID 附属组ID | 用于文件访问权限检查 |
保存的设置用户ID 保存的设置组ID | 由exec函数保存 |
实际用户ID和实际组ID在登录时取自口令文件中的登录项。通常,在一个登录会话期间这些值并不改变,但是超级用户进程有方法改变他们。
有效用户ID、有效组ID以及附属组ID决定了我们的文件访问权限。
保存的设置用户ID和保存的设置组ID在执行一个程序时包含了有效用户ID和有效组ID的副本。
st_mode 屏蔽 | 含义 |
---|---|
S_IRUSR | 用户读 |
S_IWUSR | 用户写 |
S_IXUSR | 用户执行 |
S_IRGRP | 组读 |
S_IWGRP | 组写 |
S_IXGRP | 组执行 |
S_IROTH | 其他读 |
S_IWOTH | 其他写 |
S_IXOTH | 其他执行 |
新用户ID设置为进程的有效用户ID。
1. 新文件的组ID可以是进程的有效组ID。
2. 新文件的组ID可以是它所在目录的组ID
access和faccessat函数是按实际用户ID和实际组ID进行访问权限测试的。
#include <unistd.h>
int access(const char *pathname, int mode);
int faccessat(int fd, const char *pathname, int mode, int flag);
//两个函数的返回值:若成功,返回0;若出错,返回-1
其中,如果测试文件是否已经存在,mode就为F_OK否则mode是下表中所列常量的按位或。
mode | 说明 |
---|---|
R_OK | 测试读权限 |
W_OK | 测试写权限 |
X_OK | 测试执行权限 |
faccessat函数与access函数在下面两种情况下是相同的:一种是pathname参数为绝对路径,另一种是fd参数取值为AT_FDCWD而pathname参数为相对路径。否则,faccessat计算相对于打开目录(由fd参数指向)的pathname。
flag参数可以用于改变faccessat的行为,如果flag设置为AT_EACCESS,访问检查用的是调用进程的有效用户ID和有效组ID,而不是实际用户ID和实际组ID。
umask函数为进程设置文件模式创建屏蔽字,并返回之前的值
#include <sys/stat.h>
mode_t umask(mode_t cmask);
//返回值:之前的文件模式创建屏蔽字
在文件模式创建屏蔽字中为1的位,在文件mode中的相应位一定被关闭。
这三个函数使我们可以更改现有文件的访问权限。
#include <sys/stat.h>
int chmod(const char *pathname, mode_t mode);
int fchmod(int fd, mode_t mode);
int fchmodat(int fd, const char *pathname, mode_t mode,int
flag);
//3个函数的返回值:若成功,返回0;若出错,返回-1