From c2246fdd5a63f0a4ff39ec3e71d2fdf85a938764 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 23 Mar 2014 11:58:41 +0530 Subject: [PATCH] When restoring a db on windows if renaming the old db fails because some other process has locked the file, wait a little and try again --- src/calibre/db/restore.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/calibre/db/restore.py b/src/calibre/db/restore.py index 3f513de100..c26f4dfe70 100644 --- a/src/calibre/db/restore.py +++ b/src/calibre/db/restore.py @@ -5,7 +5,7 @@ __license__ = 'GPL v3' __copyright__ = '2010, Kovid Goyal ' __docformat__ = 'restructuredtext en' -import re, os, traceback, shutil +import re, os, traceback, shutil, time from threading import Thread from operator import itemgetter @@ -269,7 +269,14 @@ class Restore(Thread): save_path = self.olddb = os.path.splitext(dbpath)[0]+'_pre_restore.db' if os.path.exists(save_path): os.remove(save_path) - os.rename(dbpath, save_path) + try: + os.rename(dbpath, save_path) + except OSError as err: + if getattr(err, 'winerror', None) == 32: # ERROR_SHARING_VIOLATION + time.sleep(4) # Wait a little for dropbox or the antivirus or whatever to release the file + os.rename(dbpath, save_path) + else: + raise shutil.copyfile(ndbpath, dbpath)