From 2bfc897266f8189a4977f700af73762565fc46fe Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 29 Oct 2012 01:52:26 +0530 Subject: [PATCH] Windows: Workaround for error while changing title/author with calibre library on a network share. Also explicitly close file handles in samefile() instead of relying on garbage collection. --- src/calibre/utils/filenames.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/calibre/utils/filenames.py b/src/calibre/utils/filenames.py index 5c31c92c33..3a327b214c 100644 --- a/src/calibre/utils/filenames.py +++ b/src/calibre/utils/filenames.py @@ -208,17 +208,22 @@ def samefile_windows(src, dst): if samestring: return True + handles = [] + def get_fileid(x): if isbytestring(x): x = x.decode(filesystem_encoding) try: h = win32file.CreateFile(x, 0, 0, None, win32file.OPEN_EXISTING, win32file.FILE_FLAG_BACKUP_SEMANTICS, 0) + handles.append(h) data = win32file.GetFileInformationByHandle(h) except (error, EnvironmentError): return None return (data[4], data[8], data[9]) a, b = get_fileid(src), get_fileid(dst) + for h in handles: + win32file.CloseHandle(h) if a is None and b is None: return False return a == b @@ -302,6 +307,8 @@ class WindowsAtomicFolderMove(object): ' operation was started'%path) try: win32file.CreateHardLink(dest, path) + if not os.path.exists(dest): + raise Exception('This apparently can happen on network shares. Sigh.') return except: pass @@ -341,6 +348,8 @@ def hardlink_file(src, dest): if iswindows: import win32file win32file.CreateHardLink(dest, src) + if not os.path.exists(dest): + raise Exception('This apparently can happen on network shares. Sigh.') return os.link(src, dest)