4.2 stat、fstat和lstat函数
#include<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);
三个函数的返回值:成功返回0,出错则返回-1
一旦给出pathname,stat函数就返回与此命名文件有关的信息结构。fstat函数获
取已在描述符filedes上打开文件的有关信息。lstat函数类似于stat,但是当命名的
文件是一个符号链接时,lstat返回该符号链接的有关信息,而不是由该符号链接引
用文件的信息
struct stat {
mode_t st_mode; /* file type & mode (permissions) */
ino_t st_ino; /* i-node number (serial number) */
dev_t st_dev; /* device number (file system) */
dev_t st_rdev; /* device number for special files */
nlink_t st_nlink; /* number of links */
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 */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modifation */
time_t 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 */
};
4.3 文件类型
文件类型信息包含在stat结构的st_mode成员中。
<sys/stat.h>中的文件类型宏(其参数都是stat结构中的st_mode成员):
S_ISREG() 普通文件
S_ISDIR() 目录文件
S_ISCHR() 字符特殊文件
S_ISBLK() 块特殊文件
S_ISFIFO() 管道或FIFO
S_ISLNK() 符号链接
S_ISSOCK() 套接字
POSIX.1允许实现将进程间通信(IPC)对象(例如,消息队列和信号量等)表示文件。
下面的宏可用来确定IPC对象的类型。其参数是指向stat结构的指针:
<sys/stat.h>中的IPC类型宏:
S_TYPEISMQ() 消息队列
S_TYPEISSEM() 信号量
S_TYPEISSHM() 共享存储对象
4.7 access函数
当用open函数打开一个文件时,内核以进程的有效用户ID和有效组ID为基础执行访
问权限测试。有时,进程也希望按其实际用户ID和实际组ID来测试其访问能力。
access函数按实际用户ID和实际组ID进行访问权限测试。
#include<unistd.h>
int access(const char *pathname,int mode);
返回值:成功返回0,出错则返回-1
其中,mode是下面所列常量的按位或(<unistd.h>):
R_OK 测试读权限
W_OK 测试写权限
X_OK 测试执行权限
F_OK 测试文件是否存在
4.8 umask函数
umask函数为进程设置文件模式创建屏蔽字,并返回以前的值。(这是少数几个没有
出错返回函数中的一个)
#include<sys/stat.h>
mode_t umask(mode_t cmask);
返回值:以前的文件模式创建屏蔽字
其中,参数cmask是由9个常量(S_IRUSR、S_IWUSR等)中的若干个按位或构成的
4.9 chmod和fchmod函数
这两个函数使我们可以更改先由文件的访问权限。
#include<sys/stat.h>
int chmod(const char *pathname,mode_t mode);
int fchmod(int filedes,mode_t mode);
返回值:成功返回0,出错则返回-1
chmod函数在指定的文件上进行操作,而fchmod函数则对已打开的文件进行操作
4.11 chown、fchown和lchown函数
下面几个chown函数可用于更改文件的用户ID和组ID。
#include<unistd.h>
int chown(const char *pathname,uid_t owner,gid_t group);
int fchown(int filedes,uid_t owner,gid_t group);
int lchown(const char *pathname,uid_t owner,gid_t group);
返回值:成功返回0,出错则返回-1
除了所引用的文件是符号链接以外,这三个函数的操作相似。在符号链接的情况下
,lchown更改符号链接本身的所有者,而不是该符号链接所指向的文件
4.13 文件截短
有时我们需要在文件尾端处截去一些数据以缩短文件。将一个文件清空为0是一个
特例,在打开文件时使用O_TRUNC标志可以做到这一点。
#include<unistd.h>
int truncate(const char *pathname,off_t length);
int ftruncate(int filedes,off_t length);
均返回:成功返回0,出错则返回-1
这两个函数将把现有的文件长度截短为length字节。
4.15 link、unlink、remove和rename函数
#include<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);
三个函数的返回值:成功返回0,出错则返回-1
一旦给出pathname,stat函数就返回与此命名文件有关的信息结构。fstat函数获
取已在描述符filedes上打开文件的有关信息。lstat函数类似于stat,但是当命名的
文件是一个符号链接时,lstat返回该符号链接的有关信息,而不是由该符号链接引
用文件的信息
struct stat {
mode_t st_mode; /* file type & mode (permissions) */
ino_t st_ino; /* i-node number (serial number) */
dev_t st_dev; /* device number (file system) */
dev_t st_rdev; /* device number for special files */
nlink_t st_nlink; /* number of links */
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 */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modifation */
time_t 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 */
};
4.3 文件类型
文件类型信息包含在stat结构的st_mode成员中。
<sys/stat.h>中的文件类型宏(其参数都是stat结构中的st_mode成员):
S_ISREG() 普通文件
S_ISDIR() 目录文件
S_ISCHR() 字符特殊文件
S_ISBLK() 块特殊文件
S_ISFIFO() 管道或FIFO
S_ISLNK() 符号链接
S_ISSOCK() 套接字
POSIX.1允许实现将进程间通信(IPC)对象(例如,消息队列和信号量等)表示文件。
下面的宏可用来确定IPC对象的类型。其参数是指向stat结构的指针:
<sys/stat.h>中的IPC类型宏:
S_TYPEISMQ() 消息队列
S_TYPEISSEM() 信号量
S_TYPEISSHM() 共享存储对象
4.7 access函数
当用open函数打开一个文件时,内核以进程的有效用户ID和有效组ID为基础执行访
问权限测试。有时,进程也希望按其实际用户ID和实际组ID来测试其访问能力。
access函数按实际用户ID和实际组ID进行访问权限测试。
#include<unistd.h>
int access(const char *pathname,int mode);
返回值:成功返回0,出错则返回-1
其中,mode是下面所列常量的按位或(<unistd.h>):
R_OK 测试读权限
W_OK 测试写权限
X_OK 测试执行权限
F_OK 测试文件是否存在
4.8 umask函数
umask函数为进程设置文件模式创建屏蔽字,并返回以前的值。(这是少数几个没有
出错返回函数中的一个)
#include<sys/stat.h>
mode_t umask(mode_t cmask);
返回值:以前的文件模式创建屏蔽字
其中,参数cmask是由9个常量(S_IRUSR、S_IWUSR等)中的若干个按位或构成的
4.9 chmod和fchmod函数
这两个函数使我们可以更改先由文件的访问权限。
#include<sys/stat.h>
int chmod(const char *pathname,mode_t mode);
int fchmod(int filedes,mode_t mode);
返回值:成功返回0,出错则返回-1
chmod函数在指定的文件上进行操作,而fchmod函数则对已打开的文件进行操作
4.11 chown、fchown和lchown函数
下面几个chown函数可用于更改文件的用户ID和组ID。
#include<unistd.h>
int chown(const char *pathname,uid_t owner,gid_t group);
int fchown(int filedes,uid_t owner,gid_t group);
int lchown(const char *pathname,uid_t owner,gid_t group);
返回值:成功返回0,出错则返回-1
除了所引用的文件是符号链接以外,这三个函数的操作相似。在符号链接的情况下
,lchown更改符号链接本身的所有者,而不是该符号链接所指向的文件
4.13 文件截短
有时我们需要在文件尾端处截去一些数据以缩短文件。将一个文件清空为0是一个
特例,在打开文件时使用O_TRUNC标志可以做到这一点。
#include<unistd.h>
int truncate(const char *pathname,off_t length);
int ftruncate(int filedes,off_t length);
均返回:成功返回0,出错则返回-1
这两个函数将把现有的文件长度截短为length字节。
4.15 link、unlink、remove和rename函数