github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/lib/python/fusepy/loopback.py (about) 1 #!/usr/bin/env python 2 3 from __future__ import with_statement 4 5 from errno import EACCES 6 from os.path import realpath 7 from sys import argv, exit 8 from threading import Lock 9 10 import os 11 12 from fuse import FUSE, FuseOSError, Operations, LoggingMixIn 13 14 15 class Loopback(LoggingMixIn, Operations): 16 def __init__(self, root): 17 self.root = realpath(root) 18 self.rwlock = Lock() 19 20 def __call__(self, op, path, *args): 21 return super(Loopback, self).__call__(op, self.root + path, *args) 22 23 def access(self, path, mode): 24 if not os.access(path, mode): 25 raise FuseOSError(EACCES) 26 27 chmod = os.chmod 28 chown = os.chown 29 30 def create(self, path, mode): 31 return os.open(path, os.O_WRONLY | os.O_CREAT, mode) 32 33 def flush(self, path, fh): 34 return os.fsync(fh) 35 36 def fsync(self, path, datasync, fh): 37 return os.fsync(fh) 38 39 def getattr(self, path, fh=None): 40 st = os.lstat(path) 41 return dict((key, getattr(st, key)) for key in ('st_atime', 'st_ctime', 42 'st_gid', 'st_mode', 'st_mtime', 'st_nlink', 'st_size', 'st_uid')) 43 44 getxattr = None 45 46 def link(self, target, source): 47 return os.link(source, target) 48 49 listxattr = None 50 mkdir = os.mkdir 51 mknod = os.mknod 52 open = os.open 53 54 def read(self, path, size, offset, fh): 55 with self.rwlock: 56 os.lseek(fh, offset, 0) 57 return os.read(fh, size) 58 59 def readdir(self, path, fh): 60 return ['.', '..'] + os.listdir(path) 61 62 readlink = os.readlink 63 64 def release(self, path, fh): 65 return os.close(fh) 66 67 def rename(self, old, new): 68 return os.rename(old, self.root + new) 69 70 rmdir = os.rmdir 71 72 def statfs(self, path): 73 stv = os.statvfs(path) 74 return dict((key, getattr(stv, key)) for key in ('f_bavail', 'f_bfree', 75 'f_blocks', 'f_bsize', 'f_favail', 'f_ffree', 'f_files', 'f_flag', 76 'f_frsize', 'f_namemax')) 77 78 def symlink(self, target, source): 79 return os.symlink(source, target) 80 81 def truncate(self, path, length, fh=None): 82 with open(path, 'r+') as f: 83 f.truncate(length) 84 85 unlink = os.unlink 86 utimens = os.utime 87 88 def write(self, path, data, offset, fh): 89 with self.rwlock: 90 os.lseek(fh, offset, 0) 91 return os.write(fh, data) 92 93 94 if __name__ == "__main__": 95 if len(argv) != 3: 96 print 'usage: %s <root> <mountpoint>' % argv[0] 97 exit(1) 98 fuse = FUSE(Loopback(argv[1]), argv[2], foreground=True)