| 1 |
#!/usr/bin/env python3 |
| 2 |
"""Coverage of the file-allocation syscalls in syscall.c at depth: |
| 3 |
do_fallocate (--preallocate) and do_punch_hole (sparse writes). |
| 4 |
|
| 5 |
These are receiver-side file operations the resolver restructure also touches. |
| 6 |
Content must survive everywhere; in addition, where the filesystem stores holes, |
| 7 |
--preallocate --sparse must end up sparse (st_blocks below the apparent size). |
| 8 |
That is a regression guard: do_fallocate() must report the preallocated length |
| 9 |
so write_sparse() punches holes in the reserved extent instead of lseek'ing over |
| 10 |
it -- a stray 0 there silently left the file fully allocated. |
| 11 |
""" |
| 12 |
|
| 13 |
import os |
| 14 |
|
| 15 |
from rsyncfns import ( |
| 16 |
FROMDIR, TODIR, |
| 17 |
assert_same, make_data_file, makepath, rmtree, run_rsync, test_fail, |
| 18 |
test_skipped, |
| 19 |
) |
| 20 |
|
| 21 |
src = FROMDIR |
| 22 |
deep = os.path.join('d1', 'd2', 'd3', 'f') |
| 23 |
|
| 24 |
|
| 25 |
def allocated(path): |
| 26 |
return os.stat(path).st_blocks * 512 |
| 27 |
|
| 28 |
# --preallocate needs fallocate/posix_fallocate, and do_punch_hole needs |
| 29 |
# FALLOC_FL_PUNCH_HOLE -- both Linux (and Cygwin) features. macOS, the *BSDs and |
| 30 |
# Solaris build without preallocation and reject the option outright ("prealloc- |
| 31 |
# ation is not supported"), so probe once with a trivial transfer and skip the |
| 32 |
# whole test where it's unavailable. |
| 33 |
rmtree(src) |
| 34 |
rmtree(TODIR) |
| 35 |
makepath(src) |
| 36 |
(src / 'probe').write_text("x\n") |
| 37 |
if run_rsync('-a', '--preallocate', f'{src}/', f'{TODIR}/', |
| 38 |
check=False, capture_output=True).returncode != 0: |
| 39 |
test_skipped("--preallocate not supported on this platform") |
| 40 |
|
| 41 |
def fs_can_punch_holes(): |
| 42 |
"""True only where the kernel can deallocate blocks via FALLOC_FL_PUNCH_HOLE |
| 43 |
-- the mechanism do_punch_hole uses for --sparse. A filesystem may report |
| 44 |
seek-based sparseness yet still keep every block on a punch (e.g. where |
| 45 |
rsync's punch falls back to writing zeros), so probe the real capability and |
| 46 |
assert the hole only where it actually frees blocks.""" |
| 47 |
import ctypes |
| 48 |
import ctypes.util |
| 49 |
KEEP_SIZE, PUNCH_HOLE = 0x01, 0x02 |
| 50 |
p = src / 'punch-probe' |
| 51 |
fd = -1 |
| 52 |
try: |
| 53 |
libc = ctypes.CDLL(ctypes.util.find_library('c') or 'libc.so.6', |
| 54 |
use_errno=True) |
| 55 |
libc.fallocate.argtypes = [ctypes.c_int, ctypes.c_int, |
| 56 |
ctypes.c_longlong, ctypes.c_longlong] |
| 57 |
fd = os.open(p, os.O_CREAT | os.O_RDWR | os.O_TRUNC, 0o644) |
| 58 |
os.write(fd, b'\xff' * 65536) |
| 59 |
before = os.fstat(fd).st_blocks |
| 60 |
ret = libc.fallocate(fd, PUNCH_HOLE | KEEP_SIZE, 0, 65536) |
| 61 |
return ret == 0 and os.fstat(fd).st_blocks < before |
| 62 |
except (OSError, AttributeError, ValueError): |
| 63 |
return False |
| 64 |
finally: |
| 65 |
if fd >= 0: |
| 66 |
os.close(fd) |
| 67 |
try: |
| 68 |
os.unlink(p) |
| 69 |
except OSError: |
| 70 |
pass |
| 71 |
|
| 72 |
|
| 73 |
can_punch = fs_can_punch_holes() |
| 74 |
|
| 75 |
|
| 76 |
def seed_plain(size=1_000_000): |
| 77 |
rmtree(src) |
| 78 |
rmtree(TODIR) |
| 79 |
makepath(src / 'd1' / 'd2' / 'd3') |
| 80 |
make_data_file(src / deep, size) |
| 81 |
|
| 82 |
|
| 83 |
def seed_holey(head=4096, hole=2 * 1024 * 1024, tail=4096): |
| 84 |
rmtree(src) |
| 85 |
rmtree(TODIR) |
| 86 |
makepath(src / 'd1' / 'd2' / 'd3') |
| 87 |
with open(src / deep, 'wb') as f: |
| 88 |
f.write(os.urandom(head)) |
| 89 |
f.write(b'\0' * hole) # a real zero run for the sparse writer |
| 90 |
f.write(os.urandom(tail)) |
| 91 |
|
| 92 |
|
| 93 |
# --- --preallocate: do_fallocate on the receiver ---------------------------- |
| 94 |
seed_plain() |
| 95 |
run_rsync('-a', '--preallocate', f'{src}/', f'{TODIR}/') |
| 96 |
assert_same(TODIR / deep, src / deep, label='--preallocate content') |
| 97 |
|
| 98 |
# --- --preallocate --sparse on a holey file: do_fallocate + do_punch_hole --- |
| 99 |
# rsync.1 promises sparse blocks for this combination where the FS supports |
| 100 |
# holes. Assert it: do_fallocate reserves the whole extent, then the zero run |
| 101 |
# must be punched back out (st_blocks well below the apparent size). |
| 102 |
seed_holey() |
| 103 |
run_rsync('-a', '--preallocate', '--sparse', f'{src}/', f'{TODIR}/') |
| 104 |
assert_same(TODIR / deep, src / deep, label='--preallocate --sparse content') |
| 105 |
if can_punch and allocated(TODIR / deep) >= os.path.getsize(TODIR / deep): |
| 106 |
test_fail(f"--preallocate --sparse left the file fully allocated " |
| 107 |
f"(allocated {allocated(TODIR / deep)} for a " |
| 108 |
f"{os.path.getsize(TODIR / deep)}-byte file); the preallocated " |
| 109 |
"extent's zero run was not punched into a hole") |
| 110 |
|
| 111 |
# --- --inplace --sparse update that introduces a zero run: do_punch_hole ---- |
| 112 |
# (sparse_end's updating_basis_or_equiv branch punches the hole in place.) |
| 113 |
seed_plain() |
| 114 |
run_rsync('-a', f'{src}/', f'{TODIR}/') # dest starts fully populated |
| 115 |
data = bytearray((src / deep).read_bytes()) |
| 116 |
data[200_000:800_000] = b'\0' * 600_000 # same size, new zero run |
| 117 |
(src / deep).write_bytes(bytes(data)) |
| 118 |
st = os.stat(src / deep) |
| 119 |
os.utime(src / deep, (st.st_atime, st.st_mtime + 100)) # force a delta update |
| 120 |
run_rsync('-a', '--inplace', '--sparse', '--no-whole-file', f'{src}/', f'{TODIR}/') |
| 121 |
assert_same(TODIR / deep, src / deep, label='--inplace --sparse content') |
| 122 |
if can_punch and allocated(TODIR / deep) >= os.path.getsize(TODIR / deep): |
| 123 |
test_fail(f"--inplace --sparse did not punch the zero run: allocated " |
| 124 |
f"{allocated(TODIR / deep)} for a {os.path.getsize(TODIR / deep)}" |
| 125 |
"-byte file") |
| 126 |
|
| 127 |
# --- --inplace --sparse must punch interior holes in matched blocks ---------- |
| 128 |
# Make source and destination byte-identical and densely allocated. With |
| 129 |
# --block-size matching the pattern size, every block match is at the same |
| 130 |
# offset, so skip_matched() sends it through write_sparse(use_seek=1). The |
| 131 |
# interior zero run must still be scanned and punched rather than merely |
| 132 |
# seeked over with the rest of the matching block. |
| 133 |
rmtree(src) |
| 134 |
rmtree(TODIR) |
| 135 |
makepath(src / 'd1' / 'd2' / 'd3', TODIR / 'd1' / 'd2' / 'd3') |
| 136 |
with open(src / deep, 'wb') as source, open(TODIR / deep, 'wb') as dest: |
| 137 |
for _ in range(256): |
| 138 |
block = os.urandom(4096) + b'\0' * 24576 + os.urandom(4096) |
| 139 |
source.write(block) |
| 140 |
dest.write(block) |
| 141 |
|
| 142 |
matched_size = os.path.getsize(TODIR / deep) |
| 143 |
matched_before = allocated(TODIR / deep) |
| 144 |
run_rsync('-a', '--ignore-times', '--inplace', '--sparse', '--no-whole-file', |
| 145 |
'--block-size=32768', f'{src}/', f'{TODIR}/') |
| 146 |
assert_same(TODIR / deep, src / deep, |
| 147 |
label='--inplace --sparse matched-block content') |
| 148 |
matched_after = allocated(TODIR / deep) |
| 149 |
if (can_punch and matched_before >= matched_size |
| 150 |
and matched_after * 2 >= matched_before): |
| 151 |
test_fail(f"--inplace --sparse left matching interior zero runs allocated: " |
| 152 |
f"{matched_after} of {matched_before} bytes remain allocated " |
| 153 |
f"after a {matched_size}-byte matched-block update") |
| 154 |
|
| 155 |
print("preallocate: --preallocate (do_fallocate) + sparse hole-punching " |
| 156 |
"(do_punch_hole) verified at depth") |