diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index 05f5cdec74..4abc851f44 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -646,12 +646,13 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): spath = os.path.join(self.library_path, *current_path.split('/')) tpath = os.path.join(self.library_path, *path.split('/')) - wam = WindowsAtomicFolderMove(spath) if iswindows and current_path else None + source_ok = current_path and os.path.exists(spath) + wam = WindowsAtomicFolderMove(spath) if iswindows and source_ok else None try: if not os.path.exists(tpath): os.makedirs(tpath) - if current_path and os.path.exists(spath): # Migrate existing files + if source_ok: # Migrate existing files self.copy_cover_to(id, os.path.join(tpath, 'cover.jpg'), index_is_id=True, windows_atomic_move=wam, use_hardlink=True) @@ -669,7 +670,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): self.conn.commit() self.data.set(id, self.FIELD_MAP['path'], path, row_is_id=True) # Delete not needed directories - if current_path and os.path.exists(spath): + if source_ok: if not samefile(spath, tpath): if wam is not None: wam.delete_originals() diff --git a/src/calibre/utils/filenames.py b/src/calibre/utils/filenames.py index b49c039ffe..5c31c92c33 100644 --- a/src/calibre/utils/filenames.py +++ b/src/calibre/utils/filenames.py @@ -314,6 +314,17 @@ class WindowsAtomicFolderMove(object): break f.write(raw) + def release_file(self, path): + key = None + for p, h in self.handle_map.iteritems(): + if samefile_windows(path, p): + key = (p, h) + break + if key is not None: + import win32file + win32file.CloseHandle(key[1]) + self.handle_map.pop(key[0]) + def close_handles(self): import win32file for h in self.handle_map.itervalues():