| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
import os |
| 10 |
import shutil |
| 11 |
|
| 12 |
from rsyncfns import ( |
| 13 |
FROMDIR, SCRATCHDIR, TODIR, |
| 14 |
build_rsyncd_conf, check_perms, checkit, makepath, rmtree, |
| 15 |
run_rsync, start_test_daemon, test_fail, |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
DAEMON_PORT = 12875 |
| 20 |
|
| 21 |
|
| 22 |
checkdir = SCRATCHDIR / 'check' |
| 23 |
|
| 24 |
FROMDIR.mkdir(parents=True, exist_ok=True) |
| 25 |
(FROMDIR / 'name1').write_text("This is the file\n") |
| 26 |
(FROMDIR / 'name2').write_text("This is the other file\n") |
| 27 |
(FROMDIR / 'dir1').mkdir() |
| 28 |
(FROMDIR / 'dir2').mkdir() |
| 29 |
|
| 30 |
os.chmod(FROMDIR / 'name1', 0o4700) |
| 31 |
os.chmod(FROMDIR / 'dir1', 0o700) |
| 32 |
os.chmod(FROMDIR / 'dir2', 0o770) |
| 33 |
|
| 34 |
|
| 35 |
checkit(['-avv', f'{FROMDIR}/', f'{checkdir}/'], FROMDIR, checkdir) |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
os.umask(0o002) |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
for entry in checkdir.iterdir(): |
| 47 |
|
| 48 |
|
| 49 |
st = entry.stat() |
| 50 |
mode = st.st_mode & ~0o6000 |
| 51 |
mode |= 0o444 |
| 52 |
if entry.is_dir() or (st.st_mode & 0o111): |
| 53 |
mode |= 0o111 |
| 54 |
os.chmod(entry, mode) |
| 55 |
|
| 56 |
|
| 57 |
plus_w = 0o222 & ~0o002 |
| 58 |
for d in (checkdir, checkdir / 'dir1', checkdir / 'dir2'): |
| 59 |
st = d.stat() |
| 60 |
os.chmod(d, st.st_mode | plus_w) |
| 61 |
|
| 62 |
checkit(['-avv', '--chmod', 'ug-s,a+rX,D+w', f'{FROMDIR}/', f'{TODIR}/'], |
| 63 |
checkdir, TODIR) |
| 64 |
|
| 65 |
def check_permcopy(chmod_arg, start_mode, expected, is_dir=False): |
| 66 |
rmtree(FROMDIR) |
| 67 |
rmtree(TODIR) |
| 68 |
makepath(FROMDIR) |
| 69 |
permcopy = FROMDIR / 'permcopy' |
| 70 |
if is_dir: |
| 71 |
permcopy.mkdir() |
| 72 |
else: |
| 73 |
permcopy.write_text('permcopy\n') |
| 74 |
os.chmod(permcopy, start_mode) |
| 75 |
run_rsync('-avv', f'--chmod={chmod_arg}', f'{FROMDIR}/', f'{TODIR}/') |
| 76 |
check_perms(TODIR / 'permcopy', expected) |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
check_permcopy('g=o,o=', 0o647, 'rw-rwx---') |
| 81 |
check_permcopy('g=u', 0o741, 'rwxrwx--x') |
| 82 |
check_permcopy('g-o', 0o775, 'rwx-w-r-x') |
| 83 |
check_permcopy('u=g', 0o4755, 'r-xr-xr-x') |
| 84 |
check_permcopy('g=u', 0o2755, 'rwxrwxr-x') |
| 85 |
check_permcopy('o=u', 0o1750, 'rwxr-xrwx', is_dir=True) |
| 86 |
|
| 87 |
rmtree(FROMDIR) |
| 88 |
rmtree(TODIR) |
| 89 |
makepath(FROMDIR) |
| 90 |
(FROMDIR / 'permcopy').write_text('permcopy\n') |
| 91 |
proc = run_rsync('-avv', '--chmod=g=ur', f'{FROMDIR}/', f'{TODIR}/', |
| 92 |
check=False, capture_output=True) |
| 93 |
if proc.returncode == 0: |
| 94 |
test_fail('--chmod=g=ur was not rejected') |
| 95 |
|
| 96 |
|
| 97 |
rmtree(FROMDIR) |
| 98 |
rmtree(checkdir) |
| 99 |
rmtree(TODIR) |
| 100 |
makepath(TODIR, FROMDIR / 'foo') |
| 101 |
(FROMDIR / 'bar').touch() |
| 102 |
|
| 103 |
checkit(['-avv', f'{FROMDIR}/', f'{checkdir}/'], FROMDIR, checkdir) |
| 104 |
os.chmod(FROMDIR / 'bar', (FROMDIR / 'bar').stat().st_mode | 0o001) |
| 105 |
|
| 106 |
checkit(['-avv', '--chmod=Fo-x', f'{FROMDIR}/', f'{TODIR}/'], checkdir, TODIR) |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
conf = build_rsyncd_conf() |
| 111 |
with open(conf, 'a') as f: |
| 112 |
f.write(f""" |
| 113 |
[test-incoming-chmod] |
| 114 |
\tpath = {TODIR} |
| 115 |
\tread only = no |
| 116 |
\tincoming chmod = Fo-x |
| 117 |
""") |
| 118 |
|
| 119 |
url = start_test_daemon(conf, DAEMON_PORT) |
| 120 |
|
| 121 |
rmtree(TODIR) |
| 122 |
makepath(TODIR) |
| 123 |
|
| 124 |
checkit(['-avv', '--no-perms', f'{FROMDIR}/', |
| 125 |
f'{url}test-incoming-chmod/'], |
| 126 |
checkdir, TODIR, allowed_codes=(0, 23)) |