Browse Source

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
master
ThomasV 2 years ago
parent
commit
c495445f51
  1. 8
      electrum/json_db.py
  2. 16
      electrum/storage.py

8
electrum/json_db.py

@ -381,10 +381,12 @@ class JsonDB(Logger):
@locked
def write(self):
if self.storage.file_exists() and not self.storage.is_encrypted():
self._append_pending_changes()
else:
if not self.storage.file_exists()\
or self.storage.is_encrypted()\
or self.storage.needs_consolidation():
self._write()
else:
self._append_pending_changes()
@locked
def _append_pending_changes(self):

16
electrum/storage.py

@ -71,10 +71,14 @@ class WalletStorage(Logger):
if self.file_exists():
with open(self.path, "r", encoding='utf-8') as f:
self.raw = f.read()
self.pos = f.seek(0, os.SEEK_END)
self.init_pos = self.pos
self._encryption_version = self._init_encryption_version()
else:
self.raw = ''
self._encryption_version = StorageEncryptionVersion.PLAINTEXT
self.pos = 0
self.init_pos = 0
def read(self):
return self.decrypted if self.is_encrypted() else self.raw
@ -83,7 +87,7 @@ class WalletStorage(Logger):
s = self.encrypt_before_writing(data)
temp_path = "%s.tmp.%s" % (self.path, os.getpid())
with open(temp_path, "w", encoding='utf-8') as f:
f.write(s)
self.pos = f.write(s)
f.flush()
os.fsync(f.fileno())
try:
@ -101,12 +105,16 @@ class WalletStorage(Logger):
def append(self, data: str) -> None:
""" append data to file. for the moment, only non-encrypted file"""
assert not self.is_encrypted()
with open(self.path, "r+") as f:
f.seek(0, os.SEEK_END)
f.write(data)
with open(self.path, "r+", encoding='utf-8') as f:
pos = f.seek(0, os.SEEK_END)
assert pos == self.pos, (self.pos, pos)
self.pos += f.write(data)
f.flush()
os.fsync(f.fileno())
def needs_consolidation(self):
return self.pos > 2 * self.init_pos
def file_exists(self) -> bool:
return self._file_exists

Loading…
Cancel
Save