文件及目录

1.文件系统

文件权限:

  • 特点 :
      1. 目录:
      • 读权限: 读取目录中的文件名列表。只有读权限,只能获取文件名列表,不能获取文件其它信息。
      • 写权限: 在目录中添加、修改、删除文件或目录。
      • 执行权限: 对目录有操作权限,类似于进入目录的权限
      • 执行权限和读、写权限:先有进入目录的权限,然后才能读、操作

文件长度:

  • 普通文件、目录文件、符号连接以字节为长度,其中符号连接是文件名的字节数。

文件系统

输入图片说明

  • 自举块:引导程序。

  • 超级块: 记录整个文件系统相关的信息的地方,它记录的信息主要有:block与inode的总量、使用量、剩余量,文件系统的挂载时间,最近一次写入数据的时间等。

  • i节点图:标记那些inode 是否为已使用

  • 块位图:标记那些block 是否为已使用

  • i节点(表):i节点存放文件有关的所有信息:文件类型、文件访问权限位、文件长度和指向文件数据块的指针、文件名、i节点编号.

  • 数据块:存放文件内容。

  • 特点 :

    • unix系统以inode号来操作系统。inode号对应的数据存了文件名、权限、增改查时etc

    • 文件名只是方便人类操作,整数操作比字符快

  • 操作

    扫描二维码关注公众号,回复: 77113 查看本文章
    • 访问 /etc/passwd 文件:

      • 先通过 / 的inode号获取 / 信息。通过权限验证后,从对应的block中访问 文件名列表,找到 /etc

      • 先通过 /etc 的inode号获取 /etc 信息。通过权限验证后,从对应的block中访问 文件名列表,找到 /etc/passwd

      • 先通过 /etc/passwd 的inode号获取 /etc/passwd 信息。通过权限验证后,从对应的block中得到文件信息

    • 修改文件名:直接替换inode中的文件名信息

    • 同一个文件系统中移动文件。创建一个新的inode节点,将旧的inode节点删除。

2.操作方法

方法名 作用 成功 失败
stat 返回文件信息 0 -1
access和faccessat 返回文件访问权限 0 -1
umask 屏蔽进程的文件模式的权限 返回之前的文件模式创建的屏蔽字
chmode、fchmode、fchmodat 变更文件的权限 0 -1
link、linkat 创建连接 0 -1
unlink、unlinkat 删除一个文件连接。如果连接为0,则文件删除 0 -1
remove 删除一个文件连接。如果连接为0,则文件删除 0 -1
rename、renameat 重命名 0 -1
symlink、symlinkat 创建符号链接 0 -1
readlink、readlinkat 读取符号链接 0 -1
futimens、utimensat 和 utimes 修改文件时间 0 -1
mkdir、mkdirat 创建目录 0 -1
rmdir 删除目录 0 -1
chdir、fchdir 打开工作目录 0 -1
getcwd 打开工作目录 0 NULL

stat、fstat、fstatat、lstat 返回pathname的信息

#include <sys/stat.h>

inct 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 fstat(int fd, const char *restrict pathname, struct stat restrict buf, int flag);
    --'成功:0;出错:-1'
    
  • 参数:

    • stat 结构
    struct stat {
        mode_t              st_mode;        '文件类型'
        ino_t               st_ino;         'i-node 数数量'
        dev_t               st_dev;         'device 数量'
        dev_t               st_rdev;        '特殊文件的 device 数量'
        nlink_t             st_nlink;       'link文件的数量'
        uid_t               st_uid;         'user ID'
        gid_t               st_gid;         'group ID'
        off_t               st_size;        '普通文件的字节大小'
        struct timespec     st_atime;       '最近被访问时间'
        struct timespec     st_mtime;       '最近被修改时间'
        struct timespec     st_ctime;       '被创建时间'
        blksize_t           st_blksize;     'I/O 的块大小'
        blkcnt_t            st_blocks;      '磁盘分配的块大小'
    }
    
    • 文件类型

      • 普通文件(regular file):常用文件类型
      • 目录文件(directory file):包涵了其它文件的名字及指向与这些文件有关信息的指针。
      • 块特殊文件 (block special file):提供设备(如磁盘)带缓冲的访问,每次访问以固定长度为单位进行。
      • 字符特殊文件 (character special file):提供设备不带缓冲的访问。
      • FIFO:用于进程间通信。
      • 套接字(socket):用于进程间的网络通信。
      • 符号链接(symbolic link):指向另外一个文件。
    • st_mode 的类型 # include<sys/stat.h>

说明
文件类
S_ISREG() 不同文件
S_ISDIR() 目录文件
S_ISCHR() 字符特殊文件
S_ISBLK() 块特殊文件
S_ISFIFO() 管道或FIFO
S_ISLNK() 符号链接
S_ISSOCK() 套接字
IPC类
S_TYPEISMQ() 消息队列
S_TYPEISSEM() 信号量
S_TYPEISSHM() 共享存储对象
  • 特点:返回与pathname 有关的新消息结构

access和faccessat 访问权限

#include <unistd.h>

int access(const char *pathname, int mode); '绝对路径'

int faccessat(int fd, const char *pathname, int mode ,int flag); '相对fd的路径'

        -- '成功:0;出错:-1'

  • 参数

    • mode :
mode 说明
R_OK 测试读权限
W_OK 测试写权限
X_OK 测试执行权限

umask : 屏蔽进程的文件模式的权限

#incLude <sys/stat.h>

mode_t umask(mode_t cmask);
 
  --- '返回之前的文件模式创建的屏蔽字'

chmode、fchmode、fchmodat : 变更文件的权限

#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);

        --- '成功:0;失败-1'
  • 参数

    • mode :
mode 说明
S_ISUID 执行时设置用户ID
S_ISGID 执行时设置组ID
S_ISXID 保存正文(粘着位)
S_IRWXU 用户(所有者)读、写和执行
S_IRUSR 用户(所有者)读
S_IWUSR 用户(所有者)写
S_IXUSR 用户(所有者)执行
S_IRWXG 组读、写和执行
S_IRGRP 组读
S_IWGRP 组写
S_IXGRP 组执行
S_IRWXO 其它读、写和执行
S_IROTH 其它读
S_IWOTH 其它写
S_IXOTH 其它执行

link、linkat、ulink、unlink、unlinkat、remove : 连接操作

#incldue <unistd.h>

' 创建连接'
int link(const char *existingpath , const char *newpath) ;

int linkat(int efd, const char *existingpath, int nfd, const char *newpath , int flag );
    
        -- '成功:0;出错:-1'

'删除一个连接'

int unlink(const char *pathname);

int unlinkat(int fd , const char *pathname ,int flag);

        -- '成功:0;失败:-1'
        
'删除'
#include <stdio.h>

int remove(const char *pathname); '成功:0;失败:-1'

rename、renameat : 重命名

#incldue <unistd.h>


int rename(const char *oldname , const char *newname) ;

int renameat(int oldfd , const char *oldname, int newfd, const char *newname);
    
        -- '成功:0;出错:-1'


symlink、symlinkat : 创建符号链接

#incldue <unistd.h>


int symlink(const char *oldname , const char *newname) ;

int symlinkat(int oldfd , const char *oldname, int newfd, const char *newname);
    
        -- '成功:0;出错:-1'


readlink、readlinkat : 读取符号链接

#incldue <unistd.h>


ssize_t readlink(const char *restrict pathname , char *restrict buf , size_t bufsize ) ;

ssize_t readlinkat(int fd , const char *restrict pathname , char *restrcit buf newfd, size_t bufsize  );
    
        -- '成功:返回读取的字节数;出错:-1'


futimens、utimensat 和 utimes: 修改文件时间

#incldue <sys/stat.h>


ssize_t futimens(int fd , const struct timespec  time[2] ) ;

ssize_t utimensat(int fd ,const char *path , const struct timespec  time[2] ,int flag ) ;
    
        -- '成功:0;出错:-1'

#include <sys/time.h>

int utimes(const char *pathname, const struct timeval time[2]);
        
        -- '成功:0;出错:-1'

mkdir、mkdirat : 创建目录

#incldue <sys/stat.h>

int mkdir(const char *pathname,mode_t mode);

int mkdirat(int fd , const char *pathname,mode_t mode);
        
        -- '成功:0;出错:-1'

rmdir : 删除目录

#incldue <sys/stat.h>

int rmdir(const char *pathname,mode_t mode);
        
        -- '成功:0;出错:-1'

读目录

#incldue <dirent.h>


DIR *opendir(const char *pahtname);

DIR *fopendir(int fd);

        -- '成功:返回指针;出错:NULL' 
        
struct dirent *readdir(DIR *dp);

        -- '成功:返回指针;若在目录尾或出错:NULL' 
        
void rewinddir(DIR  *dp);

void closedir(DIR  *dp);

        -- '成功:0;出错:-1' 
        
long telldir(DIR  *dp);

        -- '返回:与dp关联的目录中的当前位置' 
                
void seekdir(DIR  *dp , long ioc);

        -- '成功:0;出错:-1' 
                

chdir、fchdir、getcwd: 打开工作目录

#incldue <unistd.h>


int *chdir(const char *pahtname);

int fchdir(int fd);

        -- '成功:0;出错:-1' 

char *getcwd(char *buf, size_t size);


        -- '成功:返回buf;出错:NULL' 
        

猜你喜欢

转载自my.oschina.net/u/2246410/blog/1801948
今日推荐