| 1 |
|
| 2 |
"""Coverage of --append and --append-verify at depth. |
| 3 |
|
| 4 |
--append assumes each destination file is a prefix of the (longer) source and |
| 5 |
transfers only the bytes past the existing size; it does NOT re-check the data |
| 6 |
already present, and it never touches a file that is already the same size or |
| 7 |
larger. --append-verify works the same way but folds the existing data into the |
| 8 |
whole-file checksum, so a transfer whose result fails verification is re-sent |
| 9 |
with a normal --inplace pass. Exercise both on files >=3 levels deep. |
| 10 |
""" |
| 11 |
|
| 12 |
import os |
| 13 |
|
| 14 |
from rsyncfns import ( |
| 15 |
FROMDIR, TODIR, |
| 16 |
assert_same, forced_protocol, make_tree, rmtree, run_rsync, test_fail, |
| 17 |
walk_files, |
| 18 |
) |
| 19 |
|
| 20 |
src = FROMDIR |
| 21 |
deep = os.path.join('d1', 'd2', 'd3', 'f3') |
| 22 |
|
| 23 |
|
| 24 |
def seed_source(): |
| 25 |
rmtree(src) |
| 26 |
make_tree(src, depth=3, data=True, data_size=8192) |
| 27 |
return [p.relative_to(src) for p in walk_files(src)] |
| 28 |
|
| 29 |
|
| 30 |
def dest_prefix(rels, *, corrupt=False, frac=0.5): |
| 31 |
"""Build a destination holding the first `frac` of each source file (a |
| 32 |
valid prefix), optionally corrupting the deep file's leading bytes.""" |
| 33 |
rmtree(TODIR) |
| 34 |
for rel in rels: |
| 35 |
dst = TODIR / rel |
| 36 |
dst.parent.mkdir(parents=True, exist_ok=True) |
| 37 |
full = (src / rel).read_bytes() |
| 38 |
dst.write_bytes(full[: int(len(full) * frac)]) |
| 39 |
if corrupt: |
| 40 |
p = TODIR / deep |
| 41 |
bad = bytearray(p.read_bytes()) |
| 42 |
bad[0:64] = b'\x00' * 64 |
| 43 |
p.write_bytes(bytes(bad)) |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
rels = seed_source() |
| 48 |
dest_prefix(rels) |
| 49 |
run_rsync('-a', '--append', f'{src}/', f'{TODIR}/') |
| 50 |
for rel in rels: |
| 51 |
assert_same(TODIR / rel, src / rel, label=f'append {rel}') |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
proto = forced_protocol() |
| 57 |
if proto is not None and proto < 30: |
| 58 |
print(f"append: protocol {proto} -- skipping the --append/--append-verify " |
| 59 |
"split (verifying-append behaviour predates the protocol-30 split)") |
| 60 |
else: |
| 61 |
|
| 62 |
dest_prefix(rels, corrupt=True) |
| 63 |
run_rsync('-a', '--append', f'{src}/', f'{TODIR}/') |
| 64 |
if (TODIR / deep).read_bytes() == (src / deep).read_bytes(): |
| 65 |
test_fail("plain --append unexpectedly repaired a corrupted prefix " |
| 66 |
"(it should append only and trust the existing data)") |
| 67 |
|
| 68 |
|
| 69 |
dest_prefix(rels, corrupt=True) |
| 70 |
run_rsync('-a', '--append-verify', f'{src}/', f'{TODIR}/') |
| 71 |
assert_same(TODIR / deep, src / deep, label='append-verify deep') |
| 72 |
|
| 73 |
print("append: tail-only completion at depth; append-verify repairs prefix") |