1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
# yamlfs.rb
#
require "rubygems"
require 'fusefs'
include FuseFS
require 'yaml'
class YAMLFS < FuseFS::FuseDir
def initialize(filename)
@filename = filename
begin
@fs = YAML.load(IO.read(filename))
rescue Exception
@fs = Hash.new()
end
end
def save
File.open(@filename,'w') do |fout|
fout.puts(YAML.dump(@fs))
end
end
def contents(path)
items = scan_path(path)
node = items.inject(@fs) do |node,item|
item ? node[item] : node
end
node.keys.sort
end
def directory?(path)
items = scan_path(path)
node = items.inject(@fs) do |node,item|
item ? node[item] : node
end
node.is_a?(Hash)
end
def file?(path)
items = scan_path(path)
node = items.inject(@fs) do |node,item|
item ? node[item] : node
end
node.is_a?(String)
end
def touch(path)
puts "#{path} has been pushed like a button!"
end
# File reading
def read_file(path)
items = scan_path(path)
node = items.inject(@fs) do |node,item|
item ? node[item] : node
end
node.to_s
end
def size(path)
read_file(path).size
end
# File writing
def can_write?(path)
items = scan_path(path)
name = items.pop # Last is the filename.
node = items.inject(@fs) do |node,item|
item ? node[item] : node
end
node.is_a?(Hash)
rescue Exception => er
puts "Error! #{er}"
end
def write_to(path,body)
items = scan_path(path)
name = items.pop # Last is the filename.
node = items.inject(@fs) do |node,item|
item ? node[item] : node
end
node[name] = body
self.save
rescue Exception => er
puts "Error! #{er}"
end
# Delete a file
def can_delete?(path)
items = scan_path(path)
node = items.inject(@fs) do |node,item|
item ? node[item] : node
end
node.is_a?(String)
rescue Exception => er
puts "Error! #{er}"
end
def delete(path)
items = scan_path(path)
name = items.pop # Last is the filename.
node = items.inject(@fs) do |node,item|
item ? node[item] : node
end
node.delete(name)
self.save
rescue Exception => er
puts "Error! #{er}"
end
# mkdirs
def can_mkdir?(path)
items = scan_path(path)
name = items.pop # Last is the filename.
node = items.inject(@fs) do |node,item|
item ? node[item] : node
end
node.is_a?(Hash)
rescue Exception => er
puts "Error! #{er}"
end
def mkdir(path)
items = scan_path(path)
name = items.pop # Last is the filename.
node = items.inject(@fs) do |node,item|
item ? node[item] : node
end
node[name] = Hash.new
self.save
end
# rmdir
def can_rmdir?(path)
items = scan_path(path)
node = items.inject(@fs) do |node,item|
item ? node[item] : node
end
node.is_a?(Hash) && node.empty?
end
def rmdir(path)
items = scan_path(path)
name = items.pop # Last is the filename.
node = items.inject(@fs) do |node,item|
item ? node[item] : node
end
node.delete(name)
self.save
end
end
if (File.basename($0) == File.basename(__FILE__))
if (ARGV.size < 2)
puts "Usage: #{$0} <directory> <yamlfile> <options>"
exit
end
dirname, yamlfile = ARGV.shift, ARGV.shift
unless File.directory?(dirname)
puts "Usage: #{dirname} is not a directory."
exit
end
root = YAMLFS.new(yamlfile)
# Set the root FuseFS
FuseFS.set_root(root)
FuseFS.mount_under(dirname, *ARGV)
FuseFS.run # This doesn't return until we're unmounted.
end
|