-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecsbxfs.py
executable file
·239 lines (188 loc) · 7.83 KB
/
ecsbxfs.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
#!/usr/bin/env python
# Based on the following resources
# - https://medium.com/the-python-corner/writing-a-fuse-filesystem-in-python-5e0f2de3a813
# - https://gist.github.com/mastro35/0c87b3b96278ef1bd0a6401ff552195e#file-fuse-stavros-py
# - https://gist.github.com/mastro35/9ae0e4f4bbe6bda0c540986cb9f7c47c#file-dfs-py
# - https://github.com/ttsiodras/rsbep-backup/blob/master/contrib/poorZFS.py
from __future__ import with_statement
import os
import sys
import errno
import hashlib
import json
from datetime import datetime
from subprocess import check_output
from fuse import FUSE, FuseOSError, Operations
def time_now_str():
return datetime.now().strftime("%Y-%m-%d %H:%M")
def log_action(action, file):
print("{:<10} {:<30} [{}]".format(action, file, time_now_str()))
class SBXContained(Operations):
def __init__(self, root):
self.root = root
# Helpers
# =======
def _full_path(self, partial):
if partial.startswith("/"):
partial = partial[1:]
path = os.path.join(self.root, partial)
return path
def _cont_path(self, partial):
return self._full_path(partial) + ".sbxfs.sbx"
def _cont_exists(self, partial):
return os.path.exists(self._cont_path(partial))
def _contain(self, partial):
full_path = self._full_path(partial)
cont_path = self._cont_path(partial)
log_action("Encoding", partial)
check_output("blkar encode --force --sbx-version 17 --rs-data 10 --rs-parity 2 --burst 10 --pv 0 {} {}".format(full_path, cont_path), shell=True)
def _file_hash(self, partial):
BUF_SIZE = 65536
sha256 = hashlib.sha256()
with open(self._full_path(partial), 'rb') as f:
while True:
data = f.read(BUF_SIZE)
if not data:
break
sha256.update(data)
return sha256.hexdigest()
def _repair(self, partial):
if self._cont_exists(partial):
full_path = self._full_path(partial)
cont_path = self._cont_path(partial)
log_action("Opening", partial)
output = json.loads(check_output('blkar show --json '+cont_path, shell=True))
cont_hash = output["blocks"][0]["hash"].split()[2]
file_hash = self._file_hash(partial)
if cont_hash != file_hash:
print(" File corruption detected, checking container integrity")
output = json.loads(check_output('blkar repair --json '+cont_path, shell=True))
if "stats" not in output:
print(" Container cannot be repaired")
return errno.EACCES
failed_check = int(output["stats"]["numberOfBlocksFailedCheck"])
failed_to_repair = int(output["stats"]["numberOfBlocksFailedToRepairData"])
if failed_check == 0:
print(" Container does not require repair")
else:
print(" Container requires repair")
if failed_to_repair == 0:
print(" Repair successful")
else:
print(" Failed to repair container")
return errno.EACCES
print(" Replacing file with container data")
check_output('blkar decode --force --pv 0 {} {}'.format(cont_path, full_path), shell=True)
# 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)
os.chmod(full_path, mode)
if self._cont_exists(path):
cont_path = self._cont_path(path)
os.chmod(cont_path, mode)
def chown(self, path, uid, gid):
full_path = self._full_path(path)
os.chown(full_path, uid, gid)
if self._cont_exists(path):
cont_path = self._cont_path(path)
os.chmod(cont_path, mode)
def getattr(self, path, fh=None):
full_path = self._full_path(path)
st = os.lstat(full_path)
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):
dirents.extend(os.listdir(full_path))
for r in dirents:
if not r.endswith(".ecsbxfs.ecsbx"):
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):
full_path = self._full_path(path)
return os.rmdir(full_path)
def mkdir(self, path, mode):
return 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):
if self._cont_exists(path):
ret = os.unlink(self._cont_path(path))
if ret != None:
return ret
return os.unlink(self._full_path(path))
def symlink(self, name, target):
if self._cont_exists(path):
ret = os.symlink(target+".sbxfs.sbx", self._cont_path(path))
if ret != None:
return ret
return os.symlink(target, self._full_path(name))
def rename(self, old, new):
if self._cont_exists(old):
ret = os.rename(self._cont_path(old), self_cont_path(new))
if ret != None:
return ret
return os.rename(self._full_path(old), self._full_path(new))
def link(self, target, name):
if self._cont_exists(old):
ret = os.link(self._cont_path(name), self_cont_path(target))
if ret != None:
return ret
return os.link(self._full_path(name), self._full_path(target))
def utimens(self, path, times=None):
return os.utime(self._full_path(path), times)
# File methods
# ============
def open(self, path, flags):
ret = self._repair(path)
if ret != None:
return ret
full_path = self._full_path(path)
return os.open(full_path, flags)
def create(self, path, mode, fi=None):
full_path = self._full_path(path)
return os.open(full_path, os.O_WRONLY | os.O_CREAT, mode)
def read(self, path, length, offset, fh):
os.lseek(fh, offset, os.SEEK_SET)
return os.read(fh, length)
def write(self, path, buf, offset, fh):
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)
with open(full_path, 'r+') as f:
f.truncate(length)
self._contain(path)
def flush(self, path, fh):
return os.fsync(fh)
def release(self, path, fh):
os.close(fh)
self._contain(path)
def fsync(self, path, fdatasync, fh):
return self.flush(path, fh)
def main(root, mountpoint):
FUSE(SBXContained(root), mountpoint, nothreads=True, foreground=True)
if __name__ == '__main__':
if len(sys.argv) < 3:
print("Usage : ecsbxfs.py container_dir mount_point")
sys.exit()
main(sys.argv[1], sys.argv[2])