From 18bc6081a9a5bdaba6e7f41f1da1b622749f1bb3 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 9 Mar 2025 10:19:17 +0530 Subject: [PATCH] Actually get kobo db IOError workaround working Tested on my Linux system by injecting a fake IOError --- src/calibre/devices/kobo/db.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/calibre/devices/kobo/db.py b/src/calibre/devices/kobo/db.py index 840170415a..d7d40312d7 100644 --- a/src/calibre/devices/kobo/db.py +++ b/src/calibre/devices/kobo/db.py @@ -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: