From c1420ad7fb08ac5b5f9a02f8f38307b43255afb1 Mon Sep 17 00:00:00 2001 From: Mirilindo <294908613+Mirilindo@users.noreply.github.com> Date: Mon, 30 Nov 2026 21:24:24 +0100 Subject: [PATCH] file-operations: Remove the partial file when a copy is cancelled When a copy or a move between two filesystems is cancelled, the destination file is left with its final name and only part of the data (#1644). If I read the code correctly, the CANCELLED branch of copy_move_file() only frees the error, and g_file_copy() doesn't remove the file it created. Nautilus added a plain g_file_delete() there and reverted it, because it removed the user's old file when replacing one, and because a late delete by path could remove a new file the user had created with the same name in the meantime. So this only removes the destination, and only for local files, when: - G_FILE_COPY_OVERWRITE is not set, so the file can only have been created by this copy; - its id::file, saved at the first progress callback, is still the same when the copy is cancelled. If the attribute isn't available, nothing is removed. For moves, the source is only deleted after a successful copy, so it is kept either way. gvfs locations are left out: there a transfer can be reported as cancelled after the daemon has finished it, and a move may then have already deleted its source. In my tests (800 MiB files, tmpfs to ext4), cancelled copies and moves no longer leave a partial file, an old file being replaced stays intact, and a file recreated by the user during the copy is kept. Co-Authored-By: Claude Opus 5.5 --- libnemo-private/nemo-file-operations.c | 51 ++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) 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);