rfusefs 1.0.2.RC0 → 1.1.0.rc202009.34

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,168 +0,0 @@
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
@@ -1,12 +0,0 @@
1
- require 'fusefs'
2
-
3
- describe "Using fusefs compatibility mode" do
4
- describe FuseFS do
5
-
6
- it "should indicate FuseFS compatibility" do
7
- FuseFS::RFUSEFS_COMPATIBILITY.should be_false
8
- end
9
-
10
- it "should use FuseFS compatible raw calls"
11
- end
12
- end
@@ -1,295 +0,0 @@
1
- require 'spec_helper'
2
- require 'tmpdir'
3
-
4
- describe FuseFS::MetaDir do
5
-
6
- context "in ruby" do
7
-
8
- before(:each) do
9
- @metadir = FuseFS::MetaDir.new()
10
- @metadir.mkdir("/test")
11
- @metadir.mkdir("/test/hello")
12
- @metadir.mkdir("/test/hello/emptydir")
13
- @metadir.write_to("/test/hello/hello.txt","Hello World!\n")
14
- end
15
-
16
- context "general directory methods" do
17
- it "should list directory contents" do
18
- @metadir.contents("/").should =~ [ "test" ]
19
- @metadir.contents("/test").should =~ [ "hello" ]
20
- @metadir.contents("/test/hello").should =~ ["hello.txt", "emptydir" ]
21
- end
22
-
23
- it "should indicate paths which are/are not directories" do
24
- @metadir.directory?("/test").should be_true
25
- @metadir.directory?("/test/hello").should be_true
26
- @metadir.directory?("/test/hello/hello.txt").should be_false
27
- @metadir.directory?("/nodir").should be_false
28
- @metadir.directory?("/test/nodir").should be_false
29
- end
30
-
31
- it "should indicate paths which are/are not files" do
32
- @metadir.file?("/test").should be_false
33
- @metadir.file?("/test/nodir").should be_false
34
- @metadir.file?("/test/hello").should be_false
35
- @metadir.file?("/test/hello/hello.txt").should be_true
36
- end
37
-
38
- it "should indicate the size of a file" do
39
- @metadir.size("/test/hello/hello.txt").should be "Hello World!\n".length
40
- end
41
- end
42
-
43
- context "with write access" do
44
-
45
- around(:each) do |example|
46
- FuseFS::RFuseFS.context(fuse_context(),&example)
47
- end
48
-
49
- before(:each) do
50
- FuseFS::reader_uid.should == Process.uid
51
- FuseFS::reader_gid.should == Process.gid
52
- end
53
-
54
-
55
- it "should allow directory creation" do
56
- @metadir.can_mkdir?("/test/otherdir").should be_true
57
- end
58
-
59
- it "should allow file creation and update" do
60
- @metadir.can_write?("/test/hello/newfile").should be_true
61
- @metadir.can_write?("/test/hello/hello.txt").should be_true
62
- end
63
-
64
- it "should read files" do
65
- @metadir.read_file("/test/hello/hello.txt").should == "Hello World!\n"
66
- end
67
-
68
- it "should update existing files" do
69
- @metadir.write_to("/test/hello/hello.txt","new contents")
70
- @metadir.read_file("/test/hello/hello.txt").should == "new contents"
71
- end
72
-
73
- it "should not allow deletion of non empty directories" do
74
- @metadir.can_rmdir?("/test/hello").should be_false
75
- end
76
-
77
- it "should delete directories" do
78
- @metadir.rmdir("/test/hello/emptydir")
79
- @metadir.contents("/test/hello").should =~ ["hello.txt"]
80
- end
81
-
82
- it "should allow and delete files" do
83
- @metadir.can_delete?("/test/hello/hello.txt").should be_true
84
- @metadir.delete("/test/hello/hello.txt")
85
- @metadir.contents("/test/hello").should =~ ["emptydir"]
86
- end
87
-
88
- it "should move directories at same level" do
89
- before = @metadir.contents("/test/hello")
90
- @metadir.rename("/test/hello","/test/moved").should be_true
91
- @metadir.directory?("/test/moved").should be_true
92
- @metadir.contents("/test/moved").should =~ before
93
- @metadir.read_file("/test/moved/hello.txt").should == "Hello World!\n"
94
- end
95
-
96
- it "should move directories between different paths" do
97
- @metadir.mkdir("/test/other")
98
- @metadir.mkdir("/test/other/more")
99
- before = @metadir.contents("/test/hello")
100
- @metadir.rename("/test/hello","/test/other/more/hello").should be_true
101
- @metadir.contents("/test/other/more/hello").should =~ before
102
- @metadir.read_file("/test/other/more/hello/hello.txt").should == "Hello World!\n"
103
- end
104
-
105
- end
106
-
107
- context "with readonly access" do
108
- around(:each) do |example|
109
- #Simulate a different userid..
110
- FuseFS::RFuseFS.context(fuse_context(-1,-1),&example)
111
- end
112
-
113
- before(:each) do
114
- FuseFS::reader_uid.should_not == Process.uid
115
- FuseFS::reader_gid.should_not == Process.gid
116
- end
117
-
118
- it "should not allow directory creation" do
119
- @metadir.can_mkdir?("/test/anydir").should be_false
120
- @metadir.can_mkdir?("/test/hello/otherdir").should be_false
121
- end
122
-
123
- it "should not allow file creation or write access" do
124
- @metadir.can_write?("/test/hello/hello.txt").should be_false
125
- @metadir.can_write?("/test/hello/newfile").should be_false
126
- end
127
-
128
- it "should not allow file deletion" do
129
- @metadir.can_delete?("/test/hello/hello.txt").should be_false
130
- end
131
-
132
- it "should not allow directory deletion" do
133
- @metadir.can_rmdir?("/test/emptydir").should be_false
134
- end
135
-
136
- it "should not allow directory renames" do
137
- @metadir.rename("/test/emptydir","/test/otherdir").should be_false
138
- #TODO and make sure it doesn't rename
139
- end
140
-
141
- it "should not allow file renames" do
142
- @metadir.rename("test/hello/hello.txt","test/hello.txt2").should be_false
143
- #TODO and make sure it doesn't rename
144
- end
145
- end
146
-
147
- context "with subdirectory containing another FuseFS" do
148
- around(:each) do |example|
149
- FuseFS::RFuseFS.context(fuse_context(),&example)
150
- end
151
-
152
- before(:each) do
153
- @fusefs = mock("mock_fusefs")
154
- @metadir.mkdir("/test")
155
- @metadir.mkdir("/test/fusefs",@fusefs)
156
- end
157
-
158
- api_methods = [:directory?, :file?, :contents, :executable?, :size, :times, :read_file, :can_write?, :can_delete?, :delete, :can_mkdir?, :can_rmdir?, :rmdir, :touch, :raw_open, :raw_truncate, :raw_read, :raw_write, :raw_close]
159
- api_methods.each do |method|
160
- it "should pass on #{method}" do
161
- arity = FuseFS::FuseDir.instance_method(method).arity().abs - 1
162
- args = Array.new(arity) { |i| i }
163
- @fusefs.should_receive(method).with("/path/to/file",*args).and_return("anything")
164
- @metadir.send(method,"/test/fusefs/path/to/file",*args)
165
- end
166
- end
167
-
168
- it "should pass on :write_to" do
169
- @fusefs.should_receive(:write_to).with("/path/to/file","new contents\n")
170
- @metadir.write_to("/test/fusefs/path/to/file","new contents\n")
171
- end
172
-
173
- it "should pass on :mkdir" do
174
- @fusefs.should_receive(:mkdir).with("/path/to/file",nil).once().and_raise(ArgumentError)
175
- @fusefs.should_receive(:mkdir).with("/path/to/file").once().and_return("true")
176
- @metadir.mkdir("/test/fusefs/path/to/file")
177
- end
178
-
179
- it "should rename within same directory" do
180
- @fusefs.should_receive(:rename).with("/oldfile","/newfile")
181
- @metadir.rename("/test/fusefs/oldfile","/test/fusefs/newfile")
182
- end
183
-
184
- it "should pass rename down common directories" do
185
- @fusefs.should_receive(:rename).with("/path/to/file" ,"/new/path/to/file")
186
- @metadir.rename("/test/fusefs/path/to/file","/test/fusefs/new/path/to/file")
187
- end
188
-
189
- it "should rename across directories if from_path is a FuseFS object that accepts extended rename" do
190
- @fusefs.should_receive(:rename).with("/path/to/file","/nonfusepath",
191
- an_instance_of(FuseFS::MetaDir)) do | myPath, extPath, extFS |
192
- extFS.write_to(extPath,"look Mum, no hands!")
193
- end
194
-
195
- @metadir.rename("/test/fusefs/path/to/file","/test/nonfusepath").should be_true
196
- @metadir.read_file("/test/nonfusepath").should == "look Mum, no hands!"
197
- end
198
-
199
- it "should quietly return false if from_path is a FuseFS object that does not accept extended rename" do
200
- @fusefs.should_receive(:rename).
201
- with("/path/to/file","/nonfusepath",an_instance_of(FuseFS::MetaDir)).
202
- and_raise(ArgumentError)
203
- @metadir.rename("/test/fusefs/path/to/file","/test/nonfusepath").should be_false
204
-
205
- end
206
-
207
- it "should not attempt rename file unless :can_write? the destination" do
208
- @fusefs.should_receive(:can_write?).with("/newpath/to/file").and_return(false)
209
- @metadir.write_to("/test/aFile","some contents")
210
- @metadir.rename("/test/aFile","/test/fusefs/newpath/to/file").should be_false
211
- end
212
-
213
- it "should not attempt rename directory unless :can_mkdir? the destination" do
214
- @fusefs.should_receive(:can_mkdir?).with("/newpath/to/dir").and_return(false)
215
- @metadir.mkdir("/test/aDir","some contents")
216
- @metadir.rename("/test/aDir","/test/fusefs/newpath/to/dir").should be_false
217
- end
218
-
219
- end
220
-
221
- end
222
- context "in a mounted FUSE filesystem" do
223
-
224
- let(:mountpoint) { Pathname.new(Dir.mktmpdir(["rfusefs","metadir"])) }
225
- let(:metadir) { FuseFS::MetaDir.new() }
226
- let(:testdir) { mountpoint + "test" }
227
- let(:testfile) { testdir + "hello.txt" }
228
-
229
- before(:all) do
230
- metadir.mkdir("/test")
231
- metadir.write_to("/test/hello.txt","Hello World!\n")
232
- FuseFS.mount(metadir,mountpoint)
233
- #Give FUSE some time to get started
234
- sleep(0.5)
235
- end
236
-
237
- after(:all) do
238
- FuseFS.unmount(mountpoint)
239
- sleep(0.5)
240
- FileUtils.rm_r(mountpoint)
241
- end
242
-
243
- it "should list directory contents" do
244
- testdir.entries().should =~ pathnames(".","..","hello.txt")
245
- end
246
-
247
- it "should read files" do
248
- testfile.file?.should be_true
249
- testfile.read().should == "Hello World!\n"
250
- end
251
-
252
- it "should create directories" do
253
- newdir = testdir + "newdir"
254
- newdir.mkdir()
255
- newdir.directory?.should be_true
256
- testdir.entries().should =~ pathnames(".","..","hello.txt","newdir")
257
- end
258
-
259
- it "should create files" do
260
- newfile = testdir + "newfile"
261
- newfile.open("w") do |file|
262
- file << "A new file\n"
263
- end
264
- newfile.read.should == "A new file\n"
265
- end
266
-
267
- it "should move directories" do
268
- fromdir = testdir + "fromdir"
269
- fromdir.mkdir()
270
- subfile = fromdir + "afile"
271
- subfile.open("w") do |file|
272
- file << "testfile\n"
273
- end
274
-
275
- movedir = (mountpoint + "movedir")
276
- movedir.directory?.should be_false
277
- fromdir.rename(movedir)
278
- movedir.directory?.should be_true
279
-
280
- subfile = movedir + "afile"
281
- subfile.file?.should be_true
282
- subfile.read.should == "testfile\n"
283
- end
284
-
285
- it "should move files" do
286
- movefile = (mountpoint + "moved.txt")
287
- movefile.file?.should be_false
288
- testfile.should be_true
289
- testfile.rename(movefile)
290
- movefile.read.should == "Hello World!\n"
291
- end
292
-
293
- end
294
-
295
- end