Created
June 15, 2009 16:06
-
-
Save wieczo/130189 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'fusefs' | |
include FuseFS | |
class ThinFile | |
attr_accessor :name, :contents, :path | |
def initialize(name, path) | |
throw ArgumentError.new("Dateiname darf hoechstens 14 Zeichen lang sein.") if name.length > 14 | |
@name = name | |
@contents = "FOOO!" | |
@path = path | |
end | |
def size | |
@contents.size | |
end | |
end | |
class ThinFS | |
def initialize | |
@files = [] | |
@files << ThinFile.new("Hallo.txt", "/Hallo.txt") | |
end | |
# list files in directory | |
def contents(path) | |
result = [] | |
@files.each do |f| | |
result << f.name | |
end | |
return result | |
end | |
# get file sizes | |
def size(path) | |
read_file(path).size | |
end | |
def directory?(path) | |
if path == "/" | |
true | |
else | |
false | |
end | |
end | |
def can_delete? | |
puts "in can_delete?" | |
true | |
end | |
def can_write?(path) | |
true | |
end | |
# cannot create new directories | |
def can_mkdir?(path) | |
false | |
end | |
def file?(path) | |
puts "in file? #{caller}" | |
if find_file(path) | |
true | |
else | |
false | |
end | |
end | |
# show file contents | |
def read_file(path) | |
return find_file(path).contents + "\n" | |
end | |
#write to file | |
def write_to(path, body) | |
fpath, name = scan_path(path) | |
if file? path | |
f = find_file(path) | |
f.contents = body # + " MAMAMA" | |
end | |
end | |
def touch(path) | |
fpath, name = scan_path(path) | |
#puts "Vor IF" | |
if directory?(fpath) && ! file?(name) | |
#puts "in IF" | |
puts fpath, name, path | |
@files << ThinFile.new(name, path) | |
end | |
#puts "Nach IF" | |
end | |
def delete(path) | |
pos = nil | |
puts "Vor IF" | |
if file? path | |
puts "ist file" | |
@files.each_with_index do |f, i| | |
if f.path == path | |
pos = i | |
puts "Gefunden #{i}" | |
end | |
end | |
end | |
puts "Nach IF" | |
@files.delete_at(pos) if pos | |
end | |
private | |
def find_file(path) | |
result = @files.find { |f| | |
f.path == path | |
} | |
return result | |
end | |
def scan_path(path) | |
pos = nil | |
(path.size - 1).downto(0) do |i| | |
if (path[i].chr == "/") | |
pos = i | |
break | |
end | |
end | |
if pos | |
fname = path.slice(pos+1, path.size) | |
pathname = path.slice(0, pos+1) | |
return [pathname, fname] | |
end | |
end | |
end | |
if (File.basename($0) == File.basename(__FILE__)) | |
if (ARGV.size != 1) | |
puts "Verwendung: #{$0} <Verzeichnis>" # <yamlfile>" | |
exit | |
end | |
#dirname, yamlfile = ARGV | |
dirname = ARGV.shift | |
unless File.directory?(dirname) | |
puts "Verwendung: #{dirname} ist keine Verzeichnis." | |
exit | |
end | |
#root = MetaDir.new | |
#root.write_to('/contents', ThinFS.new()) | |
root = ThinFS.new() | |
# Set the root FuseFS | |
FuseFS.set_root(root) | |
FuseFS.mount_under(dirname,"allow_other") | |
FuseFS.run # This doesn't return until we're unmounted. | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment