fusetoys

various hacky fuse filesystem utilities
git clone https://a3nm.net/git/fusetoys/
Log | Files | Refs | README

loopfs_nolock.py (3310B)


      1 #!/usr/bin/python
      2 
      3 """Dummy loopback FUSE filesystem"""
      4 # difference with loopfs.py: do not try to have a RW cache, just pass open and
      5 # read and write operations directly
      6 
      7 import fuse
      8 import errno
      9 import os
     10 import sys
     11 import threading
     12 
     13 fuse.fuse_python_api = (0, 2)
     14 
     15 class LoopFS(fuse.Fuse):
     16     def __init__(self, *args, **kw):
     17         fuse.Fuse.__init__(self, *args, **kw)
     18 
     19     def fsinit(self):
     20         self.source = self.cmdline[1][0]
     21 
     22     def sourcePath(self, path):
     23         return os.path.join(self.source, path[1:])
     24 
     25     def access(self, path, mode):
     26         if not os.access(self.sourcePath(path), mode):
     27             return -errno.EACCES
     28 
     29     def chmod(self, path, mode):
     30         return os.chmod(self.sourcePath(path), mode)
     31 
     32     def chown(self, path, mode):
     33         return os.chown(self.sourcePath(path), mode)
     34 
     35     def create(self, path, flags, mode):
     36         return os.open(self.sourcePath(path), os.O_WRONLY | os.O_CREAT, mode)
     37 
     38     def getattr(self, path):
     39         return os.lstat(self.sourcePath(path))
     40 
     41     def link(self, target, source):
     42         os.link(self.sourcePath(source), self.sourcePath(target))
     43 
     44     def mkdir(self, path, mode):
     45         return os.mkdir(self.sourcePath(path), mode)
     46 
     47     def mknod(self, path, mode, rdev):
     48         return os.mknod(self.sourcePath(path), mode, rdev)
     49 
     50     def open(self, path, flags):
     51         return os.open(self.sourcePath(path), flags) 
     52 
     53     def read(self, path, size, offset):
     54         os.lseek(fh, offset, 0)
     55         return os.read(fh, size)
     56 
     57     def readdir(self, path, offset):
     58         path = self.sourcePath(path)
     59         myIno = os.lstat(path).st_ino
     60         print ("will yield dot")
     61         yield fuse.Direntry('.', ino=myIno)
     62         print ("will yield parent")
     63         try:
     64             parentIno = os.lstat(os.path.join(path, "..")).st_ino
     65             print ("yielded parent")
     66         except OSError as e:
     67             parentIno = myIno # root
     68             print ("faked parent")
     69         yield fuse.Direntry('..', ino=parentIno)
     70         print ("will yield children")
     71         for name in os.listdir(path):
     72             print ("yield %s" % os.path.join(path, name))
     73             ino = os.lstat(os.path.join(path, name)).st_ino
     74             yield fuse.Direntry(name, ino=ino)
     75         print ("alldone")
     76 
     77     def readlink(self, path):
     78         return os.readlink(self.sourcePath(path))
     79 
     80     def rename(self, old, new):
     81         return os.rename(self.sourcePath(old), self.sourcePath(new))
     82 
     83     def rmdir(self, path):
     84         return os.rmdir(self.sourcePath(path))
     85 
     86     def statfs(self):
     87         return os.statvfs(self.sourceRoot)
     88 
     89     def symlink(self, target, source):
     90         return os.symlink(self.sourcePath(source), self.sourcePath(target))
     91 
     92     def truncate(self, path, length, fh=None):
     93         with open(self.sourcePath(path), 'r+') as f:
     94             return f.truncate(length)
     95 
     96     def unlink(self, path):
     97         return os.unlink(self.sourcePath(path))
     98 
     99     def utimens(self, path, ts_acc, ts_mod):
    100         times = (ts_acc.tv_sec, ts_mod.tv_sec)
    101         return os.utime(self.sourcePath(path), times)
    102 
    103     def write(self, path, data, offset):
    104         os.lseek(fh, offset, 0)
    105         return os.write(fh, data)
    106 
    107 
    108 if __name__ == "__main__":
    109     loopfs = LoopFS()
    110     fuse_opts = loopfs.parse(['-o', 'fsname=loopfs'] + sys.argv[1:])
    111     loopfs.main()
    112