-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.py
460 lines (364 loc) · 13.5 KB
/
fs.py
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
'''
An In-Memory Filesystem with Crash Recovery.
Crash recovery is built keeping the Memento Design Pattern in mind
'''
from errno import *
from time import time
import sys
from os import getuid, getgid, mkdir, path
from stat import *
from copy import copy, deepcopy
from data import *
from inode import *
from history import *
from decorators import *
import argparse
import pickle
from fuse import FUSE, FuseOSError, Operations, LoggingMixIn
class MyFs(Operations):
fs_name = "Myfs"
def __init__(self, backupstore):
self.root_inode = Inode(
1,
"/",
S_IFDIR | 0o755,
time(),
time(),
time(),
getuid(),
getgid()
)
self.crash_history = CrashHistory(backupstore)
self.name2inode = {}
self.fd = 0
self.setup_pseudo_files()
# setup files 'restore' and 'store' which
# restore the fs to the latest state and store
# the current state of the fs in the crash_history object
def setup_pseudo_files(self):
restore = Inode(
1,
"restore",
S_IFREG | 0o755,
time(),
time(),
time(),
getuid(),
getgid()
)
store = Inode(
1,
"store",
S_IFREG | 0o755,
time(),
time(),
time(),
getuid(),
getgid()
)
versions = Inode(
1,
"versions",
S_IFDIR | 0o755,
time(),
time(),
time(),
getuid(),
getgid()
)
self.root_inode.inodes.append(restore)
self.root_inode.inodes.append(store)
self.root_inode.inodes.append(versions)
# fill up the versions directory with states.
# if the states are already present in the media
# device
states = self.get_fs_versions()
for state in states:
state_inode = Inode(
1,
state,
S_IFREG | 0o755,
time(),
time(),
time(),
getuid(),
getgid()
)
versions.inodes.append(state_inode)
def get_fs_versions(self):
return self.crash_history.get_all_versions()
def get_version_creation(self, version):
return self.crash_history.get_version_date(version)
def handle_fs_state(self, path, version):
if path == "/restore":
latest_state = self.crash_history.get_latest_history(version)
if latest_state:
self.root_inode = copy(latest_state)
self.wipe_out_cache()
else:
# create a pseudo file to represent each version.
# this is for the user to check the versions present in
# the system
# first check if this state is being overwritten.
version_inode = self.get_inode("/versions/" + version, [S_IFREG])
if version_inode is None:
version_inode = Inode(
1,
version,
S_IFREG | 0o755,
time(),
time(),
time(),
getuid(),
getgid(),
)
version_dir_inode = self.get_inode("/versions", [S_IFDIR])
if version_dir_inode:
version_dir_inode.inodes.append(version_inode)
self.crash_history.add_to_history(copy(self.root_inode), version)
@staticmethod
def get_filename_and_parentdir(path):
path_list = path.split('/')
# need to extract the current filename and parent_dirname
# I think there may be a more idiomatic way to do this
curr_filename = path_list[-1]
parent_dir = '/'.join(path_list[:len(path_list) - 1])
return curr_filename, parent_dir
# we didnt cache the filename2inode mapping.
# Search in the filesystem hierarchy.
# really expensive tho :(
def search_root_inode(self, name, mode):
# if name is just root inode...
if name == '/':
return self.root_inode
# otherwise...
# split the name upinto a list
# much easier
# bad at naming sorry
name_list = name.split('/')
name_list[0] = '/' # did this cuz it looks nice
name_list_iter = iter(name_list) # who likes indexing anyways?
next(name_list_iter) # skip the root inode
# start at root inode
temp_node = self.root_inode
while True:
found_node = False
try:
curr_name = next(name_list_iter)
except StopIteration:
return temp_node
for node in temp_node.inodes:
if ((node.name == curr_name) and (node.get_inode_type() in mode)):
temp_node = node
found_node = True
break
if found_node == False:
return None
def get_inode(self, name, mode):
# mode is the type of file we are searching for.
# if we are searching for the parent dir, then we need
# to ignore files and links
# we need to get the inode give the filename
# first check the name2inode cache
try:
inode = self.name2inode[name]
# oopsie inode not cached.
# Need to search from the root_inode. bleh.
except KeyError:
inode = self.search_root_inode(name, mode)
if inode is None:
return None
# now cache it
self.name2inode[name] = inode
return inode
def wipe_out_cache(self):
self.name2inode = {}
def invalidate_cache(self, path):
try:
del self.name2inode[path]
except KeyError:
pass
@logger
def chmod(self, path, mode):
inode = self.get_inode(path, [S_IFDIR, S_IFREG, S_IFLNK])
if inode is None:
raise FuseOSError(ENOENT)
inode.set_mode(mode)
@logger
def chown(self, path, uid, gid):
inode = self.get_inode(path, [S_IFDIR, S_IFREG, S_IFLNK])
if inode is None:
raise FuseOSError(ENOENT)
inode.set_uid(uid)
inode.set_gid(gid)
# need to implement to overwrite files
# Increase the size of the file
@logger
def truncate(self, path, length):
pass
@logger
def write(self, path, data, offset, fh):
filename, parent_dir = MyFs.get_filename_and_parentdir(path)
# intercept write to the restore and store files
if path in ("/restore", "/store"):
self.handle_fs_state(path, data.decode('utf-8').rstrip())
if parent_dir == "/versions":
raise FuseOSError(EPERM)
inode = self.get_inode(path, [S_IFREG, S_IFLNK])
if inode is None:
raise FuseOSError(ENOENT)
inode.set_data(data)
return len(data)
@logger
def read(self, path, size, offset, fh):
filename, parent_dir = MyFs.get_filename_and_parentdir(path)
# prevent reading of the restore and store files
if path in ("/restore", "/store") or parent_dir == "/versions":
raise FuseOSError(EPERM)
inode = self.get_inode(path, [S_IFREG, S_IFLNK])
if inode is None:
raise FuseOSError(ENOENT)
return inode.get_data()
@logger
def link(self, dst, src):
dst_filename, dst_parent_dir = MyFs.get_filename_and_parentdir(dst)
src_filename, src_parent_dir = MyFs.get_filename_and_parentdir(src)
if src in ("/restore", "/store") or src_parent_dir in ("/versions"):
raise FuseOSError(EPERM)
src_inode = self.get_inode(src, [S_IFREG])
dst_parent_inode = self.get_inode(dst_parent_dir, [S_IFDIR])
if src_inode is None or dst_parent_inode is None:
raise FuseOSError(ENONET)
# first inc link count
src_inode.inc_nlink()
# don't like this. Need to figure out a way to not break
# other code tho
dst_inode = Inode(0, dst_filename)
dst_inode.set_inode(src_inode.get_inode())
dst_parent_inode.inodes.append(dst_inode)
@logger
def unlink(self, path):
curr_filename, parent_dir = MyFs.get_filename_and_parentdir(path)
if parent_dir == "/versions" or path in ("/restore", "/store"):
raise FuseOSError(EPERM)
curr_inode = self.get_inode(path, [S_IFREG, S_IFLNK])
parent_inode = self.get_inode(parent_dir, [S_IFDIR])
if ((parent_inode is None) or (curr_inode is None)):
raise FuseOSError(ENOENT)
# first check the curr_inode link count
# delete from parent inode only if link count is 0
curr_inode.dec_nlink()
parent_inode.inodes.remove(curr_inode)
if curr_inode.get_nlink() == 0:
del curr_inode
self.invalidate_cache(path)
@logger
def rename(self, old, new):
if old in ("/restore", "/store", "/versions"):
raise FuseOSError(EPERM)
curr_inode = self.get_inode(old, [S_IFREG, S_IFDIR])
if curr_inode is None:
raise FuseOSError(ENOENT)
new_filename, parent_dir = MyFs.get_filename_and_parentdir(new)
curr_inode.set_name(new_filename)
self.invalidate_cache(old)
@logger
def symlink(self, target, source):
target_filename, target_parent = MyFs.get_filename_and_parentdir(target)
source_filename, source_parent = MyFs.get_filename_and_parentdir(target)
if source_parent == "/versions" or source in ("/restore", "/store"):
raise FuseOSError(EPERM)
target_parent_inode = self.get_inode(target_parent, [S_IFDIR])
source_inode = self.get_inode(source, [S_IFREG, S_IFLNK])
if target_parent_inode is None or source_inode is None:
return 1
target_inode = Inode(
1,
target_filename,
S_IFLNK | 0o777,
time(),
time(),
time(),
getuid(),
getgid()
)
target_parent_inode.inodes.append(target_inode)
target_inode.set_data(source)
return 0
@logger
def readlink(self, path):
link_inode = self.get_inode(path, [S_IFLNK])
if link_inode is None:
raise FuseOSError(ENOENT)
return link_inode.get_data()
@logger
def create(self, path, mode):
curr_filename, parent_dir = MyFs.get_filename_and_parentdir(path)
inode = Inode(
1,
curr_filename,
S_IFREG | 0o755,
time(),
time(),
time(),
getuid(),
getgid()
)
parent_inode = self.get_inode(parent_dir, [S_IFDIR])
if parent_inode is None:
raise FuseOSError(ENOENT)
parent_inode.inodes.append(inode)
return 0
@logger
def mkdir(self, path, mode):
curr_filename, parent_dir = MyFs.get_filename_and_parentdir(path)
if parent_dir == "/versions":
raise FuseOSError(EPERM)
inode = Inode(
1,
curr_filename,
S_IFDIR | 0o755,
time(),
time(),
time(),
getuid(),
getgid()
)
parent_inode = self.get_inode(parent_dir, [S_IFDIR])
if parent_inode is None:
raise FuseOSError(ENOENT)
parent_inode.inodes.append(inode)
return 0
@logger
def rmdir(self, path):
curr_filename, parent_dir = MyFs.get_filename_and_parentdir(path)
if path == "/versions":
raise FuseOSError(EPERM)
curr_inode = self.get_inode(path, [S_IFDIR])
parent_inode = self.get_inode(parent_dir, [S_IFDIR])
if (parent_inode is None) or (curr_inode is None):
raise FuseOSError(ENOENT)
if curr_inode.inodes:
raise FuseOSError(EPERM)
parent_inode.inodes.remove(curr_inode)
del curr_inode
self.invalidate_cache(path)
@logger
def readdir(self, path, fh):
node = self.get_inode(path, [S_IFDIR, S_IFREG, S_IFLNK])
if node is None:
return None
for node in node.inodes:
yield node.name
@logger
def getattr(self, path, fh=None):
node = self.get_inode(path, [S_IFDIR, S_IFREG, S_IFLNK])
if node is None:
raise FuseOSError(ENOENT)
return node.get_inode_info()
if __name__ == "__main__":
fs_parser = argparse.ArgumentParser()
fs_parser.add_argument('mountpoint', help="The mount point of this filesystem")
fs_parser.add_argument('backupstore', help="The mount point of the backup filesystem(Preferably a different device)")
args = fs_parser.parse_args()
fuse = FUSE(MyFs(args.backupstore), args.mountpoint, foreground=True, nothreads=True)