diff --git a/libnemo-private/nemo-file-operations.c b/libnemo-private/nemo-file-operations.c index aa2f74e64..fe904e51f 100644 --- a/libnemo-private/nemo-file-operations.c +++ b/libnemo-private/nemo-file-operations.c @@ -4182,8 +4182,29 @@ typedef struct { goffset last_size; SourceInfo *source_info; TransferInfo *transfer_info; + GFile *dest; + gboolean want_dest_id; + char *dest_id; } ProgressData; +static char * +query_file_id (GFile *file) +{ + GFileInfo *info; + char *id; + + info = g_file_query_info (file, G_FILE_ATTRIBUTE_ID_FILE, + G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, NULL); + if (info == NULL) { + return NULL; + } + + id = g_strdup (g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILE)); + g_object_unref (info); + + return id; +} + static void copy_file_progress_callback (goffset current_num_bytes, goffset total_num_bytes, @@ -4194,6 +4215,13 @@ copy_file_progress_callback (goffset current_num_bytes, pdata = user_data; + /* The first callback means the destination now exists: remember which + * file it is, so a cancelled copy only removes the file it created. */ + if (pdata->want_dest_id) { + pdata->want_dest_id = FALSE; + pdata->dest_id = query_file_id (pdata->dest); + } + new_size = current_num_bytes - pdata->last_size; if (new_size > 0) { @@ -4564,6 +4592,16 @@ copy_move_file (CopyMoveJob *copy_job, pdata.last_size = 0; pdata.source_info = source_info; pdata.transfer_info = transfer_info; + pdata.dest = dest; + /* Without OVERWRITE, GIO creates the destination itself and fails if it + * already exists. With OVERWRITE, the destination may be the user's old + * file (GIO writes to a temporary file and only renames it at the end), + * so it must never be removed. Local files only: with gvfs, a transfer + * can be reported as cancelled after the daemon has finished it, and a + * move has then already deleted its source. */ + pdata.want_dest_id = !overwrite && g_file_is_native (dest) && + (!copy_job->is_move || g_file_is_native (src)); + pdata.dest_id = NULL; if (copy_job->is_move) { res = g_file_move (src, dest, @@ -4581,6 +4619,19 @@ copy_move_file (CopyMoveJob *copy_job, &error); } + /* Don't leave a partial file behind on cancel (a move only deletes its + * source after a successful copy). Only delete the destination if it is + * still the file this copy created: it may have been replaced meanwhile. */ + if (!res && IS_IO_ERROR (error, CANCELLED) && pdata.dest_id != NULL) { + char *id = query_file_id (dest); + + if (g_strcmp0 (id, pdata.dest_id) == 0) { + g_file_delete (dest, NULL, NULL); + } + g_free (id); + } + g_clear_pointer (&pdata.dest_id, g_free); + if (res) { transfer_info->num_files ++; report_copy_progress (copy_job, source_info, transfer_info);