| 1 | #!/usr/bin/env python3 |
| 2 | # Regression guard: a --no-whole-file delta whose basis is reached via an |
| 3 | # ABSOLUTE --partial-dir must reconstruct correctly, not spuriously fail |
| 4 | # verification and strand the data in the partial-dir. |
| 5 | # |
| 6 | # This locks in the fix for the "failed verification" family reported as #724 |
| 7 | # (and the FreeBSD lseek variant #725): an interrupted/delta transfer resumed |
| 8 | # against an absolute --partial-dir looped on exit code 23 |
| 9 | # ("failed verification -- update put into partial-dir"), leaving the correct |
| 10 | # data in the partial-dir and never populating the destination. |
| 11 | # |
| 12 | # Root cause (same as #897): the receiver opened the basis with |
| 13 | # secure_relative_open(NULL, fnamecmp, ...), whose front door rejects an |
| 14 | # ABSOLUTE relpath with EINVAL. An absolute --partial-dir makes fnamecmp |
| 15 | # absolute, so the basis fd was -1, no basis was mapped, and the matched-block |
| 16 | # sum_update() (guarded by `if (mapbuf)`) was skipped -- so the whole-file |
| 17 | # verification checksum was computed over the literal data only and always |
| 18 | # mismatched, even though the in-place bytes were correct. Fixed by |
| 19 | # secure_basis_open() (commit 31fbb17d), which treats an operator-trusted |
| 20 | # absolute basis as (trusted dir + O_NOFOLLOW leaf). |
| 21 | # |
| 22 | # Deterministic (the partial-dir file IS the delta basis, so matched blocks are |
| 23 | # guaranteed) and portable: no daemon, no ZFS, any uid. Unlike the flaky |
| 24 | # --copy-dest/--backup-dir manifestations of the same bug, this reproduces every |
| 25 | # run. PASS on the fixed base; on a broken rsync (3.4.0..3.4.3) it FAILS with |
| 26 | # rc=23 + "failed verification" and the destination file missing/wrong. |
| 27 | |
| 28 | import subprocess |
| 29 | |
| 30 | from rsyncfns import ( |
| 31 | SCRATCHDIR, make_data_file, makepath, rmtree, rsync_argv, test_fail, |
| 32 | ) |
| 33 | |
| 34 | src = SCRATCHDIR / 'pdsrc' |
| 35 | dst = SCRATCHDIR / 'pddst' |
| 36 | partial = SCRATCHDIR / 'pdpartial' # an ABSOLUTE --partial-dir |
| 37 | for d in (src, dst, partial): |
| 38 | rmtree(d) |
| 39 | makepath(src, dst, partial) |
| 40 | |
| 41 | # The file to transfer, and a near-copy in the partial-dir to serve as the |
| 42 | # delta basis (a few changed bytes mid-file => a real delta with matched blocks). |
| 43 | make_data_file(src / 'big.dat', 1200 * 1024) |
| 44 | import shutil |
| 45 | shutil.copy2(src / 'big.dat', partial / 'big.dat') |
| 46 | with open(partial / 'big.dat', 'r+b') as fh: |
| 47 | fh.seek(600000) |
| 48 | fh.write(b'PARTIALDIR_BASIS_DELTA') |
| 49 | |
| 50 | # Absolute --partial-dir => the receiver's basis path (fnamecmp) is absolute. |
| 51 | proc = subprocess.run( |
| 52 | rsync_argv('-a', '--no-whole-file', '--partial', |
| 53 | f'--partial-dir={partial}', f'{src}/', f'{dst}/'), |
| 54 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
| 55 | out = proc.stdout or '' |
| 56 | print(out) |
| 57 | |
| 58 | if 'failed verification' in out or 'lseek returned' in out: |
| 59 | test_fail( |
| 60 | "#724/#725: absolute --partial-dir delta resume spuriously failed " |
| 61 | "verification (the receiver opened the absolute basis via " |
| 62 | "secure_relative_open(NULL, abs) -> EINVAL -> no basis mapped -> the " |
| 63 | "matched blocks were dropped from the verify checksum). Expected the " |
| 64 | "secure_basis_open() fix (31fbb17d) to reconstruct correctly:\n" + out) |
| 65 | |
| 66 | got = dst / 'big.dat' |
| 67 | if proc.returncode != 0: |
| 68 | test_fail(f"absolute --partial-dir delta transfer failed (rc=" |
| 69 | f"{proc.returncode}); the data was likely stranded in the " |
| 70 | f"partial-dir:\n{out}") |
| 71 | if not got.is_file(): |
| 72 | test_fail(f"destination file {got} was not created -- data stranded in the " |
| 73 | f"absolute --partial-dir (the #724/#725 symptom)") |
| 74 | if not __import__('filecmp').cmp(str(src / 'big.dat'), str(got), shallow=False): |
| 75 | test_fail("destination content differs from source after an absolute " |
| 76 | "--partial-dir delta resume") |