Debian Codegraph @

testsuite/sparse_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """Coverage of -S (--sparse) at depth.
3
4 A file with a large hole, several directory levels deep, should arrive sparse
5 (its allocated blocks much smaller than its apparent size) and byte-identical
6 when copied with -S.
7
8 We do NOT assert the converse (that a plain copy fills the hole): whether a
9 zero-run is stored as a hole when -S is absent is the filesystem's choice, not
10 rsync's -- ZFS/APFS transparently sparsify zero blocks, so a --no-sparse copy
11 can legitimately stay sparse there.
12 """
13
14 import os
15
16 from rsyncfns import (
17 FROMDIR, TODIR,
18 assert_same, makepath, rmtree, run_rsync, test_fail, test_skipped,
19 )
20
21 src = FROMDIR
22 deep = os.path.join('d1', 'd2', 'd3', 'holey')
23 SIZE = 4 * 1024 * 1024
24
25
26 def make_sparse(path):
27 with open(path, 'wb') as f:
28 f.write(b'head')
29 f.seek(SIZE - 4)
30 f.write(b'tail')
31
32
33 def allocated(path):
34 return os.stat(path).st_blocks * 512
35
36
37 rmtree(src)
38 rmtree(TODIR)
39 makepath(src / 'd1' / 'd2' / 'd3')
40 make_sparse(src / deep)
41
42 # Confirm the source filesystem actually made a sparse file; otherwise the
43 # whole premise (and any dest comparison) is meaningless here.
44 if allocated(src / deep) >= SIZE:
45 test_skipped("source filesystem did not create a sparse file")
46
47 # --- with -S the hole is preserved at the destination -----------------------
48 run_rsync('-a', '-S', f'{src}/', f'{TODIR}/')
49 assert_same(TODIR / deep, src / deep, label='-S content')
50 if allocated(TODIR / deep) >= SIZE:
51 test_fail(f"-S did not preserve the hole at depth "
52 f"(allocated {allocated(TODIR / deep)} for a {SIZE}-byte file)")
53
54 # --- a plain copy reproduces the content too (allocation is FS-defined) ------
55 rmtree(TODIR)
56 run_rsync('-a', '--no-sparse', f'{src}/', f'{TODIR}/')
57 assert_same(TODIR / deep, src / deep, label='no-sparse content')
58
59 print("sparse: -S preserves a deep hole; content correct with and without it")