require "bfs"
include Bfs
f = File.open("/boot/home")
puts Attr.read(f, "BEOS:TYPE")
クラスメソッドの Attr::read はファイルオブジェクトと,属性名を引き数に取り,値を返します.ただし,以下のタイプの読み取りには対応していない為,必ずバイト列 (文字列) を返します.
タイプについて: file:///boot/develop/headers/be/support/TypeConstants.h
require "tempfile.rb"
require "bfs"
include Bfs
f = Tempfile.new("attr_test")
puts Attr.write(f, "my:type", "text")
puts Attr.read(f, "my:type")
テストのため,一時的なファイルに属性を書き込むために,tempfile.rb を使っています.このファイルはプログラム終了時に自動的に削除されます.
クラスメソッドの Attr::write はファイルオブジェクトと,属性名,属性値を引き数に取り,書き込んだバイト数を返します.
属性値のタイプはある程度は自動的に判別しますが,それ以外のタイプは以下のように,第 4 引き数に自分で指定する必要があります.
require "tempfile.rb"
require "bfs"
include Bfs
f = Tempfile.new("attr_test")
puts Attr.write(f, "BEOS:TYPE", "text/plain", MIME_STRING_TYPE)
puts Attr.read(f, "BEOS:TYPE")
これ以外は B_STRING_TYPE と判断し,オブジェクトを Marshal.dump した文字列を書き込みます.
require "tempfile.rb"
require "bfs"
include Bfs
f = Tempfile.new("attr_test")
puts Attr.write(f, "my:type", "text")
p Attr.entries(f)
puts Attr.remove(f, "my:type")
p Attr.entries(f)
クラスメソッドの Attr::remove はファイルオブジェクトと,属性名を引き数に取り,属性を削除します.
require "bfs"
include Bfs
f = File.open("/boot/home")
info = Attr.stat(f, "BEOS:TYPE")
puts info["size"]
puts info["type"]
type = ""
format("%x", info["type"]).scan(/(..)/) {| c |
type << c.pop.hex.chr
}
puts type
Attr::stat はファイルオブジェクトと,属性名を引き数に取り,ハッシュを返します.ただ,タイプは数値なので,文字列へ変換しています.
タイプについて: file:///boot/develop/headers/be/support/TypeConstants.h
require "bfs"
puts Bfs::Attr.entries("/boot/home")
最初に bfs クラスライブラリをロードします.2 行目で,ホームディレクトリの属性を配列で取得し,puts で配列の中身を表示しています.また,このプログラムは次のようにも記述できます.
require "bfs"
include Bfs
puts Attr.entries("/boot/home")
それぞれのクラス (Attr や,Index など) は,Bfs というモジュールで名前空間 (ネームスペース) を分けているので,2 行目でインクルードして使っています.