25行でファイルシステム
Dokan RubyバインディングのFuseFS互換インタフェースを利用すると、25行でファイルシステムを書くことが出来ます。
dokanとdokan-rubyをダウンロードして、dokan.sys dokan.dll mounter.exe dokan_lib.so dokanfs.rbを同じディレクトリに置いて、次のようなプログラムを書きます(dokan-ruby\sample\hello.rbにあります)。
require 'dokanfs'
class Hello
def initialize
@msg = "hello, world"
end
def contents path
["hello.txt"]
end
def file? path
path =~ /hello.txt/
end
def directory? path
path == "/"
end
def read_file path
@msg
end
def size path
@msg.length
end
end
FuseFS.set_root(Hello.new)
FuseFS.mount_under("r")
FuseFS.run
実行してみます。
> ruby hello.rb
すると、Rドライブが作成され、Rドライブを開くとhello.txtというファイルがあります。これを開くとhello,worldと書かれています。アンマウントするには、
> dokanctl.exe /u r
を実行します。
Dokan Rubyを使うと、非常に手軽にファイルシステムが作成出来ます。ぜひみなさんも、Rubyを使用してオリジナルファイルシステムを作ってみてください。

Recent Comments