fusefs 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ require "rubygems"
2
+ require 'fusefs'
3
+
4
+ class HelloDir
5
+ def contents(path)
6
+ ['hello.txt']
7
+ end
8
+
9
+ def file?(path)
10
+ path == '/hello.txt'
11
+ end
12
+
13
+ def read_file(path)
14
+ "Hello, World!\n"
15
+ end
16
+
17
+ def size(path)
18
+ read_file(path).size
19
+ end
20
+ end
21
+
22
+ hellodir = HelloDir.new
23
+ FuseFS.set_root( hellodir )
24
+
25
+ # Mount under a directory given on the command line.
26
+ FuseFS.mount_under ARGV.shift
27
+ FuseFS.run
@@ -0,0 +1,53 @@
1
+ # openurifs.rb
2
+ #
3
+
4
+ require "rubygems"
5
+ require 'fusefs'
6
+ include FuseFS
7
+
8
+ require 'open-uri'
9
+
10
+ class OpenUriFS < FuseFS::FuseDir
11
+ def contents(path)
12
+ # The 'readme' file
13
+ []
14
+ end
15
+ def directory?(path)
16
+ uri = scan_path(path)
17
+ fn = uri.pop
18
+ return true if fn =~ /\.(com|org|net|us|de|jp|ru|uk|biz|info)$/
19
+ return true if fn =~ /^\d+\.\d+\.\d+\.\d+$/
20
+ ! (fn =~ /\./) # Does the last item doesn't contain a '.' ?
21
+ end
22
+ def file?(path)
23
+ !directory?(path)
24
+ end
25
+ def read_file(path)
26
+ proto, rest = split_path(path)
27
+ uri = "#{proto}://#{rest}"
28
+ open(uri).read
29
+ end
30
+ end
31
+
32
+ if (File.basename($0) == File.basename(__FILE__))
33
+ if (ARGV.size != 1)
34
+ puts "Usage: #{$0} <directory>"
35
+ exit
36
+ end
37
+
38
+ dirname = ARGV.shift
39
+
40
+ unless File.directory?(dirname)
41
+ puts "Usage: #{dirname} is not a directory."
42
+ exit
43
+ end
44
+
45
+ root = OpenUriFS.new
46
+
47
+ # Set the root FuseFS
48
+ FuseFS.set_root(root)
49
+
50
+ FuseFS.mount_under(dirname)
51
+
52
+ FuseFS.run # This doesn't return until we're unmounted.
53
+ end
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # RailsFS, as written by _why_the_lucky_stiff
4
+ #
5
+ # Full instructions:
6
+ # http://redhanded.hobix.com/inspect/railsfsAfterACoupleMinutesOfToolingWithFuseWhoa.html
7
+ #
8
+ =begin
9
+ Instructions cut and paste from _why's blog:
10
+
11
+ Save the railfs.rb script as script/filesys in your Rails app. (If you'd
12
+ rather not cut-and-paste the above, it's here.)
13
+
14
+ Now, run mkdir ~/railsmnt. Then, script/filesys ~/railsmnt. The rules are as
15
+ follows:
16
+
17
+ * ls ~/railsmnt will give you a list of tables.
18
+ * ls ~/railsmnt/table will list IDs from the table.
19
+ * cat ~/railsmnt/table/id will display a record in YAML.
20
+ * vim ~/railsmnt/table/id to edit the record in YAML!
21
+
22
+ =end
23
+
24
+ require "rubygems"
25
+ require 'fusefs'
26
+ require File.dirname(__FILE__) + '/../config/environment'
27
+
28
+ class RailsFS < FuseFS::FuseDir
29
+ def initialize
30
+ @classes = {}
31
+ require 'find'
32
+ Find.find( File.join(RAILS_ROOT, 'app/models') ) do |model|
33
+ if /(\w+)\.rb$/ =~ model
34
+ model = $1
35
+ ( @classes[model] = Kernel::const_get( Inflector.classify( model ) ) ).
36
+ find :first rescue @classes.delete( model )
37
+ end
38
+ end
39
+ end
40
+ def directory? path
41
+ tname, key = scan_path path
42
+ table = @classes[tname]
43
+ if table.nil?; false # /table
44
+ elsif key; false # /table/id
45
+ else; true end
46
+ end
47
+ def file? path
48
+ tname, key = scan_path path
49
+ table = @classes[tname]
50
+ key and table and table.find( key )
51
+ end
52
+ def can_delete?; true end
53
+ def can_write? path; file? path end
54
+ def contents path
55
+ tname, key = scan_path path
56
+ table = @classes[tname]
57
+ if tname.nil?; @classes.keys.sort # /
58
+ else; table.find( :all ).map { |o| o.id.to_s } end # /table
59
+ end
60
+ def write_to path, body
61
+ obj = YAML::load( body )
62
+ obj.save
63
+ end
64
+ def read_file path
65
+ tname, key = scan_path path
66
+ table = @classes[tname]
67
+ YAML::dump( table.find( key ) )
68
+ end
69
+ end
70
+
71
+ if (File.basename($0) == File.basename(__FILE__))
72
+ root = RailsFS.new
73
+ FuseFS.set_root(root)
74
+ FuseFS.mount_under(ARGV[0])
75
+ FuseFS.run # This doesn't return until we're unmounted.
76
+ end
77
+
@@ -0,0 +1,134 @@
1
+ # sqlfs.rb
2
+ #
3
+ # The SQL-db proof of concept for FuseFS
4
+ #
5
+ # Author: Greg Millam
6
+
7
+ require "rubygems"
8
+ require 'fusefs'
9
+
10
+ require 'mysql'
11
+
12
+ class SqlFS < FuseFS::FuseDir
13
+ class DBTable
14
+ attr_accessor :name, :key, :fields
15
+ end
16
+ def initialize(host,user,pass,db)
17
+ @sql = Mysql.connect(host,user,pass,db)
18
+ @tables = Hash.new(nil)
19
+
20
+ tables = @sql.query('show tables')
21
+
22
+ tables.each do |i,|
23
+ table = DBTable.new
24
+ table.name = i
25
+ table.fields = {}
26
+ res = @sql.query("describe #{i}")
27
+ res.each do |field,type,null,key,default,extra|
28
+ table.fields[field] = type
29
+ if (key =~ /pri/i)
30
+ table.key = field
31
+ end
32
+ end
33
+ @tables[i] = table if table.key
34
+ end
35
+ end
36
+ def directory?(path)
37
+ tname, key, field = scan_path(path)
38
+ table = @tables[tname]
39
+ case
40
+ when tname.nil?
41
+ true # This means "/"
42
+ when table.nil?
43
+ false
44
+ when field
45
+ false # Always a file
46
+ when key
47
+ res = @sql.query("SELECT #{table.key}, 1 FROM #{table.name} WHERE #{table.key} = '#{Mysql.escape_string(key)}'")
48
+ res.num_rows > 0 # If there was a result, it exists.
49
+ else
50
+ true # It's just a table.
51
+ end
52
+ end
53
+ def file?(path)
54
+ tname, key, field = scan_path(path)
55
+ table = @tables[tname]
56
+ case
57
+ when field.nil?
58
+ false # Only field entries are files.
59
+ when table.nil?
60
+ false
61
+ when ! @tables[tname].fields.include?(field)
62
+ false # Invalid field.
63
+ when field
64
+ res = @sql.query("SELECT #{table.key}, 1 FROM #{table.name} WHERE #{table.key} = '#{Mysql.escape_string(key)}'")
65
+ res.num_rows > 0
66
+ end
67
+ end
68
+ def can_delete?(path)
69
+ # This helps editors, but we don't really use it.
70
+ true
71
+ end
72
+ def can_write?(path)
73
+ # Since this is basically only for editing files,
74
+ # we just call file?
75
+ file?(path)
76
+ end
77
+ def contents(path)
78
+ # since this is only called when directory? is true,
79
+ # We'll assume valid entries.
80
+ tname, key, field = scan_path(path)
81
+ table = @tables[tname]
82
+ case
83
+ when tname.nil?
84
+ @tables.keys.sort # Just the tables.
85
+ when key
86
+ table.fields.keys.sort
87
+ else
88
+ # I limit to 200 so 'ls' doesn't hang all the time :D
89
+ res = @sql.query("SELECT #{table.key}, 1 FROM #{table.name} ORDER BY #{table.key} LIMIT 100")
90
+ ret = []
91
+ res.each do |val,one|
92
+ ret << val if val.size > 0
93
+ end
94
+ ret
95
+ end
96
+ end
97
+ def write_to(path,body)
98
+ # Since this is only called after can_write?(), we assume
99
+ # Valid fields.
100
+ tname, key, field = scan_path(path)
101
+ table = @tables[tname]
102
+ res = @sql.query("UPDATE #{table.name} SET #{field} = '#{Mysql.escape_string(body)}' WHERE #{table.key} = '#{key}'")
103
+ end
104
+ def read_file(path)
105
+ # Again, as this is only called after file?, assume valid fields.
106
+ tname, key, field = scan_path(path)
107
+ table = @tables[tname]
108
+ res = @sql.query("SELECT #{field} FROM #{table.name} WHERE #{table.key} = '#{key}'")
109
+ res.fetch_row[0]
110
+ end
111
+ end
112
+
113
+ if (File.basename($0) == File.basename(__FILE__))
114
+ if ARGV.size != 5
115
+ puts "Usage: #{$0} <directory> <host> <user> <pass> <db>"
116
+ exit
117
+ end
118
+
119
+ dirname, host, user, pass, db = ARGV
120
+
121
+ if (! File.directory?(dirname))
122
+ puts "#{dirname} is not a directory"
123
+ end
124
+
125
+ root = SqlFS.new(host,user,pass,db)
126
+
127
+ # Set the root FuseFS
128
+ FuseFS.set_root(root)
129
+
130
+ # root.contents("/quotes")
131
+
132
+ FuseFS.mount_under(dirname)
133
+ FuseFS.run # This doesn't return until we're unmounted.
134
+ end
@@ -0,0 +1,168 @@
1
+ # yamlfs.rb
2
+ #
3
+
4
+ require "rubygems"
5
+ require 'fusefs'
6
+ include FuseFS
7
+
8
+ require 'yaml'
9
+
10
+ class YAMLFS < FuseFS::FuseDir
11
+ def initialize(filename)
12
+ @filename = filename
13
+ begin
14
+ @fs = YAML.load(IO.read(filename))
15
+ rescue Exception
16
+ @fs = Hash.new()
17
+ end
18
+ end
19
+ def save
20
+ File.open(@filename,'w') do |fout|
21
+ fout.puts(YAML.dump(@fs))
22
+ end
23
+ end
24
+ def contents(path)
25
+ items = scan_path(path)
26
+ node = items.inject(@fs) do |node,item|
27
+ item ? node[item] : node
28
+ end
29
+ node.keys.sort
30
+ end
31
+ def directory?(path)
32
+ items = scan_path(path)
33
+ node = items.inject(@fs) do |node,item|
34
+ item ? node[item] : node
35
+ end
36
+ node.is_a?(Hash)
37
+ end
38
+ def file?(path)
39
+ items = scan_path(path)
40
+ node = items.inject(@fs) do |node,item|
41
+ item ? node[item] : node
42
+ end
43
+ node.is_a?(String)
44
+ end
45
+ def touch(path)
46
+ puts "#{path} has been pushed like a button!"
47
+ end
48
+
49
+ # File reading
50
+ def read_file(path)
51
+ items = scan_path(path)
52
+ node = items.inject(@fs) do |node,item|
53
+ item ? node[item] : node
54
+ end
55
+ node.to_s
56
+ end
57
+
58
+ def size(path)
59
+ read_file(path).size
60
+ end
61
+
62
+ # File writing
63
+ def can_write?(path)
64
+ items = scan_path(path)
65
+ name = items.pop # Last is the filename.
66
+ node = items.inject(@fs) do |node,item|
67
+ item ? node[item] : node
68
+ end
69
+ node.is_a?(Hash)
70
+ rescue Exception => er
71
+ puts "Error! #{er}"
72
+ end
73
+ def write_to(path,body)
74
+ items = scan_path(path)
75
+ name = items.pop # Last is the filename.
76
+ node = items.inject(@fs) do |node,item|
77
+ item ? node[item] : node
78
+ end
79
+ node[name] = body
80
+ self.save
81
+ rescue Exception => er
82
+ puts "Error! #{er}"
83
+ end
84
+
85
+ # Delete a file
86
+ def can_delete?(path)
87
+ items = scan_path(path)
88
+ node = items.inject(@fs) do |node,item|
89
+ item ? node[item] : node
90
+ end
91
+ node.is_a?(String)
92
+ rescue Exception => er
93
+ puts "Error! #{er}"
94
+ end
95
+ def delete(path)
96
+ items = scan_path(path)
97
+ name = items.pop # Last is the filename.
98
+ node = items.inject(@fs) do |node,item|
99
+ item ? node[item] : node
100
+ end
101
+ node.delete(name)
102
+ self.save
103
+ rescue Exception => er
104
+ puts "Error! #{er}"
105
+ end
106
+
107
+ # mkdirs
108
+ def can_mkdir?(path)
109
+ items = scan_path(path)
110
+ name = items.pop # Last is the filename.
111
+ node = items.inject(@fs) do |node,item|
112
+ item ? node[item] : node
113
+ end
114
+ node.is_a?(Hash)
115
+ rescue Exception => er
116
+ puts "Error! #{er}"
117
+ end
118
+ def mkdir(path)
119
+ items = scan_path(path)
120
+ name = items.pop # Last is the filename.
121
+ node = items.inject(@fs) do |node,item|
122
+ item ? node[item] : node
123
+ end
124
+ node[name] = Hash.new
125
+ self.save
126
+ end
127
+
128
+ # rmdir
129
+ def can_rmdir?(path)
130
+ items = scan_path(path)
131
+ node = items.inject(@fs) do |node,item|
132
+ item ? node[item] : node
133
+ end
134
+ node.is_a?(Hash) && node.empty?
135
+ end
136
+ def rmdir(path)
137
+ items = scan_path(path)
138
+ name = items.pop # Last is the filename.
139
+ node = items.inject(@fs) do |node,item|
140
+ item ? node[item] : node
141
+ end
142
+ node.delete(name)
143
+ self.save
144
+ end
145
+ end
146
+
147
+ if (File.basename($0) == File.basename(__FILE__))
148
+ if (ARGV.size < 2)
149
+ puts "Usage: #{$0} <directory> <yamlfile> <options>"
150
+ exit
151
+ end
152
+
153
+ dirname, yamlfile = ARGV.shift, ARGV.shift
154
+
155
+ unless File.directory?(dirname)
156
+ puts "Usage: #{dirname} is not a directory."
157
+ exit
158
+ end
159
+
160
+ root = YAMLFS.new(yamlfile)
161
+
162
+ # Set the root FuseFS
163
+ FuseFS.set_root(root)
164
+
165
+ FuseFS.mount_under(dirname, *ARGV)
166
+
167
+ FuseFS.run # This doesn't return until we're unmounted.
168
+ end