rfusefs 1.0.3 → 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,369 +0,0 @@
1
- require 'spec_helper'
2
- require 'tmpdir'
3
- require 'sys/filesystem'
4
- require 'ffi-xattr'
5
-
6
- describe FuseFS::MetaDir do
7
-
8
- context 'in ruby' do
9
-
10
- before(:each) do
11
- @metadir = FuseFS::MetaDir.new()
12
- @metadir.mkdir('/test')
13
- @metadir.mkdir('/test/hello')
14
- @metadir.mkdir('/test/hello/emptydir')
15
- @metadir.write_to('/test/hello/hello.txt', "Hello World!\n")
16
- end
17
-
18
- context 'general directory methods' do
19
- it 'should list directory contents' do
20
- expect(@metadir.contents('/')).to match_array(['test'])
21
- expect(@metadir.contents('/test')).to match_array(['hello'])
22
- expect(@metadir.contents('/test/hello')).to match_array(['hello.txt', 'emptydir'])
23
- end
24
-
25
- it 'should indicate paths which are/are not directories' do
26
- expect(@metadir.directory?('/test')).to be_truthy
27
- expect(@metadir.directory?('/test/hello')).to be_truthy
28
- expect(@metadir.directory?('/test/hello/hello.txt')).to be_falsey
29
- expect(@metadir.directory?('/nodir')).to be_falsey
30
- expect(@metadir.directory?('/test/nodir')).to be_falsey
31
- end
32
-
33
- it 'should indicate paths which are/are not files' do
34
- expect(@metadir.file?('/test')).to be_falsey
35
- expect(@metadir.file?('/test/nodir')).to be_falsey
36
- expect(@metadir.file?('/test/hello')).to be_falsey
37
- expect(@metadir.file?('/test/hello/hello.txt')).to be_truthy
38
- end
39
-
40
- it 'should indicate the size of a file' do
41
- expect(@metadir.size('/test/hello/hello.txt')).to be "Hello World!\n".length
42
- end
43
-
44
- it 'should report filesystem statistics' do
45
- expect(@metadir.statistics('/')).to eq([13, 5, nil, nil])
46
- end
47
- end
48
-
49
- context 'with write access' do
50
-
51
- around(:each) do |example|
52
- FuseFS::Fuse::Root.context(fuse_context(), &example)
53
- end
54
-
55
- before(:each) do
56
- expect(FuseFS::reader_uid).to eq(Process.uid)
57
- expect(FuseFS::reader_gid).to eq(Process.gid)
58
- end
59
-
60
-
61
- it 'should allow directory creation' do
62
- expect(@metadir.can_mkdir?('/test/otherdir')).to be_truthy
63
- end
64
-
65
- it 'should allow file creation and update' do
66
- expect(@metadir.can_write?('/test/hello/newfile')).to be_truthy
67
- expect(@metadir.can_write?('/test/hello/hello.txt')).to be_truthy
68
- end
69
-
70
- it 'should read files' do
71
- expect(@metadir.read_file('/test/hello/hello.txt')).to eq("Hello World!\n")
72
- end
73
-
74
- it 'should update existing files' do
75
- @metadir.write_to('/test/hello/hello.txt', 'new contents')
76
- expect(@metadir.read_file('/test/hello/hello.txt')).to eq('new contents')
77
- end
78
-
79
- it 'should not allow deletion of non empty directories' do
80
- expect(@metadir.can_rmdir?('/test/hello')).to be_falsey
81
- end
82
-
83
- it 'should delete directories' do
84
- @metadir.rmdir('/test/hello/emptydir')
85
- expect(@metadir.contents('/test/hello')).to match_array(['hello.txt'])
86
- end
87
-
88
- it 'should allow and delete files' do
89
- expect(@metadir.can_delete?('/test/hello/hello.txt')).to be_truthy
90
- @metadir.delete('/test/hello/hello.txt')
91
- expect(@metadir.contents('/test/hello')).to match_array(['emptydir'])
92
- end
93
-
94
- it 'should move directories at same level' do
95
- before = @metadir.contents('/test/hello')
96
- expect(@metadir.rename('/test/hello', '/test/moved')).to be_truthy
97
- expect(@metadir.directory?('/test/moved')).to be_truthy
98
- expect(@metadir.contents('/test/moved')).to match_array(before)
99
- expect(@metadir.read_file('/test/moved/hello.txt')).to eq("Hello World!\n")
100
- end
101
-
102
- it 'should move directories between different paths' do
103
- @metadir.mkdir('/test/other')
104
- @metadir.mkdir('/test/other/more')
105
- before = @metadir.contents('/test/hello')
106
- expect(@metadir.rename('/test/hello', '/test/other/more/hello')).to be_truthy
107
- expect(@metadir.contents('/test/other/more/hello')).to match_array(before)
108
- expect(@metadir.read_file('/test/other/more/hello/hello.txt')).to eq("Hello World!\n")
109
- end
110
-
111
- it 'should maintain filesystem statistics' do
112
- # remove a directory
113
- @metadir.rmdir('/test/hello/emptydir')
114
-
115
- # replace text for (the only) existing file
116
- @metadir.write_to('/test/hello/hello.txt', 'new text')
117
-
118
- expect(@metadir.statistics('/')).to eq([8, 4, nil, nil])
119
- end
120
- end
121
-
122
- context 'with readonly access' do
123
- around(:each) do |example|
124
- #Simulate a different userid..
125
- FuseFS::Fuse::Root.context(fuse_context(-1, -1), &example)
126
- end
127
-
128
- before(:each) do
129
- expect(FuseFS::reader_uid).not_to eq(Process.uid)
130
- expect(FuseFS::reader_gid).not_to eq(Process.gid)
131
- end
132
-
133
- it 'should not allow directory creation' do
134
- expect(@metadir.can_mkdir?('/test/anydir')).to be_falsey
135
- expect(@metadir.can_mkdir?('/test/hello/otherdir')).to be_falsey
136
- end
137
-
138
- it 'should not allow file creation or write access' do
139
- expect(@metadir.can_write?('/test/hello/hello.txt')).to be_falsey
140
- expect(@metadir.can_write?('/test/hello/newfile')).to be_falsey
141
- end
142
-
143
- it 'should not allow file deletion' do
144
- expect(@metadir.can_delete?('/test/hello/hello.txt')).to be_falsey
145
- end
146
-
147
- it 'should not allow directory deletion' do
148
- expect(@metadir.can_rmdir?('/test/emptydir')).to be_falsey
149
- end
150
-
151
- it 'should not allow directory renames' do
152
- expect(@metadir.rename('/test/emptydir', '/test/otherdir')).to be_falsey
153
- #TODO and make sure it doesn't rename
154
- end
155
-
156
- it 'should not allow file renames' do
157
- expect(@metadir.rename('test/hello/hello.txt', 'test/hello.txt2')).to be_falsey
158
- #TODO and make sure it doesn't rename
159
- end
160
- end
161
-
162
- context 'with subdirectory containing another FuseFS' do
163
- around(:each) do |example|
164
- FuseFS::Fuse::Root.context(fuse_context(), &example)
165
- end
166
-
167
- before(:each) do
168
- @fusefs = double('mock_fusefs')
169
- @metadir.mkdir('/test')
170
- @metadir.mkdir('/test/fusefs', @fusefs)
171
- end
172
-
173
- 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]
174
- api_methods.each do |method|
175
- it "should pass on #{method}" do
176
- arity = FuseFS::FuseDir.instance_method(method).arity().abs - 1
177
- args = Array.new(arity) { |i| i }
178
- expect(@fusefs).to receive(method).with('/path/to/file', *args).and_return('anything')
179
- @metadir.send(method, '/test/fusefs/path/to/file', *args)
180
- end
181
- end
182
-
183
- it 'should pass on :write_to' do
184
- expect(@fusefs).to receive(:write_to).with('/path/to/file', "new contents\n")
185
- @metadir.write_to('/test/fusefs/path/to/file', "new contents\n")
186
- end
187
-
188
- it 'should pass on :mkdir' do
189
- expect(@fusefs).to receive(:mkdir).with('/path/to/file', nil).once().and_raise(ArgumentError)
190
- expect(@fusefs).to receive(:mkdir).with('/path/to/file').once().and_return('true')
191
- @metadir.mkdir('/test/fusefs/path/to/file')
192
- end
193
-
194
- it 'should rename within same directory' do
195
- expect(@fusefs).to receive(:rename).with('/oldfile', '/newfile')
196
- @metadir.rename('/test/fusefs/oldfile', '/test/fusefs/newfile')
197
- end
198
-
199
- it 'should pass rename down common directories' do
200
- expect(@fusefs).to receive(:rename).with('/path/to/file', '/new/path/to/file')
201
- @metadir.rename('/test/fusefs/path/to/file', '/test/fusefs/new/path/to/file')
202
- end
203
-
204
- it 'should rename across directories if from_path is a FuseFS object that accepts extended rename' do
205
- expect(@fusefs).to receive(:rename).with('/path/to/file', '/nonfusepath',
206
- an_instance_of(FuseFS::MetaDir)) do |myPath, extPath, extFS|
207
- extFS.write_to(extPath, 'look Mum, no hands!')
208
- end
209
-
210
- expect(@metadir.rename('/test/fusefs/path/to/file', '/test/nonfusepath')).to be_truthy
211
- expect(@metadir.read_file('/test/nonfusepath')).to eq('look Mum, no hands!')
212
- end
213
-
214
- it 'should quietly return false if from_path is a FuseFS object that does not accept extended rename' do
215
- expect(@fusefs).to receive(:rename).
216
- with('/path/to/file', '/nonfusepath', an_instance_of(FuseFS::MetaDir)).
217
- and_raise(ArgumentError)
218
- expect(@metadir.rename('/test/fusefs/path/to/file', '/test/nonfusepath')).to be_falsey
219
-
220
- end
221
-
222
- it 'should not attempt rename file unless :can_write? the destination' do
223
- expect(@fusefs).to receive(:can_write?).with('/newpath/to/file').and_return(false)
224
- @metadir.write_to('/test/aFile', 'some contents')
225
- expect(@metadir.rename('/test/aFile', '/test/fusefs/newpath/to/file')).to be_falsey
226
- end
227
-
228
- it 'should not attempt rename directory unless :can_mkdir? the destination' do
229
- expect(@fusefs).to receive(:can_mkdir?).with('/newpath/to/dir').and_return(false)
230
- @metadir.mkdir('/test/aDir', 'some contents')
231
- expect(@metadir.rename('/test/aDir', '/test/fusefs/newpath/to/dir')).to be_falsey
232
- end
233
-
234
- it 'should pass on #statistics' do
235
- expect(@fusefs).to receive(:statistics).with('/path/to/file')
236
-
237
- @metadir.statistics('test/fusefs/path/to/file')
238
- end
239
-
240
- it 'should pass on #statistics for root' do
241
- expect(@fusefs).to receive(:statistics).with('/')
242
-
243
- @metadir.statistics('test/fusefs')
244
- end
245
- end
246
-
247
- end
248
- context 'in a mounted FUSE filesystem' do
249
-
250
- before(:all) do
251
- metadir = FuseFS::MetaDir.new()
252
- mountpoint = Pathname.new(Dir.mktmpdir(['rfusefs', 'metadir']))
253
-
254
- metadir.mkdir('/test')
255
- metadir.write_to('/test/hello.txt', "Hello World!\n")
256
- metadir.xattr('/test/hello.txt')['user.test'] = 'an extended attribute'
257
- metadir.xattr('/test')['user.test'] = 'a dir attribute'
258
- FuseFS.mount(metadir, mountpoint)
259
- #Give FUSE some time to get started
260
- sleep(0.5)
261
- @metadir = metadir
262
- @mountpoint = mountpoint
263
- end
264
-
265
- after(:all) do
266
- FuseFS.unmount(@mountpoint)
267
- sleep(0.5)
268
- FileUtils.rm_r(@mountpoint)
269
- end
270
-
271
- let(:testdir) { mountpoint + 'test' }
272
- let(:testfile) { testdir + 'hello.txt' }
273
- let(:metadir) { @metadir}
274
- let(:mountpoint) { @mountpoint }
275
-
276
- it 'should list directory contents' do
277
- expect(testdir.entries()).to match_array(pathnames('.', '..', 'hello.txt'))
278
- end
279
-
280
- it 'should read files' do
281
- expect(testfile.file?).to be_truthy
282
- expect(testfile.read()).to eq("Hello World!\n")
283
- end
284
-
285
- it 'should read and write extended attributes from files' do
286
- x = Xattr.new(testfile.to_s)
287
- expect(x['user.test']).to eq('an extended attribute')
288
-
289
- x['user.new'] = 'new'
290
-
291
- expect(Xattr.new(testfile.to_s)['user.new']).to eq('new')
292
- end
293
-
294
- it 'should write extended attributes for directories' do
295
- x = Xattr.new(testdir.to_s)
296
-
297
- expect(x['user.test']).to eq('a dir attribute')
298
- x['user.new'] = 'new dir'
299
-
300
- expect(Xattr.new(testdir.to_s)['user.new']).to eq('new dir')
301
- end
302
-
303
-
304
- it 'should create directories' do
305
- newdir = testdir + 'newdir'
306
- newdir.mkdir()
307
- expect(newdir.directory?).to be_truthy
308
- expect(testdir.entries()).to match_array(pathnames('.', '..', 'hello.txt', 'newdir'))
309
- end
310
-
311
- it 'should create files' do
312
- newfile = testdir + 'newfile'
313
- newfile.open('w') do |file|
314
- file << "A new file\n"
315
- end
316
- expect(newfile.read).to eq("A new file\n")
317
- end
318
-
319
- it 'should move directories' do
320
- fromdir = testdir + 'fromdir'
321
- fromdir.mkdir()
322
- subfile = fromdir + 'afile'
323
- subfile.open('w') do |file|
324
- file << "testfile\n"
325
- end
326
-
327
- movedir = (mountpoint + 'movedir')
328
- expect(movedir.directory?).to be_falsey
329
- fromdir.rename(movedir)
330
- expect(movedir.directory?).to be_truthy
331
-
332
- subfile = movedir + 'afile'
333
- expect(subfile.file?).to be_truthy
334
- expect(subfile.read).to eq("testfile\n")
335
- end
336
-
337
- it 'should move files' do
338
- movefile = (mountpoint + 'moved.txt')
339
- expect(movefile.file?).to be_falsey
340
- expect(testfile).to be_truthy
341
- testfile.rename(movefile)
342
- expect(movefile.read).to eq("Hello World!\n")
343
- end
344
-
345
-
346
- it 'should report filesystem statistics' do
347
- bigfile = testdir + 'bigfile'
348
- bigfile.open('w') do |file|
349
- file << ('x' * 2048)
350
- end
351
-
352
- statfs = Sys::Filesystem.stat(mountpoint.to_s)
353
-
354
- # These are fixed
355
- expect(statfs.block_size).to eq(1024)
356
- expect(statfs.fragment_size).to eq(1024)
357
-
358
- # These are dependant on the tests above creating files/directories
359
- expect(statfs.files).to eq(8)
360
- statfs.files_available == 8
361
-
362
- # assume test are less than 1 block, so dependant on bigfile above
363
- expect(statfs.blocks).to eq(2)
364
- expect(statfs.blocks_available).to eq(0)
365
- expect(statfs.blocks_free).to eq(0)
366
- end
367
- end
368
-
369
- end