|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import stat |
| 5 | +import struct |
| 6 | + |
| 7 | +from rsyncfns import SCRATCHDIR, makepath, run_rsync, test_fail |
| 8 | + |
| 9 | +source = SCRATCHDIR / 'source' |
| 10 | +destination = SCRATCHDIR / 'destination' |
| 11 | +makepath(source) |
| 12 | +makepath(destination) |
| 13 | +target = destination / 'target' |
| 14 | +target.write_text('target contents\n') |
| 15 | +target.chmod(0o640) |
| 16 | +links = [('link', 'target'), ('dangling', 'missing')] |
| 17 | +for name, link in links: |
| 18 | + os.symlink(link, source / name) |
| 19 | + |
| 20 | +batch = SCRATCHDIR / 'symlinks.batch' |
| 21 | +run_rsync('-rlp', '--checksum-seed=1', f'--only-write-batch={batch}', |
| 22 | + f'{source}/', f'{destination}/') |
| 23 | + |
| 24 | +# Supply BSD-style link modes without relying on the source filesystem |
| 25 | +mode = stat.S_IMODE((source / 'link').lstat().st_mode) |
| 26 | +wanted = 0o400 if mode != 0o400 else 0o755 |
| 27 | +encoded_mode = struct.pack('<I', 0o120000 | mode) |
| 28 | +data = batch.read_bytes() |
| 29 | +if data.count(encoded_mode) != 1: |
| 30 | + test_fail('expected one shared symlink mode in the batch') |
| 31 | +batch.write_bytes(data.replace(encoded_mode, struct.pack('<I', 0o120000 | wanted))) |
| 32 | + |
| 33 | +for _ in range(2): |
| 34 | + run_rsync('-rlp', f'--read-batch={batch}', f'{destination}/') |
| 35 | + for name, link in links: |
| 36 | + if not (destination / name).is_symlink() or os.readlink(destination / name) != link: |
| 37 | + test_fail(f'{name}: symlink was not preserved') |
| 38 | + if stat.S_IMODE(target.stat().st_mode) != 0o640 or target.read_text() != 'target contents\n': |
| 39 | + test_fail('symlink chmod changed the target') |
| 40 | + if (destination / 'missing').exists(): |
| 41 | + test_fail('dangling symlink target was created') |
0 commit comments