| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
import os |
| 21 |
import shlex |
| 22 |
import subprocess |
| 23 |
import sys |
| 24 |
|
| 25 |
from rsyncfns import ( |
| 26 |
RSYNC, RSYNC_PEER, SRCDIR, SCRATCHDIR, |
| 27 |
make_variety_tree, compare_trees, rmtree, |
| 28 |
xattrs_supported, acls_supported, devices_supported, owners_supported, |
| 29 |
test_skipped, test_fail, split_rsync_cmd, rsh_cmd, |
| 30 |
) |
| 31 |
|
| 32 |
SSH = rsh_cmd() |
| 33 |
WITH_X = xattrs_supported() |
| 34 |
WITH_A = acls_supported() |
| 35 |
|
| 36 |
os.chdir(SCRATCHDIR) |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
SRC = SCRATCHDIR / 'trav-src' |
| 41 |
info = make_variety_tree(SRC, depth=3, with_acls=WITH_A, with_xattrs=WITH_X, |
| 42 |
with_devices=devices_supported(), |
| 43 |
with_owners=owners_supported()) |
| 44 |
TR = info['transfer_root'] |
| 45 |
|
| 46 |
WS = SCRATCHDIR / 'trav' |
| 47 |
rmtree(WS) |
| 48 |
WS.mkdir() |
| 49 |
|
| 50 |
findings = [] |
| 51 |
|
| 52 |
|
| 53 |
def run2(binary_cmd, args): |
| 54 |
"""Run rsync without failing on nonzero (old and new may legitimately |
| 55 |
differ in success). Returns (returncode, combined_output).""" |
| 56 |
proc = subprocess.run(shlex.split(binary_cmd) + args, |
| 57 |
capture_output=True, text=True) |
| 58 |
return proc.returncode, proc.stdout + proc.stderr |
| 59 |
|
| 60 |
|
| 61 |
def diff_trees(a, b, label): |
| 62 |
return compare_trees(a, b, label, with_acls=WITH_A, |
| 63 |
with_xattrs=WITH_X) |
| 64 |
|
| 65 |
|
| 66 |
def observe(case, new_dest, old_dest, rc_new, rc_old, out_new, out_old): |
| 67 |
notes = [] |
| 68 |
if rc_new != rc_old: |
| 69 |
notes.append(f"exit codes differ: new={rc_new} old={rc_old}") |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
for d in (new_dest, old_dest): |
| 76 |
try: |
| 77 |
os.utime(d, (1000000000, 1000000000)) |
| 78 |
except OSError: |
| 79 |
pass |
| 80 |
tdiffs = diff_trees(new_dest, old_dest, case) |
| 81 |
if tdiffs: |
| 82 |
notes.append("result trees differ:\n" + "\n".join(tdiffs[:8])) |
| 83 |
if notes: |
| 84 |
findings.append((case, "\n".join(notes))) |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
def case_sender_root(): |
| 89 |
os.symlink(str(TR), WS / 'linkdir') |
| 90 |
out = {} |
| 91 |
for role, binc in (('new', RSYNC), ('old', RSYNC_PEER)): |
| 92 |
d = WS / f'A-{role}' |
| 93 |
rmtree(d); d.mkdir() |
| 94 |
rc, o = run2(binc, ['-a', f'{WS}/linkdir/', f'{d}/']) |
| 95 |
out[role] = (d, rc, o) |
| 96 |
observe('A-sender-root-symlink', |
| 97 |
out['new'][0], out['old'][0], |
| 98 |
out['new'][1], out['old'][1], out['new'][2], out['old'][2]) |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
def case_sender_intermediate(): |
| 103 |
base = WS / 'B-base' |
| 104 |
(base / 'realcomp' / 'inner').mkdir(parents=True) |
| 105 |
|
| 106 |
(base / 'realcomp' / 'inner' / 'file1').write_text('hello-1\n') |
| 107 |
(base / 'realcomp' / 'inner' / 'file2').write_text('hello-2\n') |
| 108 |
os.symlink('realcomp', base / 'linkcomp') |
| 109 |
for relflag in ([], ['-R']): |
| 110 |
tag = 'B-sender-intermediate' + ('-R' if relflag else '') |
| 111 |
out = {} |
| 112 |
for role, binc in (('new', RSYNC), ('old', RSYNC_PEER)): |
| 113 |
d = WS / f'{tag}-{role}' |
| 114 |
rmtree(d); d.mkdir() |
| 115 |
rc, o = run2(binc, ['-a'] + relflag |
| 116 |
+ [f'{base}/linkcomp/inner/', f'{d}/']) |
| 117 |
out[role] = (d, rc, o) |
| 118 |
observe(tag, out['new'][0], out['old'][0], |
| 119 |
out['new'][1], out['old'][1], out['new'][2], out['old'][2]) |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
def case_receiver_root(): |
| 124 |
out = {} |
| 125 |
for role, binc in (('new', RSYNC), ('old', RSYNC_PEER)): |
| 126 |
real = WS / f'C-real-{role}' |
| 127 |
real.mkdir() |
| 128 |
link = WS / f'C-link-{role}' |
| 129 |
os.symlink(str(real), link) |
| 130 |
rc, o = run2(binc, ['-a', '--keep-dirlinks', f'{TR}/', f'{link}/']) |
| 131 |
out[role] = (real, rc, o) |
| 132 |
observe('C-receiver-root-symlink', out['new'][0], out['old'][0], |
| 133 |
out['new'][1], out['old'][1], out['new'][2], out['old'][2]) |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
def case_receiver_intermediate(): |
| 140 |
for kdl in ([], ['--keep-dirlinks']): |
| 141 |
tag = 'D-receiver-intermediate' + ('-kdl' if kdl else '') |
| 142 |
out = {} |
| 143 |
for role, binc in (('new', RSYNC), ('old', RSYNC_PEER)): |
| 144 |
base = WS / f'{tag}-base-{role}' |
| 145 |
(base / 'realcomp').mkdir(parents=True) |
| 146 |
os.symlink('realcomp', base / 'linkcomp') |
| 147 |
rc, o = run2(binc, ['-a'] + kdl |
| 148 |
+ [f'{TR}/', f'{base}/linkcomp/inner/']) |
| 149 |
landing = base / 'realcomp' / 'inner' |
| 150 |
if not landing.exists(): |
| 151 |
landing.mkdir(parents=True, exist_ok=True) |
| 152 |
out[role] = (landing, rc, o) |
| 153 |
observe(tag, out['new'][0], out['old'][0], |
| 154 |
out['new'][1], out['old'][1], out['new'][2], out['old'][2]) |
| 155 |
|
| 156 |
|
| 157 |
if split_rsync_cmd(RSYNC) == split_rsync_cmd(RSYNC_PEER): |
| 158 |
test_skipped("no old peer selected (RSYNC_PEER == RSYNC); nothing to " |
| 159 |
"compare for symlink-traversal divergence") |
| 160 |
|
| 161 |
case_sender_root() |
| 162 |
case_sender_intermediate() |
| 163 |
case_receiver_root() |
| 164 |
case_receiver_intermediate() |
| 165 |
|
| 166 |
if findings: |
| 167 |
msg = [f"variety-symlink-traversal: {len(findings)} traversal case(s) " |
| 168 |
f"diverge between current and peer={RSYNC_PEER!r}:"] |
| 169 |
for case, desc in findings: |
| 170 |
msg.append(f"\n========== {case} ==========") |
| 171 |
msg.append(desc) |
| 172 |
test_fail("\n".join(msg)) |
| 173 |
|
| 174 |
print("variety-symlink-traversal: current and peer agree on all traversal cases") |