| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import os |
| 17 |
|
| 18 |
from rsyncfns import ( |
| 19 |
TMPDIR, is_a_link, run_rsync, test_fail, |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
def assert_symlink(path, target): |
| 24 |
if not is_a_link(path): |
| 25 |
test_fail(f"File {path} is not a symlink") |
| 26 |
actual = os.readlink(path) |
| 27 |
if actual != target: |
| 28 |
test_fail(f"symlink {path} target is {actual!r}, expected {target!r}") |
| 29 |
|
| 30 |
|
| 31 |
def assert_notexist(path): |
| 32 |
|
| 33 |
|
| 34 |
if os.path.exists(path) or os.path.islink(path): |
| 35 |
test_fail(f"File {path} unexpectedly exists") |
| 36 |
|
| 37 |
|
| 38 |
def assert_regular_file(path): |
| 39 |
if is_a_link(path): |
| 40 |
test_fail(f"File {path} is a symlink, expected a regular file") |
| 41 |
if not os.path.isfile(path): |
| 42 |
test_fail(f"File {path} is not a regular file") |
| 43 |
|
| 44 |
|
| 45 |
os.chdir(TMPDIR) |
| 46 |
|
| 47 |
os.mkdir("from") |
| 48 |
with open("from/linked_file", "w") as f: |
| 49 |
f.write("payload\n") |
| 50 |
|
| 51 |
|
| 52 |
abs_target = os.path.abspath("from/linked_file") |
| 53 |
os.symlink(abs_target, "from/abs_link") |
| 54 |
os.symlink("linked_file", "from/rel_link") |
| 55 |
|
| 56 |
|
| 57 |
if os.path.realpath("from/abs_link") != os.path.realpath("from/linked_file"): |
| 58 |
test_fail("test setup: abs_link does not resolve to linked_file") |
| 59 |
|
| 60 |
|
| 61 |
print("baseline: -a without --safe-links preserves the absolute symlink") |
| 62 |
run_rsync('-a', 'from/', 'to-plain') |
| 63 |
assert_symlink("to-plain/abs_link", abs_target) |
| 64 |
assert_symlink("to-plain/rel_link", "linked_file") |
| 65 |
|
| 66 |
|
| 67 |
print("--safe-links drops the in-tree-resolving absolute symlink") |
| 68 |
proc = run_rsync('-av', '--safe-links', 'from/', 'to-safe', |
| 69 |
capture_output=True) |
| 70 |
out = proc.stdout + proc.stderr |
| 71 |
if 'ignoring unsafe symlink' not in out: |
| 72 |
test_fail(f"expected 'ignoring unsafe symlink' message, got:\n{out}") |
| 73 |
|
| 74 |
|
| 75 |
assert_notexist("to-safe/abs_link") |
| 76 |
|
| 77 |
assert_symlink("to-safe/rel_link", "linked_file") |
| 78 |
|
| 79 |
assert_regular_file("to-safe/linked_file") |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
print("--copy-unsafe-links materialises the absolute link as a file") |
| 84 |
run_rsync('-a', '--copy-unsafe-links', 'from/', 'to-copy') |
| 85 |
assert_regular_file("to-copy/abs_link") |
| 86 |
assert_symlink("to-copy/rel_link", "linked_file") |