-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrestfuse.py
More file actions
390 lines (302 loc) · 13.6 KB
/
restfuse.py
File metadata and controls
390 lines (302 loc) · 13.6 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/usr/bin/env python
from collections import defaultdict
from errno import ENOENT
from stat import S_IFDIR, S_IFLNK, S_IFREG
from sys import argv, exit
from time import time
from tornado import httpserver
from tornado import ioloop
from tornado import web
import sys
import tornado
from fusepy.fuse24 import FUSE, FuseOSError, Operations, LoggingMixIn, fuse_get_context
from tornado.options import define, options
import restfs.factories.ObjectFactory as OF
from restfsc.manager.ConnectionManager import ConnectionManger
from restfsc.service.CacheService import CacheService
from restfs.objects.Grant import Grant
import restfs.factories.S3ACPFactory as S3ACP
import restfs.utils.ACPHelper as ACPHelper
from restfs.objects.ACP import ACP
define("conf" , default="restfs.cnf" , help="configuration file")
define("root_bucket", default='yyyy', help="port of resource locator")
class RestFSClient(LoggingMixIn, Operations):
def __init__(self):
self.cache = CacheService()
self.files = {}
self.data = defaultdict(str)
self.fd = 0
now = time()
self.files['/'] = dict(st_mode=(S_IFDIR | 0755), st_ctime=now,
st_mtime=now, st_atime=now, st_nlink=2)
def access (self, path, mode):
pass
def chmod(self, path, mode):
print '#################'
print '##### CH MOD ###'
print '#################'
uid, gid, pid = fuse_get_context()
idObj = self.cache.lookup(options.root_bucket,path)
context = ""
prop = self.cache.getProperties(options.root_bucket, idObj, OF.PROPERTY_OBJECT, uid, gid, context)
prop.mode = mode
self.cache.setAttributes(options.root_bucket, idObj, OF.PROPERTY_OBJECT, prop, uid, gid, context)
self.files[path]['st_mode'] &= 0770000
self.files[path]['st_mode'] |= prop.mode
return 0
def chown(self, path, uid, gid):
print '#################'
print '##### CH OWN ###'
print '#################'
#uid and gid correct? are the new or old one ?
old_uid, old_gid, pid = fuse_get_context()
idObj = self.cache.lookup(options.root_bucket,path)
context = ""
prop = self.cache.getProperties(options.root_bucket, idObj, OF.PROPERTY_OBJECT, old_uid, old_gid, context)
if gid & uid:
prop.mode = uid+gid+prop.mode[-1]
elif uid:
prop.mode = uid+prop.mode[1:]
self.cache.setAttributes(options.root_bucket, idObj, OF.PROPERTY_OBJECT, prop, uid, gid, context)
self.files[path]['st_uid'] = uid
self.files[path]['st_gid'] = gid
def create(self, path, mode):
print '#################'
print '##### CREATE ###'
print path
print '#################'
uid, gid, pid = fuse_get_context()
context = ""
st_mode=(S_IFREG | mode)
self.cache.createObject(options.root_bucket,path, OF.TYPE_FILE, uid, gid, st_mode, context)
print '#################'
print OF.TYPE_FILE
print '#################'
self.files[path] = dict(st_mode=(S_IFREG | mode), st_nlink=1,
st_size=0, st_ctime=time(), st_mtime=time(), st_atime=time())
self.fd += 1
#FIXME idObj need a number
return self.fd
def getattr(self, path, fh=None):
print '#################'
print '##### GET-ATT ###'
print '#################'
uid, gid, pid = fuse_get_context()
context = ""
## IL PROBLEMA su MAC e il lookup non il raise, l'unica e fare come fa lui
## e quindi aver la lista e controllare li.. eliminando il lookup
try :
print 'PATH',path
idObj = self.cache.lookup(options.root_bucket,path)
if idObj == None or idObj == 'null':
raise FuseOSError(ENOENT)
print 'FH', idObj
'''prop = self.cache.getProperties(options.root_bucket, idObj, OF.PROPERTY_OBJECT, uid, gid, context)
self.files[path] = dict(st_mode=(prop['mode']), st_nlink=prop['read_counter'],\
st_size=prop['size'], st_ctime=prop['creation_date'], st_mtime=prop['last_modify'], st_atime=time())
'''
#prop = self.cache.getProperties(options.root_bucket, idObj, OF.PROPERTY_OBJECT, uid, gid, context)
except :
raise FuseOSError(ENOENT)
st = self.files[path]
return st
def getxattr(self, path, name, position=0):
print '#################'
print '##### GetXattr ###'
print '#################'
attrs = self.files[path].get('attrs', {})
try:
return attrs[name]
except KeyError:
return '' # Should return ENOATTR
def listxattr(self, path):
print '#################'
print '##### listXattr ###'
print '#################'
attrs = self.files[path].get('attrs', {})
return attrs.keys()
def mkdir(self, path, mode):
print '#################'
print '##### MKDIR ###'
print '#################'
uid, gid, pid = fuse_get_context()
context = ""
st_mode = (S_IFDIR | mode)
self.cache.createObject(options.root_bucket,path, OF.TYPE_DIR, uid, gid, st_mode, context)
idObj = self.cache.lookup(options.root_bucket, path)
prop = self.cache.getProperties(options.root_bucket, idObj,OF.PROPERTY_OBJECT,uid,gid,context)
self.files[path] = dict(st_mode=(S_IFDIR | mode), st_nlink=prop['read_counter'],\
st_size=prop['size'], st_ctime=prop['creation_date'], st_mtime=prop['last_modify'], st_atime=time())
self.files['/']['st_nlink'] += 1
print sys.exc_info()
def open(self, path, flags):
print '#################'
print '##### OPEN ###'
print flags
print '#################'
uid, gid, pid = fuse_get_context()
context = ""
idObj = self.cache.lookup(options.root_bucket, path)
stream = self.cache.load(options.root_bucket, idObj, uid, gid, context)
#self.cache.open(options.root_bucket, path, flags, uid, gid, context)
self.fd += 1
return self.fd
def read(self, path, size, offset, fh):
print '#################'
print '##### READ ###'
print '#################'
uid, gid, pid = fuse_get_context()
context = ""
idObj = self.cache.lookup(options.root_bucket, path)
stream = self.cache.load(options.root_bucket, idObj, uid, gid, context)
return self.data[path][offset:offset + size]
def readdir(self, path, fh):
print '#################'
print '##### READ DIR ###'
print '#################'
uid, gid, pid = fuse_get_context()
# FIXME Translation "." e ".."
context = ""
idObjDir = self.cache.lookup(options.root_bucket,path)
lista = self.cache.getObjectList(options.root_bucket, idObjDir, uid, gid, context)
propDir = self.cache.getProperties(options.root_bucket, idObjDir,OF.PROPERTY_OBJECT,uid,gid,context)
dizio = dict()
if len(lista) != 0:
for elem in lista :
elem = '/'+str(elem)
if path != '/':
pathn = path+elem
idObj = self.cache.lookup(options.root_bucket,pathn)
else:
idObj = self.cache.lookup(options.root_bucket,path)
prop = self.cache.getProperties(options.root_bucket, idObj,OF.PROPERTY_OBJECT,uid,gid,context)
dizio[elem] = dict(st_mode=(prop['mode']), st_nlink=prop['read_counter'],\
st_size=prop['size'], st_ctime=prop['creation_date'], \
st_mtime=prop['last_modify'], st_atime=time())
dizio['/.']=dict(st_mode=(propDir['mode']), st_nlink=propDir['read_counter'],\
st_size=propDir['size'], st_ctime=propDir['creation_date'], \
st_mtime=propDir['last_modify'], st_atime=time())
print '#############'
print dizio
print '###########'
return ['..'] +[x[1:] for x in dizio if x != '/']
def readlink(self, path):
print '#################'
print '##### READ LINK ###'
print '#################'
return self.data[path]
def removexattr(self, path, name):
print '#################'
print '##### REMOVE XATTr ###'
print '#################'
uid, gid, pid = fuse_get_context()
context = ""
idObj = self.cache.lookup(options.root_bucket,path)
attrs = self.files[path].get('attrs', {})
try:
del attrs[name]
self.cache.delProperty(idObj, OF.PROPERTY_USER+name, uid, gid, context)
except KeyError:
pass # Should return ENOATTR
#Key Error Should return ENOATTR
def rename(self, old, new):
print '#################'
print '##### RENAME ###'
print '#################'
uid, gid, pid = fuse_get_context()
context = ""
idObj_old = self.cache.lookup(options.root_bucket,old)
prop_old = self.cache.getProperties(options.root_bucket, idObj_old, OF.PROPERTY_OBJECT, uid, gid, context)
#self.cache.removeObject(options.root_bucket, idObj_old, uid, gid, context)
#self.cache.remove(options.root_bucket,idObj,uid,gid,context)
prop_old['object_name'] = new
self.cache.removeObject(options.root_bucket, idObj_old, uid, gid, context)
self.cache.setAttributesFromNew(options.root_bucket, idObj_old, prop_old, uid, gid, context)
self.files[new] = self.files.pop(old)
def rmdir(self, path):
print '#################'
print '##### RMDIR ###'
print '#################'
uid, gid, pid = fuse_get_context()
context = ""
idObj = self.cache.lookup(options.root_bucket,path)
#self.cache.remove(options.root_bucket,idObj,uid,gid,context)
self.cache.removeObject(options.root_bucket, idObj, uid, gid, context)
self.files.pop(path)
self.files['/']['st_nlink'] -= 1
def setxattr(self, path, name, value, options, position=0):
print '#################'
print '##### SETxattr ###'
print '#################'
uid, gid, pid = fuse_get_context()
context = ""
# Options ? / Positions ?
idObj = self.cache.lookup(options.root_bucket,path)
self.cache.setProperty(idObj, OF.PROPERTY_USER+name, value, uid, gid, context)
attrs = self.files[path].setdefault('attrs', {})
attrs[name] = value
def statfs(self, path):
print '#################'
print '##### statfs ###'
print '#################'
return dict(f_bsize=512, f_blocks=4096, f_bavail=2048)
def symlink(self, target, source):
print '#################'
print '##### symlink ###'
print '#################'
self.files[target] = dict(st_mode=(S_IFLNK | 0777), st_nlink=1,
st_size=len(source))
self.data[target] = source
def truncate(self, path, length, fh=None):
print '#################'
print '##### truncate ###'
print '#################'
self.data[path] = self.data[path][:length]
self.files[path]['st_size'] = length
def unlink(self, path):
print '#################'
print '##### unlink ###'
print '#################'
uid, gid, pid = fuse_get_context()
context = ""
idObj = self.cache.lookup(options.root_bucket,path)
self.cache.remove(options.root_bucket,idObj, uid, gid, context)
self.cache.removeObject(options.root_bucket,idObj, uid, gid, context)
self.files.pop(path)
def utimens(self, path, times=None):
print '#################'
print '##### utimes ###'
print '#################'
now = time()
atime, mtime = times if times else (now, now)
self.files[path]['st_atime'] = atime
self.files[path]['st_mtime'] = mtime
def write(self, path, data_handler, offset, fh):
print '#################'
print '##### WRITE ###'
print '#################'
uid, gid, context = fuse_get_context()
xattr = self.listxattr(path)
object_acl = S3ACP.ACL_FULL_CONTROL
acp = ACP()
grant = Grant()
grant.uid = uid
grant.permission = ACPHelper.s3AclToPerm(object_acl)
grantList = [grant]
acp.setByGrants(grantList)
storage_class="STANDARD"
content_type=None
self.cache.write(options.root_bucket, path, uid, gid, context, storage_class, object_acl ,content_type, xattr, data_handler)
self.data[path] = self.data[path][:offset] + data_handler
self.files[path]['st_size'] = len(self.data[path])
return len(data_handler)
if __name__ == "__main__":
if len(argv) != 2:
print 'usage: %s <mountpoint>' % argv[0]
exit(1)
tornado.options.parse_command_line()
# Read File
tornado.options.parse_config_file(options.conf)
# Connection Manager
# Cache manager
fuse = FUSE(RestFSClient(), argv[1], foreground=True)