用 Vala 查檔案的 Extended Attribute

其實 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]$

已發佈

分類:

作者:

標籤:

留言

在〈用 Vala 查檔案的 Extended Attribute〉中有 2 則留言

  1. 「Hiroshi Yui」的個人頭像

    我…修正了… Orz

  2. 「Xin....」的個人頭像
    Xin….

    谷哥大大的協作平台最近換位址了
    不過他還是很貼心的show出類似連結