-
Notifications
You must be signed in to change notification settings - Fork 1
/
encfilesmanager.py
170 lines (127 loc) · 5.18 KB
/
encfilesmanager.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
import os
import threading
from aesmix import MixSlice
from time import time
from filebytecontent import FileByteContent
LOCK = threading.Lock()
class EncFilesManager():
def __init__(self, key=None, iv=None):
self.key = key if key is not None else b'K' * 16
self.iv = iv if iv is not None else b'I' * 16
self.open_files = {}
self.open_counters = {}
self.touched_files = {}
self.public_metafiles = {}
self.private_metafiles = {}
self.atimes = {}
self.mtimes = {}
def __contains__(self, path):
return path in self.open_files
# ------------------------------------------------------ Helpers
def _decrypt(self, path):
public_metafiles = self.public_metafiles[path]
reader = MixSlice.load_from_file(path, public_metafiles)
return reader.decrypt()
def _encrypt(self, path):
plaintext = self.open_files[path].read_all()
public_metafile = self.public_metafiles[path]
private_metafile = self.private_metafiles[path]
owner = MixSlice.encrypt(plaintext, self.key, self.iv)
owner.save_to_files(path, public_metafile, private_metafile)
# ------------------------------------------------------ Methods
def open(self, path, public_metafile_path, private_metafile_path, mtime):
with LOCK:
if path in self.open_files:
self.open_counters[path] += 1
return
self.public_metafiles[path] = public_metafile_path
self.private_metafiles[path] = private_metafile_path
self.open_files[path] = FileByteContent(self._decrypt(path))
self.open_counters[path] = 1
self.touched_files[path] = False
self.atimes[path] = int(time())
self.mtimes[path] = mtime
def create(self, path, public_metafile_path, private_metafile_path):
with LOCK:
if path not in self.open_files:
self.public_metafiles[path] = public_metafile_path
self.private_metafiles[path] = private_metafile_path
self.open_files[path] = FileByteContent(b'')
self.open_counters[path] = 1
self.atimes[path] = int(time())
self.mtimes[path] = self.atimes[path]
else:
self.open_counters[path] += 1
self.touched_files[path] = True
self.flush(path)
def read_bytes(self, path, offset, length):
with LOCK:
if path not in self.open_files:
return None
return self.open_files[path].read_bytes(offset, length)
def write_bytes(self, path, buf, offset):
with LOCK:
if path not in self.open_files:
return 0
bytes_written = self.open_files[path].write_bytes(buf, offset)
self.touched_files[path] = True
self.mtimes[path] = int(time())
return bytes_written
def truncate_bytes(self, path, length):
with LOCK:
if path not in self.open_files:
return
self.open_files[path].truncate(length)
self.touched_files[path] = True
self.mtimes[path] = int(time())
def flush(self, path):
with LOCK:
if path not in self.open_files:
return
file_already_exists = os.path.exists(path)
if file_already_exists:
os.utime(path, (self.atimes[path], self.mtimes[path]))
with LOCK:
if not self.touched_files[path]:
return
self.touched_files[path] = False
self._encrypt(path)
if not file_already_exists:
os.utime(path, (self.atimes[path], self.mtimes[path]))
def release(self, path):
with LOCK:
if path not in self.open_files:
return
self.open_counters[path] -= 1
if self.open_counters[path] > 0:
return
del self.open_files[path]
del self.open_counters[path]
del self.touched_files[path]
del self.public_metafiles[path]
del self.private_metafiles[path]
del self.atimes[path]
del self.mtimes[path]
def cur_size(self, path):
with LOCK:
if path not in self.open_files:
return 0
return len(self.open_files[path])
def rename(self, old, new):
with LOCK:
if old not in self.open_files:
return
self.open_files[new] = self.open_files[old]
self.open_counters[new] = self.open_counters[old]
self.touched_files[new] = self.touched_files[old]
self.public_metafiles[new] = self.public_metafiles[old]
self.private_metafiles[new] = self.private_metafiles[old]
self.atimes[new] = self.atimes[old]
self.mtimes[new] = self.mtimes[old]
del self.open_files[old]
del self.open_counters[old]
del self.touched_files[old]
del self.public_metafiles[old]
del self.private_metafiles[old]
del self.atimes[old]
del self.mtimes[old]