diff options
author | Victor Shyba <victor.shyba@gmail.com> | 2014-12-15 20:24:02 -0300 |
---|---|---|
committer | Victor Shyba <victor.shyba@gmail.com> | 2014-12-15 20:24:02 -0300 |
commit | 1f54059cf3661a8df6b484806270790b5ccfe396 (patch) | |
tree | a360bbd8d373e67350bf2c8df7b0d7fb685520ef | |
parent | 4d2565e927717c8b27b0a620c1c6a47a80cca2aa (diff) |
related to #194 - solves a concurrency problem on encrypted whoosh caused by unnecessary writes on close callback.
-rw-r--r-- | service/pixelated/support/encrypted_file_storage.py | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/service/pixelated/support/encrypted_file_storage.py b/service/pixelated/support/encrypted_file_storage.py index c6549c06..5661b5e5 100644 --- a/service/pixelated/support/encrypted_file_storage.py +++ b/service/pixelated/support/encrypted_file_storage.py @@ -15,6 +15,7 @@ # along with Pixelated. If not, see <http://www.gnu.org/licenses/>. import io +from hashlib import sha512 import os from whoosh.filedb.filestore import FileStorage @@ -45,13 +46,16 @@ class EncryptedFileStorage(FileStorage): return EncryptedFileStorage(path, self.masterkey).create() def file_length(self, name): - return self.length_cache[name] + return self.length_cache[name][0] def _encrypt_index_on_close(self, name): def wrapper(struct_file): struct_file.seek(0) content = struct_file.file.read() - self.length_cache[name] = len(content) + file_hash = sha512(content).digest() + if name in self.length_cache and file_hash == self.length_cache[name][1]: + return + self.length_cache[name] = (len(content), file_hash) encrypted_content = self.f.encrypt(content) with open(self._fpath(name), 'w+b') as f: f.write(encrypted_content) @@ -60,5 +64,5 @@ class EncryptedFileStorage(FileStorage): def _open_encrypted_file(self, name, onclose=lambda x: None): file_content = open(self._fpath(name), "rb").read() decrypted = self.f.decrypt(file_content) - self.length_cache[name] = len(decrypted) + self.length_cache[name] = (len(decrypted), sha512(decrypted).digest()) return BufferFile(buffer(decrypted), name=name, onclose=onclose) |