2013-12-16 54 views
2

我在Ubuntu bash中遇到了一个有趣的行为,我无法完全理解。 如果我将扩展文件属性添加到文件,然后更改它 - 从文件中删除属性。从我的角度来看,没问题。扩展文件属性和bash

[email protected]:~/tmp$ echo "aaa" > testattr 
[email protected]:~/tmp$ setfattr --name "user.test" --value "Tested" testattr 
[email protected]:~/tmp$ getfattr --name "user.test" testattr 
# file: testattr 
user.test="Tested" 
[email protected]:~/tmp$ vi testattr 
< change something in file and save it > 
[email protected]:~/tmp$ getfattr --name "user.test" testattr 
testattr: user.test: No such attribute 

但是,如果我用bash写文件 - 文件属性留在原来的位置。 有人可以解释这种行为吗?

[email protected]:~/tmp$ echo "aaa" > testattr 
[email protected]:~/tmp$ setfattr --name "user.test" --value "Tested" testattr 
[email protected]:~/tmp$ getfattr --name "user.test" testattr 
# file: testattr 
user.test="Tested" 
[email protected]:~/tmp$ echo "bbb" > testattr 
[email protected]:~/tmp$ getfattr --name "user.test" testattr 
# file: testattr 
user.test="Tested" 

回答

5

vi正在删除已编辑的文件并将其替换为新文件。这就是为什么不保留属性的原因。

这是该文件的操作日志。

$ mkdir test 
$ touch test/file 
$ inotifywait -m -r test 
Setting up watches. Beware: since -r was given, this may take a while! 
Watches established. 
# vi is now running in another shell 
test/ OPEN file 
test/ CREATE .file.swp 
test/ OPEN .file.swp 
test/ CREATE .file.swpx 
test/ OPEN .file.swpx 
test/ CLOSE_WRITE,CLOSE .file.swpx 
test/ DELETE .file.swpx 
test/ CLOSE_WRITE,CLOSE .file.swp 
test/ DELETE .file.swp 
test/ CREATE .file.swp 
test/ OPEN .file.swp 
test/ MODIFY .file.swp 
test/ ATTRIB .file.swp 
test/ CLOSE_NOWRITE,CLOSE file 
test/ OPEN file 
test/ CLOSE_NOWRITE,CLOSE file 
test/ MODIFY .file.swp 
test/ CREATE 4913 
test/ OPEN 4913 
test/ ATTRIB 4913 
test/ CLOSE_WRITE,CLOSE 4913 
test/ DELETE 4913 
test/ MOVED_FROM file  # old file moved 
test/ MOVED_TO file~ 
test/ CREATE file   # new file created 
test/ OPEN file 
test/ MODIFY file 
test/ CLOSE_WRITE,CLOSE file 
test/ ATTRIB file 
test/ ATTRIB file 
test/ MODIFY .file.swp 
test/ DELETE file~ 
test/ CLOSE_WRITE,CLOSE .file.swp 
test/ DELETE .file.swp 

请参阅this answer以禁用该行为。

+0

谢谢你的解释。实际上,我宁愿尝试弄清楚如果文件被改变如何让属性被丢弃=) – incogn1to

+1

@PāvelsReško您可以使用相同的'inotify'库来检测修改后的文件并删除它们的属性。 –

+0

@PāvelsReško实际上'bash'显示了最期望的行为,它会在不删除和重新创建的情况下更改文件内容。在这种情况下,预计不会出现“vi”行为。 –