为什么 vim 可以覆盖其主目录下的其他用户文件

why vim can overwrite other user file under its home directory

例如root touch一个普通用户(命名为bob)主目录下的一个新文件:

/home/bob $ ls -alh a.txt
-rw-r--r-- 1 root root 0 Jul 16 17:45 a.txt

现在用户 bob 使用 vim 打开它,它应该是只读的。

我试图用 :w! 强制覆盖它,它被保存并且 owner:group 更改为 bob:

/home/bob $ ls -alh a.txt
-rw-r--r-- 1 bob bob 4 Jul 16 17:47 a.txt

IMO,我认为它不能在权限被拒绝的情况下保存,但它可以,而且 owner:group 也改变了。

而且这个只能在bob的home目录下,如果在外目录下,比如/tmp之类的,就不能像我想的那样写了

谁能解释一下? :w! 实际执行哪些流程?谢谢

Bob 可以读取该文件,因此 Bob 可以在 Vim 中打开它。

该目录可由 Bob 写入,因此 Bob 将能够取消链接(删除)其中的任何文件,并向其中写入任何新文件。当您使用 :w!.

时会发生这种情况

/tmp 目录并非如此,因为它可能设置了 "sticky" 位。

来自 OS X sticky(8) 手册:

A directory whose 'sticky bit' is set becomes an append-only directory, or, more accurately, a directory in which the deletion of files is restricted. A file in a sticky directory may only be removed or renamed by a user if the user has write permission for the directory and the user is the owner of the file, the owner of the directory, or the super-user. This feature is usefully applied to directories such as /tmp which must be publicly writable but should deny users the license to arbitrarily delete or rename each others' files.