storage: remember file length when writing to file
- make append fail if the actual file length differs - consolidate when file length > 2 * last consolidated length
This commit is contained in:
@@ -381,10 +381,12 @@ class JsonDB(Logger):
|
|||||||
|
|
||||||
@locked
|
@locked
|
||||||
def write(self):
|
def write(self):
|
||||||
if self.storage.file_exists() and not self.storage.is_encrypted():
|
if not self.storage.file_exists()\
|
||||||
self._append_pending_changes()
|
or self.storage.is_encrypted()\
|
||||||
else:
|
or self.storage.needs_consolidation():
|
||||||
self._write()
|
self._write()
|
||||||
|
else:
|
||||||
|
self._append_pending_changes()
|
||||||
|
|
||||||
@locked
|
@locked
|
||||||
def _append_pending_changes(self):
|
def _append_pending_changes(self):
|
||||||
|
|||||||
@@ -71,10 +71,14 @@ class WalletStorage(Logger):
|
|||||||
if self.file_exists():
|
if self.file_exists():
|
||||||
with open(self.path, "r", encoding='utf-8') as f:
|
with open(self.path, "r", encoding='utf-8') as f:
|
||||||
self.raw = f.read()
|
self.raw = f.read()
|
||||||
|
self.pos = f.seek(0, os.SEEK_END)
|
||||||
|
self.init_pos = self.pos
|
||||||
self._encryption_version = self._init_encryption_version()
|
self._encryption_version = self._init_encryption_version()
|
||||||
else:
|
else:
|
||||||
self.raw = ''
|
self.raw = ''
|
||||||
self._encryption_version = StorageEncryptionVersion.PLAINTEXT
|
self._encryption_version = StorageEncryptionVersion.PLAINTEXT
|
||||||
|
self.pos = 0
|
||||||
|
self.init_pos = 0
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
return self.decrypted if self.is_encrypted() else self.raw
|
return self.decrypted if self.is_encrypted() else self.raw
|
||||||
@@ -83,7 +87,7 @@ class WalletStorage(Logger):
|
|||||||
s = self.encrypt_before_writing(data)
|
s = self.encrypt_before_writing(data)
|
||||||
temp_path = "%s.tmp.%s" % (self.path, os.getpid())
|
temp_path = "%s.tmp.%s" % (self.path, os.getpid())
|
||||||
with open(temp_path, "w", encoding='utf-8') as f:
|
with open(temp_path, "w", encoding='utf-8') as f:
|
||||||
f.write(s)
|
self.pos = f.write(s)
|
||||||
f.flush()
|
f.flush()
|
||||||
os.fsync(f.fileno())
|
os.fsync(f.fileno())
|
||||||
try:
|
try:
|
||||||
@@ -101,12 +105,16 @@ class WalletStorage(Logger):
|
|||||||
def append(self, data: str) -> None:
|
def append(self, data: str) -> None:
|
||||||
""" append data to file. for the moment, only non-encrypted file"""
|
""" append data to file. for the moment, only non-encrypted file"""
|
||||||
assert not self.is_encrypted()
|
assert not self.is_encrypted()
|
||||||
with open(self.path, "r+") as f:
|
with open(self.path, "r+", encoding='utf-8') as f:
|
||||||
f.seek(0, os.SEEK_END)
|
pos = f.seek(0, os.SEEK_END)
|
||||||
f.write(data)
|
assert pos == self.pos, (self.pos, pos)
|
||||||
|
self.pos += f.write(data)
|
||||||
f.flush()
|
f.flush()
|
||||||
os.fsync(f.fileno())
|
os.fsync(f.fileno())
|
||||||
|
|
||||||
|
def needs_consolidation(self):
|
||||||
|
return self.pos > 2 * self.init_pos
|
||||||
|
|
||||||
def file_exists(self) -> bool:
|
def file_exists(self) -> bool:
|
||||||
return self._file_exists
|
return self._file_exists
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user