| 1 |
|
| 2 |
"""Coverage of the comparison/skip options at depth: -c, -I, --size-only, |
| 3 |
--modify-window. |
| 4 |
|
| 5 |
These decide WHETHER a file is transferred. Each is checked on a file several |
| 6 |
levels deep using a "stealth change" (same size, same mtime, different content) |
| 7 |
that the default quick check deliberately skips. |
| 8 |
""" |
| 9 |
|
| 10 |
import os |
| 11 |
|
| 12 |
from rsyncfns import ( |
| 13 |
FROMDIR, TODIR, |
| 14 |
assert_same, make_tree, rmtree, run_rsync, test_fail, |
| 15 |
) |
| 16 |
|
| 17 |
src = FROMDIR |
| 18 |
deep = os.path.join('d1', 'd2', 'd3', 'f3') |
| 19 |
|
| 20 |
|
| 21 |
def seed(): |
| 22 |
rmtree(src) |
| 23 |
rmtree(TODIR) |
| 24 |
make_tree(src, depth=3, data=True, data_size=4096) |
| 25 |
run_rsync('-a', f'{src}/', f'{TODIR}/') |
| 26 |
|
| 27 |
|
| 28 |
def stealth_change(): |
| 29 |
"""Change the deep source file's content but restore the destination's |
| 30 |
size+mtime, so the quick check sees them as equal.""" |
| 31 |
st = os.stat(TODIR / deep) |
| 32 |
data = bytearray((src / deep).read_bytes()) |
| 33 |
data[0] ^= 0xFF |
| 34 |
(src / deep).write_bytes(bytes(data)) |
| 35 |
os.utime(src / deep, (st.st_atime, st.st_mtime)) |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
seed() |
| 40 |
stealth_change() |
| 41 |
run_rsync('-a', f'{src}/', f'{TODIR}/') |
| 42 |
if (TODIR / deep).read_bytes() == (src / deep).read_bytes(): |
| 43 |
test_fail("default quick check unexpectedly transferred a same-size, " |
| 44 |
"same-mtime change (test setup is wrong)") |
| 45 |
|
| 46 |
run_rsync('-a', '-c', f'{src}/', f'{TODIR}/') |
| 47 |
assert_same(TODIR / deep, src / deep, label='-c caught stealth change') |
| 48 |
|
| 49 |
seed() |
| 50 |
stealth_change() |
| 51 |
run_rsync('-a', '-I', f'{src}/', f'{TODIR}/') |
| 52 |
assert_same(TODIR / deep, src / deep, label='-I caught stealth change') |
| 53 |
|
| 54 |
|
| 55 |
def samesize_newmtime(): |
| 56 |
data = bytearray((src / deep).read_bytes()) |
| 57 |
data[0] ^= 0xFF |
| 58 |
(src / deep).write_bytes(bytes(data)) |
| 59 |
st = os.stat(src / deep) |
| 60 |
os.utime(src / deep, (st.st_atime, st.st_mtime + 100)) |
| 61 |
|
| 62 |
|
| 63 |
seed() |
| 64 |
samesize_newmtime() |
| 65 |
run_rsync('-a', '--size-only', f'{src}/', f'{TODIR}/') |
| 66 |
if (TODIR / deep).read_bytes() == (src / deep).read_bytes(): |
| 67 |
test_fail("--size-only transferred a same-size file (should have skipped)") |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
seed() |
| 72 |
samesize_newmtime() |
| 73 |
run_rsync('-a', f'{src}/', f'{TODIR}/') |
| 74 |
assert_same(TODIR / deep, src / deep, label='default caught mtime change') |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
seed() |
| 80 |
st = os.stat(TODIR / deep) |
| 81 |
os.utime(src / deep, (st.st_atime, st.st_mtime + 1)) |
| 82 |
p = run_rsync('-ain', f'{src}/', f'{TODIR}/', capture_output=True) |
| 83 |
if 'f3' not in p.stdout: |
| 84 |
test_fail("a 1s mtime change was not itemized without --modify-window") |
| 85 |
p = run_rsync('-ain', '--modify-window=2', f'{src}/', f'{TODIR}/', |
| 86 |
capture_output=True) |
| 87 |
if 'f3' in p.stdout: |
| 88 |
test_fail("--modify-window=2 did not absorb a 1s mtime difference") |
| 89 |
|
| 90 |
print("compare: -c / -I / --size-only / --modify-window verified at depth") |