keywords : stat access chmod chown chgrp bit mask umask

linux系统中的权限详解

这位博主写的很清楚的,目前对我有用的就是文件的基本权限跟默认权限的使用。剩下的有需要可以去看。

下面是学习的总结

stat

stat 可以作为一个函数进行使用,直接man stat的话进入的是stat(1),然后一直pagedown 会看到SEE ALSO
在这里插入图片描述
stat 同时也是一个linux内核中的一个api
man 2 stat里面就展示了它的描述

These functions return information about a file, in the buffer pointed
to by statbuf.

在stat(2)里面包括了fstat()、stat()还有lstat()

   stat() and fstatat() retrieve information about the file pointed to by
   pathname; the differences for fstatat() are described below.
	这里说了fstat跟stat都是从路径名指向的那个文件里面取信息的

   lstat() is identical to stat(), except that if pathname is a  symbolic
   link,  then it returns information about the link itself, not the file
   that it refers to.
	这里说lstat是独立于stat的,要求就是路径名是一个符号链接,然后返回的信息也是符号链接的信息,而不是文件本身的信息
   fstat() is identical to stat(),  except  that  the  file  about  which
   information is to be retrieved is specified by the file descriptor fd.
   不一样的是fstat的话是从fd里面工作的,有了fd就证明你文件开了,所有效率比stat高一点。

stat和lstat的实现代码

stat 就是用来 返回文件的各种信息
例子:

#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[])
{
	if(argc < 2)
	{
		printf("usage: ./a.out filename\n");
		exit(-1);
			
	}
	
	printf("*******usage of stat**********\n");
	struct stat buf;
	
	memset(&buf,0,sizeof(buf));
	int ret = 0;
	ret  = stat(argv[1],&buf);
	
	printf("st_ino %ld\n",buf.st_ino);
	perror("??");
	printf("*******usage of stat**********\n");
	
	
	
	printf("*******usage of lstat**********\n");
	memset(&buf,0,sizeof(buf));
	ret = lstat(argv[2],&buf);
	printf("st_ino %ld\n",buf.st_ino);
	perror("??");
	printf("*******usage of lstat**********\n");
	
	
	return 0;
	
	
}

这里用程序写了一下stat跟lstat
结果是这样的在这里插入图片描述
123是statandlstat.c的符号链接,传进去stat测statandlstat.c,lstat测123,结果都没问题能工作。
有个疑问,试过把argv[1]传进去给lstat发现也能工作。跟stat一样。

printf("*******usage of fstat**********\n");
	int fd = 0;
	fd = open(argv[1],O_RDONLY,0444);
	printf("fd = %d\n",fd);
	memset(&buf,0,sizeof(buf));
	ret = fstat(fd,&buf);
	printf("st_ino = %ld\n",buf.st_ino);

在这里插入图片描述

fstat也不难,都是根据man手册然后操作api来实现功能的。
fstat的话就跟上面的不一样,它是利用了fd来实现获取文件信息的。

access

在terminal里面直接stat + 文件的话就显示了文件的各种信息,会看到有
access

access的description

DESCRIPTION
access() checks whether the calling process can access the file pathname. If pathname is a symbolic link, it is dereferenced.

主要的话就知道有几个mode位以及代表的意思

F_OK tests for the existence of the file. R_OK, W_OK, and X_OK
test whether the file exists and grants read, write, and execute
permissions, respectively.

在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
	if(argc < 2)
	{
		printf("usage: ./a.out filename\n");
		exit(-1);
			
	}
	int ret = 0;
	ret = access(argv[1],F_OK);
	if (0 == ret)
	{
		printf("file exist\n");
	}
	else
	{
		perror("F_OK");
	}

在这里插入图片描述
R_OK、W_OK、X_OK就不贴上来了,跟F_OK是一个模式的。

chmod

chmod 就是用来改这个文件的权限的,分属主 组 其他这三个
学习的时候主要是没看懂chmod里面的bit mask 后来慢慢的懂了,竖着来看分三组,4转换成2进制就是100、2的话就是010、1的话就是1,这不就是对应了之前将的mode未到rwx对应421咩,这样看就懂了。

chown 和chgrp

chown是改变文件的属主权限的,比如之前在czp这个属主的,现在可以chown root 1.txt 这样
至于chgrp也是一样的理解

umask

最后讲了一下umask
在terminal里面直接umask可以查看当前的umask是多少,当然也可以umask 0044这样去设置,0044转换成2进制就是100 100 然后呢实际新建的文件就会本来是rwx rwx 也就是111 111的,然后减掉你umask的100 100 ,最后得到的只有011 011,所以只有可写可执行,没有可读的权限了,但是实际操作过程中呢,这个x是不存在的,新建文件的时候只有r跟w的权限。反正大概意思差不多。

算是虎头蛇尾啦,后面懒得贴图上来了。算OK啦这篇

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值