用 Vala 查檔案的 Extended Attribute
Categories:General
其實 GNOME Shell 提醒了我一件長久以來一直很想做的東西。
想把這個東西做出來,我至少必須先確認一件事情:我可以為檔案設定 metadata,即所謂的 extended attribute,如此我預想中的使用者介面才能夠存取多樣化的物件屬性。
現在可以確定的是,在 Linux 底下,我可以設定 xattr 無誤。
[yhh@haruka ~/test]$ attr -s .TYPE -V "C Source Code" hello.c Attribute ".TYPE" set to a 13 byte value for hello.c: C Source Code [yhh@haruka ~/test]$ attr -l hello.c Attribute ".TYPE" has a 13 byte value for hello.c [yhh@haruka ~/test]$ attr -g .TYPE hello.c Attribute ".TYPE" had a 13 byte value for hello.c: C Source Code [yhh@haruka ~/test]$
剛剛又用 Vala 寫了一個 PoC 小程式來驗證使用 GIO 存取 xattr 的可行性,筆記一下:
int main (string[] args) {
var file = File.new_for_path ("hello.c");
FileInfo file_info;
try {
file_info = file.query_info ("*", FileQueryInfoFlags.NONE);
}
catch (Error e) {
stderr.printf ("Error: %s\n", e.message);
return 1;
}
stdout.printf ("Has .TYPE attribute? %s\n",
file_info.has_attribute ("xattr::.TYPE").to_string());
stdout.printf ("File Type: %s\n",
file_info.get_attribute_string ("xattr::.TYPE"));
return 0;
}
執行結果:
[yhh@haruka ~/test]$ valac --pkg gio-2.0 testGFileAttrib.vala [yhh@haruka ~/test]$ ./testGFileAttrib Has .TYPE attribute? true File Type: C Source Code [yhh@haruka ~/test]$
2 comments
Xin.... ·
不過他還是很貼心的show出類似連結
Hiroshi Yui ·