Skip to content

sync: replace conflicting files and directories - #9696

Draft
Minghan2005 wants to merge 2 commits into
rclone:masterfrom
Minghan2005:codex/fix-sync-file-dir-replacement
Draft

Minghan2005 wants to merge 2 commits into
rclone:masterfrom
Minghan2005:codex/fix-sync-file-dir-replacement

Conversation

@Minghan2005

Copy link
Copy Markdown

What does this change do?

sync now replaces a destination directory with a source file, or a destination file with a source directory, when they have the same transformed name.

The traversal groups entries by transformed basename but still prefers same-type matches. This preserves the behavior of backends such as Drive that can expose a file and directory with the same name. Only a remaining cross-type pair is treated as a replacement.

For a real type conflict, sync removes the destination before continuing. If --backup-dir is configured, files from the conflicting directory are moved into the backup tree before its directories are removed. --immutable still refuses the replacement, and an active filtered sync refuses to purge a directory unless --delete-excluded is set.

The root cause was that file and directory sort keys included their type. A same-name cross-type pair therefore arrived as separate source-only and destination-only entries, so delete-after queued the upload before removing the conflicting destination.

Tests cover both replacement directions with normal listing and ListR, backup-directory preservation, same-type precedence, immutable mode, and filtered-sync safety.

Linked issue

Fixes #2231

For new or changed backends

Not applicable; this changes core sync traversal only.

Verification

  • go build
  • RCLONE_CONFIG=/notfound go test ./fs/march ./fs/sync
  • RCLONE_CONFIG=/notfound go test -race ./fs/march ./fs/sync
  • PATH="$PWD:$PATH" RCLONE_CONFIG=/notfound make quicktest BRANCH=codex-fix-sync-file-dir-replacement
  • golangci-lint run ./fs/march/... ./fs/sync/... (0 issues)

Checklist

  • This change is trivial OR it has been discussed and agreed in the linked issue.
  • I have read the contribution guidelines.
  • (If I used AI tools to help write this code) I have read and understood the AI-assisted contributions guidance, and I have tested and take ownership of this change myself.
  • I have added tests for all changes in this PR if appropriate.
  • I have added documentation for the changes if appropriate (not applicable for this bug fix).
  • All commit messages are in house style.
  • (Backend changes only) Not applicable.
  • This Pull Request is ready for review.

@Minghan2005

Copy link
Copy Markdown
Author

CI note: the only failing job is linux_386. Both cmd/mount and cmd/mount2 fail because fusermount3 returns Operation not permitted; the same failures also occurred on the current master build (run 30533115308). All other jobs on this PR passed, including lint and the other OS builds. I don't have permission to rerun upstream Actions jobs. Could a maintainer please rerun the failed job?

@Minghan2005
Minghan2005 force-pushed the codex/fix-sync-file-dir-replacement branch from 11ea93a to 435ea30 Compare August 26, 2026 13:32
Sync previously treated an identically named file and directory as unrelated entries. With delete-after this attempted the replacement before removing the conflicting destination entry, so the transfer failed.

Group entries by their transformed basename while still preferring same-type matches for backends that can hold both kinds. For an actual type conflict, remove or back up the destination before continuing the sync. Preserve immutable and filtered-sync safety checks.

Fixes rclone#2231
@Minghan2005
Minghan2005 force-pushed the codex/fix-sync-file-dir-replacement branch from 435ea30 to 5298b77 Compare August 26, 2026 13:35
@Minghan2005

Copy link
Copy Markdown
Author

Rebased this branch onto current master and updated the head to 2544b6b0.

The new base includes the upstream GitHub Actions FUSE workaround from be6c689, so the historical linux_386 failure caused by /usr/local/bin/fusermount3: Operation not permitted is no longer present in the workflow baseline.

An additional ordering issue found during review is also fixed: the grouped marcher now sorts by transformed basename before entry type, so adjacent names such as a and a- cannot cause an existing destination a to be misclassified as destination-only and deleted after upload. The external sorter now preserves keys containing the internal separator, and the grouped path retains out-of-order validation.

Fresh local verification on the rebased branch passed:

  • focused tests for fs/list, fs/march, and fs/sync;
  • focused race tests;
  • go vet for those packages;
  • full make quicktest;
  • golangci-lint run ./... with 0 issues;
  • full rclone build.

The new workflow run is currently action_required before any job starts: https://github.com/rclone/rclone/actions/runs/32978218339

Could a maintainer approve this fork workflow when convenient? I will keep the PR as Draft until the new checks have actually completed.

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.

Sync fails when a new file has the same name as an old directory

1 participant