| 1 | #!/usr/bin/env python3 |
| 2 | # Exercise the DPC_MAXDEPTH (64) boundary of the held ancestor-dirfd cache. |
| 3 | # |
| 4 | # The receiver/generator (get_dir_fd) and the sender (held_dir_path_fd) resolve |
| 5 | # a path against a persistent ancestor-dirfd stack capped at DPC_MAXDEPTH=64 |
| 6 | # path components (syscall.c). A directory deeper than that is declined by the |
| 7 | # cache (dpc_dir_fd returns -1) and must fall back to the un-cached |
| 8 | # secure_relative_open() walk -- which handles arbitrary depth -- and still |
| 9 | # produce a byte-identical result with no overflow/crash at the array bound. |
| 10 | # |
| 11 | # We build a single chain with a file at EVERY level past the cap, so dir-depths |
| 12 | # both within the cache (<=64 components, fast path) and beyond it (>64, the |
| 13 | # fallback) are transferred in one run, exercising the fast-path -> fallback |
| 14 | # handoff on the sender AND the receiver (whose caches are independent). |
| 15 | # Plain local transfer + a no-chroot daemon push; no root required. |
| 16 | |
| 17 | import os |
| 18 | |
| 19 | from rsyncfns import ( |
| 20 | SCRATCHDIR, checkit, make_tree, rmtree, start_test_daemon, test_fail, |
| 21 | test_skipped, walk_files, write_daemon_conf, |
| 22 | ) |
| 23 | |
| 24 | DEPTH = 70 # > DPC_MAXDEPTH (64): the deepest files use the fallback |
| 25 | DAEMON_PORT = 12905 |
| 26 | |
| 27 | base = SCRATCHDIR / 'deep-path' |
| 28 | src = base / 'src' |
| 29 | rmtree(base) |
| 30 | |
| 31 | # One deep chain, a file at each level: src/f0, src/d1/f1, ..., src/d1/.../d70/f70. |
| 32 | try: |
| 33 | make_tree(src, depth=DEPTH, data=True) |
| 34 | except OSError as e: |
| 35 | test_skipped(f"cannot build a {DEPTH}-deep tree ({e})") |
| 36 | |
| 37 | # Sanity: the deepest file's parent really has more components than the cache |
| 38 | # cap, so the fallback is genuinely exercised (not just the fast path). |
| 39 | deepest = max(walk_files(src), key=lambda p: len(p.relative_to(src).parts)) |
| 40 | parent_components = len(deepest.relative_to(src).parts) - 1 # drop the filename |
| 41 | if parent_components <= 64: |
| 42 | test_fail(f"deep tree only {parent_components} components deep -- " |
| 43 | "does not cross DPC_MAXDEPTH (64); test is ineffective") |
| 44 | |
| 45 | # --- local transfer: sender AND receiver caches both cross the boundary ----- |
| 46 | dest = base / 'dest' |
| 47 | checkit(['-a', f'{src}/', str(dest)], src, dest) |
| 48 | |
| 49 | # --- daemon push: the daemon receiver uses the confined resolver ------------ |
| 50 | mod = base / 'module' |
| 51 | mod.mkdir(parents=True, exist_ok=True) |
| 52 | conf = write_daemon_conf([ |
| 53 | ('deep', {'path': str(mod), 'use chroot': 'no', 'read only': 'no'}), |
| 54 | ]) |
| 55 | daemon_url = start_test_daemon(conf, DAEMON_PORT).rstrip('/') |
| 56 | checkit(['-a', f'{src}/', f'{daemon_url}/deep/'], src, mod, allowed_codes=(0, 23)) |
| 57 | |
| 58 | print(f"deep-path: {DEPTH}-deep tree round-trips past the dir-fd cache cap " |
| 59 | "(local + daemon)") |