Linux系统调用七、与文件权限有关的系统API串讲

简介: Linux系统调用七、与文件权限有关的系统API串讲

🛸1. access函数

  • 包含头文件
#include <unistd.h>
  • 函数原型
int access(const char *pathname, int mode);
  • 函数功能
    判断文件权限以及文件是否存在。access() checks whether the calling process can access the file pathname. If pathname is a symbolic link, it is dereferenced.
  • 函数参数
  • pathname:文件名(及路径)
  • mode:要判断的选项
  • R_OK:是否有读权限
  • W_OK:是否有写权限
  • X_OK:是否有执行权限
  • F_OK:是否存在
  • 函数返回值
  • 成功返回0(有对应权限或文件存在)。On success (all requested permissions granted), zero is returned.
  • 失败返回-1并设置errno。On error (at least one bit in mode asked for a permission that is denied, or some other error occurred), -1 is returned, and errno is set appropriately.

下面通过一个例子演示access函数的用法。

/************************************************************
  >File Name  : access_test.c
  >Author     : QQ
  >Company    : QQ
  >Create Time: 2022年05月15日 星期日 21时23分19秒
************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
  if(argc < 2)
  {
    printf("not found filename\n");
    return -1;
  } 
  if(access(argv[1], F_OK) == 0)
  {
    printf("%s: F_OK\n", argv[1]);  
  }
  else
  {
    printf("no file name is %s\n", argv[1]);
    return 0;
  }
  if(access(argv[1], R_OK) == 0)
  {
    printf("%s: R_OK\n", argv[1]);  
  }
  else
  {
    printf("can not read %s\n", argv[1]); 
  }
  if(access(argv[1], W_OK) == 0)
  {
    printf("%s: W_OK\n", argv[1]);  
  }
  else
  {
    printf("can not write %s\n", argv[1]);  
  }
  if(access(argv[1], X_OK) == 0)
  {
    printf("%s: X_OK\n", argv[1]);  
  }
  else
  {
    printf("can not execution %s\n", argv[1]);  
  }
  return 0;
}

下面测试一下代码,来说明access函数的作用,首先我们在普通用户下测试该函数

现在我们切换到root用户,或者在普通用户下使用sudo命令,再运行一下该函数

对比上面两次测试结果,这时候我们发现一个奇怪的现象,同一个文件1.txt在qq用户下使用access函数返回的是无写权限,但是在root用户下使用access函数返回的是有写权限,这是为什么呢?首先,我们通过 ls -l 命令来查看并分析一下文件1.txt的权限位。首先可以看到,文件归属于root用户,并且该文件对归属用户的权限位是 rw- ,有写权限,对其它用户的权限位是 r-- ,无写权限。也就是说,这个文件1.txt对root用户有写权限,对其他用户比如qq无写权限。现在原因就比较清晰了,access函数在判断权限的时候是判断有效用户的权限,比如说有一个文件对usr1无权限,我们使用access函数获取时确实没有执行权限,但是如果用sudo去执行的话(或者在root用户下执行),就相当于判断这个文件对root用户的权限。也就是说,access函数是判断一个文件相对于某个用户的权限,而不是说文件本身的权限,access函数返回的是文件对某一用户的权限。

🛸2. chmod函数

  • 包含头文件
#include <sys/stat.h>
  • 函数原型
int chmod(const char *path, mode_t mode);
int fchmod(int fd, mode_t mode);
  • 函数功能
    修改某文件的权限,对应于命令chmod是在shell命令行修改权限,使用该函数可以在文件中修改另一文件的权限。These system calls change the permissions of a file.
  • 函数参数
  • path:文件名(路径)
  • mode:文件的权限,可以通过与运算设定多个权限。The new file permissions are specified in mode, which is a bit mask created by ORing together zero or more of the following: S_ISUID, S_ISGID, S_ISVTX, etc. 更多的参数请见man手册。
  • 函数返回值
  • 成功返回0。On success, zero is returned.
  • 失败返回-1并设置errno。On error, -1 is returned, and errno is set appropriately.

🛸3. chown函数

  • 包含头文件
#include <unistd.h>
  • 函数原型
int chown(const char *path, uid_t owner, gid_t group);
int fchown(int fd, uid_t owner, gid_t group);
int lchown(const char *path, uid_t owner, gid_t group);
  • 函数功能
    These system calls change the owner and group of a file. 改变用户和用户组。
  • 函数参数
  • path:文件名及路径
  • owner:用户ID,uid_t类型,可以在/etc/passwd查看,或使用stat函数获取st_uid
  • group:组ID,gid_t类型,可以在/etc/group查看,或使用stat函数获取st_gid
  • 函数返回值
  • 成功返回0。On success, zero is returned.
  • 失败返回-1并设置errno。On error, -1 is returned, and errno is set appropriately.

注意,在查看man手册的时候,如果直接使用 man chown 的话,假如有同名命令会显示同名命令。所以,如果你查看man手册如果发现没有函数原型,说明该函数有同名命令,需要加上章节才能查看函数的帮助手册 man 2 chown,系统调用都在第2章节。

🛸4. rename函数

  • 包含头文件
#include <stdio.h>
  • 函数原型
int rename(const char *oldpath, const char *newpath);
  • 函数功能
    rename() renames a file, moving it between directories if required. 重命名文件(也可重命名目录,目录也是文件)。
  • 函数参数
  • oldpath:旧的文件名(或路径)
  • newpath:新的文件名(或路径)
  • 函数返回值
  • 成功返回0。On success, zero is returned.
  • 失败返回-1并设置errno。On error, -1 is returned, and errno is set appropriately.

示例分析

/************************************************************
  >File Name  : rename_test.c
  >Author     : QQ
  >Company    : QQ
  >Create Time: 2022年05月17日 星期二 11时44分22秒
************************************************************/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
  if(argc < 3)
  {
    printf("not found oldpathname & newpathname\n");
    return -1;  
  }
  rename(argv[1], argv[2]);
  return 0;
}

rename函数可以为文件或目录重命名

rename函数在重命名时也可以改变文件的路径,相当于移动且重命名

🛸5. truncate函数

  • 包含头文件
#include <unistd.h>
#include <sys/types.h>
  • 函数原型
int truncate(const char *path, off_t length);
int ftruncate(int fd, off_t length);
  • 函数功能
    截断文件,将文件截断为指定大小。The truncate() and ftruncate() functions cause the regular file named by path or referenced by fd to be truncated to a size of precisely length bytes.
  • 函数参数
  • path:文件名(路径),必须存在,只有是已经存在的文件才能去截断。
  • length:指定截断后的长度,如果这个长度length小于文件大小,那么就会把文件截断为指定大小;如果length大于文件本身长度,那么就会填充文件直到文件达到length指定的长度(用^@填充)。If the file previously was larger than this size, the extra data is lost. If the file previously was shorter, it is extended, and the extended part reads as null bytes (‘\0’).
  • 函数返回值
  • 成功返回0。On success, zero is returned.
  • 失败返回-1并设置errno。On error, -1 is returned, and errno is set appropriately.

下面通过一个例子来说明truncate函数的用法。

/************************************************************
  >File Name  : truncate_test.c
  >Author     : QQ
  >Company    : QQ
  >Create Time: 2022年05月15日 星期日 22时13分45秒
************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc, char* argv[])
{
  if(argc < 3)
  {
    printf("not found filename\n");
    return -1;  
  }
  printf("file %s truncate : 1024 byte\n", argv[1]);
  truncate(argv[1], 1024);
  printf("file %s truncate : 4 byte\n", argv[2]);
  truncate(argv[2], 4);
  return 0;
}

运行函数,查看测试结果

使用vim编辑器打开可以看到被扩展的文件中填充了很多@字符(其实都是空字节 \0 只不过在不同平台上显示出来不一样而已)。


相关文章
|
1月前
|
Ubuntu Linux
计算机基础知识:linux系统怎么安装?
在虚拟机软件中创建一个新的虚拟机,并选择相应操作系统类型和硬盘空间大小等参数。将下载的 ISO 镜像文件加载到虚拟机中。启动虚拟机,进入安装界面,并按照步骤进行安装。安装完成后,可以在虚拟机中使用 Linux 系统。
|
1月前
|
缓存 监控 Linux
Linux系统清理缓存(buff/cache)的有效方法。
总结而言,在大多数情形下你不必担心Linux中buffer与cache占用过多内存在影响到其他程序运行;因为当程序请求更多内存在没有足够可用资源时,Linux会自行调整其占有量。只有当你明确知道当前环境与需求并希望立即回收这部分资源给即将运行重负载任务之前才考虑上述方法去主动干预。
498 10
|
1月前
|
安全 Linux 数据安全/隐私保护
为Linux系统的普通账户授予sudo访问权限的过程
完成上述步骤后,你提升的用户就能够使用 `sudo`命令来执行管理员级别的操作,而无需切换到root用户。这是一种更加安全和便捷的权限管理方式,因为它能够留下完整的权限使用记录,并以最小权限的方式工作。需要注意的是,随意授予sudo权限可能会使系统暴露在风险之中,尤其是在用户不了解其所执行命令可能带来的后果的情况下。所以在配置sudo权限时,必须谨慎行事。
256 0
|
1月前
|
Ubuntu Linux 开发者
国产 Linux 发行版再添新成员,CutefishOS 系统简单体验
当然,系统生态构建过程并不简单,不过为了帮助国产操作系统优化生态圈,部分企业也开始用国产操作系统替代 Windows,我们相信肯定会有越来越多的精品软件登录 Linux 平台。
86 0
|
1月前
|
Ubuntu 安全 Linux
Linux系统入门指南:从零开始学习Linux
Shell脚本是一种强大的自动化工具,可以帮助您简化重复的任务或创建复杂的脚本程序。了解Shell脚本的基本语法和常用命令,以及编写和运行Shell脚本的步骤,将使您更高效地处理日常任务。
145 0
|
1月前
|
Ubuntu Linux 图形学
Linux学习之Linux桌面系统有哪些?
Cinnamon:与MATE类似,Cinnamon 拥有 GNOME 和 Unity 等其它桌面环境所没有的种种功能,是高度可定制的桌面环境,不需要任何外部插件、窗口组件和调整工具来定制桌面。
94 0
|
1月前
|
Ubuntu 安全 Linux
十款常用Linux系统介绍
本文不是什么大盘点。市面上有好几百款发行版,每款发行版在某个方面都与众不同。不可能在此全部罗列,本文只罗列了十款最常见的Linux发行版(世界上只有两种人,一种是懂二进制的,另一种是不懂二进制的)。请宣传Linux的魅力或威力。
|
1月前
|
Ubuntu 安全 Linux
linux系统|Ubuntu 18.10 如期正式发布,新面孔新技术都来了
微软公司也终于沉不住气要在linux开源系统开疆扩土了。mscode这样的工具的确好用,虽然差第一名那么一点儿,但是最老版的公司出版的软件的确很是让人动心!
|
1月前
|
Ubuntu Linux 数据安全/隐私保护
Win10安装Linux子系统教程!如何在Win10系统中安装Ubuntu!
登录系统后,输入cd /返回上一级,然后再输入“ls”查看一下系统文件目录,看看对不对!
|
1月前
|
Web App开发 安全 Linux
Linux 比起其他系统的5 个优点和 5 个缺点
对Linux系统感兴趣的朋友,可以点击下方书籍进行学习。