diff --git a/src/calibre/db/backend.py b/src/calibre/db/backend.py index abe0d12b11..21e360b68a 100644 --- a/src/calibre/db/backend.py +++ b/src/calibre/db/backend.py @@ -1669,7 +1669,7 @@ class DB(object): def move_library_to(self, all_paths, newloc, progress=lambda x: x): if not os.path.exists(newloc): os.makedirs(newloc) - old_dirs = set() + old_dirs, old_files = set(), set() items, path_map = self.get_top_level_move_items(all_paths) for x in items: src = os.path.join(self.library_path, x) @@ -1683,13 +1683,14 @@ class DB(object): if os.path.exists(dest): os.remove(dest) shutil.copyfile(src, dest) + old_files.add(src) x = path_map[x] if not isinstance(x, unicode): x = x.decode(filesystem_encoding, 'replace') progress(x) dbpath = os.path.join(newloc, os.path.basename(self.dbpath)) - opath = self.dbpath + opath, odir = self.dbpath, self.library_path self.conn.close() self.library_path, self.dbpath = newloc, dbpath if self._conn is not None: @@ -1703,8 +1704,17 @@ class DB(object): for loc in old_dirs: try: shutil.rmtree(loc) - except: + except EnvironmentError: pass + for loc in old_files: + try: + os.remove(loc) + except EnvironmentError: + pass + try: + os.rmdir(odir) + except EnvironmentError: + pass def restore_book(self, book_id, path, formats): self.execute('UPDATE books SET path=? WHERE id=?', (path.replace(os.sep, '/'), book_id)) diff --git a/src/calibre/db/tests/filesystem.py b/src/calibre/db/tests/filesystem.py index 6647aafbb7..96b28d8a80 100644 --- a/src/calibre/db/tests/filesystem.py +++ b/src/calibre/db/tests/filesystem.py @@ -108,6 +108,7 @@ class FilesystemTest(BaseTest): all_ids = cache.all_book_ids() fmt1 = cache.format(1, 'FMT1') cov = cache.cover(1) + odir = cache.backend.library_path with TemporaryDirectory('moved_lib') as tdir: cache.move_library_to(tdir) self.assertIn('moved_lib', cache.backend.library_path) @@ -117,6 +118,8 @@ class FilesystemTest(BaseTest): cache.reload_from_db() self.assertEqual(all_ids, cache.all_book_ids()) cache.backend.close() + self.assertFalse(os.path.exists(odir)) + os.mkdir(odir) # needed otherwise tearDown() fails def test_long_filenames(self): ' Test long file names '