From ca206053ac8b467d8d2a08ed27d6b69fb087e7a2 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Mon, 26 Aug 2013 14:22:32 -0700 Subject: [PATCH] fix import deletion race By moving the duplicate file removal to the manipulate_files coroutine, we ensure that all previous albums are fully moved/copied before trying to delete their duplicate files. --- beets/importer.py | 18 +++++++++++++----- docs/changelog.rst | 5 +++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/beets/importer.py b/beets/importer.py index a5371fabf..a42d2760a 100644 --- a/beets/importer.py +++ b/beets/importer.py @@ -768,13 +768,12 @@ def apply_choices(session): # Delete duplicate files that are located inside the library # directory. + task.duplicate_paths = [] for duplicate_path in [i.path for i in duplicate_items]: if session.lib.directory in util.ancestry(duplicate_path): - log.debug(u'deleting replaced duplicate %s' % - util.displayable_path(duplicate_path)) - util.remove(duplicate_path) - util.prune_dirs(os.path.dirname(duplicate_path), - session.lib.directory) + # Mark the path for deletion in the manipulate_files + # stage. + task.duplicate_paths.append(duplicate_path) # Add items -- before path changes -- to the library. We add the # items now (rather than at the end) so that album structures @@ -823,6 +822,15 @@ def manipulate_files(session): if task.should_skip(): continue + # Remove duplicate files marked for deletion. + if task.remove_duplicates: + for duplicate_path in task.duplicate_paths: + log.debug(u'deleting replaced duplicate %s' % + util.displayable_path(duplicate_path)) + util.remove(duplicate_path) + util.prune_dirs(os.path.dirname(duplicate_path), + session.lib.directory) + # Move/copy/write files. items = task.imported_items() task.old_paths = [item.path for item in items] # For deletion. diff --git a/docs/changelog.rst b/docs/changelog.rst index 08cd8f127..fbf913590 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -14,6 +14,11 @@ Changelog * :doc:`/plugins/fetchart`: Look for images in the Cover Art Archive for the release group in addition to the specific release. Thanks to Filipe Fortes. +* Fix a race in the importer that could cause files to be deleted before they + were imported. This happened when importing one album, importing a duplicate + album, and then asking for the first album to be replaced with the second. + The situation could only arise when importing music from the library + directory and when the two albums are imported close in time. 1.2.1 (June 22, 2013)