Debian Codegraph @

testsuite/hardlinks_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/hardlinks.test.
3 #
4 # Verify rsync -H detects hard links and re-creates them on the receiver.
5 # Also covers the incremental-recursion path (lots of small files), the
6 # remote (lsh.sh) path, --link-dest / --copy-dest, --checksum without
7 # slipping HLINK_BUMP boundaries, and the single-file / single-directory
8 # corner cases that have broken in the past.
9
10 import os
11 import shutil
12 import subprocess
13
14 from rsyncfns import (
15 CHKDIR, FROMDIR, OUTFILE, RSYNC, RSYNC_PEER, SRCDIR, TODIR,
16 checkit, makepath, rsync_argv, test_fail, test_skipped, rsync_path_arg, rsh_cmd,
17 )
18
19
20 SSH = rsh_cmd()
21
22 FROMDIR.mkdir(parents=True, exist_ok=True)
23 name1 = FROMDIR / 'name1'
24 name2 = FROMDIR / 'name2'
25 name3 = FROMDIR / 'name3'
26 name4 = FROMDIR / 'name4'
27 name1.write_text("This is the file\n")
28 try:
29 os.link(name1, name2)
30 except OSError:
31 test_skipped("Can't create hardlink")
32 try:
33 os.link(name2, name3)
34 except OSError:
35 test_fail("Can't create hardlink")
36 shutil.copy(name2, name4)
37
38 text = bytearray()
39 for f in sorted(SRCDIR.glob('*.c')):
40 text.extend(f.read_bytes())
41 (FROMDIR / 'text').write_bytes(bytes(text))
42
43 checkit(['-aHivv', '--debug=HLINK5', f'{FROMDIR}/', f'{TODIR}/'], FROMDIR, TODIR)
44
45 # Force a delta-overwrite on one of the linked names; -H should still
46 # leave name1..name3 hard-linked on the destination.
47 with open(TODIR / 'name1', 'a') as f:
48 f.write("extra extra\n")
49
50 checkit(['-aHivv', '--debug=HLINK5', '--no-whole-file',
51 f'{FROMDIR}/', f'{TODIR}/'], FROMDIR, TODIR)
52
53 # Add a new link under a subdir that doesn't exist on the dest yet, plus
54 # pile on lots of small files to exercise incremental recursion's link
55 # bookkeeping across batches.
56 makepath(FROMDIR / 'subdir' / 'down' / 'deep')
57
58 cdir = FROMDIR / 'subdir'
59 chars = list('abcdefghijklmnopqrstuvwxyz0123456789')
60 for x in chars:
61 for y in chars:
62 (cdir / f'{x}{y}').touch()
63
64 os.link(name1, FROMDIR / 'subdir' / 'down' / 'deep' / 'new-file')
65 (TODIR / 'text').unlink()
66
67 checkit(['-aHivve', SSH, '--debug=HLINK5', f'--rsync-path={rsync_path_arg()}',
68 f'{FROMDIR}/', f'localhost:{TODIR}/'], FROMDIR, TODIR)
69
70 # --link-dest and --copy-dest should also keep hard-linked entries.
71 checkit(['-aHivv', '--debug=HLINK5', f'--link-dest={TODIR}',
72 f'{FROMDIR}/', f'{CHKDIR}/'], TODIR, CHKDIR)
73
74 shutil.rmtree(CHKDIR, ignore_errors=True)
75 checkit(['-aHivv', '--debug=HLINK5', f'--copy-dest={TODIR}',
76 f'{FROMDIR}/', f'{CHKDIR}/'], FROMDIR, CHKDIR)
77
78 # Make a hard link whose other end is outside the source -- the dest
79 # stays single-linked -- and re-sync with --checksum.
80 (FROMDIR / 'solo').write_text("This is another file\n")
81 try:
82 os.link(FROMDIR / 'solo', CHKDIR / 'solo')
83 except OSError:
84 test_fail("Can't create hardlink")
85
86 # Make sure --checksum doesn't slip the offset due to an HLINK_BUMP() change.
87 proc = subprocess.run(
88 rsync_argv('-aHivc', '--debug=HLINK5', f'{FROMDIR}/', f'{CHKDIR}/'),
89 capture_output=True, text=True,
90 )
91 OUTFILE.write_text(proc.stdout)
92 print(proc.stdout, end='')
93 if proc.returncode != 0:
94 test_fail(f"-aHivc run exited {proc.returncode}")
95 if 'solo' in proc.stdout:
96 test_fail("Erroneous copy of solo file occurred!")
97
98 # Single-file with -H is a regression-prone path; just confirm it copies.
99 shutil.rmtree(TODIR, ignore_errors=True)
100 TODIR.mkdir(parents=True, exist_ok=True)
101 subprocess.run(rsync_argv('-aHivv', '--debug=HLINK5', str(name1), f'{TODIR}/'))
102 diff = subprocess.run(['diff', '-u', str(name1), str(TODIR / 'name1')])
103 if diff.returncode != 0:
104 test_fail("solo copy of name1 failed")
105
106 # Single-directory with -H is the 3.4.0 regression.
107 shutil.rmtree(FROMDIR, ignore_errors=True)
108 shutil.rmtree(TODIR, ignore_errors=True)
109 makepath(FROMDIR / 'sym', TODIR)
110 subprocess.run(rsync_argv('-aH', str(FROMDIR / 'sym'), str(TODIR)))
111 diff = subprocess.run(['diff', '-r', '-u', str(FROMDIR), str(TODIR)])
112 if diff.returncode != 0:
113 test_fail("solo copy of sym failed")