Debian Codegraph @

testsuite/xattrs-hlink_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/xattrs.test (and, via a Makefile-built
3 # symlink, of xattrs-hlink.test).
4 #
5 # Test that rsync -X preserves extended attributes through a transfer,
6 # plus the --link-dest / --copy-dest / --fake-super interactions.
7 # The hlink variant additionally enables -H and tests that hard links
8 # survive alongside xattrs.
9
10 import os
11 import subprocess
12 import sys
13
14 from rsyncfns import (
15 CHKDIR, FROMDIR, RSYNC_PREFIX, RUSR, SCRATCHDIR, TMPDIR, TODIR, TOOLDIR,
16 checkit, cp_touch, makepath, run_rsync, test_fail, test_skipped,
17 xattr_set as xset, xattr_dump, xattrs_supported,
18 )
19
20
21 if not xattrs_supported():
22 test_skipped("Rsync is configured without xattr support (or no xattr "
23 "tooling on this platform)")
24
25
26 script_name = os.path.basename(sys.argv[0] if sys.argv[0] else __file__)
27 hlink_variant = 'hlink' in script_name
28
29 lnkdir = TMPDIR / 'lnk'
30 makepath(lnkdir, FROMDIR / 'foo' / 'bar')
31
32 (FROMDIR / 'file0').write_text("now\n")
33 (FROMDIR / 'file1').write_text("something\n")
34 (FROMDIR / 'file2').write_text("else\n")
35 (FROMDIR / 'foo' / 'file3').write_text("deep\n")
36 (FROMDIR / 'file4').write_text("normal\n")
37 (FROMDIR / 'foo' / 'bar' / 'file5').write_text("deeper\n")
38
39 makepath(CHKDIR / 'foo')
40 (CHKDIR / 'file1').write_text("wow\n")
41 cp_touch(FROMDIR / 'foo' / 'file3', CHKDIR / 'foo')
42
43 dirs = ['foo', 'foo/bar']
44 files = ['file0', 'file1', 'file2', 'foo/file3', 'file4', 'foo/bar/file5']
45
46 # Read the source dir's tls listing to extract its uid:gid -- used in
47 # the fake-super %stat encoding below. Format: "MODE SIZE UID.GID ..."
48 tls_out = subprocess.check_output(
49 [str(TOOLDIR / 'tls'), str(FROMDIR / 'foo')], text=True,
50 ).strip()
51 # Extract "UID.GID" -> "UID:GID"
52 import re
53 m = re.search(r' (\d+)\.(\d+) ', tls_out)
54 if not m:
55 test_fail(f"can't parse uid/gid from tls output: {tls_out!r}")
56 uid_gid = f"{m.group(1)}:{m.group(2)}"
57
58 os.chdir(FROMDIR)
59
60 try:
61 xset('foo', 'foo', 'file0')
62 except OSError:
63 test_skipped("Unable to set an xattr")
64 xset('bar', 'bar', 'file0')
65
66 xset('short', 'this is short', 'file1')
67 xset('long',
68 'this is a long attribute that will be truncated in the initial data send',
69 'file1')
70 xset('good', 'this is good', 'file1')
71 xset('nice', 'this is nice', 'file1')
72
73 xset('foo', 'foo', 'file2')
74 xset('bar', 'bar', 'file2')
75 xset('long',
76 'a long attribute for our new file that tests to ensure that this works',
77 'file2')
78
79 xset('dir1', 'need to test directory xattrs too', 'foo')
80 xset('dir2', 'another xattr', 'foo')
81 xset('dir3', 'this is one last one for the moment', 'foo')
82
83 xset('dir4', 'another dir test', 'foo/bar')
84 xset('dir5', 'one last one', 'foo/bar')
85
86 xset('foo', 'new foo', 'foo/file3', 'foo/bar/file5')
87 xset('bar', 'new bar', 'foo/file3', 'foo/bar/file5')
88 xset('long',
89 'this is also a long attribute that will be truncated in the initial data send',
90 'foo/file3', 'foo/bar/file5')
91 xset(f'{RUSR}.equal',
92 'this long attribute should remain the same and not need to be transferred',
93 'foo/file3', 'foo/bar/file5')
94
95 xset('dir0', 'old extra value', CHKDIR / 'foo')
96 xset('dir1', 'old dir value', CHKDIR / 'foo')
97
98 xset('short', 'old short', CHKDIR / 'file1')
99 xset('extra', 'remove me', CHKDIR / 'file1')
100
101 xset('foo', 'old foo', CHKDIR / 'foo' / 'file3')
102 xset(f'{RUSR}.equal',
103 'this long attribute should remain the same and not need to be transferred',
104 CHKDIR / 'foo' / 'file3')
105
106 if hlink_variant:
107 try:
108 os.link(FROMDIR / 'foo' / 'bar' / 'file5', FROMDIR / 'foo' / 'bar' / 'file6')
109 except OSError:
110 test_skipped("Can't create hardlink")
111 files.append('foo/bar/file6')
112 dashH = ['-H']
113 altDest = '--link-dest'
114 else:
115 dashH = []
116 altDest = '--copy-dest'
117
118
119 def _save_xattrs(paths, dest_file):
120 """Snapshot the xattrs of `paths` (relative to cwd) into dest_file."""
121 dest_file.write_text(xattr_dump(*paths))
122
123
124 _save_xattrs(dirs + files, SCRATCHDIR / 'xattrs.txt')
125
126 XFILT = ['-f-x_system.*', '-f-x_security.*']
127
128 # Simple xattr copy.
129 checkit(['-avX', *XFILT, *dashH, '--super', '.', f'{CHKDIR}/'], FROMDIR, CHKDIR)
130
131 os.chdir(CHKDIR)
132 got = xattr_dump(*(dirs + files))
133 expected = (SCRATCHDIR / 'xattrs.txt').read_text()
134 if got != expected:
135 from difflib import unified_diff
136 sys.stdout.write(''.join(unified_diff(
137 expected.splitlines(keepends=True),
138 got.splitlines(keepends=True),
139 fromfile='expected', tofile='got',
140 )))
141 test_fail("xattr listing differs after simple -X copy")
142
143 os.chdir(FROMDIR)
144
145 if dashH:
146 for fn in files:
147 name = os.path.basename(fn)
148 os.link(fn, lnkdir / name)
149
150 checkit(['-aiX', *XFILT, *dashH, '--super', f'{altDest}=../chk', '.', '../to'],
151 FROMDIR, TODIR)
152
153 os.chdir(TODIR)
154 got = xattr_dump(*(dirs + files))
155 if got != expected:
156 test_fail("xattr listing differs after --copy-dest / --link-dest copy")
157
158 if dashH:
159 import shutil
160 shutil.rmtree(lnkdir, ignore_errors=True)
161
162 os.chdir(FROMDIR)
163 import shutil
164 shutil.rmtree(TODIR, ignore_errors=True)
165
166 xset('nice', 'this is nice, but different', 'file1')
167
168 _save_xattrs(dirs + files, SCRATCHDIR / 'xattrs.txt')
169
170 checkit(['-aiX', *XFILT, *dashH, '--fake-super', '--link-dest=../chk', '.', '../to'],
171 CHKDIR, TODIR)
172
173 os.chdir(TODIR)
174 got = xattr_dump(*(dirs + files))
175 expected = (SCRATCHDIR / 'xattrs.txt').read_text()
176 if got != expected:
177 test_fail("xattr listing differs after --fake-super --link-dest copy")
178
179 # Hard-link sanity for the hlink variant: file1 should be alone, every
180 # other file should share its inode with at least one peer.
181 if dashH:
182 ls_to = SCRATCHDIR / 'ls-to'
183 from rsyncfns import rsync_ls_lR
184 ls_to.write_text(rsync_ls_lR(TODIR))
185 one_link = []
186 for line in ls_to.read_text().splitlines():
187 # tls prints "mode size uid.gid links date time path" -- column
188 # index 3 (zero-based) is the link count; we collect non-directory
189 # entries whose link count is exactly 1.
190 if line.startswith('d') or not line.strip():
191 continue
192 cols = line.split()
193 if len(cols) >= 4 and cols[3] == '1':
194 one_link.append(line)
195 other_one = [ln for ln in one_link if './file1' not in ln]
196 if other_one:
197 print("Missing hard links on:")
198 print('\n'.join(other_one))
199 test_fail("hardlink check failed")
200 if not one_link:
201 test_fail("Too many hard links on file1!")
202
203 os.chdir(CHKDIR)
204 os.chmod('.', 0o700)
205 for p in dirs + files:
206 os.chmod(p, os.stat(p).st_mode & ~0o077)
207
208 xset('nice', 'this is nice, but different', 'file1')
209 xset(f'{RSYNC_PREFIX}.%stat', f'40000 0,0 {uid_gid}', *dirs)
210 xset(f'{RSYNC_PREFIX}.%stat', f'100000 0,0 {uid_gid}', *files)
211
212 _save_xattrs(dirs + files, SCRATCHDIR / 'xattrs.txt')
213
214 os.chdir(FROMDIR)
215 shutil.rmtree(TODIR, ignore_errors=True)
216
217 # When run by a non-root tester, this verifies no-user-perm files/dirs
218 # can still be transferred.
219 checkit(['-aiX', *XFILT, *dashH, '--fake-super', '--chmod=a=', '.', '../to'],
220 CHKDIR, TODIR)
221
222 os.chdir(TODIR)
223 got = xattr_dump(*(dirs + files))
224 expected = (SCRATCHDIR / 'xattrs.txt').read_text()
225 if got != expected:
226 test_fail("xattr listing differs after --fake-super --chmod=a= copy")
227
228 os.chdir(FROMDIR)
229 shutil.rmtree(TODIR, ignore_errors=True)
230 shutil.rmtree(CHKDIR, ignore_errors=True)
231
232 run_rsync('-aX', 'file1', 'file2')
233 run_rsync('-aX', 'file1', 'file2', '../chk/')
234 run_rsync('-aX', '--del', '../chk/', '.')
235 run_rsync('-aX', 'file1', '../lnk/')
236 if dashH:
237 os.link(CHKDIR / 'file1', lnkdir / 'extra-link')
238
239 _save_xattrs(['file1', 'file2'], SCRATCHDIR / 'xattrs.txt')
240
241 checkit(['-aiiX', *XFILT, *dashH, f'{altDest}=../lnk', '.', '../to'],
242 CHKDIR, TODIR)
243
244 if dashH:
245 (lnkdir / 'extra-link').unlink()
246
247 os.chdir(TODIR)
248 got = xattr_dump('file1', 'file2')
249 expected = (SCRATCHDIR / 'xattrs.txt').read_text()
250 if got != expected:
251 test_fail("xattr listing differs after --link-dest=../lnk copy")
252
253 os.chdir(FROMDIR)
254 (TODIR / 'file2').unlink()
255
256 with open('file1', 'a') as f:
257 f.write("extra\n")
258 run_rsync('-aX', '.', '../chk/')
259
260 checkit(['-aiiX', *XFILT, '.', '../to'], CHKDIR, TODIR)
261
262 os.chdir(TODIR)
263 got = xattr_dump('file1', 'file2')
264 if got != expected:
265 test_fail("xattr listing differs after the final round")