根据我对目录的读取权限的理解,它允许列出目录中的哪些文件以及关于它的文件.
给定具有0744权限的目录,由userA拥有:
[userA@localhost ~]$mkdir -m 0744 /tmp/semi-secret
[userA@localhost ~]$ls -ld /tmp/semi-secret/
drwxr--r--. 2 userA userA 6 Aug 29 10:15 /tmp/semi-secret/
[userA@localhost tmp]$touch semi-secret/foobar.txt
[userA@localhost tmp]$chmod 0600 semi-secret/foobar.txt
对于userB,ls命令显示文件foobar.txt的存在.
[userB@localhost ~]$ls -l /tmp/semi-secret/
ls: cannot access /tmp/semi-secret/foobar.txt: Permission denied
total 0
-????????? ? ? ? ? ? foobar.txt
但是为什么test -e命令以非零状态退出?!它唯一的工作是确认文件是否存在,并且目录权限应该允许.
[userB@localhost ~]$test -e /tmp/semi-secret/foobar.txt || echo "The file doesn't exist."
The file doesn't exist.
解决方法:
由于即使stat(2)系统调用也不能用于错过权限中x位的目录中的任何文件,因此基于stat(2)系统调用的任何内容都将在此类目录中失败.
如果您的目录设置了r位,您确实能够使用readdir()读取该目录的内容,但您无法统计或打开任何文件.
相反,如果您知道相关文件名但是不能使用readdir()来检索相关目录的内容,则设置了x位的目录允许您使用stat(2)或打开文件.
test -e以非零状态退出,以防文件不存在(符号链接解析后,因此也适用于现有符号链接到不存在或不可访问的文件)或尝试stat时发生任何错误( 2)相关文件.
标签:linux,permissions,test
来源: https://codeday.me/bug/20190816/1663726.html