From b3d75cb48538743b73098cc14e12f735c5ed3e9c Mon Sep 17 00:00:00 2001 From: Ruud Date: Tue, 23 Sep 2014 10:02:36 +0200 Subject: [PATCH] Check if file got moved successful on move/copy close #3893 --- couchpotato/core/plugins/renamer.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/couchpotato/core/plugins/renamer.py b/couchpotato/core/plugins/renamer.py index 01e78f0..aeeb354 100755 --- a/couchpotato/core/plugins/renamer.py +++ b/couchpotato/core/plugins/renamer.py @@ -596,7 +596,7 @@ class Renamer(Plugin): dst = rename_files[src] if dst in group['renamed_files']: - log.error('File "%s" already exists, adding random string at the end to prevent data loss', dst) + log.error('File "%s" already renamed once, adding random string at the end to prevent data loss', dst) dst = '%s.random-%s' % (dst, randomString()) # Create dir @@ -797,6 +797,9 @@ Remove it if you want it to be renamed (again, or at least let it try again) dest = sp(dest) try: + if os.path.exists(dest): + raise Exception('Destination "%s" already exists' % dest) + move_type = self.conf('file_action') if use_default: move_type = self.conf('default_file_action') @@ -806,10 +809,14 @@ Remove it if you want it to be renamed (again, or at least let it try again) log.info('Moving "%s" to "%s"', (old, dest)) shutil.move(old, dest) except: - if os.path.exists(dest): + exists = os.path.exists(dest) + if exists and os.path.getsize(old) == os.path.getsize(dest): log.error('Successfully moved file "%s", but something went wrong: %s', (dest, traceback.format_exc())) os.unlink(old) else: + # remove faultly copied file + if exists: + os.unlink(dest) raise elif move_type == 'copy': log.info('Copying "%s" to "%s"', (old, dest)) @@ -1219,7 +1226,7 @@ Remove it if you want it to be renamed (again, or at least let it try again) except Exception as e: log.error('Failed moving left over file %s to %s: %s %s', (leftoverfile, move_to, e, traceback.format_exc())) # As we probably tried to overwrite the nfo file, check if it exists and then remove the original - if os.path.isfile(move_to): + if os.path.isfile(move_to) and os.path.getsize(leftoverfile) == os.path.getsize(move_to): if cleanup: log.info('Deleting left over file %s instead...', leftoverfile) os.unlink(leftoverfile)