Skip to content

file-operations: Remove the partial file when a copy is cancelled - #3917

Open
Mirilindo wants to merge 1 commit into
linuxmint:masterfrom
Mirilindo:fix-cancel-partial-copy
Open

Mirilindo wants to merge 1 commit into
linuxmint:masterfrom
Mirilindo:fix-cancel-partial-copy

Conversation

@Mirilindo

Copy link
Copy Markdown
Contributor

Hi,

I looked at #1644 (cancelling a copy leaves the last file truncated at the destination) and it still happens with 6.8.5 (master has the same code in nemo-file-operations.c). When I copy a big file from /tmp (tmpfs) to my home on another filesystem and click Cancel halfway, the file stays at the destination with its final name and only part of the data. It's the same for a move between two filesystems (the source is kept, so nothing is lost there), and for a folder, where only the file being copied at that moment is truncated. On a single filesystem (tmpfs, btrfs or ext4) I couldn't see it in my tests, because the copy happened in one go before it could be cancelled.

If I read the code correctly, the CANCELLED branch in copy_move_file() only frees the error, and g_file_copy() doesn't remove the file it created when it is cancelled.

Nautilus tried this and reverted it. Before writing anything I checked what Nautilus does. They added a g_file_delete (dest) in the same branch (GNOME/nautilus@e4bc182d4b4, 2023) and reverted it three weeks after it was merged (GNOME/nautilus@545e0d3e72c), after this discussion: https://gitlab.gnome.org/GNOME/nautilus/-/merge_requests/1106#note_1831385. Two problems were raised there:

  1. When the user chose to replace an existing file, the copy usually goes to a temporary .goutputstream-* file that is only renamed at the end, so on cancel the destination is still the user's old, untouched file, and deleting it loses data.
  2. On a slow USB drive, the delete could happen after the cached data had been written, and by then the user may have deleted the partial file and created a new one with the same name, which would then be removed.

So this PR only removes the destination when it can be sure the file is the one this copy created, and only for local files:

  • not when G_FILE_COPY_OVERWRITE is set. Without it, g_file_copy() refuses to touch an existing destination, so the file can only have been created by this copy;
  • at the first progress callback, it saves the destination's id::file (device and inode for local files). On cancel, it reads it again and only deletes the file if it's still the same. If the attribute isn't available, nothing is deleted, which is the current behaviour.

For moves between two local filesystems, GIO only deletes the source after the copy succeeded, and the delete checks for cancellation first, so removing the partial destination is safe there too (Nautilus left moves out). I left gvfs locations out on purpose: there the transfer runs in the gvfs daemon, and g_file_move() can return CANCELLED after the daemon has finished and already deleted the source (when moving from an MTP device, for example, the daemon deletes the source right after the transfer, without checking for cancellation). Deleting the destination then would lose the file. I checked this sequence with a small D-Bus test (a fake daemon, not gvfs itself).

How I tested it: with the 6.8.5 package from my distribution on two machines (CachyOS, one with Hyprland and one with KDE), and a local build of master with this change on the KDE one, copying 800 MiB files from tmpfs to btrfs (Hyprland) or ext4 (KDE). (The 6.8.5 column mixes both machines: the first case on both, the last one on the KDE one, the others on the Hyprland one.) Each run used its own D-Bus session and temporary XDG_* folders. The script started the copies through org.Nemo.FileOperations, I clicked Cancel in the progress window, and the script then compared the files by sha256 (I can share the script if it's useful). I had to slow Nemo down with systemd-run --user --scope -p MemoryHigh=350M -p MemoryMax=1G -p CPUQuota=5%, otherwise the copy finished before the progress window appeared.

case 6.8.5 master + this PR
copy, new file truncated removed (cancelled at 408 of 800 MiB)
copy, "Replace" then cancel old file intact old file intact
move, new file truncated, source intact removed, source intact (cancelled at 416 of 800 MiB)
move, "Replace" then cancel old file intact, source intact old file intact, source intact
folder of 3 files 1st complete, 2nd truncated, 3rd missing 1st complete, 2nd removed (cancelled after at least 230 MiB of it), 3rd missing
folder merged into an existing one, "Replace all" 1st new, 2nd and 3rd old and intact same
destination deleted and recreated during the copy new file kept new file kept (Nemo had written 569 of 800 MiB)

For the last case, I also built a version without the id::file check, and there the new file was deleted. Finally, I ran the first and last cases on a 1 GB FAT USB stick (mounted with flush, 300 MiB files), which is close to the slow USB drive case from the Nautilus discussion: the partial file was removed, and the recreated file was kept. Copies that are not cancelled still finish normally (checked with small 4 MiB files).

What I didn't test: network mounts, other filesystems on removable drives (exFAT, NTFS), and a write error during the copy. Since #3881, Nemo itself cancels the job after a write error on removable drives and network mounts, so I would expect the partial file to be removed there as well. With gvfs locations nothing changes: the partial file stays, as today. The check adds one local g_file_query_info() per copied file. And there is still a very small window between reading id::file again and deleting the file. I think only GLib, which holds the file descriptor, could close it, and that's also where the Nautilus discussion suggested fixing it.

Fixes #1644.

Since this change deletes files, I tried to check everything carefully. I hope I haven't missed anything, but I would prefer that someone more experienced double-checks it. If you'd rather not handle this in Nemo, or if I misread something, I'd be glad to know, and I'm fine with closing this. I just want to help with a piece of software I use and like.

🤖 Generated with Claude Code

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 (linuxmint#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 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Canceling a copy leaves the last file corrupted at the destination

1 participant