forked from Nassty/fusql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fusql.py
348 lines (254 loc) · 9.48 KB
/
fusql.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
from errno import *
from stat import *
import fuse
import os
import time
import fusqldb
import fusqlogger
import common
fuse.fuse_python_api = (0, 2)
class Metadata(fuse.Stat):
@fusqlogger.log()
def __init__(self, mode, isDir):
fuse.Stat.__init__(self)
if isDir:
self.st_mode = S_IFDIR | mode
self.st_nlink = 2
else:
self.st_mode = S_IFREG | mode
self.st_nlink = 1
now = int(time.time())
self.st_atime = now
self.st_mtime = now
self.st_ctime = now
self.st_uid = os.getuid()
self.st_gid = os.getgid()
self.st_size = 0
class FuSQL(fuse.Fuse):
@fusqlogger.log()
def __init__(self, *args, **kw):
fuse.Fuse.__init__(self, *args, **kw)
self.db = fusqldb.FusqlDb("test.db")
root_mode = S_IRUSR|S_IXUSR|S_IWUSR|S_IRGRP|S_IXGRP|S_IXOTH|S_IROTH
file_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH
# Create shared metadata for files and directories
self.dir_metadata = Metadata(root_mode, True)
self.file_metadata = Metadata(file_mode, False)
# Dictionary mapping inode_path -> (size, is_directory)
self.paths = []
self.paths.append('/')
# Fill with all tables as folders
for table_name in self.db.get_tables():
table_path = "/" + table_name
self.paths.append(table_path)
table_structure = self.db.get_table_structure(table_name)
# Fill with all rows as folders
for row_id in self.db.get_elements_by_field("id", table_name):
row_path = table_path + "/" + str(row_id)
self.paths.append(row_path)
for column in table_structure:
column_name = column[0]
column_type = column[1]
column_path = row_path + "/" + column_name + "." + column_type
self.paths.append(column_path)
@fusqlogger.log()
def getattr(self, path):
spath = path.split("/")
is_dir = len(spath) != 4
if path in self.paths:
if is_dir:
result = self.dir_metadata
else:
table_name = spath[1]
row_id = int(spath[2])
column_name = spath[3].rsplit(".", 1)[0]
data = self.db.get_element_data(table_name, column_name, row_id)
result = self.file_metadata
result.st_size = len(data)
else:
result = -ENOENT
return result
@fusqlogger.log()
def open(self, path, flags):
return 0
@fusqlogger.log()
def read(self, path, size, offset):
spath = path.split("/")
table_name = spath[1]
element_id = int(spath[2])
element_column = spath[3].rsplit(".", 1)[0]
data = self.db.get_element_data(table_name, element_column, element_id)
result = data[offset:offset+size]
return result
@fusqlogger.log(showReturn=True)
def mknod(self, path, mode, rdev):
spath = path.split("/")
# Files MUST be inside a table and a row
if len(spath) != 4:
return -EPERM
# Files must end in a known type
file_type = spath[3].split(".")[-1]
if file_type not in common.FILE_TYPE_TRANSLATOR.keys():
return -EPERM
table_name = spath[1]
element_id = int(spath[2])
column_name = spath[3].rsplit(".", 1)[0]
column_type = common.FILE_TYPE_TRANSLATOR[file_type]
self.db.create_column(table_name, column_name, column_type)
# TODO: fill all elements of the table
new_elements = []
for dir_name in self.paths:
if dir_name.startswith("/" + table_name + "/"):
new_column_path = dir_name + "/" + column_name + "." + file_type
new_elements.append(new_column_path)
self.paths = self.paths + new_elements
return 0
@fusqlogger.log(showReturn=True)
def write(self, path, buf, offset, fh=None):
spath = path.split("/")
table_name = spath[1]
row_id = int(spath[2])
column_name = spath[3].rsplit(".", 1)[0]
prev_data = self.db.get_element_data(table_name, column_name, row_id)
prev_size = len(prev_data)
write_size = len(buf)
if offset + write_size > prev_size:
self.truncate(path, offset + write_size)
new_data = prev_data[:offset] + buf + prev_data[offset+len(buf):]
self.db.set_element_data(table_name, column_name, row_id, new_data)
return write_size
@fusqlogger.log()
def truncate(self, path, size, fh=None):
spath = path.split("/")
table_name = spath[1]
element_id = int(spath[2])
column_name = spath[3].rsplit(".", 1)[0]
prev_data = self.db.get_element_data(table_name, column_name, element_id)
prev_size = len(prev_data)
if size > prev_size:
new_data = prev_data + (size - prev_size)*"0"
else:
new_data = prev_data[0:size]
self.db.set_element_data(table_name, column_name, element_id, new_data)
return 0
@fusqlogger.log()
def unlink(self, path):
return 0
@fusqlogger.log(showReturn=True)
def rename(self, path_from, path_to):
spath_from = path_from.split("/")
spath_to = path_to.split("/")
table_from = spath_from[1]
table_to = spath_to[1]
# Must be at the same deep
if len(spath_from) != len(spath_to):
return -EINVAL
if len(spath_from) == 3:
# If its a row
id_from = int(spath_from[2])
id_to = int(spath_to[2])
if table_from != table_to:
return -EINVAL
self.db.set_element_data(table_to, "id", id_from, id_to)
else:
# If its a table
self.db.rename_table(table_from, table_to)
for dir_name in self.paths:
if dir_name.startswith(path_from):
dir_to = dir_name.replace(path_from, path_to)
self.paths.append(dir_to)
self.paths.remove(dir_name)
return 0
@fusqlogger.log()
def chmod(self, path, mode):
return 0
@fusqlogger.log()
def chown(self, path, uid, gid):
return 0
@fusqlogger.log()
def utime(self, path, times):
return 0
@fusqlogger.log(showReturn=True)
def mkdir(self, path, mode):
spath = path.split("/")
# Folders can only be at the root (if it's a tables)
# or inside another folder (if it's a row)
if len(spath) == 2:
is_table = True
elif len(spath) == 3:
is_table = False
else:
return -EFAULT
table_name = spath[1]
table_path = "/" + table_name
if is_table:
self.db.create_table(table_name)
self.paths.append(table_path)
else:
try:
element_id = int(spath[2])
except ValueError:
return -EFAULT
self.db.create_row(table_name, element_id)
row_path = table_path + "/" + str(element_id)
self.paths.append(row_path)
table_structure = self.db.get_table_structure(table_name)
# Fill the row with the column files
for column in table_structure:
column_name = column[0]
column_type = column[1]
column_path = row_path + "/" + column_name + "." + column_type
self.paths.append(column_path)
return 0
@fusqlogger.log(showReturn=True)
def rmdir(self, path):
spath = path.split("/")
result = 0
if len(spath) == 2:
is_table = True
elif len(spath) == 3:
is_table = False
table_name = spath[1]
def remove_paths(path):
for i in self.paths:
if i.startswith(path):
self.paths.remove(i)
if is_table:
table_elements = self.db.get_all_elements(table_name)
if len(table_elements) == 0:
self.db.delete_table(table_name)
remove_paths(path)
else:
result = -ENOTEMPTY
else:
row_id = int(spath[2])
self.db.delete_table_element(table_name, row_id)
remove_paths(path)
return result
@fusqlogger.log()
def readdir(self, path, offset):
result = ['.', '..']
if path != "/":
path = path + "/"
for i in self.paths:
if i.startswith(path) and i != "/":
name = i.split(path)[1]
name = name.split("/")[0]
if name not in result:
result.append(name)
for i in result:
yield fuse.Direntry(i)
@fusqlogger.log()
def release(self, path, fh=None):
return 0
if __name__ == '__main__':
fs = FuSQL()
fs.parse(errex=1)
fs.main()