-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.py
More file actions
439 lines (360 loc) · 14.8 KB
/
FileSystem.py
File metadata and controls
439 lines (360 loc) · 14.8 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/usr/bin/env python
__author__ = 'Kanika'
import logging
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 time import time
import datetime
from fuse import FUSE, FuseOSError, Operations, LoggingMixIn
from xmlrpclib import Binary
import sys, pickle, xmlrpclib
from xmlrpclib import Binary
import sys, pickle, xmlrpclib
count = 0
from fuse import FUSE, FuseOSError, Operations, LoggingMixIn
if not hasattr(__builtins__, 'bytes'):
bytes = str
class FileNode:
def __init__(self,name,isFile,path,url):
self.name = name
self.path = path
#self.url = url
self.isFile = isFile # true if node is a file, false if is a directory.
self.putdata("data","") # used if it is a file
self.put("meta",{})
self.put("list_nodes",{})# contains a tuple of <name:FileNode> used only if it is a dir.
def put(self,key,value):
key = self.path+"&&"+key
print("REached here",value)
#rpc = xmlrpclib.Server(url)
rpc.put(Binary(key), Binary(pickle.dumps(value)))
print "controll"
def putdata(self,key,value):
key = self.path+"&&"+key
#rpc = xmlrpclib.Server(url)
rpc.putdata(Binary(key), Binary(pickle.dumps(value)))
def get(self,key):
key = self.path+"&&"+key
print("Inside get",key)
#rpc = xmlrpclib.Server(url)
res = rpc.get(Binary(key))
if "value" in res:
print("response",pickle.loads(res["value"].data))
return pickle.loads(res["value"].data)
else:
print None
return None
def getdata(self,key):
key = self.path+"&&"+key
#rpc = xmlrpclib.Server(url)
print("Inside get the key is",key)
res = rpc.getdata(Binary(key))
print res
if "value" in res:
return pickle.loads(res["value"].data)
else:
return None
def set_data(self,data_blob):
self.putdata("data",data_blob)
def set_meta(self,meta):
self.put("meta",meta)
def get_data(self):
#print "Data is here"
#val = self.getdata("data")
return self.getdata("data")
def get_meta(self):
return self.get("meta")
def list_nodes(self):
return self.get("list_nodes").values()
def add_node(self,newnode):
list_nodes = self.get("list_nodes")
list_nodes[newnode.name]=newnode
self.put("list_nodes",list_nodes)
def contains_node(self,name): # returns node object if it exists
if (self.isFile==True):
return None
else:
if name in self.get("list_nodes").keys():
return self.get("list_nodes")[name]
else:
return None
class FS:
def __init__(self,url):
self.url = url
self.root = FileNode('/',False,'/',url)
now = time()
self.fd = 0
self.root.set_meta(dict(st_mode=(S_IFDIR | 0755), st_ctime=now,st_mtime=now,\
st_atime=now, st_nlink=2))
# returns the desired FileNode object
def get_node_wrapper(self,path): # pathname of the file being probed.
# Handle special case for root node
if path == '/':
return self.root
PATH = path.split('/') # break pathname into a list of components
name = PATH[-1]
PATH[0]='/' # splitting of a '/' leading string yields "" in first slot.
return self.get_node(self.root,PATH,name)
def get_node(self,parent,PATH,name):
next_node = parent.contains_node(PATH[1])
if (next_node == None or next_node.name == name):
return next_node
else:
return self.get_node(next_node,PATH[1:],name)
def get_parent_node(self,path):
parent_path = "/"+("/".join(path.split('/')[1:-1]))
parent_node = self.get_node_wrapper(parent_path)
return parent_node
def add_node(self,node,path):
parent_path = "/"+("/".join(path.split('/')[1:-1]))
parent_node = self.get_node_wrapper(parent_path)
parent_node.add_node(node)
if (not node.isFile):
meta = parent_node.get("meta")
meta['st_nlink']+=1
parent_node.put("meta",meta)
else:
self.fd+=1
return self.fd
def add_dir(self,path,mode):
# create a file node
temp_node = FileNode(path.split('/')[-1],False,path,self.url)
temp_node.set_meta(dict(st_mode=(S_IFDIR | mode), st_nlink=2,
st_size=0, st_ctime=time(), st_mtime=time(),
st_atime=time()))
# Add node to the FS
self.add_node(temp_node,path)
def add_file(self,path,mode):
# create a file node
temp_node = FileNode(path.split('/')[-1],True,path,self.url)
temp_node.set_meta(dict(st_mode=(S_IFREG | mode), st_nlink=1,
st_size=0, st_ctime=time(), st_mtime=time(),
st_atime=time()))
# Add node to the FS
# before we do that, we have to manipulate the path string to point
self.add_node(temp_node,path)
self.fd+=1
return self.fd
def write_file(self,path,data=None, offset=0, fh=None):
# file will already have been created before this call
# get the corresponding file node
filenode = self.get_node_wrapper(path)
# if data == None, this is just a truncate request,using offset as
# truncation parameter equivalent to length
node_data = filenode.getdata("data")
node_meta = filenode.get("meta")
if (data==None):
node_data = node_data[:offset]
node_meta['st_size'] = offset
else:
node_data = node_data[:offset]+data
node_meta['st_size'] = len(node_data)
filenode.putdata("data",node_data)
filenode.put("meta",node_meta)
def read_file(self,path,offset=0,size=None):
# get file node
filenode = self.get_node_wrapper(path)
# if size==None, this is a readLink request
if (size==None):
return filenode.get_data()
else:
# return requested portion data
print filenode.getdata("data")
return filenode.getdata("data")[offset:offset + size]
def rename_node(self,old,new):
# first check if parent exists i.e. destination path is valid
future_parent_node = self.get_parent_node(new)
if (future_parent_node == None):
raise FuseOSError(ENOENT)
return
# get old filenodeobject and its parent filenode object
filenode = self.get_node_wrapper(old)
parent_filenode = self.get_parent_node(old)
# remove node from parent
list_nodes = parent_filenode.get("list_nodes")
del list_nodes[filenode.name]
parent_filenode.put("list_nodes",list_nodes)
# if filenode is a directory decrement 'st_link' of parent
if (not filenode.isFile):
parent_meta = parent_filenode.get("meta")
parent_meta["st_nlink"]-=1
parent_filenode.put("meta",parent_meta)
# add filenode to new parent, also change the name
filenode.name = new.split('/')[-1]
future_parent_node.add_node(filenode)
def utimens(self,path,times):
filenode = self.get_node_wrapper(path)
now = time()
atime, mtime = times if times else (now, now)
meta = filenode.get("meta")
meta['st_atime'] = atime
meta['st_mtime'] = mtime
filenode.put("meta",meta)
def delete_node(self,path):
# get parent node
parent_filenode = self.get_parent_node(path)
# get node to be deleted
filenode = self.get_node_wrapper(path)
# remove node from parents list
list_nodes = parent_filenode.get("list_nodes")
del list_nodes[filenode.name]
parent_filenode.put("list_nodes",list_nodes)
# if its a dir reduce 'st_nlink' in parent
if (not filenode.isFile):
parents_meta = parent_filenode.get("meta")
parents_meta["st_nlink"]-=1
parent_filenode.put("meta",parents_meta)
def link_nodes(self,target,source):
# create a new target node.
temp_node = FileNode(target.split('/')[-1],True,target,self.url)
temp_node.set_meta(dict(st_mode=(S_IFLNK | 0777), st_nlink=1,
st_size=len(source)))
temp_node.set_data(source)
# add the new node to FS
self.add_node(temp_node,target)
def update_meta(self,path,mode=None,uid=None,gid=None):
# get the desired filenode.
filenode = self.get_node_wrapper(path)
# if chmod request
meta = filenode.get("meta")
if (uid==None):
meta["st_mode"] &= 0770000
meta["st_mode"] |= mode
else: # a chown request
meta['st_uid'] = uid
meta['st_gid'] = gid
filenode.put("meta",meta)
class Memory(LoggingMixIn, Operations):
'Example memory filesystem. Supports only one level of files.'
def __init__(self,url):
global count # count is a global variable, can be used inside any function.
count +=1 # increment count for very method call, to track count of calls made.
print ("CallCount {} " " Time {}".format(count,datetime.datetime.now().time())) # print the parameters passed to the method as input.(used for debugging)
print('In function __init__()') #print name of the method called
self.FS = FS(url)
def getattr(self, path, fh=None):
global count
count +=1
print ("CallCount {} " " Time {} arguments:{} {} {}".format(count,datetime.datetime.now().time(),type(self),path,type(fh)))
print('In function getattr()')
file_node = self.FS.get_node_wrapper(path)
if (file_node == None):
raise FuseOSError(ENOENT)
else:
return file_node.get_meta()
def readdir(self, path, fh):
global count
count +=1
print ("CallCount {} " " Time {}".format(count,datetime.datetime.now().time()))
print('In function readdir()')
file_node = self.FS.get_node_wrapper(path)
m = ['.','..']+[x.name for x in file_node.list_nodes()]
print m
return m
def mkdir(self, path, mode):
global count
count +=1
print ("CallCount {} " " Time {}" "," "argumnets:" " " "path;{}" "," "mode:{}".format(count,datetime.datetime.now().time(),path,mode))
print('In function mkdir()')
# create a file node
self.FS.add_dir(path,mode)
def create(self, path, mode):
global count
count +=1
print ("CallCount {} " " Time {} path {} mode {}".format(count,datetime.datetime.now().time(),path,mode))
print('In function create()')
return self.FS.add_file(path,mode) # returns incremented fd.
def write(self, path, data, offset, fh):
global count
count +=1
print ("CallCount {} " " Time {}".format(count,datetime.datetime.now().time()))
print ("Path:{}" " " "data:{}" " " "offset:{}" " " "filehandle{}".format(path,data,offset,fh))
print('In function write()')
self.FS.write_file(path, data, offset, fh)
return len(data)
def open(self, path, flags):
global count
count +=1
print ("CallCount {} " " Time {}" " " "argumnets:" " " "path:{}" "," "flags:{}".format(count,datetime.datetime.now().time(),path,flags))
print('In function open()')
self.FS.fd += 1
return self.FS.fd
def read(self, path, size, offset, fh):
global count
count +=1
print ("CallCount {} " " Time {}" " " "arguments:" " " "path:{}" "," "size:{}" "," "offset:{}" "," "fh:{}".format(count,datetime.datetime.now().time(),path,size,offset,fh))
print('In function read()')
return self.FS.read_file(path,offset,size)
def rename(self, old, new):
global count
count +=1
print ("CallCount {} " " Time {}".format(count,datetime.datetime.now().time()))
print('In function rename()')
self.FS.rename_node(old,new)
def utimens(self, path, times=None):
global count
count +=1
print ("CallCount {} " " Time {} Path {}".format(count,datetime.datetime.now().time(),path))
print('In function utimens()')
self.FS.utimens(path,times)
def rmdir(self, path):
global count
count +=1
print ("CallCount {} " " Time {}".format(count,datetime.datetime.now().time()))
print('In function rmdir()')
self.FS.delete_node(path)
def unlink(self, path):
global count
count +=1
print ("CallCount {} " " Time {}".format(count,datetime.datetime.now().time()))
print('In function unlink()')
self.FS.delete_node(path)
def symlink(self, target, source):
global count
count +=1
print ("CallCount {} " " Time {}" "," "Target:{}" "," "Source:{}".format(count,datetime.datetime.now().time(),target,source))
print('In function symlink()')
self.FS.link_nodes(target,source)
def readlink(self, path):
global count
count +=1
print ("CallCount {} " " Time {}".format(count,datetime.datetime.now().time()))
print('In function readlink()')
return self.FS.read_file(path)
def truncate(self, path, length, fh=None):
global count
print ("CallCount {} " " Time {}""," "arguments:" "path:{}" "," "length:{}" "," "fh:{}".format(count,datetime.datetime.now().time(),path,length,fh))
print('In function truncate()')
self.FS.write_file(path,offset=length)
def chmod(self, path, mode):
global count
count +=1
print ("CallCount {} " " Time {}".format(count,datetime.datetime.now().time()))
print('In function chmod()')
self.FS.update_meta(path,mode=mode)
return 0
def chown(self, path, uid, gid):
global count
count +=1
print ("CallCount {} " " Time {}".format(count,datetime.datetime.now().time()))
print('In function chown()')
self.FS.update_meta(path,uid=uid,gid=gid)
if __name__ == "__main__":
if len(argv) != 3:
print 'usage: %s <mountpoint> <remote hashtable>' % argv[0]
exit(1)
url = argv[2]
logger = logging.getLogger('fuse.log-mixin')
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# add the handlers to the logger
logger.addHandler(ch)
url = argv[2]
print argv[1]
rpc = xmlrpclib.Server(url,allow_none=True)
# Create a new HtProxy object using the URL specified at the command-line
fuse = FUSE(Memory(url), argv[1], foreground=True)