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.

This commit is contained in:
Kovid Goyal 2012-10-29 01:52:26 +05:30
parent faea33fc56
commit 2bfc897266

View File

@ -208,17 +208,22 @@ def samefile_windows(src, dst):
if samestring: if samestring:
return True return True
handles = []
def get_fileid(x): def get_fileid(x):
if isbytestring(x): x = x.decode(filesystem_encoding) if isbytestring(x): x = x.decode(filesystem_encoding)
try: try:
h = win32file.CreateFile(x, 0, 0, None, win32file.OPEN_EXISTING, h = win32file.CreateFile(x, 0, 0, None, win32file.OPEN_EXISTING,
win32file.FILE_FLAG_BACKUP_SEMANTICS, 0) win32file.FILE_FLAG_BACKUP_SEMANTICS, 0)
handles.append(h)
data = win32file.GetFileInformationByHandle(h) data = win32file.GetFileInformationByHandle(h)
except (error, EnvironmentError): except (error, EnvironmentError):
return None return None
return (data[4], data[8], data[9]) return (data[4], data[8], data[9])
a, b = get_fileid(src), get_fileid(dst) a, b = get_fileid(src), get_fileid(dst)
for h in handles:
win32file.CloseHandle(h)
if a is None and b is None: if a is None and b is None:
return False return False
return a == b return a == b
@ -302,6 +307,8 @@ class WindowsAtomicFolderMove(object):
' operation was started'%path) ' operation was started'%path)
try: try:
win32file.CreateHardLink(dest, path) win32file.CreateHardLink(dest, path)
if not os.path.exists(dest):
raise Exception('This apparently can happen on network shares. Sigh.')
return return
except: except:
pass pass
@ -341,6 +348,8 @@ def hardlink_file(src, dest):
if iswindows: if iswindows:
import win32file import win32file
win32file.CreateHardLink(dest, src) win32file.CreateHardLink(dest, src)
if not os.path.exists(dest):
raise Exception('This apparently can happen on network shares. Sigh.')
return return
os.link(src, dest) os.link(src, dest)