From 5d3418f22ca996f1621bde222eaaaa5e20a54583 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 22 Oct 2012 21:57:44 +0530 Subject: [PATCH] When changing the title/author of a book, use hard links instead of copying the file, for a large speedup. --- src/calibre/library/database2.py | 7 ++++++- src/calibre/utils/filenames.py | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index 2839f8ea6d..9fd42d0dce 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -646,7 +646,7 @@ 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 else None + wam = WindowsAtomicFolderMove(spath) if iswindows and current_path else None try: if not os.path.exists(tpath): os.makedirs(tpath) @@ -1375,6 +1375,11 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): if hasattr(dest, 'flush'): dest.flush() elif dest and not samefile(dest, path): + try: + os.link(path, dest) + return + except: + pass with lopen(path, 'rb') as f, lopen(dest, 'wb') as d: shutil.copyfileobj(f, d) diff --git a/src/calibre/utils/filenames.py b/src/calibre/utils/filenames.py index 1609088c49..ffd2ab0c14 100644 --- a/src/calibre/utils/filenames.py +++ b/src/calibre/utils/filenames.py @@ -300,6 +300,11 @@ class WindowsAtomicFolderMove(object): if handle is None: raise ValueError(u'The file %r did not exist when this move' ' operation was started'%path) + try: + win32file.CreateHardLink(dest, path) + return + except: + pass with lopen(dest, 'wb') as f: while True: hr, raw = win32file.ReadFile(handle, 1024*1024)