Repository navigation
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
CANCELLEDbranch incopy_move_file()only frees the error, andg_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:.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.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:
G_FILE_COPY_OVERWRITEis set. Without it,g_file_copy()refuses to touch an existing destination, so the file can only have been created by this copy;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 returnCANCELLEDafter 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 throughorg.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 withsystemd-run --user --scope -p MemoryHigh=350M -p MemoryMax=1G -p CPUQuota=5%, otherwise the copy finished before the progress window appeared.For the last case, I also built a version without the
id::filecheck, and there the new file was deleted. Finally, I ran the first and last cases on a 1 GB FAT USB stick (mounted withflush, 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 readingid::fileagain 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