第四章 文件和目录

本章主要介绍文件目录的创建、删除、读写、设置访问权限以及获取文件或目录的属性等;

唯一需要强调的可能就是:目录也是一种文件

获取文件目录的属性



关键函数

stat/fstat/lstat用于获取文件的信息,如文件的所有者,权限,修改和访问时间等等

     #include <sys/stat.h>

     int
     fstat(int fildes, struct stat *buf); // 获取已打开的文件的相关信息

     int
     fstat64(int fildes, struct stat64 *buf); // 获取已打开的文件的相关信息

     int
     lstat(const char *restrict path, struct stat *restrict buf); // 获取符号链接文件的相关信息[不是被链接的文件的信息]

     int
     lstat64(const char *restrict path, struct stat64 *restrict buf); // 获取符号链接文件的相关信息[不是被链接的文件的信息] 

     int
     stat(const char *restrict path, struct stat *restrict buf);// 通过文件名获取文件的相关信息

     int
     stat64(const char *restrict path, struct stat64 *restrict buf);//通过文件名获取相关的信息

关键结构体

struct stat {
         dev_t    st_dev;    /* device inode resides on */
         ino_t    st_ino;    /* inode‘s number */
         mode_t   st_mode;   /* inode protection mode */
         nlink_t  st_nlink;  /* number or hard links to the file */
         uid_t    st_uid;    /* user-id of owner */
         gid_t    st_gid;    /* group-id of owner */
         dev_t    st_rdev;   /* device type, for special file inode */
         struct timespec st_atimespec;  /* time of last access */
         struct timespec st_mtimespec;  /* time of last data modification */
         struct timespec st_ctimespec;  /* time of last file status change */
         off_t    st_size;   /* file size, in bytes */
         quad_t   st_blocks; /* blocks allocated for file */
         u_long   st_blksize;/* optimal file sys I/O ops blocksize */
         u_long   st_flags;  /* user defined flags for file */
         u_long   st_gen;    /* file generation number */
 };

 struct stat64 {
         dev_t           st_dev;           /* ID of device containing file */
         mode_t          st_mode;          /* Mode of file (see below) */
         nlink_t         st_nlink;         /* Number of hard links */
         ino64_t         st_ino;          /* File serial number */
         uid_t           st_uid;           /* User ID of the file */
         gid_t           st_gid;           /* Group ID of the file */
         dev_t           st_rdev;          /* Device ID */
         struct timespec st_atimespec;     /* time of last access */
         struct timespec st_mtimespec;     /* time of last data modification */
         struct timespec st_ctimespec;     /* time of last status change */
         struct timespec st_birthtimespec; /* time of file creation(birth) */
         off_t           st_size;          /* file size, in bytes */
         blkcnt_t        st_blocks;        /* blocks allocated for file */
         blksize_t       st_blksize;       /* optimal blocksize for I/O */
         uint32_t        st_flags;         /* user defined flags for file */
         uint32_t        st_gen;           /* file generation number */
         int32_t         st_lspare;        /* RESERVED: DO NOT USE! */
         int64_t         st_qspare[2];     /* RESERVED: DO NOT USE! */
 };
 

access



<unistd.h>

int access(const char* path, int mode);

测试文件、目录访问权限

mode 参数:

R_OK   是否可读

W_OK  是否可写

X_OK   是否可执行

F_OK   是否存在

被测试文件具有对应的权限时,函数返回0;否则返回-1

umask



<sys/types.h>

<sys/stat.h>

mode_t umask(mode_t cmask);

在程序运行时创建屏蔽字,控制所有新创建文件的许可权限

参数 mode_t为下列9种权限的‘或’

---------------

S_IRUSR     用户-读

S_IWUSR    用户-写

S_IXUSR     用户-执行

S_IRGRP      组-读

S_IWGRP     组-写

S_IXGRP      组-执行

S_IROTH     其他-读

S_IWOTH    其他-写

S_IXOTH     其他-执行

-----------------

chmod/fchmod



chmod用于对未打开的文件进行权限变更;fchmod用于对已打开的文件进行权限变更;

<sys/types.h>

<sys/stat.h>

int chmod(const char* path, mode_t mode);

int fchmod(int fileds, mode_t mode);

权限更改成功返回0;否则返回-1;

参数:mode

--------------------------

S_ISUID        执行时设置-用户-ID

S_ISGID        执行时设置-组-ID

S_ISVTX        保存正文

S_IRWXU      用户(所有者)读、写和执行

S_IRUSR       用户(所有者)读

S_IWUSR      用户(所有者)写

S_IXUSR       用户(所有者)执行

S_IRWXG       组读、写和执行

S_IRGRP        组读

S_IWGRP       组写

S_IXGRP        组执行

S_IRWXO       其他读、写和执行

S_IROTH        其他读

S_IWOTH       其他写

S_IXOTH        其他执行

--------------------------

说明:要成功更改文件权限,进程的有效用户ID必须是文件的所有者,或者具有超级用户许可权限

chown/fchown/lchown



变更文件所有者

<sys/types.h>

<unistd.h>

int chown(const char* path, uid_t owner, gid_t group);

int fchown(int fileds, uid_t owner, gid_t group);

int lchown(const char* path, uid_t owner, gid_t group);

说明:lchown改变的符号链接本身的所有者,不是符号链接所指向文件的所有者

要成功更改文件所有者需要程序执行用户拥有相应的权限

truncate/ftruncate[文件截短]



<sys/types.h>

<unistd.h>

int truncate(const char* path, off_t length);

int ftruncate(int filedes, off_t length);

函数执行成功返回0;出错返回-1;

如果文件的长度大于length,则函数执行成功后超过length意外的数据不能再继续访问; 如果文件长度小于length,则文件尾部没有数据,形成一个空洞;

link/unlink/symlink/readlink/rename/remove



创建链接、删除链接、创建符号链接、读链接、重命名、删除

mkdir/rmdir/opendir/readdir/closedir/rewinddir/chdir/fchdir/getcwd



目录操作函数

utime[获取文件的最后访问时间、修改时间]




reference

Unix环境高级编程

https://developer.apple.com/library/ios/documentation/System/Conceptual/ManPages_iPhoneOS/man2/stat.2.html

时间: 2024-07-30 19:14:49

第四章 文件和目录的相关文章

APUE学习笔记:第四章 文件和目录

4.1 引言 本章将描述文件的特征和文件的性质 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); 三个函数的返

APUE读书笔记-第四章 文件和目录

到第四章了,不知什么时候才能把这本书看完,耽误的时间太多了. 第四章是在第三章的基础上,主要描述文件系统的其他性质和文件的性质. 4.2 stat.fstat.fstatat.lstat函数 首先来看看这四个函数的原型: #include <sys/stat.h> ///usr/include/x86_64-linux-gnu/sys/ int stat (const char *__restrict __file, struct stat *__restrict __buf) int fst

apue第四章 文件和目录

函数stat,fstat,fstatat, lstat #include <sys/stat.h> int stat(const char *restrict pathname, struct stat *restrict buf); int fstat(int fd, struct stat *buf); int lstat(const char *restrict pathname, struct stat *restrict buf); int lstat(int fd, const c

《python编程》第四章——文件和目录工具

1.文件和目录,他们本身就是我们在电脑里看到的那些一个个文件.目录在windows中就是文件夹,很好理解. 2.我们用到的大多数用途,就是利用open内建函数及其文件对象来处理文件. 3.open函数返回的文件对象有多种方法,read,readline,readlines,write,writelines,close,seek,flush(将缓存区的数据强制转移到磁盘),fileno(获取底层的文件句柄)等. 4.打开一个文件的整个步骤都是分为三步的,1.打开,同时指定打开来做什么用(比如wri

第四章 文件的基本管理和XFS文件系统备份恢复

第四章 文件的基本管理和XFS文件系统备份恢复 本节所讲内容: 4.1 Linux系统目录结构和相对/绝对路径. 4.2 创建/复制/删除文件,rm -rf / 意外事故 4.3 查看文件内容的命令 4.4 实战:xfs文件系统的备份和恢复   4.1 Linux系统目录结构和相对/绝对路径 4.1.1系统目录结构 在WIN系统中,查看文件先进入相应的盘符,然后进入文件目录 ?? 在WIN中,它是多根 c:\ d:\ e:\ Linux只有一个根目录   ?? 使用tree命令查看linux目录

分分钟钟学会Python - 第四章 文件操作

第四章 文件操作 4.1 文件基本操作 obj = open('路径',mode='模式',encoding='编码') obj.write() # 写入 obj.read() # 读取 obj.close() #关闭 4.2 打开模式 r / w / a [只读只写字符串] * r+ / w+ / a+ [可读可写字符串] rb / wb / ab [只读只写二进制] * r+b / w+b / a+b [可读可写二进制] 4.3 操作 read() , 全部读到内存 read(1) 1表示一

python第四章文件操作

第四章 文件操作 4.1 文件基本操作 obj = open('路径',mode='模式',encoding='编码') # 打开文件 obj.write() # 把内容写入文件 obj.read() # 读取文件内容 obj.close() # 关闭文件(保存文件,把内存上的数据写入到文件上-->硬盘上,01010101存储的) # 3步:1.打开文件. 2. 操作文件 3. 关闭文件 4.2打开文件 4.2.1语句 file = open('文件路径',mode = 'r',encoding

第四章文件操作

第四章 文件操作 4.1 文件基本操作 obj = open('路径',mode='模式',encoding='编码') obj.write() obj.read() obj.close() with open... 4.2 打开模式 r / w / a r+ / w+ / a+ rb / wb / ab r+b / w+b / a+b 4.3 操作 read() , 全部读到内存 read(1) 1表示一个字符 obj = open('a.txt',mode='r',encoding='utf

四. Linux文件与目录权限

文件与目录权限,umask, chgrp, chown, chmod 1. 文件与目录权限 (1) 查看/etc/passwd文件属性 [[email protected] ~]# ll -h --full-time /etc/passwd [[email protected] ~]#-rw-r--r--. 1 root root 2.3K 2016-11-09 21:07:03.303125300 +0800 /etc/passwd (2) 文件和目录权限的意义 文件权限 r(read) :