| 1 |
|
| 2 |
"""Coverage: backup.c make_backup() copy-fallback for non-regular files. |
| 3 |
|
| 4 |
make_backup() first tries link_or_rename() -- hard-link, then rename -- |
| 5 |
into the backup dir. On Linux that always succeeds for symlinks/FIFOs on |
| 6 |
the same filesystem, so the copy-fallback (lines ~261-349: make_file() + |
| 7 |
do_symlink_at()/do_mknod_at()/copy_file()) is never reached by the suite. |
| 8 |
|
| 9 |
When --backup-dir is on a DIFFERENT filesystem, both link() and rename() |
| 10 |
fail with EXDEV and make_backup() falls through to recreating each item: |
| 11 |
do_symlink_at() for symlinks, do_mknod_at() for FIFOs/specials, |
| 12 |
copy_file() for regular files. This test puts the backup dir on tmpfs |
| 13 |
(probed via /dev/shm) and asserts each type is backed up correctly. |
| 14 |
""" |
| 15 |
|
| 16 |
import os |
| 17 |
import shutil |
| 18 |
import stat |
| 19 |
import subprocess |
| 20 |
import tempfile |
| 21 |
|
| 22 |
from rsyncfns import ( |
| 23 |
SCRATCHDIR, FROMDIR, |
| 24 |
makepath, rmtree, rsync_argv, test_fail, test_skipped, |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
scratch_dev = os.stat(SCRATCHDIR).st_dev |
| 30 |
TMPFS = None |
| 31 |
for cand in ('/dev/shm', '/run/shm', os.environ.get('TMPDIR', '/tmp')): |
| 32 |
try: |
| 33 |
if os.stat(cand).st_dev != scratch_dev and os.access(cand, os.W_OK): |
| 34 |
TMPFS = cand |
| 35 |
break |
| 36 |
except OSError: |
| 37 |
continue |
| 38 |
if TMPFS is None: |
| 39 |
test_skipped("no writable cross-device dir (tmpfs) for --backup-dir EXDEV path") |
| 40 |
|
| 41 |
src = FROMDIR |
| 42 |
dst = SCRATCHDIR / 'bak-xdev-dst' |
| 43 |
for d in (src, dst): |
| 44 |
rmtree(d) |
| 45 |
makepath(src, dst) |
| 46 |
|
| 47 |
bak = tempfile.mkdtemp(prefix='rsync-bak-xdev-', dir=TMPFS) |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
(dst / 'reg').write_text('gen1\n') |
| 58 |
(src / 'reg').write_text('gen1\n' * 2) |
| 59 |
os.symlink('target-gen1', dst / 'lnk') |
| 60 |
os.symlink('target-gen2', src / 'lnk') |
| 61 |
have_fifo = True |
| 62 |
try: |
| 63 |
os.mkfifo(dst / 'fifo', 0o644) |
| 64 |
except OSError: |
| 65 |
have_fifo = False |
| 66 |
(src / 'fifo').write_text('replaces-fifo\n') |
| 67 |
|
| 68 |
have_lnk = (dst / 'lnk').is_symlink() |
| 69 |
|
| 70 |
try: |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
r = subprocess.run( |
| 75 |
rsync_argv('-rlpD', '--debug=backup', |
| 76 |
'--backup', f'--backup-dir={bak}', |
| 77 |
f'{src}/', f'{dst}/'), |
| 78 |
capture_output=True, text=True, |
| 79 |
) |
| 80 |
if r.returncode != 0: |
| 81 |
test_fail(f"--backup --backup-dir=<cross-dev> failed " |
| 82 |
f"(rc={r.returncode}):\n{r.stderr}") |
| 83 |
|
| 84 |
|
| 85 |
breg = os.path.join(bak, 'reg') |
| 86 |
if not os.path.isfile(breg): |
| 87 |
test_fail(f"regular-file backup not created at {breg}") |
| 88 |
if open(breg).read() != 'gen1\n': |
| 89 |
test_fail(f"regular-file backup has wrong content " |
| 90 |
f"(expected dst's original 'gen1\\n', got {open(breg).read()!r})") |
| 91 |
|
| 92 |
|
| 93 |
if have_lnk: |
| 94 |
blnk = os.path.join(bak, 'lnk') |
| 95 |
if not os.path.islink(blnk): |
| 96 |
test_fail(f"symlink backup not created at {blnk}") |
| 97 |
if os.readlink(blnk) != 'target-gen1': |
| 98 |
test_fail(f"symlink backup target wrong: {os.readlink(blnk)!r} != 'target-gen1'") |
| 99 |
|
| 100 |
|
| 101 |
if have_fifo: |
| 102 |
bfifo = os.path.join(bak, 'fifo') |
| 103 |
if not (os.path.exists(bfifo) and stat.S_ISFIFO(os.lstat(bfifo).st_mode)): |
| 104 |
test_fail(f"FIFO backup not created at {bfifo}") |
| 105 |
|
| 106 |
print(f"backup-crossdev-copy: --backup-dir on {TMPFS} (EXDEV) -> " |
| 107 |
f"reg=COPY{', lnk=SYMLINK' if have_lnk else ''}" |
| 108 |
f"{', fifo=MKNOD' if have_fifo else ''}") |
| 109 |
finally: |
| 110 |
shutil.rmtree(bak, ignore_errors=True) |