很容易意识到文件的权限与删除该文件的能力无关。修改目录列表的能力由目录的权限控制。
但是,多年来我一直认为写权限的目的是允许修改目录,而执行权限用于“搜索” - 列出文件或更改目录。
今天,我发现,一个人不能rm
在一个目录中的文件,除非两者的写入和执行位设置。事实上,如果没有execute set,write 几乎没有用。
$ tree foo/
foo/
??? file_to_delete
0 directories, 1 file
$ chmod -x foo
$ ls -ld foo
drw-rw-r-- 2 ire_and_curses users 4096 Sep 18 22:08 foo/
$ rm foo/file_to_delete
rm: cannot remove ‘foo/file_to_delete’: Permission denied
$ chmod +x foo/
$ rm foo/file_to_delete
$ tree foo/
foo/
0 directories, 0 files
$
Run Code Online (Sandbox Code Playgroud)
我发现这种行为非常令人惊讶。对于目录,需要 execute 以使 write 在实践中有用的原因是什么?
如果没有执行位,您就无法stat()
对目录中的文件运行 a,这意味着您无法确定这些文件的 inode 信息。要删除文件,您必须知道将返回的信息stat()
.
对此的演示:
\n\n$ ls -ld test\ndrw------- 2 alienth alienth 4096 Sep 18 23:45 test\n\n$ stat test/file\nstat: cannot stat \xe2\x80\x98test/file\xe2\x80\x99: Permission denied\n\n$ strace -e newfstatat rm test/file\nnewfstatat(AT_FDCWD, "test/file", 0x1a3f368, AT_SYMLINK_NOFOLLOW) = -1 EACCES (Permission denied)\nnewfstatat(AT_FDCWD, "test/file", 0x7fff13d4f4f0, AT_SYMLINK_NOFOLLOW) = -1 EACCES (Permission denied)\nrm: cannot remove \xe2\x80\x98test/file\xe2\x80\x99: Permission denied\n+++ exited with 1 +++\n
Run Code Online (Sandbox Code Playgroud)\n\n您还可以通过一个简单的例子来演示这一点ls -l
. 您的用户可能可以读取和写入目录的元数据信息,但如果不执行,您将无法确定目录中文件的详细信息。
$ ls -l test\nls: cannot access test/file: Permission denied\ntotal 0\n-????????? ? ? ? ? ? file\n
Run Code Online (Sandbox Code Playgroud)\n