Also use lock when initializing db manager

This commit is contained in:
Kovid Goyal 2025-03-11 11:35:07 +05:30
parent 05bc7e966e
commit 3bbbe6257b
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -56,21 +56,22 @@ class Database:
self.dbpath = path self.dbpath = path
self.needs_copy = True self.needs_copy = True
self.use_row_factory = True self.use_row_factory = True
try: with kobo_db_lock:
connect()
self.needs_copy = False
except apsw.IOError:
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:
shutil.copyfileobj(src, dest)
wal = self.path_on_device + '-wal'
if os.path.exists(wal):
shutil.copy2(wal, dest.name + '-wal')
try: try:
connect(dest.name) connect()
except Exception: self.needs_copy = False
os.remove(dest.name) except apsw.IOError:
raise 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:
shutil.copyfileobj(src, dest)
wal = self.path_on_device + '-wal'
if os.path.exists(wal):
shutil.copy2(wal, dest.name + '-wal')
try:
connect(dest.name)
except Exception:
os.remove(dest.name)
raise
def __enter__(self) -> apsw.Connection: def __enter__(self) -> apsw.Connection:
kobo_db_lock.acquire() kobo_db_lock.acquire()