github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/lib/python/fusepy/memory.py (about) 1 #!/usr/bin/env python 2 3 from collections import defaultdict 4 from errno import ENOENT 5 from stat import S_IFDIR, S_IFLNK, S_IFREG 6 from sys import argv, exit 7 from time import time 8 9 from fuse import FUSE, FuseOSError, Operations, LoggingMixIn 10 11 12 class Memory(LoggingMixIn, Operations): 13 """Example memory filesystem. Supports only one level of files.""" 14 15 def __init__(self): 16 self.files = {} 17 self.data = defaultdict(str) 18 self.fd = 0 19 now = time() 20 self.files['/'] = dict(st_mode=(S_IFDIR | 0755), st_ctime=now, 21 st_mtime=now, st_atime=now, st_nlink=2) 22 23 def chmod(self, path, mode): 24 self.files[path]['st_mode'] &= 0770000 25 self.files[path]['st_mode'] |= mode 26 return 0 27 28 def chown(self, path, uid, gid): 29 self.files[path]['st_uid'] = uid 30 self.files[path]['st_gid'] = gid 31 32 def create(self, path, mode): 33 self.files[path] = dict(st_mode=(S_IFREG | mode), st_nlink=1, 34 st_size=0, st_ctime=time(), st_mtime=time(), st_atime=time()) 35 self.fd += 1 36 return self.fd 37 38 def getattr(self, path, fh=None): 39 if path not in self.files: 40 raise FuseOSError(ENOENT) 41 st = self.files[path] 42 return st 43 44 def getxattr(self, path, name, position=0): 45 attrs = self.files[path].get('attrs', {}) 46 try: 47 return attrs[name] 48 except KeyError: 49 return '' # Should return ENOATTR 50 51 def listxattr(self, path): 52 attrs = self.files[path].get('attrs', {}) 53 return attrs.keys() 54 55 def mkdir(self, path, mode): 56 self.files[path] = dict(st_mode=(S_IFDIR | mode), st_nlink=2, 57 st_size=0, st_ctime=time(), st_mtime=time(), st_atime=time()) 58 self.files['/']['st_nlink'] += 1 59 60 def open(self, path, flags): 61 self.fd += 1 62 return self.fd 63 64 def read(self, path, size, offset, fh): 65 return self.data[path][offset:offset + size] 66 67 def readdir(self, path, fh): 68 return ['.', '..'] + [x[1:] for x in self.files if x != '/'] 69 70 def readlink(self, path): 71 return self.data[path] 72 73 def removexattr(self, path, name): 74 attrs = self.files[path].get('attrs', {}) 75 try: 76 del attrs[name] 77 except KeyError: 78 pass # Should return ENOATTR 79 80 def rename(self, old, new): 81 self.files[new] = self.files.pop(old) 82 83 def rmdir(self, path): 84 self.files.pop(path) 85 self.files['/']['st_nlink'] -= 1 86 87 def setxattr(self, path, name, value, options, position=0): 88 # Ignore options 89 attrs = self.files[path].setdefault('attrs', {}) 90 attrs[name] = value 91 92 def statfs(self, path): 93 return dict(f_bsize=512, f_blocks=4096, f_bavail=2048) 94 95 def symlink(self, target, source): 96 self.files[target] = dict(st_mode=(S_IFLNK | 0777), st_nlink=1, 97 st_size=len(source)) 98 self.data[target] = source 99 100 def truncate(self, path, length, fh=None): 101 self.data[path] = self.data[path][:length] 102 self.files[path]['st_size'] = length 103 104 def unlink(self, path): 105 self.files.pop(path) 106 107 def utimens(self, path, times=None): 108 now = time() 109 atime, mtime = times if times else (now, now) 110 self.files[path]['st_atime'] = atime 111 self.files[path]['st_mtime'] = mtime 112 113 def write(self, path, data, offset, fh): 114 self.data[path] = self.data[path][:offset] + data 115 self.files[path]['st_size'] = len(self.data[path]) 116 return len(data) 117 118 119 if __name__ == "__main__": 120 if len(argv) != 2: 121 print 'usage: %s <mountpoint>' % argv[0] 122 exit(1) 123 fuse = FUSE(Memory(), argv[1], foreground=True)