Debian Codegraph @

testsuite/scanner-batch-flag-mismatch_test.py

raw symbols SCIP
1 #!/usr/bin/python3
2 # Regression test for run5 0016: a --read-batch whose recorded stream-flags
3 # enable a metadata class (xattrs/acls/uid/gid) that the reading command line
4 # did not. setup_protocol() used to compute xattrs_ndx BEFORE check_batch_flags()
5 # flipped preserve_xattrs on, leaving xattrs_ndx=0 -- so F_XATTR(file) wrote a
6 # 4-byte int at offset 0 of every file_struct, clobbering file->dirname into a
7 # wild pointer that flist_sort_and_clean() then dereferenced (heap overflow).
8 import subprocess
9 from rsyncfns import (
10 FROMDIR, SCRATCHDIR, forced_protocol, makepath, rmtree, run_rsync, rsync_argv,
11 test_fail, test_skipped, xattr_set, xattrs_supported,
12 )
13
14 if not xattrs_supported():
15 test_skipped("needs xattr support to write an -X batch")
16
17 proto = forced_protocol()
18 if proto is not None and proto < 30:
19 test_skipped("xattrs (-X) need protocol 30+")
20
21 src = FROMDIR
22 rmtree(src)
23 makepath(src)
24 # Several files, each with an xattr, so the post-clobber flist_sort_and_clean()
25 # walks file->dirname across many entries. Some platforms advertise xattr
26 # support yet reject a user-namespace xattr on a plain file (e.g. FreeBSD's
27 # setextattr) -- skip cleanly there rather than fail in setup.
28 for i in range(40):
29 f = src / f'f{i:03d}'
30 f.write_text('x\n')
31 try:
32 xattr_set('user.m', 'v', f)
33 except Exception as e:
34 test_skipped(f"cannot set a user xattr on a regular file here: {e}")
35
36 batch = SCRATCHDIR / 'xbatch'
37 wb_dest = SCRATCHDIR / 'wb-dest'
38 rmtree(wb_dest)
39 # Write the batch WITH -X: the stream-flags record preserve_xattrs.
40 run_rsync('-a', '-X', f'--only-write-batch={batch}', f'{src}/', str(wb_dest))
41
42 dest = SCRATCHDIR / 'rb-dest'
43 rmtree(dest)
44 makepath(dest)
45 # Replay with BARE -a (no -X): the batch flips preserve_xattrs on after the
46 # *_ndx were computed. Must complete cleanly now.
47 r = subprocess.run(rsync_argv('-a', f'--read-batch={batch}', str(dest)),
48 capture_output=True, text=True)
49 if r.returncode < 0 or r.returncode >= 128:
50 test_fail(f'read-batch crashed (rc={r.returncode}): {r.stderr.strip()[:200]}')
51 if not (dest / 'f000').exists():
52 test_fail(f'read-batch did not replay the files (rc={r.returncode}): '
53 f'{r.stderr.strip()[:200]}')
54 print("scanner-batch-flag-mismatch: -X batch replayed with bare -a is clean")