APUE学习——Ch4.文件和目录

来源:互联网 发布:mac vim 配置文件 编辑:程序博客网 时间:2024/06/06 12:40

描述文件系统的其他特征和文件的性质

1.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 {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;time_t st_atime;time_t st_mtime;time_t st_ctime;blksize_t st_blksize;blkcnt_t st_blocks;mode_t st_attr;}; 
2.文件类型

unix系统中的文件类型包括:

(1)普通文件(regular file),最常用的文件类型。

(2)目录文件(directory file),包含了其他文件的名字以及指向与这些文件有关信息的指针。

(3)块特殊文件(block special file),提供对设备带缓冲的访问,每次访问以固定长度为单位。

(4)字符特殊文件(character special file),提供对设备不带缓冲的访问,每次访问长度可变。系统中的所有设备要么是字符特殊文件,要么是块特殊文件。

(5)FIFO,用于进程间通信,有时也将其称为命名通道(named pipe)。

(6)套接字(socket),用于进程间的网络通信,套接字也可用于在一台宿主机上进程之间的非网络通信。

(7)符号链接(symbolic link),这种类型指向另一文件。

文件类型信息包含在stat结构的st_mode成员中,可以用表4-1中宏确定文件类型,这些宏的参数都是stat结构中的st_mode成员。

Figure 4.1. File type macros in <sys/stat.h>

Macro

Type of file

S_ISREG()

regular file

S_ISDIR()

directory file

S_ISCHR()

character special file

S_ISBLK()

block special file

S_ISFIFO()

pipe or FIFO

S_ISLNK()

symbolic link

S_ISSOCK()

socket


3.文件访问权限

Figure 4.6. The nine file access permission bits, from <sys/stat.h>

st_mode mask

Meaning

S_IRUSR

user-read

S_IWUSR

user-write

S_IXUSR

user-execute

S_IRGRP

group-read

S_IWGRP

group-write

S_IXGRP

group-execute

S_IROTH

other-read

S_IWOTH

other-write

S_IXOTH

other-execute


4.access函数

access函数按照实际用户ID和实际组ID进行访问权限测试。

#include <unistd.h>int access(const char* pathname, int mode);/* 成功则返回0,出错则返回-1 */

Figure 4.7. The mode constants foraccess function, from<unistd.h>

mode

Description

R_OK

test for read permission

W_OK

test for write permission

X_OK

test for execute permission

F_OK

test for existence of file


5.umask函数

umask函数为进程设置文件模式创建屏蔽字,并返回以前的值(没有出错返回)。

#include <sys/stat.h>mode_t umask(mode_t cmask);/* 返回以前的文件模式创建屏蔽字 */



6.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函数则对已打开的文件进行操作。为了改变一个用户的权限位,进程的有效用户ID必须等于文件的所有者ID,或者该进程必须具有超级用户权限。

参数mode是表4-11中所示常量的某种按位或运算构成的。


7.chown、fchown和lchown函数

用于更改文件的用户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 */

8.文件长度

stat结构成员st_size表示以字节为单位的文件长度。此字段只对普通文件、目录文件和符号链接有意义。


9.文件的时间

对每个文件保持三个时间字段:

(1)st_atime,文件数据的最后访问时间,例如read

(2)st_mtime,文件数据的最后修改时间(修改文件的实际内容),例如write

(3)st_ctime,i节点状态的最后更改时间,例如chmod、chown

一个文件的访问和修改时间可以用utime函数更改


 10.mkdir和rmdir函数

用mkdir函数创建目录,用rmdir函数删除目录:

#include <sys/stat.h>int mkdir(const char* pathname, mode_t mode);/* 成功返回0,出错则返回-1 */

删除一个空目录:

#include <sys/stat.h>int rmdir(const char* pathname);/* 成功返回0,出错则返回-1 */


11.读目录

对某个目录具有访问权限的任一用户都可读该目录:

#include <dirent.h>DIR *open(const char* pathname);/* 成功返回指针,出错则返回NULL */struct dirent *readdir(DIR *dp);/* 成功则返回指针,若在目录结尾或出错则返回NULL */void rewinddir(DIR *dp);int closedir(DIR *dp);/* 成功则返回0,出错则返回-1 */long telldir(DIR *dp);/* 与dp关联的目录中的当前位置  */void seekdir(DIR *dp, long loc);

头文件<dirent.h>中定义的dirent结构与实现有关,典型的UNIX实现对此结构所作的定义至少包含下列两个成员:

struct dirent{    ino_t d_ino; /*  i-node number */    char d_name[NAME_MAX+1]; /*null-terminated filename */}

小结:

对文件的所有属性以及操作文件的所有函数有完整的了解对UNIX编程是非常重要的。



原创粉丝点击