Skip to content

delete: preserve empty dirs in --backup-dir when deleting#967

Open
x15sr71 wants to merge 3 commits into
RsyncProject:masterfrom
x15sr71:fix/backup-empty-dir-delete
Open

delete: preserve empty dirs in --backup-dir when deleting#967
x15sr71 wants to merge 3 commits into
RsyncProject:masterfrom
x15sr71:fix/backup-empty-dir-delete

Conversation

@x15sr71
Copy link
Copy Markdown

@x15sr71 x15sr71 commented Jun 5, 2026

Description

When using --backup --backup-dir --delete, rsync was silently removing empty directories from the destination instead of preserving them in --backup-dir. Files in those directories were backed up correctly, but the empty directories themselves were not.

Root cause

delete_item() routed S_ISDIR directly to do_rmdir_at(), bypassing the make_backups/backup_dir logic that only existed in the non-directory else branch.

Fix

Fix attempt a rename() into backup-dir before rmdir(). If the destination already exists (because child file backups already created that directory in backup-dir, as noted by @steadytao in the issue), fall through to rmdir() of the now-empty source. If rename() fails for any other unexpected reason (e.g. EXDEV), emit a warning and fall through to rmdir().

Fixes #842.

Tests added in testsuite/backup-empty-dir_test.py:

  • Phase 1: basic --backup does not keep empty directories in --backup-dir #842 reproducer — empty dir appears in backup-dir
  • Phase 2: collision safety — existing backup-dir/sub/ from child file
    backups is not destroyed when parent dir is later backed up
  • Phase 3: nested empty directories (a/b/c)
  • Phase 4: no regression without --backup-dir — empty dir still deleted
  • Phase 5: --delete-delay variant — backup path reached for queued deletions

this demonstrates a subtle bug in PR 967
@tridge
Copy link
Copy Markdown
Member

tridge commented Jun 5, 2026

@x15sr71 thanks for the PR! There is a subtle bug with it in the handling of files that should not go into the backup dir. I've added another test to the PR that demonstrates the bug

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.

--backup does not keep empty directories in --backup-dir

2 participants