From b55ef85fd78e0032f3cd37b59f59e53f490438b6 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 4 Oct 2019 15:20:58 +0530 Subject: [PATCH] Possible workaround for some windows machines where the viewer is getting access denied errors while renaming a directory --- src/calibre/gui2/viewer/convert_book.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/calibre/gui2/viewer/convert_book.py b/src/calibre/gui2/viewer/convert_book.py index 2b53e09653..85d756ff27 100644 --- a/src/calibre/gui2/viewer/convert_book.py +++ b/src/calibre/gui2/viewer/convert_book.py @@ -59,6 +59,17 @@ def robust_rmtree(x): return False +def robust_rename(a, b): + retries = 10 if iswindows else 1 # retry on windows to get around the idiotic mandatory file locking + for i in range(retries): + try: + os.rename(a, b) + return True + except EnvironmentError: + time.sleep(0.1) + return False + + def clear_temp(temp_path): now = time.time() for x in os.listdir(temp_path): @@ -159,7 +170,12 @@ def prepare_book(path, convert_func=do_convert, max_age=30 * DAY, force=False): entries = metadata['entries'] instances = entries.setdefault(key, []) os.rmdir(ans) - os.rename(src_path, ans) + if not robust_rename(src_path, ans): + raise Exception(( + 'Failed to rename: "{}" to "{}" probably some software such as an antivirus or file sync program' + ' running on your computer has locked the files' + ).format(src_path, ans)) + instance['status'] = 'finished' for q in instances: if q['id'] == instance['id']: