Class: FuseFS::RFuseFS

Inherits:
Object
  • Object
show all
Defined in:
lib/fuse/rfusefs-fuse.rb

Overview

Implements RFuseFS The path supplied to these methods is generally validated by FUSE itself with a prior “getattr” call so we do not revalidate here. sourceforge.net/apps/mediawiki/fuse/index.php?title=FuseInvariants

Constant Summary collapse

CHECK_FILE =
"/._rfuse_check_"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ RFuseFS

Returns a new instance of RFuseFS.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fuse/rfusefs-fuse.rb', line 100

def initialize(root)
    @root = root
    @created_files = { }

    # Keep track of changes to file counts and sizes made via Fuse - for #statfs
    @adj_nodes = 0
    @adj_size = 0

    #Define method missing for our filesystem
    #so we can just call all the API methods as required.
    def @root.method_missing(method,*args)
        # our filesystem might implement method_missing itself
        super
    rescue NoMethodError
        DEFAULT_FS.send(method,*args)
    end
end

Class Method Details

.context(ctx, &block) ⇒ Object



486
487
488
489
490
491
492
493
494
495
# File 'lib/fuse/rfusefs-fuse.rb', line 486

def self.context(ctx,&block)
    begin
        Thread.current[:fusefs_reader_uid] = ctx.uid
        Thread.current[:fusefs_reader_gid] = ctx.gid
        yield
    ensure
        Thread.current[:fusefs_reader_uid] = nil
        Thread.current[:fusefs_reader_gid] = nil
    end
end

.method_missing(method, *args) ⇒ Object

Define method missing for our filesystem so we can just call all the API methods as required.



110
111
112
113
114
115
# File 'lib/fuse/rfusefs-fuse.rb', line 110

def @root.method_missing(method,*args)
    # our filesystem might implement method_missing itself
    super
rescue NoMethodError
    DEFAULT_FS.send(method,*args)
end

Instance Method Details

#flush(ctx, path, ffi) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
# File 'lib/fuse/rfusefs-fuse.rb', line 323

def flush(ctx,path,ffi)
    return wrap_context(ctx,__method__,path,ffi) if ctx
    fh = ffi.fh

    if fh && !fh.raw && fh.modified?
        #write contents to the file and mark it unmodified
        @root.write_to(path,fh.flush())
        #if it was created with mknod it now exists in the filesystem...
        @created_files.delete(path)
    end
end

#fsync(ctx, path, datasync, ffi) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/fuse/rfusefs-fuse.rb', line 308

def fsync(ctx,path,datasync,ffi)
    return wrap_context(ctx,__method__,path,datasync,ffi) if ctx
    fh = ffi.fh

    if fh && fh.raw
        if FuseFS::RFUSEFS_COMPATIBILITY
            @root.raw_sync(path,datasync != 0,fh.raw)
        else
            @root.raw_sync(path,datasync != 0)
        end
    else
        flush(nil,path,ffi)
    end
end

#ftruncate(ctx, path, offset, ffi) ⇒ Object

ftruncate - eg called after opening a file for write without append sizes are adjusted at file close



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/fuse/rfusefs-fuse.rb', line 194

def ftruncate(ctx,path,offset,ffi)

    return wrap_context(ctx,__method__,path,offset,ffi) if ctx

    fh = ffi.fh

    if fh.raw
        @root.raw_truncate(path,offset,fh.raw)
        if (offset <= 0)
            fh.contents = ""
        else
            fh.contents = fh.contents[0..offset]
        end
    end
end

#getattr(ctx, path) ⇒ Object



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
# File 'lib/fuse/rfusefs-fuse.rb', line 134

def getattr(ctx,path)

    return wrap_context(ctx,__method__,path) if ctx

    uid = Process.uid
    gid = Process.gid

    if  path == "/" || @root.directory?(path)
        #set "w" flag based on can_mkdir? || can_write? to path + "/._rfuse_check"
        write_test_path = (path == "/" ? "" : path) + CHECK_FILE

        mode = (@root.can_mkdir?(write_test_path) || @root.can_write?(write_test_path)) ? 0777 : 0555
        atime,mtime,ctime = @root.times(path)
        #nlink is set to 1 because apparently this makes find work.
        return RFuse::Stat.directory(mode,{ :uid => uid, :gid => gid, :nlink => 1, :atime => atime, :mtime => mtime, :ctime => ctime })
    elsif @created_files.has_key?(path)
        return @created_files[path]
    elsif @root.file?(path)
        #Set mode from can_write and executable
        mode = 0444
        mode |= 0222 if @root.can_write?(path)
        mode |= 0111 if @root.executable?(path)
        size = size(path)
        atime,mtime,ctime = @root.times(path)
        return RFuse::Stat.file(mode,{ :uid => uid, :gid => gid, :size => size, :atime => atime, :mtime => mtime, :ctime => ctime })
    else
        raise Errno::ENOENT.new(path)
    end

end

#getxattr(ctx, path, name) ⇒ Object

Raises:

  • (Errno::ENODATA)


414
415
416
417
418
419
# File 'lib/fuse/rfusefs-fuse.rb', line 414

def getxattr(ctx,path,name)
    return wrap_context(ctx,__method__,path,name) if ctx
    result = @root.xattr(path)[name]
    raise Errno::ENODATA.new("No attribute #{name}") unless result
    result.to_s
end

#listxattr(ctx, path) ⇒ Object



421
422
423
424
# File 'lib/fuse/rfusefs-fuse.rb', line 421

def listxattr(ctx,path)
    return wrap_context(ctx,__method__,path) if ctx
    @root.xattr(path).keys
end

#mkdir(ctx, path, mode) ⇒ Object

getattr



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/fuse/rfusefs-fuse.rb', line 165

def mkdir(ctx,path,mode)

    return wrap_context(ctx,__method__,path,mode) if ctx

    unless @root.can_mkdir?(path)
        raise Errno::EACCES.new(path)
    end

    @root.mkdir(path)
    @adj_nodes += 1
end

#mknod(ctx, path, mode, major, minor) ⇒ Object

mkdir



177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/fuse/rfusefs-fuse.rb', line 177

def mknod(ctx,path,mode,major,minor)

    return wrap_context(ctx,__method__,path,mode,major,minor) if ctx

    unless ((RFuse::Stat::S_IFMT & mode) == RFuse::Stat::S_IFREG ) && @root.can_write?(path)
        raise Errno::EACCES.new(path)
    end

    now = Time.now
    stat = RFuse::Stat.file(mode,{ :uid => Process.uid, :gid => Process.gid, :atime => now, :mtime => now, :ctime => now })

    @created_files[path] = stat
    @adj_nodes += 1
end

#mountedObject



478
479
480
# File 'lib/fuse/rfusefs-fuse.rb', line 478

def mounted()
    @root.mounted()
end

#open(ctx, path, ffi) ⇒ Object

Open. Create a FileHandler and store in fuse file info This will be returned to us in read/write No O_CREATE (mknod first?), no O_TRUNC (truncate first)



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/fuse/rfusefs-fuse.rb', line 233

def open(ctx,path,ffi)
    return wrap_context(ctx,__method__,path,ffi) if ctx
    fh = FileHandle.new(path,ffi.flags)

    #Save the value return from raw_open to be passed back in raw_read/write etc..
    if (FuseFS::RFUSEFS_COMPATIBILITY)
        fh.raw = @root.raw_open(path,fh.raw_mode,true)
    else
        fh.raw = @root.raw_open(path,fh.raw_mode)
    end

    unless fh.raw
        if fh.rdonly?
            fh.contents = @root.read_file(path)
        elsif fh.writing?
            unless @root.can_write?(path)
                raise Errno::EACCES.new(path)
            end

            if @created_files.has_key?(path)
                fh.create
            else
                if fh.rdwr? || fh.append?
                    fh.contents = @root.read_file(path)
                else #wronly && !append
                    #We should get a truncate 0, but might as well play it safe
                    fh.contents = ""
                end
            end
        else
            raise Errno::ENOPERM.new(path)
        end
    end

    #If we get this far, save our filehandle in the FUSE structure
    ffi.fh=fh
end

#read(ctx, path, size, offset, ffi) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/fuse/rfusefs-fuse.rb', line 271

def read(ctx,path,size,offset,ffi)
    return wrap_context(ctx,__method__,path,size,offset,ffi) if ctx

    fh = ffi.fh

    if fh.raw
        if FuseFS::RFUSEFS_COMPATIBILITY
            return @root.raw_read(path,offset,size,fh.raw)
        else
            return @root.raw_read(path,offset,size)
        end
    elsif offset >= 0
        return fh.read(offset,size)
    else
        #TODO: Raise? what does a negative offset mean
        return ""
    end
rescue EOFError
    return ""
end

#readdir(ctx, path, filler, offset, ffi) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/fuse/rfusefs-fuse.rb', line 118

def readdir(ctx,path,filler,offset,ffi)

    return wrap_context(ctx,__method__,path,filler,offset,ffi) if ctx

    #Always have "." and ".."
    filler.push(".",nil,0)
    filler.push("..",nil,0)

    files = @root.contents(path)

    files.each do | filename |
        filler.push(filename,nil,0)
    end

end

#release(ctx, path, ffi) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/fuse/rfusefs-fuse.rb', line 335

def release(ctx,path,ffi)
    return wrap_context(ctx,__method__,path,ffi) if ctx


    fh = ffi.fh
    if fh && fh.raw
        if (FuseFS::RFUSEFS_COMPATIBILITY)
            @root.raw_close(path,fh.raw)
        else
            @root.raw_close(path)
        end
        # if was handled as raw, then assume the file has now been created (or not)
        @created_files.delete(path)
    else
        # Probably just had flush called, but no harm calling it again
        flush(nil,path,ffi)
    end
end

#removexattr(ctx, path, name) ⇒ Object



426
427
428
429
# File 'lib/fuse/rfusefs-fuse.rb', line 426

def removexattr(ctx,path,name)
    return wrap_context(ctx,__method__,path,name) if ctx
    @root.xattr(path).delete(name)
end

#rename(ctx, from, to) ⇒ Object

def symlink(path,as) end



392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/fuse/rfusefs-fuse.rb', line 392

def rename(ctx,from,to)
    return wrap_context(ctx,__method__,from,to) if ctx

    if @root.rename(from,to)
        # nothing to do
    elsif @root.file?(from) && @root.can_write?(to) &&  @root.can_delete?(from)
        contents = @root.read_file(from)
        @root.write_to(to,contents)
        @root.delete(from)
    else
        raise Errno::EACCES.new("Unable to move directory #{from}")
    end
end

#rmdir(ctx, path) ⇒ Object



380
381
382
383
384
385
386
387
# File 'lib/fuse/rfusefs-fuse.rb', line 380

def rmdir(ctx,path)
    return wrap_context(ctx,__method__,path) if ctx

    unless @root.can_rmdir?(path)
        raise Errno::EACCES.new(path)
    end
    @root.rmdir(path)
end

#setxattr(ctx, path, name, value, flags) ⇒ Object

def link(path,as) end



409
410
411
412
# File 'lib/fuse/rfusefs-fuse.rb', line 409

def setxattr(ctx,path,name,value,flags)
    return wrap_context(ctx,__method__,path,name,value,flags) if ctx
    @root.xattr(path)[name]=value
end

#statfs(ctx, path) ⇒ Object

Some random numbers to show with df command bsize preferred block size = 1K unless @root provides something different frsize = bsize (but apparently unused) blocks = total number of blocks bfree = number of free blocks bavail = bfree if mounted -o allow_other files = count of all files ffree - count of free file inode



450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/fuse/rfusefs-fuse.rb', line 450

def statfs(ctx,path)
    return wrap_context(ctx,__method__,path) if ctx
    block_size = 1024

    stats = @root.statistics(path)
    case stats
    when Array
        used_space, used_files, total_space, total_files = stats
        used_files ||= 0
        used_space ||= 0
        total_files ||= used_files
        total_space ||= used_space
        result = RFuse::StatVfs.new(
          "bsize" => block_size,
          "frsize" => block_size,
          "blocks" => total_space / block_size,
          "bfree" => (total_space - used_space)/block_size,
          "bavail" => (total_space - used_space)/block_size,
          "files" => total_files,
          "ffree" => (total_files - used_files)
        )
        return result
    else
        #expected to quack like rfuse:statvfs
        return stats
    end
end

#truncate(ctx, path, offset) ⇒ Object

truncate a file outside of open files



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/fuse/rfusefs-fuse.rb', line 211

def truncate(ctx,path,offset)
    return wrap_context(ctx,__method__,path,offset) if ctx

    unless @root.can_write?(path)
        raise Errno::EACESS.new(path)
    end

    current_size = size(path)
    unless @root.raw_truncate(path,offset)
        contents = @root.read_file(path)
        if (offset <= 0)
           @root.write_to(path,"")
        elsif offset < contents.length
            @root.write_to(path,contents[0..offset] )
        end
    end
    @adj_size = @adj_size - current_size + (offset <= 0 ? 0 : offset)
end


367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/fuse/rfusefs-fuse.rb', line 367

def unlink(ctx,path)
    return wrap_context(ctx,__method__,path) if ctx

    unless @root.can_delete?(path)
        raise Errno::EACCES.new(path)
    end

    @adj_size = @adj_size - size(path)

    @created_files.delete(path)
    @root.delete(path)
end

#unmountedObject



482
483
484
# File 'lib/fuse/rfusefs-fuse.rb', line 482

def unmounted()
    @root.unmounted()
end

#utime(ctx, path, actime, modtime) ⇒ Object

def chown(path,uid,gid) end



360
361
362
363
364
365
# File 'lib/fuse/rfusefs-fuse.rb', line 360

def utime(ctx,path,actime,modtime)
    return wrap_context(ctx,__method__,path,actime,modtime) if ctx

    #Touch...
    @root.touch(path,modtime) if @root.respond_to?(:touch)
end

#write(ctx, path, buf, offset, ffi) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/fuse/rfusefs-fuse.rb', line 292

def write(ctx,path,buf,offset,ffi)
    return wrap_context(ctx,__method__,path,buf,offset,ffi) if ctx
    fh = ffi.fh

    if fh.raw
        if FuseFS::RFUSEFS_COMPATIBILITY
            return @root.raw_write(path,offset,buf.length,buf,fh.raw)
        else
            @root.raw_write(path,offset,buf.length,buf)
            return buf.length
        end
    else
        return fh.write(offset,buf)
    end
end