github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/lib/python/fusepy/memoryll.py (about) 1 #!/usr/bin/env python 2 3 from collections import defaultdict 4 from errno import ENOENT, EROFS 5 from stat import S_IFMT, S_IMODE, S_IFDIR, S_IFREG 6 from sys import argv, exit 7 from time import time 8 9 from fusell import FUSELL 10 11 12 class Memory(FUSELL): 13 def create_ino(self): 14 self.ino += 1 15 return self.ino 16 17 def init(self, userdata, conn): 18 self.ino = 1 19 self.attr = defaultdict(dict) 20 self.data = defaultdict(str) 21 self.parent = {} 22 self.children = defaultdict(dict) 23 24 self.attr[1] = {'st_ino': 1, 'st_mode': S_IFDIR | 0777, 'st_nlink': 2} 25 self.parent[1] = 1 26 27 forget = None 28 29 def getattr(self, req, ino, fi): 30 print 'getattr:', ino 31 attr = self.attr[ino] 32 if attr: 33 self.reply_attr(req, attr, 1.0) 34 else: 35 self.reply_err(req, ENOENT) 36 37 def lookup(self, req, parent, name): 38 print 'lookup:', parent, name 39 children = self.children[parent] 40 ino = children.get(name, 0) 41 attr = self.attr[ino] 42 43 if attr: 44 entry = {'ino': ino, 'attr': attr, 'atttr_timeout': 1.0, 'entry_timeout': 1.0} 45 self.reply_entry(req, entry) 46 else: 47 self.reply_err(req, ENOENT) 48 49 def mkdir(self, req, parent, name, mode): 50 print 'mkdir:', parent, name 51 ino = self.create_ino() 52 ctx = self.req_ctx(req) 53 now = time() 54 attr = { 55 'st_ino': ino, 56 'st_mode': S_IFDIR | mode, 57 'st_nlink': 2, 58 'st_uid': ctx['uid'], 59 'st_gid': ctx['gid'], 60 'st_atime': now, 61 'st_mtime': now, 62 'st_ctime': now} 63 64 self.attr[ino] = attr 65 self.attr[parent]['st_nlink'] += 1 66 self.parent[ino] = parent 67 self.children[parent][name] = ino 68 69 entry = {'ino': ino, 'attr': attr, 'atttr_timeout': 1.0, 'entry_timeout': 1.0} 70 self.reply_entry(req, entry) 71 72 def mknod(self, req, parent, name, mode, rdev): 73 print 'mknod:', parent, name 74 ino = self.create_ino() 75 ctx = self.req_ctx(req) 76 now = time() 77 attr = { 78 'st_ino': ino, 79 'st_mode': mode, 80 'st_nlink': 1, 81 'st_uid': ctx['uid'], 82 'st_gid': ctx['gid'], 83 'st_rdev': rdev, 84 'st_atime': now, 85 'st_mtime': now, 86 'st_ctime': now} 87 88 self.attr[ino] = attr 89 self.attr[parent]['st_nlink'] += 1 90 self.children[parent][name] = ino 91 92 entry = {'ino': ino, 'attr': attr, 'atttr_timeout': 1.0, 'entry_timeout': 1.0} 93 self.reply_entry(req, entry) 94 95 def open(self, req, ino, fi): 96 print 'open:', ino 97 self.reply_open(req, fi) 98 99 def read(self, req, ino, size, off, fi): 100 print 'read:', ino, size, off 101 buf = self.data[ino][off:(off + size)] 102 self.reply_buf(req, buf) 103 104 def readdir(self, req, ino, size, off, fi): 105 print 'readdir:', ino 106 parent = self.parent[ino] 107 entries = [('.', {'st_ino': ino, 'st_mode': S_IFDIR}), 108 ('..', {'st_ino': parent, 'st_mode': S_IFDIR})] 109 for name, child in self.children[ino].items(): 110 entries.append((name, self.attr[child])) 111 self.reply_readdir(req, size, off, entries) 112 113 def rename(self, req, parent, name, newparent, newname): 114 print 'rename:', parent, name, newparent, newname 115 ino = self.children[parent].pop(name) 116 self.children[newparent][newname] = ino 117 self.parent[ino] = newparent 118 self.reply_err(req, 0) 119 120 def setattr(self, req, ino, attr, to_set, fi): 121 print 'setattr:', ino, to_set 122 a = self.attr[ino] 123 for key in to_set: 124 if key == 'st_mode': 125 # Keep the old file type bit fields 126 a['st_mode'] = S_IFMT(a['st_mode']) | S_IMODE(attr['st_mode']) 127 else: 128 a[key] = attr[key] 129 self.attr[ino] = a 130 self.reply_attr(req, a, 1.0) 131 132 def write(self, req, ino, buf, off, fi): 133 print 'write:', ino, off, len(buf) 134 self.data[ino] = self.data[ino][:off] + buf 135 self.attr[ino]['st_size'] = len(self.data[ino]) 136 self.reply_write(req, len(buf)) 137 138 if __name__ == '__main__': 139 if len(argv) != 2: 140 print 'usage: %s <mountpoint>' % argv[0] 141 exit(1) 142 fuse = Memory(argv[1])