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