Actually get kobo db IOError workaround working

Tested on my Linux system by injecting a fake IOError
This commit is contained in:
Kovid Goyal 2025-03-09 10:19:17 +05:30
parent eda6dcb7e3
commit 18bc6081a9
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -8,7 +8,7 @@ from contextlib import closing, suppress
import apsw
from calibre.prints import debug_print
from calibre.ptempfile import PersistentTemporaryFile
from calibre.ptempfile import PersistentTemporaryFile, TemporaryDirectory
def row_factory(cursor, row):
@ -16,19 +16,15 @@ def row_factory(cursor, row):
def copy_db(conn: apsw.Connection, dest_path: str):
conn.cache_flush()
with PersistentTemporaryFile() as f:
needs_remove = True
try:
with closing(apsw.Connection(f.name)) as dest, conn.backup('main', dest, 'main') as b:
with suppress(AttributeError): # need a new enough version of apsw
conn.cache_flush()
with TemporaryDirectory() as tdir:
tempdb = os.path.join(tdir, 'temp.sqlite')
with closing(apsw.Connection(tempdb)) as dest, dest.backup('main', conn, 'main') as b:
while not b.done:
b.step()
shutil.move(f.name, dest_path)
needs_remove = False
finally:
if needs_remove:
with suppress(OSError):
os.remove(f.name)
with suppress(apsw.BusyError, apsw.LockedError):
b.step()
shutil.move(tempdb, dest_path)
class Database: