Skip to content

Commit ef735fd

Browse files
committed
fix: copy the backup db if it can't be moved
On NixOS files in addon_data cannot be moved. This handles the error and tries to copy the db as second chance before giving up. really fixes #337
1 parent 84959db commit ef735fd

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

resources/lib/common.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import xbmc,xbmcgui,xbmcaddon,xbmcvfs,json,os
1+
import xbmc,xbmcgui,xbmcaddon,xbmcvfs,json,os,shutil
22
from pathlib import Path
33
from urllib.parse import urlencode
44
from datetime import datetime as dt
@@ -266,7 +266,8 @@ def check_db(self):
266266
new_matches = []
267267
no_matches = []
268268
xbmc.log(msg='IAGL: Extracted zipped db with version {} to path {}'.format(self.config.addon.get('version'),str(self.config.files.get('db').parent)),level=xbmc.LOGDEBUG)
269-
self.config.files.get('addon_data_db_zipped').rename(self.config.files.get('db_zipped_backup')) #Rename new zipped db to backup
269+
#Rename or copy new zipped db to backup
270+
self._move_or_copy_zipped_db()
270271
if old_uids is not None:
271272
new_uids = db.query_db(db.get_query('get_all_uids_in_new_db',old_uids=','.join(['"{}"'.format(x) for x in old_uids])),return_as='dict')
272273
no_match_uids = [x for x in old_uids if x not in [y.get('uid') for y in new_uids if isinstance(y.get('uid'),str)]]
@@ -380,7 +381,7 @@ def check_db(self):
380381
selected = xbmcgui.Dialog().select(heading=self.get_loc(30373),list=[self.get_loc(30377),self.get_loc(30378)],useDetails=False)
381382
if selected == 1:
382383
xbmc.log(msg='IAGL: User requested not to be asked about update again. Moving new db to backup.',level=xbmc.LOGDEBUG)
383-
self.config.files.get('addon_data_db_zipped').rename(self.config.files.get('db_zipped_backup'))
384+
self._move_or_copy_zipped_db()
384385
else:
385386
xbmc.log(msg='IAGL: User will be asked about update again later...',level=xbmc.LOGDEBUG)
386387
else:
@@ -394,7 +395,7 @@ def check_db(self):
394395
if result:
395396
xbmcaddon.Addon(id=self.config.addon.get('addon_name')).setSetting(id='db_version',value=self.config.addon.get('version'))
396397
xbmc.log(msg='IAGL: Extracted zipped db with version {} to path {}'.format(self.config.addon.get('version'),str(self.config.files.get('db').parent)),level=xbmc.LOGDEBUG)
397-
self.config.files.get('addon_data_db_zipped').rename(self.config.files.get('db_zipped_backup'))
398+
self._move_or_copy_zipped_db()
398399
else:
399400
xbmc.log(msg='IAGL: Error extracting addon db: {}'.format(self.config.files.get('addon_data_db_zipped')),level=xbmc.LOGERROR)
400401
else:
@@ -404,6 +405,15 @@ def check_db(self):
404405
xbmc.log(msg='IAGL: addon database file not found, unable to restore from backup.',level=xbmc.LOGERROR)
405406
return result
406407

408+
def _move_or_copy_zipped_db(self):
409+
try:
410+
self.config.files.get('addon_data_db_zipped').rename(self.config.files.get('db_zipped_backup'))
411+
except Exception:
412+
try:
413+
shutil.copy(self.config.files.get('addon_data_db_zipped'),self.config.files.get('db_zipped_backup'))
414+
except Exception as e:
415+
xbmc.log(msg='IAGL: Error creating a backup of the db: {}'.format(e),level=xbmc.LOGERROR)
416+
407417
def reset_db(self):
408418
result = False
409419
xbmc.log(msg='IAGL: userdata db reset requested',level=xbmc.LOGDEBUG)

0 commit comments

Comments
 (0)