forked from micheleberetta98/freya-fs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreyafs.py
216 lines (166 loc) · 6.89 KB
/
freyafs.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
import os
import errno
import stat
import shutil
from fuse import FuseOSError, Operations
from cache import Cache
from metadata import Metadata
def is_metadata(path=''):
return path == ".freyafs"
class FreyaFS(Operations):
def __init__(self, root, mountpoint):
self.root = root
# Retrieve FreyaFS metadata
self.metadata = Metadata(os.path.join(root, ".freyafs"))
# Keep track of open files
self.cache = Cache()
print(f"[*] FreyaFS mounted")
print(f"Now, through the FreyaFS mountpoint ({mountpoint}), you can use a Mix&Slice encrypted filesystem seemlessly.")
print(f"FreyaFS will persist your encrypted data at {root}.")
# --------------------------------------------------------------------- Helpers
def _full_path(self, partial):
partial = partial.lstrip("/")
path = os.path.join(self.root, partial)
return path
def _is_file(self, path):
if not os.path.exists(self._full_path(path)):
return False
attr = self.getattr(path)
return attr['st_mode'] & stat.S_IFREG == stat.S_IFREG
# --------------------------------------------------------------------- Filesystem methods
def access(self, path, mode):
full_path = self._full_path(path)
if not os.access(full_path, mode):
raise FuseOSError(errno.EACCES)
def chmod(self, path, mode):
full_path = self._full_path(path)
return os.chmod(full_path, mode)
def chown(self, path, uid, gid):
full_path = self._full_path(path)
return os.chown(full_path, uid, gid)
# Attributi di path (file o cartella)
def getattr(self, path, fh=None):
full_path = self._full_path(path)
st = os.lstat(full_path)
if path not in self.metadata:
return dict((key, getattr(st, key)) for key in ('st_atime', 'st_ctime',
'st_gid', 'st_mode', 'st_mtime', 'st_nlink', 'st_size', 'st_uid'))
try:
return {
'st_mode': stat.S_IFREG | (st.st_mode & ~stat.S_IFDIR),
'st_nlink': 1,
'st_atime': st.st_atime,
'st_ctime': st.st_ctime,
'st_gid': st.st_gid,
'st_mtime': st.st_mtime,
'st_size': self.metadata[path].size,
'st_uid': st.st_uid
}
except:
return dict((key, getattr(st, key)) for key in ('st_atime', 'st_ctime',
'st_gid', 'st_mode', 'st_mtime', 'st_nlink', 'st_size', 'st_uid'))
def readdir(self, path, fh):
full_path = self._full_path(path)
dirents = ['.', '..']
if os.path.isdir(full_path):
real_stuff = os.listdir(full_path)
virtual_stuff = [
x for x in real_stuff if not is_metadata(x)]
dirents.extend(virtual_stuff)
for r in dirents:
yield r
def readlink(self, path):
pathname = os.readlink(self._full_path(path))
if pathname.startswith("/"):
# Path name is absolute, sanitize it.
return os.path.relpath(pathname, self.root)
else:
return pathname
def mknod(self, path, mode, dev):
return os.mknod(self._full_path(path), mode, dev)
def rmdir(self, path):
os.rmdir(self._full_path(path))
def mkdir(self, path, mode):
os.mkdir(self._full_path(path), mode)
def statfs(self, path):
full_path = self._full_path(path)
stv = os.statvfs(full_path)
return dict((key, getattr(stv, key)) for key in ('f_bavail', 'f_bfree',
'f_blocks', 'f_bsize', 'f_favail', 'f_ffree', 'f_files', 'f_flag',
'f_frsize', 'f_namemax'))
def unlink(self, path):
full_path = self._full_path(path)
shutil.rmtree(full_path)
self.metadata.remove(path)
return
def symlink(self, name, target):
return os.symlink(name, self._full_path(target))
def rename(self, old, new):
full_old_path = self._full_path(old)
full_new_path = self._full_path(new)
if self._is_file(old):
# Rinomino un file
if self._is_file(new):
self.unlink(new)
os.rename(full_old_path, full_new_path)
if full_old_path in self.cache:
self.cache.rename(full_old_path, full_new_path)
self.metadata.rename(old, new)
else:
# Rinomino una cartella
os.rename(full_old_path, full_new_path)
self.metadata.renamedir(old, new)
def link(self, target, name):
return os.link(self._full_path(target), self._full_path(name))
def utimens(self, path, times=None):
os.utime(self._full_path(path), times)
# --------------------------------------------------------------------- File methods
def open(self, path, flags):
full_path = self._full_path(path)
info = self.metadata[path]
attr = self.getattr(path)
mtime = attr['st_mtime']
self.cache.open(full_path, info.key, info.iv, mtime)
return 0
def create(self, path, mode, fi=None):
full_path = self._full_path(path)
key, iv = self.metadata.add(path)
self.cache.create(full_path, key, iv)
return 0
def read(self, path, length, offset, fh):
full_path = self._full_path(path)
if full_path in self.cache:
return self.cache.read_bytes(full_path, offset, length)
os.lseek(fh, offset, os.SEEK_SET)
return os.read(fh, length)
def write(self, path, buf, offset, fh):
full_path = self._full_path(path)
if full_path in self.cache:
bytes_written = self.cache.write_bytes(full_path, buf, offset)
self.metadata.update(path, self.cache.get_size(full_path))
return bytes_written
os.lseek(fh, offset, os.SEEK_SET)
return os.write(fh, buf)
def truncate(self, path, length, fh=None):
full_path = self._full_path(path)
if full_path in self.cache:
self.cache.truncate_bytes(full_path, length)
self.metadata.update(path, length)
return
with open(full_path, 'r+') as f:
f.truncate(length)
def flush(self, path, fh):
full_path = self._full_path(path)
if full_path in self.cache:
info = self.metadata[path]
self.cache.flush(full_path, info.key, info.iv)
return 0
return os.fsync(fh)
def release(self, path, fh):
full_path = self._full_path(path)
if full_path in self.cache:
self.cache.release(full_path)
return 0
return os.close(fh)
def fsync(self, path, fdatasync, fh):
return self.flush(path, fh)