-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgdocsfs.py
398 lines (315 loc) · 11.4 KB
/
gdocsfs.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
#!/usr/bin/env python
from __future__ import with_statement
import os
import sys
import errno
import gdoc
from collections import defaultdict
from fuse import FUSE, FuseOSError, Operations
from stat import S_IFDIR, S_IFLNK, S_IFREG
from time import time
# Toggle debug mode
DEBUG = False
class GDocsFS(Operations):
def __init__(self, root):
self.root = root
self.files = {}
# Dict: file path -> document ID (did)
self.path_dids = {}
# Dict: file descriptor -> document ID (did)
self.fd_dids = {}
# New stuff
self.data = {}
self.fd = 3
now = time()
self.files['/'] = {}
# Each file maps to an attribute dict and a subdirs dict
self.files['/']['subdirs'] = {}
self.files['/']['attr'] = dict(
st_mode=(S_IFDIR | 0o755),
st_ctime=now,
st_mtime=now,
st_atime=now,
st_nlink=2
)
# Helpers
# =======
def _full_path(self, partial):
if partial.startswith("/"):
partial = partial[1:]
path = os.path.join(self.root, partial)
return path
# Given a path to a file, traverses down and returns the dict associated
# with the given file
def _get_file_dict(self, path, files):
assert(path != '')
# if path == '':
# return files
if path == '/':
return files['/']
if path[0] == '/':
return self._get_file_dict(path[1:], files['/']['subdirs'])
folders = path.split('/')
if len(folders) == 1:
if folders[0] not in files:
raise FuseOSError(errno.ENOENT)
return files[folders[0]]
# Construct new path with top level dir removed
path = [x for x in folders if x != '']
new_path = '/'.join(path[1:])
curr_file = path[0]
if curr_file not in files:
raise FuseOSError(errno.ENOENT)
new_files = files[curr_file]['subdirs']
return self._get_file_dict(new_path, new_files)
# Updates nlink values of every directory along path by given amount
def _update_nlinks(self, path, files, delta):
# Should not be called on root
assert(path != '/')
if path == '':
return
folders = path.split('/')
if len(folders) == 1:
if folders[0] not in files:
raise FuseOSError(errno.ENOENT)
files[folders[0]]['attr']['st_nlink'] += delta
return
# Construct new path with top level dir removed
path = [x for x in path if x != '']
new_path = '/'.join(path[1:])
curr_file = path[0]
if curr_file not in files:
raise FuseOSError(errno.ENOENT)
# Update nlink
files[curr_file]['attr']['st_nlink'] += delta
# Recurse
return self._update_nlinks(new_path, files, delta)
# Generate dict for dir
def _gen_dict(self, mode):
d = {}
d['attr'] = dict(
st_mode=(S_IFDIR | mode),
st_nlink=2,
st_size=0,
st_ctime=time(),
st_mtime=time(),
st_atime=time(),
)
d['subdirs'] = {}
return d
# Recursive mkdir
# TODO: handle errno for dirs which already exist (don't throw err for calls
# to this function from create())
def _mkdir_helper(self, path, files, mode):
# Should not create root
if path == '/':
return files
if path == '':
return files
folders = path.split('/')
if len(folders) == 1:
if folders[0] not in files:
files[folders[0]] = self._gen_dict(mode)
else:
files[folders[0]]['attr']['st_nlink'] += 1
return files
# return files
# Construct new path with top level dir removed
path = [x for x in folders if x != '']
new_path = '/'.join(path[1:])
curr_file = path[0]
if curr_file not in files:
files[curr_file] = self._gen_dict(mode)
else:
# If the dir already exists, increase nlink
files[curr_file]['attr']['st_nlink'] += 1
new_files = files[curr_file]['subdirs']
added = self._mkdir_helper(new_path, new_files, mode)
files[curr_file]['subdirs'] = added
# print('Returning', files)
return files
# Filesystem methods
# ==================
def access(self, path, mode):
return
print('access', path)
full_path = self._full_path(path)
if not os.access(full_path, mode):
raise FuseOSError(errno.EACCES)
print('done access')
def chmod(self, path, mode):
print('chmod')
file_info = self._get_file_dict(path, self.files)
file_info['attr']['st_mode'] &= 0o770000
file_info['attr']['st_mode'] |= mode
return 0
def chown(self, path, uid, gid):
print('chown')
file_info = self._get_file_dict(path, self.files)
file_info['attr']['st_uid'] = uid
file_info['attr']['st_gid'] = gid
def getattr(self, path, fh=None):
# print('getattr', path)
# print(self.files)
file_info = self._get_file_dict(path, self.files)
# if path not in self.files:
# raise FuseOSError(errno.ENOENT)
return file_info['attr']
def readdir(self, path, fh):
print('readdir', path)
file_info = self._get_file_dict(path, self.files)
return ['.', '..'] + [x for x in file_info['subdirs']]
def readlink(self, path):
print('readlink')
return self.data[path]
def rmdir(self, path):
print('rmdir')
assert(path != '/')
# with multiple level support, need to raise ENOTEMPTY if contains any files
file_info = self._get_file_dict(path, self.files)
if len(file_info['subdirs'].keys()) > 0:
raise FuseOSError(errno.ENOTEMPTY)
# Get rid of the last file in the path
path_minus_one = '/' + '/'.join([x for x in path.split('/') if x != ''][:-1])
dir_to_delete = [x for x in path.split('/') if x != ''][-1]
assert(path_minus_one != '')
file_info = self._get_file_dict(path_minus_one, self.files)
file_info['subdirs'].pop(dir_to_delete)
# Update nlinks
self.files['/']['attr']['st_nlink'] -= 1
self._update_nlinks(path_minus_one[1:], self.files, -1)
def mkdir(self, path, mode):
print('mkdir', path)
# Create the dirs
self.files['/']['subdirs'] = self._mkdir_helper(path[1:], self.files['/']['subdirs'], mode)
# Update nlink of root
self.files['/']['attr']['st_nlink'] += 1
# print(self.files)
def statfs(self, path):
print('statfs')
return dict(f_bsize=512, f_blocks=4096, f_bavail=2048)
def unlink(self, path):
print('unlink', path)
if path in self.data:
self.data.pop(path)
dirs = path.split('/')[1:]
files = self.files['/']['subdirs']
for i in dirs[:-1]:
if i != '':
files = files[i]['subdirs']
if dirs[-1] in files:
files.pop(dirs[-1])
if path in self.path_dids:
doc_id = self.path_dids[path]
gdoc.delete_doc(doc_id)
def symlink(self, target, source):
print('symlink', target, source)
self.mkdir(target, 0o777)
file_info = self._get_file_dict(target, self.files)
file_info['attr'] = dict(
st_mode=(S_IFLNK | 0o777),
st_nlink=1,
st_size=len(source)
)
self.data[target] = source
# TODO: implement rename with new updated heirarchical file structure
def rename(self, old, new):
print('rename', old, new)
if old in self.data:
self.data[new] = self.data.pop(old)
if old in self.files:
self.files[new] = self.files.pop(old)
def utimens(self, path, times=None):
print('utimens')
now = time()
atime, mtime = times if times else (now, now)
file_info = self._get_file_dict(path, self.files)
file_info['attr']['st_atime'] = atime
file_info['attr']['st_mtime'] = mtime
# File methods
# ============
def open(self, path, flags):
print('open', path, oct(flags))
self.fd += 1
doc_id = self.path_dids[path]
self.fd_dids[self.fd] = doc_id
return self.fd
def create(self, path, mode):
print('create', path)
assert(path != '/')
# Create a new document
doc_id = gdoc.create_doc(path)
# Map path -> document ID
self.path_dids[path] = doc_id
# Get path to file
folders = path.split('/')
path_minus_one = '/' + '/'.join([x for x in folders if x != ''][:-1])
file_to_create = [x for x in folders if x != ''][-1]
# Create dir till file, add files attributes
self.files['/']['subdirs'] = self._mkdir_helper(path_minus_one[1:], self.files['/']['subdirs'], mode)
assert(path_minus_one != '')
file_info = self._get_file_dict(path_minus_one, self.files)
new_file = {}
new_file['attr'] = dict(
st_mode=(S_IFREG | mode),
st_nlink=1,
st_size=0,
st_ctime=time(),
st_mtime=time(),
st_atime=time()
)
file_info['subdirs'][file_to_create] = new_file
print(self.files)
self.fd += 1
self.fd_dids[self.fd] = doc_id
return self.fd
def read(self, path, length, offset, fh):
print('read', offset, path)
if fh not in self.fd_dids:
return FuseOSError(errno.EBADF)
# Get the doc ID
doc_id = self.fd_dids[fh]
string, _ = gdoc.read_doc(doc_id, offset, length)
return string
def write(self, path, buf, offset, fh):
print('write', offset, path)
if fh not in self.fd_dids:
return FuseOSError(errno.EBADF)
# Get the doc ID
doc_id = self.fd_dids[fh]
gdoc.write_doc(doc_id, offset, buf)
assert(path != '')
file_info = self._get_file_dict(path, self.files)
file_info['attr']['st_size'] += len(buf)
return len(buf)
def truncate(self, path, length, fh=None):
print('truncate')
# Get the current content
doc_id = self.path_dids[path]
curr_content, _ = gdoc.read_doc(doc_id, 0, None)
# Make sure extending the file fills it in with zero bytes
null = bytes([0])
new_content = curr_content.ljust(length, null)
# Write the new truncated content
gdoc.write_doc(doc_id, 0, new_content)
assert(path != '')
file_info = self._get_file_dict(path, self.files)
file_info['attr']['st_size'] = length
# No buffers so no need for this function
def flush(self, path, fh):
print('flush')
return
def release(self, path, fh):
print('release')
# Remove fd -> doc ID mapping and close fd
self.fd_dids[fh] = None
def fsync(self, path, fdatasync, fh):
print('fsync')
return self.flush(path, fh)
def main(mountpoint):
# Initialize gDocs services
gdoc.initialize()
# Run filesystem
FUSE(GDocsFS(mountpoint), mountpoint, nothreads=True, foreground=True, debug=DEBUG)
if __name__ == '__main__':
main(sys.argv[1])