在这里插入图片描述

本文参考《APUE》
为我的团队成员们作准备,详情见:https://blog.csdn.net/qq\_43762191/article/details/106697094
CSDN搜“看,未来”:https://blog.csdn.net/qq\_43762191

文章目录

本文代码皆为使用示例。

stat

先来看是三个函数:stat、fstat、lstat

 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>

 int stat(const char *pathname, struct stat *buf);
 int fstat(int fd, struct stat *buf);
 int lstat(const char *pathname, struct stat *buf);
//返回值:若成功,返回0;若出错,返回-1

这三个函数干嘛用呢?获取文件信息。那我稍微解释一下吧,这个不是重点。


stat函数返回与pathname有关的信息结构。stat称为跟踪链接。
fstat函数获得已在描述符fd上打开的文件的有关信息。
lstat函数与stat函数类似,但当命令文件是一个符号链接时,lstat返回该符号链接的有关信息,而不是由该符号链接引用的文件的信息。lstat称为不跟踪符号链接。

这三个函数有一个相同类型的参数struct stat *类型的buf,buf是一个指针。

文件类型

头文件:#include<sys/stat.h>
在这里插入图片描述
看个实验测试,书里代码太长我就不搬运了:

#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>

using namespace _GNU_SOURCE

在这里插入图片描述

拿去运行一下呢:

在这里插入图片描述

用户访问权限

每个文件有9个访问权限位,可以分成三类。

在这里插入图片描述chmod(1)命令用于修改这9个权限位。

access函数

access函数用于测试用户对于文件的权限:
在这里插入图片描述

#include <unistd.h>

int access(const char *pathname, int mode);
//返回值:若成功,返回0,若出错,返回-1

使用示例:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char* argv[])
{
    if(argc < 2){
        printf("./aut filename\n");
        exit(1);
    }

    if(access(argv[1], F_OK) < 0){
        perror("access");
        exit(1);
    }
    else{
        printf("%s : file exist\n", argv[1]);
    }

    if(access(argv[1], R_OK) < 0){
        perror("access error");
    }
    else{
        printf("read access OK\n");
    }
    
    if(open(argv[1], O_RDONLY) < 0){
        perror("open error");
        exit(1);
    }
    else{
        printf("open read OK\n");
    }
    return 0;
}

chmod、fchmod

这俩函数,用于修改现有文件权限。

#include <sys/stat.h>

int chmod(const char *pathname, mode_t mode);
int fchmod(int fd, mode_t mode);
//返回值:若成功,返回0,若出错,返回-1

mode参数除了之前的9个,还有6个如下:
在这里插入图片描述
使用示例:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

int main()
{
        struct stat buf;

        if(stat("foo", &buf) < 0){
                perror("stat err");
                exit(1);
        }
        if(chmod("foo", (buf.st_mode & ~S_IXGRP) | S_ISGID) < 0){
                perror("chmod foo err");
                exit(1);
        }
        return 0;
}

link / unlink

使用link函数创建一个指向现有文件的链接:

link

#include <unistd.h>

int link(const char *oldpath, const char *newpath);
//返回值:若成功,返回0, 若出错,返回-1
  • 这个函数创建一个新目录项newpath,它引用现有的文件,如果newpath已经存在,则返回出错。只创建newpath最后一个分量,路径中的其他部分应当已经存在。
  • 不允许对目录创建硬链接。
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main()
{
        if(link("file", "file.link") < 0){
                perror("link err");
                exit(1);
        }
}

unlink

为了删除一个现有的目录项:

#include <unistd.h>

int unlink(const char *pathname);
//返回值:若成功,返回0,若出错,返回-1
  • unlink删除目录项,并由pathname所引用文件的链接数减1
  • 如果要解除对文件链接,必须对包含该目录项的目录具有写和执行权限。
  • 只有当链接计数达到0时,该文件的内容才可被删除,如果内核首先检查是否有进程打开了该文件, 在检查连接数,二者计数都为0时,那么才删除文件的内容。
int main()
{
        int fd;
        if((fd = open("file.link", O_RDONLY)) < 0){//打开file.link文件
                perror("open file.link err");
                exit(1);
        }
        printf("open file.link\n");
        if(unlink("file.link") < 0){//解除file.link
                perror("unlink file.link err");
                exit(1);
        }
        if(unlink("file") < 0){//解除file
                perror("unlink file.link err");
                exit(1);
        }
        sleep(10);

        printf("done\n");
        close(fd);
}

mkdir / rmdir

mkdir

mkdir函数可以创建新的目录,其中“.”和“…”自动创建。
所指定的文件访问权限mode由进程的文件模式创建屏蔽字修改。
目录至少要设置一个执行权限位。

#include <sys/stat.h>
#include <sys/types.h>

int mkdir(const char *pathname, mode_t mode);
//返回值:若成功,返回0, 若出错返回-1

rmdir

rmdir函数删除一个空目录。空目录只有“.”和“…”这两项目录
若调用这两个函数时目录链接计数为0,并且也没有其他进程打开此目录,则释放由此目录占用的空间。

#include <sys/stat.h>
#include <sys/types.h>

int mkdir(const char *pathname, mode_t mode);
//返回值:若成功,返回0, 若出错返回-1

读目录

对某个目录具有访问权限的任一用户都可以读该目录,但是,为了防止文件系统混乱,只有内核才能写目录。
一个目录有写和执行权限决定了在该目录下可以新建文件以及删除文件,不代表能否写目录本身。

#include <sys/types.h>
#include <dirent.h>

DIR *opendir(const char *name);
DIR *fdopendir(int fd);
//返回值:若成功,返回指针,若出错,返回NULL

struct dirent *readdir(DIR *dirp);
//返回值:若成功,返回指针,若出差或在目录尾,返回NULL

void rewinddir(DIR *dirp);

int closedir(DIR *dirp);
//返回值:若成功,返回0,若出错返回-1

long telldir(DIR *dirp);
//返回值:与drip关联的目录中的当前位置

void seekdir(DIR *dirp, long loc);

一般由opendir和fopendir返回指向DIR结构的指针由另外5个函数调用。

大概先来这些。

在这里插入图片描述

标签: Linux, 文件, 目录, int, include, 参考, 返回, link, stat

相关文章推荐

添加新评论,含*的栏目为必填