2013-12-16 53 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」行爲。 –