Cleanup wal file copying a bit more

This commit is contained in:
Kovid Goyal 2025-03-20 08:29:52 +05:30
parent 9b0a0312c4
commit 341d414043
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -21,6 +21,10 @@ def row_factory(cursor: apsw.Cursor, row):
return {k[0]: row[i] for i, k in enumerate(cursor.getdescription())} return {k[0]: row[i] for i, k in enumerate(cursor.getdescription())}
def wal_path(path: str) -> str:
return path + '-wal'
def copy_db(conn: apsw.Connection, dest_path: str): def copy_db(conn: apsw.Connection, dest_path: str):
with suppress(AttributeError): # need a new enough version of apsw with suppress(AttributeError): # need a new enough version of apsw
conn.cache_flush() conn.cache_flush()
@ -33,9 +37,12 @@ def copy_db(conn: apsw.Connection, dest_path: str):
with suppress(apsw.BusyError, apsw.LockedError): with suppress(apsw.BusyError, apsw.LockedError):
b.step() b.step()
shutil.move(tempdb, dest_path) shutil.move(tempdb, dest_path)
dest_wal = dest_path + '-wal' twal, dwal = wal_path(tempdb), wal_path(dest_path)
if os.path.exists(dest_wal): # truncate WAL file to zero if os.path.exists(twal): # this should never happen as sqlite is supposed to delete -wal file when last connection to db is closed
open(dest_wal, 'w').close() shutil.move(twal, dwal)
else:
with suppress(FileNotFoundError):
os.remove(dwal)
class Database: class Database:
@ -65,9 +72,9 @@ class Database:
debug_print(f'Kobo: I/O error connecting to {self.path_on_device} copying it into temporary storage and connecting there') debug_print(f'Kobo: I/O error connecting to {self.path_on_device} copying it into temporary storage and connecting there')
with open(self.path_on_device, 'rb') as src, PersistentTemporaryFile(suffix='-kobo-db.sqlite') as dest: with open(self.path_on_device, 'rb') as src, PersistentTemporaryFile(suffix='-kobo-db.sqlite') as dest:
shutil.copyfileobj(src, dest) shutil.copyfileobj(src, dest)
wal = self.path_on_device + '-wal' wal = wal_path(self.path_on_device)
if os.path.exists(wal): if os.path.exists(wal):
shutil.copy2(wal, dest.name + '-wal') shutil.copy2(wal, wal_path(dest.name))
try: try:
connect(dest.name) connect(dest.name)
except Exception: except Exception: