-
-
Notifications
You must be signed in to change notification settings - Fork 570
delete: preserve empty dirs in --backup-dir when deleting #967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
x15sr71
wants to merge
10
commits into
RsyncProject:master
Choose a base branch
from
x15sr71:fix/backup-empty-dir-delete
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
210e228
delete: preserve empty dirs in --backup-dir when deleting; add regres…
x15sr71 3b2a499
Merge branch 'master' into fix/backup-empty-dir-delete
x15sr71 b9ddeb5
testsuite: check that only the right files go into backup
tridge fa8b6fe
delete: use rmdir+mkdir instead of rename to safely back up empty dirs
x15sr71 69a6470
Merge branch 'master' into fix/backup-empty-dir-delete
x15sr71 851a24d
delete: treat EEXIST like ENOTEMPTY for rmdir on Solaris
x15sr71 9213f37
Merge branch 'master' into fix/backup-empty-dir-delete
x15sr71 a950976
Merge branch 'master' into fix/backup-empty-dir-delete
x15sr71 764ddb6
Merge branch 'master' into fix/backup-empty-dir-delete
x15sr71 3cfc8f4
Merge branch 'master' into fix/backup-empty-dir-delete
x15sr71 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| #!/usr/bin/env python3 | ||
| # Regression test for issue #842: | ||
| # rsync --backup --backup-dir --delete must preserve empty directories | ||
| # in --backup-dir rather than silently removing them. | ||
|
|
||
| import re | ||
| import subprocess | ||
|
|
||
| from rsyncfns import ( | ||
| FROMDIR, TODIR, TMPDIR, | ||
| assert_exists, assert_not_exists, | ||
| makepath, rmtree, | ||
| run_rsync, rsync_argv, test_fail, | ||
| ) | ||
|
|
||
| bakdir = TMPDIR / 'bak' | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Phase 1: basic reproducer from issue #842. | ||
| # src/ is empty; dst/sub/empty_dir/ and dst/sub/file.txt get deleted. | ||
| # Both must land in backup-dir, not be silently discarded. | ||
| # --------------------------------------------------------------------------- | ||
| makepath(FROMDIR, TODIR / 'sub' / 'empty_dir', bakdir) | ||
| (TODIR / 'sub' / 'file.txt').write_text('data\n') | ||
|
|
||
| proc = subprocess.run( | ||
| rsync_argv('-r', '--delete', '--backup', f'--backup-dir={bakdir}', | ||
| '--info=backup', f'{FROMDIR}/', f'{TODIR}/'), | ||
| capture_output=True, text=True, | ||
| ) | ||
| if proc.returncode != 0: | ||
| test_fail(f'rsync failed (phase 1): {proc.stderr}') | ||
| output = proc.stdout + proc.stderr | ||
|
|
||
| assert_exists(bakdir / 'sub' / 'file.txt', label='phase1: file.txt in backup-dir') | ||
| assert_exists(bakdir / 'sub' / 'empty_dir', label='phase1: empty_dir in backup-dir') | ||
| if not (bakdir / 'sub' / 'empty_dir').is_dir(): | ||
| test_fail('phase1: backup-dir/sub/empty_dir must be a directory') | ||
| assert_not_exists(TODIR / 'sub', label='phase1: dst/sub removed after sync') | ||
|
|
||
| if not re.search(r'backed up sub/empty_dir to ', output, re.MULTILINE): | ||
| test_fail('phase1: no "backed up sub/empty_dir" log line in rsync output') | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Phase 2: collision safety (steadytao concern, issue #842). | ||
| # File backups create backup_dir/sub/ first. The subsequent attempt to back | ||
| # up sub/ itself must not wipe those already-preserved child files. | ||
| # --------------------------------------------------------------------------- | ||
| rmtree(FROMDIR); rmtree(TODIR); rmtree(bakdir) | ||
| makepath(FROMDIR, TODIR / 'sub' / 'empty_dir', bakdir) | ||
| (TODIR / 'sub' / 'file1.txt').write_text('file1\n') | ||
| (TODIR / 'sub' / 'file2.txt').write_text('file2\n') | ||
|
|
||
| run_rsync('-r', '--delete', '--backup', f'--backup-dir={bakdir}', | ||
| f'{FROMDIR}/', f'{TODIR}/') | ||
|
|
||
| assert_exists(bakdir / 'sub' / 'file1.txt', label='phase2: file1.txt survives') | ||
| assert_exists(bakdir / 'sub' / 'file2.txt', label='phase2: file2.txt survives') | ||
| assert_exists(bakdir / 'sub' / 'empty_dir', label='phase2: empty_dir in backup-dir') | ||
| assert_not_exists(TODIR / 'sub', label='phase2: dst/sub removed after sync') | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Phase 3: nested empty directories (a/b/c). | ||
| # --------------------------------------------------------------------------- | ||
| rmtree(FROMDIR); rmtree(TODIR); rmtree(bakdir) | ||
| makepath(FROMDIR, TODIR / 'a' / 'b' / 'c', bakdir) | ||
|
|
||
| run_rsync('-r', '--delete', '--backup', f'--backup-dir={bakdir}', | ||
| f'{FROMDIR}/', f'{TODIR}/') | ||
|
|
||
| assert_exists(bakdir / 'a' / 'b' / 'c', label='phase3: a/b/c in backup-dir') | ||
| if not (bakdir / 'a' / 'b' / 'c').is_dir(): | ||
| test_fail('phase3: backup-dir/a/b/c must be a directory') | ||
| assert_not_exists(TODIR / 'a', label='phase3: dst/a removed after sync') | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Phase 4: no regression without --backup-dir. | ||
| # Empty dir must still be deleted; no empty_dir~ artefact must appear. | ||
| # --------------------------------------------------------------------------- | ||
| rmtree(FROMDIR); rmtree(TODIR) | ||
| makepath(FROMDIR, TODIR / 'empty_dir') | ||
|
|
||
| run_rsync('-r', '--delete', '--backup', f'{FROMDIR}/', f'{TODIR}/') | ||
|
|
||
| assert_not_exists(TODIR / 'empty_dir', label='phase4: empty_dir removed without --backup-dir') | ||
| assert_not_exists(TODIR / 'empty_dir~', label='phase4: no empty_dir~ created') | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Phase 5: --delete-delay variant. | ||
| # Deletions are queued and executed after the transfer; the backup path | ||
| # in delete_item() must still be reached for empty directories. | ||
| # --------------------------------------------------------------------------- | ||
| rmtree(FROMDIR); rmtree(TODIR); rmtree(bakdir) | ||
| makepath(FROMDIR, TODIR / 'sub' / 'empty_dir', bakdir) | ||
| (TODIR / 'sub' / 'file.txt').write_text('data\n') | ||
|
|
||
| run_rsync('-r', '--delete-delay', '--backup', f'--backup-dir={bakdir}', | ||
| f'{FROMDIR}/', f'{TODIR}/') | ||
|
|
||
| assert_exists(bakdir / 'sub' / 'empty_dir', label='phase5: empty_dir backed up with --delete-delay') | ||
| assert_exists(bakdir / 'sub' / 'file.txt', label='phase5: file.txt backed up with --delete-delay') | ||
| assert_not_exists(TODIR / 'sub', label='phase5: dst/sub removed after sync') |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| #!/usr/bin/env python3 | ||
| # Regression test for the bug in PR #967. | ||
| # | ||
| # PR #967 makes delete_item() rename a directory into --backup-dir before | ||
| # falling back to rmdir, so empty dirs are preserved in the backup tree | ||
|
Comment on lines
+4
to
+5
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This still describes the superseded rename implementation. Please update it to describe the protected-directory regression the final test guards. |
||
| # (issue #842). The rename branch fires whenever DEL_DIR_IS_EMPTY is set, | ||
| # but that flag is NOT proof of emptiness: delete_dir_contents() sets it on | ||
| # a child even when the child's own content deletion returned DR_NOT_EMPTY | ||
| # (a grandchild survived). do_rmdir_at() was safe there -- it fails with | ||
| # ENOTEMPTY -- but do_rename_at() happily moves the whole non-empty subtree | ||
| # into the backup-dir. | ||
| # | ||
| # A "protect" filter (P) pins a file against deletion, leaving its parent | ||
| # non-empty. rsync must leave that file where it is. With the PR bug, the | ||
| # pinned parent gets renamed wholesale into --backup-dir, relocating data | ||
| # rsync deliberately chose not to delete. | ||
| # | ||
| # Correct behaviour (master): the protected file stays in the destination. | ||
| # Buggy behaviour (PR #967): the protected file is moved into --backup-dir. | ||
|
|
||
| from rsyncfns import ( | ||
| FROMDIR, TODIR, TMPDIR, | ||
| assert_exists, assert_not_exists, | ||
| makepath, rmtree, run_rsync, test_fail, | ||
| ) | ||
|
|
||
| bakdir = TMPDIR / 'bak' | ||
|
|
||
| # The pinned file must be the ONLY thing in its nested directory, and that | ||
| # directory must have no sibling whose own backup would pre-create the | ||
| # backup-dir counterpart -- otherwise the rename collides (EEXIST/ENOTEMPTY) | ||
| # and harmlessly falls back to rmdir, masking the bug. 'sibling.txt' lives | ||
| # one level up so it creates bak/sub/ but not bak/sub/inner/. | ||
| rmtree(FROMDIR); rmtree(TODIR); rmtree(bakdir) | ||
| makepath(FROMDIR, TODIR / 'sub' / 'inner', bakdir) | ||
| (TODIR / 'sub' / 'inner' / 'keep.txt').write_text('keepme\n') | ||
| (TODIR / 'sub' / 'sibling.txt').write_text('sib\n') | ||
|
|
||
| # src is empty: everything under dst/ is extraneous and would be deleted, | ||
| # except sub/inner/keep.txt which the protect filter pins in place. | ||
| run_rsync('-r', '--delete', '--backup', f'--backup-dir={bakdir}', | ||
| '--filter=P sub/inner/keep.txt', | ||
| f'{FROMDIR}/', f'{TODIR}/') | ||
|
|
||
| # The protected file must remain exactly where it was. | ||
| assert_exists(TODIR / 'sub' / 'inner' / 'keep.txt', | ||
| label='protected keep.txt must stay in the destination') | ||
|
|
||
| # ...and must NOT have been relocated into the backup-dir. | ||
| assert_not_exists(bakdir / 'sub' / 'inner' / 'keep.txt', | ||
| label='protected keep.txt must NOT be moved into backup-dir') | ||
|
|
||
| # Its pinned parent directory must also remain in place. | ||
| assert_exists(TODIR / 'sub' / 'inner', | ||
| label='pinned dir sub/inner must stay in the destination') | ||
|
|
||
| # The non-pinned sibling should still be backed up normally. | ||
| assert_exists(bakdir / 'sub' / 'sibling.txt', | ||
| label='non-pinned sibling.txt backed up as usual') | ||
|
|
||
| if not (TODIR / 'sub' / 'inner' / 'keep.txt').is_file(): | ||
| test_fail('protected file was relocated by the backup-dir rename (PR #967 bug)') | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This removes
fbufbefore backup creation is known to succeed. Ifget_backup_name()returnsNULLordo_mkdir_at()fails, the directory is gone butokremains true and deletion reports success.ACCESSPERMSalso loses the original directory mode. Please handle backup failure without reporting success and preserve the original attributes with failure and mode-preservation tests.