From f59c8d8a47ee93c4bdb05c04f08ac116874fdb90 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 23 Mar 2011 12:07:46 -0600 Subject: [PATCH] When adding formats do not corrupt the added file if the user tries to add an existing format to itself --- src/calibre/library/database2.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index b23c8ff4a4..0cae7be2f8 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -1121,8 +1121,10 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): pdir = os.path.dirname(dest) if not os.path.exists(pdir): os.makedirs(pdir) - with lopen(dest, 'wb') as f: - shutil.copyfileobj(stream, f) + if not getattr(stream, 'name', False) or \ + os.path.abspath(dest) != os.path.abspath(stream.name): + with lopen(dest, 'wb') as f: + shutil.copyfileobj(stream, f) stream.seek(0, 2) size=stream.tell() self.conn.execute('INSERT INTO data (book,format,uncompressed_size,name) VALUES (?,?,?,?)',