Debian Codegraph @

testsuite/growing-file_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Regression: a file that grows between file-list scan and data send must not
3 # abort the whole transfer.
4 #
5 # The sender records each file's length when it scans the file list, but re-
6 # stats and maps the file at its *current* size when it later sends the data
7 # (sender.c: do_fstat + map_file + match_sums use st.st_size). A file that is
8 # appended to in that window -- e.g. a live log rotated/written during a nightly
9 # backup -- is therefore transmitted longer than its flist-recorded length.
10 #
11 # Stock rsync tolerates this: the receiver writes the grown content and the
12 # transfer completes. A hardening check added to receive_data()
13 # (offset + i > total_size / offset + len > total_size) turned it into a fatal
14 # "received more data than file length" RERR_PROTOCOL, tearing down the whole
15 # connection so every file after the growing one is skipped.
16 #
17 # The receive_data() check is transport-agnostic (same code for a local, a
18 # remote-shell, and a daemon receiver), so this drives the simplest case: a
19 # local transfer with the growth injected during an earlier, throttled file.
20 #
21 # RED (unfixed tree): rsync exits RERR_PROTOCOL (2) with "received more data
22 # than file length".
23 # GREEN (checks removed): rsync exits 0 and the grown file is copied.
24
25 import filecmp
26 import os
27 import subprocess
28 import threading
29 import time
30
31 from rsyncfns import SCRATCHDIR, make_data_file, rmtree, rsync_argv, test_fail
32
33 base = SCRATCHDIR / 'growing-file'
34 src = base / 'src'
35 dst = base / 'dst'
36 rmtree(base)
37 src.mkdir(parents=True)
38 dst.mkdir(parents=True)
39
40 # 'aaa_big' sorts first and is sent first; --bwlimit throttles it so the append
41 # to the later file lands well after the flist is built (--no-inc-recursive
42 # forces the whole flist up front) but well before that file is sent.
43 make_data_file(src / 'aaa_big', 6 * 1024 * 1024)
44
45 grow = src / 'zzz_grow'
46 make_data_file(grow, 4096) # flist records this small length
47 orig_size = grow.stat().st_size
48
49 grow_done = threading.Event()
50
51
52 def appender():
53 # After the flist scan, during aaa_big's ~4s throttled transfer.
54 time.sleep(0.6)
55 with open(grow, 'ab') as fh:
56 fh.write(os.urandom(256 * 1024))
57 fh.flush()
58 os.fsync(fh.fileno())
59 grow_done.set()
60
61
62 t = threading.Thread(target=appender)
63 t.start()
64
65 proc = subprocess.run(
66 rsync_argv('-a', '--no-inc-recursive', '--bwlimit=1500', f'{src}/', f'{dst}/'),
67 capture_output=True, text=True)
68
69 t.join()
70
71 # Guard against a vacuous run: the file must actually have grown during the
72 # transfer, else we never exercised the scenario.
73 if not grow_done.is_set() or grow.stat().st_size <= orig_size:
74 test_fail("test setup: source file did not grow during the transfer")
75
76 if proc.returncode != 0:
77 if 'received more data than file length' in (proc.stdout + proc.stderr):
78 test_fail(
79 "rsync aborted the whole transfer with RERR_PROTOCOL when a file "
80 "grew during the run (received more data than file length); it "
81 "should tolerate the growth like stock rsync.\n"
82 f"exit={proc.returncode}\n{proc.stdout}{proc.stderr}")
83 test_fail(f"rsync exited {proc.returncode}\n{proc.stdout}{proc.stderr}")
84
85 if not (dst / 'zzz_grow').is_file():
86 test_fail("the grown file was not transferred to the destination")
87 if not (dst / 'aaa_big').is_file():
88 test_fail("the earlier (throttled) file was not transferred")
89
90 # Prove the scenario was actually exercised (not a vacuous pass): the sender
91 # transmitted the *grown* content (more than the flist-recorded orig_size), and
92 # the destination is a byte-for-byte copy of the final source.
93 final_src = grow.stat().st_size
94 dst_size = (dst / 'zzz_grow').stat().st_size
95 if dst_size <= orig_size:
96 test_fail(f"destination only got {dst_size} bytes (<= original {orig_size}); "
97 "the growth-during-transfer path was not exercised")
98 if dst_size != final_src:
99 test_fail(f"destination size {dst_size} != final source size {final_src}; "
100 "the grown content was not fully transferred")
101 if not filecmp.cmp(str(grow), str(dst / 'zzz_grow'), shallow=False):
102 test_fail("destination content does not match the grown source")
103
104 print("growing-file: a file appended to during the transfer no longer aborts "
105 "the run; the grown file (and all later files) transfer intact")