Linux学习入门

1. 文件模式 (S_IFMT & mode) 测试文件类型的:比如 普通文件 目录文件 设备文件等,见后文stat fstat lstat测试例子。

2. 文件权限

  • 先看一般情况下open函数创建的文件,测试代码如下:

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

    int main(void) { int fd = 0;

    1fd = open("./file.txt",O_RDWR|O_CREAT,0777); 2 3if (fd == -1) 4{ 5 perror("open"); 6} 7 8close(fd);

    }

 

执行后的结果可以看出,group组和其他组没有写权限,open函数中明明写的mode为0777,这是因为umask的作用,终端执行umask命令,可以看到结果为0022,屏蔽了group和other组的写权限,具体可以查阅umask的相关资料

  • 如果我们需要使我们创建的文件指定我们的设定的权限,可以这么做,在线程中设定umask后在创建文件,此处设定umask并不改变系统的umask值

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

    int main(void) { int fd = 0;

    1if (umask(0000) == -1) 2{ 3 perror("umask"); 4} 5 6fd = open("./file.txt",O_RDWR|O_CREAT,0777); 7 8if (fd == -1) 9{ 10 perror("open"); 11} 12 13close(fd);

    }

 

从测试结果可以看出,文件权限为所设定的权限,并且系统的umask值未修改,一般情况下shell中才会用umask,修改文件权限一般用chmod 或fchmod函数。

  • chmod或fchmod,二者区别 传入文件名和文件描述符

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

    int main(void) { int fd = 0;

    #if 0 if (umask(0000) == -1) { perror("umask"); } #endif fd = open("./file.txt",O_RDWR|O_CREAT,0777);

    1fchmod(fd,0777); 2 3if (fd == -1) 4{ 5 perror("open"); 6} 7 8close(fd);

    }

测试结果与umask一致。

chown fchown此处不再演示,与chmod一致。

  • 获取文件的信息,以及测试文件类型

stat以及fstat用来获取文件的信息,结构体如下,可通过man 2 stat查看:

         struct stat {
               dev_t     st_dev;     /* ID of device containing file */
               ino_t     st_ino;     /* inode number */
               mode_t    st_mode;    /* protection */
               nlink_t   st_nlink;   /* number of hard links */
               uid_t     st_uid;     /* user ID of owner */
               gid_t     st_gid;     /* group ID of owner */
               dev_t     st_rdev;    /* device ID (if special file) */
               off_t     st_size;    /* total size, in bytes */
               blksize_t st_blksize; /* blocksize for filesystem I/O */
               blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
               time_t    st_atime;   /* time of last access */
               time_t    st_mtime;   /* time of last modification */
               time_t    st_ctime;   /* time of last status change */
           };

下面例子获取了文件的状态,并判断文件是否是普通文件

1#include <sys/types.h> 2#include <sys/stat.h> 3#include <fcntl.h> 4#include <stdio.h> 5#include <unistd.h> 6 7int main(void) 8{ 9 int fd = 0; 10 struct stat buf; 11 int status; 12 13 if (umask(0000) == -1) 14 { 15 perror("umask"); 16 } 17 18 fd = open("./file.txt",O_RDWR|O_CREAT,0777); 19 20 if (fd == -1) 21 { 22 perror("open"); 23 } 24 25 fstat(fd,&buf); 26 if (status == -1) 27 { 28 perror("stat"); 29 } 30 if (S_ISREG(fd) != -1) 31 { 32 printf("this is a regular file1!\n"); 33 } 34 if (S_IFREG == (S_IFMT & (buf.st_mode))) 35 { 36 printf("this is a regular file2!\n"); 37 } 38 close(fd); 39}

用两种方法来测试文件的类型:S_ISREG(m)和bits flags定义:

           S_IFMT     0170000   bit mask for the file type bit fields
           S_IFSOCK   0140000   socket
           S_IFLNK    0120000   symbolic link
           S_IFREG    0100000   regular file
           S_IFBLK    0060000   block device
           S_IFDIR    0040000   directory
           S_IFCHR    0020000   character device
           S_IFIFO    0010000   FIFO
           S_ISUID    0004000   set-user-ID bit
           S_ISGID    0002000   set-group-ID bit (see below)
           S_ISVTX    0001000   sticky bit (see below)
           S_IRWXU    00700     mask for file owner permissions
           S_IRUSR    00400     owner has read permission
           S_IWUSR    00200     owner has write permission
           S_IXUSR    00100     owner has execute permission
           S_IRWXG    00070     mask for group permissions
           S_IRGRP    00040     group has read permission

运行结果:

此外文件的时间操作(ctime atime mtime),重命名rename以及截断 truncate ,ftruncate ,access测试权限此处不再演示了。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

springCloud 搭建eureka服务之天坑

这里我是采用gradle来管理jar包的。1、使用idea创建一个gradle项目。2、编辑settings.gradle文件rootProject.name'jtm'//include'jtm_core'//include'jtm_sys'//include'jtm_eureka'

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )