大家先了解一些背景知识:
1) 给文件增加了i权限,那文件不能被更改,不能删除,也不能修改名字以及权限。
2) 给文件增加a权限,文件可以追加内容,不能删除,不能修改内容,不能修改名字以及权限。
3) vim一个文件,如果不正常退出,再次编辑时是会提示一些信息的,并且有一个隐藏的文件.xxx.swp
了解以上知识后,再来看下面的现象:
1) 如果给一个文件增加a权限,用vim编辑文件,增加内容(注意是在文件末尾增加内容,不要修改其他内容),并不会成功。
2) 如果给一个目录增加i权限或者a权限,在该目录下面vim一个文件,更改文件内容可以正常保存。
既然a权限可以追加内容,那为何vim一个文件在末尾增加内容不能成功?既然i权限不能修改,那为何在目录里面变更文件内容却可以成功?
关于这两点,你有没有疑惑?下面我们来分析原因。
先不管i或者a权限,
在一个没有i或者a权限的目录下,编辑一个没有i或者a权限的文件,
用strace来查看其执行过程。
mkdir /tmp/test
strace vim /tmp/test/aminglinux.txt 2>/tmp/vim.log
写入一个数字1,然后保存退出。再来查看vim.log的内容。
less /tmp/vim.log
大部分内容你不用关心,只需要看这几行:
stat("/tmp/test/aminglinux.txt", 0x7fff072ecb10) = -1 ENOENT (No such file or directory)
access("/tmp/test/aminglinux.txt", W_OK) = -1 ENOENT (No such file or directory)
open("/tmp/test/aminglinux.txt", O_RDONLY) = -1 ENOENT (No such file or directory)
readlink("/tmp/test/aminglinux.txt", 0x7fff072eb360, 4095) = -1 ENOENT (No such file or directory)
open("/tmp/test/.aminglinux.txt.swp", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/tmp/test/.aminglinux.txt.swp", O_RDWR|O_CREAT|O_EXCL, 0600) = 3
open("/tmp/test/.aminglinux.txt.swx", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/tmp/test/.aminglinux.txt.swx", O_RDWR|O_CREAT|O_EXCL, 0600) = 4
unlink("/tmp/test/.aminglinux.txt.swx") = 0
unlink("/tmp/test/.aminglinux.txt.swp") = 0
stat("/tmp/test/.aminglinux.txt.swp", 0x7fff072ec310) = -1 ENOENT (No such file or directory)
lstat("/tmp/test/.aminglinux.txt.swp", 0x7fff072ec3e0) = -1 ENOENT (No such file or directory)
lstat("/tmp/test/.aminglinux.txt.swp", 0x7fff072ec8a0) = -1 ENOENT (No such file or directory)
open("/tmp/test/.aminglinux.txt.swp", O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0600) = 3
stat("/tmp/test/aminglinux.txt", 0x7fff072eac40) = -1 ENOENT (No such file or directory)
stat("/tmp/test/aminglinux.txt", 0x7fff072ebe20) = -1 ENOENT (No such file or directory)
stat("/tmp/test/aminglinux.txt", 0x7fff072eadf0) = -1 ENOENT (No such file or directory)
write(1, ""/tmp/test/aminglinux.txt"", 26) = 26
stat("/tmp/test/aminglinux.txt", 0x7fff072ec050) = -1 ENOENT (No such file or directory)
open("/tmp/test/aminglinux.txt", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 4
stat("/tmp/test/aminglinux.txt", {st_mode=S_IFREG|0644, st_size=2, ...}) = 0
stat("/tmp/test/aminglinux.txt", {st_mode=S_IFREG|0644, st_size=2, ...}) = 0
unlink("/tmp/test/.aminglinux.txt.swp") = 0
看起来乱乱的,其实大概的过程就是vim /tmp/test/aminglinux.txt时,先看有没有.aminglinux.txt.swp以及.aminglinux.txt.swx,因为这两个文件就是vim产生的临时文件,swp先产生,如果swp存在就产生第二个swx。写入的内容先存到swp里,当保存退出vim时,再把swp的内容存到aminglinux.txt里,最后删除掉swp文件。
有了这个认识之后,我们再来分析上面提到的现象1。如果文件给了a权限,那么在编辑该文件时,会产生swp文件,当保存退出时,swp文件内容会写入该文件,这相当于更改该文件,很线上a权限是不允许的。
再来分析现象2,按照我们的推测,如果目录给了a权限,增加文件没问题,也就是说产生swp或者swx文件没有问题,当然把swp或者swx内容写入到文件里时也不会有问题,但swp或者swx文件却不会被删除了,所以再次编辑文件时就会提示临时文件已经存在了。但这并不会影响修改文件内容。
如果给目录设置了i权限的话,vim编辑文件,要产生swp或swx肯定会出错啊,但为何依然能正常编辑文件? 下面继续用strace来分析一下。
chattr +i /tmp/test
strace vim /tmp/test/aminglinux.txt 2> /tmp/vim.log
看vim.log里面和aminglinux.txt相关的信息
stat("/tmp/test/aminglinux.txt", {st_mode=S_IFREG|0644, st_size=4, ...}) = 0
stat("/tmp/test/aminglinux.txt", {st_mode=S_IFREG|0644, st_size=4, ...}) = 0
stat("/tmp/test/aminglinux.txt", {st_mode=S_IFREG|0644, st_size=4, ...}) = 0
stat("/tmp/test/aminglinux.txt", {st_mode=S_IFREG|0644, st_size=4, ...}) = 0
access("/tmp/test/aminglinux.txt", W_OK) = 0
open("/tmp/test/aminglinux.txt", O_RDONLY) = 3
readlink("/tmp/test/aminglinux.txt", 0x7fff49efc6f0, 4095) = -1 EINVAL (Invalid argument)
open("/tmp/test/.aminglinux.txt.swp", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/tmp/test/.aminglinux.txt.swp", O_RDWR|O_CREAT|O_EXCL, 0600) = -1 EACCES (Permission denied)
stat("/tmp/test/.aminglinux.txt.swp", 0x7fff49efd6a0) = -1 ENOENT (No such file or directory)
lstat("/tmp/test/.aminglinux.txt.swp", 0x7fff49efd770) = -1 ENOENT (No such file or directory)
lstat("/tmp/test/.aminglinux.txt.swp", 0x7fff49efdc30) = -1 ENOENT (No such file or directory)
open("/tmp/test/.aminglinux.txt.swp", O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0600) = -1 EACCES (Permission denied)
readlink("/tmp/test/aminglinux.txt", 0x7fff49efc6f0, 4095) = -1 EINVAL (Invalid argument)
open("/root/tmp/aminglinux.txt.swp", O_RDONLY) = -1 ENOTDIR (Not a directory)
open("/root/tmp/aminglinux.txt.swp", O_RDWR|O_CREAT|O_EXCL, 0600) = -1 ENOTDIR (Not a directory)
stat("/root/tmp/aminglinux.txt.swp", 0x7fff49efd6a0) = -1 ENOTDIR (Not a directory)
lstat("/root/tmp/aminglinux.txt.swp", 0x7fff49efd770) = -1 ENOTDIR (Not a directory)
lstat("/root/tmp/aminglinux.txt.swp", 0x7fff49efdc30) = -1 ENOTDIR (Not a directory)
open("/root/tmp/aminglinux.txt.swp", O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0600) = -1 ENOTDIR (Not a directory)
readlink("/tmp/test/aminglinux.txt", 0x7fff49efc6f0, 4095) = -1 EINVAL (Invalid argument)
open("/var/tmp/aminglinux.txt.swp", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/var/tmp/aminglinux.txt.swp", O_RDWR|O_CREAT|O_EXCL, 0600) = 4
open("/var/tmp/aminglinux.txt.swx", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/var/tmp/aminglinux.txt.swx", O_RDWR|O_CREAT|O_EXCL, 0600) = 5
unlink("/var/tmp/aminglinux.txt.swx") = 0
unlink("/var/tmp/aminglinux.txt.swp") = 0
stat("/var/tmp/aminglinux.txt.swp", 0x7fff49efd6a0) = -1 ENOENT (No such file or directory)
lstat("/var/tmp/aminglinux.txt.swp", 0x7fff49efd770) = -1 ENOENT (No such file or directory)
lstat("/var/tmp/aminglinux.txt.swp", 0x7fff49efdc30) = -1 ENOENT (No such file or directory)
open("/var/tmp/aminglinux.txt.swp", O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0600) = 4
chmod("/var/tmp/aminglinux.txt.swp", 0644) = 0
open("/tmp/test/aminglinux.txt", O_RDONLY) = 3
stat("/tmp/test/aminglinux.txt", {st_mode=S_IFREG|0644, st_size=4, ...}) = 0
stat("/tmp/test/aminglinux.txt", {st_mode=S_IFREG|0644, st_size=4, ...}) = 0
stat("/tmp/test/aminglinux.txt", {st_mode=S_IFREG|0644, st_size=4, ...}) = 0
access("/tmp/test/aminglinux.txt", W_OK) = 0
write(1, ""aminglinux.txt"", 16) = 16
stat("aminglinux.txt", {st_mode=S_IFREG|0644, st_size=4, ...}) = 0
access("aminglinux.txt", W_OK) = 0
getxattr("aminglinux.txt", "system.posix_acl_access", 0x7fff49efd050, 132) = -1 ENODATA (No data available)
stat("aminglinux.txt", {st_mode=S_IFREG|0644, st_size=4, ...}) = 0
open("aminglinux.txt", O_WRONLY|O_CREAT|O_TRUNC, 0644) = 3
chmod("aminglinux.txt", 0100644) = 0
setxattr("aminglinux.txt", "system.posix_acl_access", "x02x00x00x00x01x00x06x00xffxffxffxffx04x00x04x00xffxffxffxff x00x04x00xffxffxffxff", 28, 0) = 0
stat("/tmp/test/aminglinux.txt", {st_mode=S_IFREG|0644, st_size=6, ...}) = 0
unlink("/var/tmp/aminglinux.txt.swp") = 0
我相信你可以看到Permission denied的提示,这是因为当前目录有i权限,不能增加文件,也就不能在当前目录下生成临时文件。当然,vim如果遇到这样的问题,它还是会“曲线救国”的,于是先找/root/tmp/,但是并没有该目录,只好继续找/var/tmp/,这个目录存在,所以就在这个目录里生成了临时文件(并不是隐藏的)。后面的操作就不用多说了。
既然vim可以在/var/tmp/下生成临时文件,自然也可以在已经设置了i权限的目录里编辑文件的,这样现象2也解释通了。上面罗嗦了这么多,其实我就想表达如下观点:
vim编辑文件时,会在该文件所在目录生成临时隐藏文件.swp和.swx,如果那目录不可写就会到/root/tmp/下或者/var/tmp/下生成临时文件(非隐藏),当编辑的文件保存后,临时文件删除。
全部0条评论
快来发表一下你的评论吧 !