Debian Codegraph @

testsuite/partial_nowrite_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 #
3 # Test that --partial and --delay-updates work as expected when then
4 # permissions of the destination file prevent writing to it.
5
6 import os
7 from pathlib import Path
8 import shutil
9 import subprocess
10 import sys
11 import tempfile
12
13 from rsyncfns import make_data_file, cp_p, makepath, checkit, RSYNC, TMPDIR, get_testuid, get_rootuid
14
15 BASEDIR = TMPDIR
16
17 FROMDIR = BASEDIR / 'from'
18 TODIR = BASEDIR / 'to'
19
20 makepath(FROMDIR)
21 makepath(TODIR)
22
23 makepath(FROMDIR)
24 make_data_file(FROMDIR / 'some_file', 1 * 1024 * 1024)
25 os.chmod(FROMDIR / 'some_file', 0o444)
26
27 makepath(TODIR / '.~tmp~')
28 os.chmod(TODIR / '.~tmp~', 0o700)
29 cp_p(FROMDIR / 'some_file', TODIR / '.~tmp~' / 'some_file')
30
31 is_root = get_testuid() == get_rootuid()
32
33 # As root the read-only dest temp wouldn't deny the write (root bypasses DAC),
34 # so the EACCES path under test never fires. On Linux we can drop
35 # CAP_DAC_OVERRIDE with setpriv inside a private mount namespace to force it;
36 # where that isn't possible -- non-Linux, Python < 3.12, no mount privilege, or
37 # a build dir the cap-dropped root can't even traverse (owned by an
38 # unprivileged user with restrictive perms, e.g. a CI tree owned by the ssh
39 # user at 0700) -- just run as root: the transfer still succeeds, it merely
40 # doesn't exercise the chmod-retry path here (non-root runs do).
41 _cwd_st = os.stat(os.getcwd())
42 _cwd_traversable = ((_cwd_st.st_uid == 0 and _cwd_st.st_mode & 0o100)
43 or _cwd_st.st_mode & 0o001)
44 if (is_root and sys.platform == 'linux' and hasattr(os, 'unshare')
45 and shutil.which('setpriv') and _cwd_traversable):
46 try:
47 cwd = Path(os.getcwd())
48 chown_target = None
49 for p in reversed(cwd.parents):
50 st = p.stat()
51 if not (st.st_uid == 0 or st.st_mode & 0o005):
52 chown_target = p
53 break
54 if chown_target is not None:
55 os.unshare(os.CLONE_NEWNS)
56 subprocess.run(['mount', '--make-rprivate', '/'], check=True)
57 tempdir = tempfile.mkdtemp()
58 subprocess.run(['mount', '--bind', cwd, tempdir], check=True)
59 subprocess.run(['mount', '-t', 'tmpfs', '-o', 'mode=0755', 'tmpfs', chown_target], check=True)
60 makepath(cwd)
61 subprocess.run(['mount', '--bind', tempdir, cwd], check=True)
62 subprocess.run(['umount', tempdir], check=True)
63 os.rmdir(tempdir)
64 import rsyncfns
65 rsyncfns.RSYNC = "setpriv --inh-caps -all --bounding-set -all " + RSYNC
66 except (OSError, subprocess.CalledProcessError):
67 pass # mount namespace denied (unprivileged container) -- run as root
68
69
70 checkit(['-avv', '--partial', '--delay-updates', f'{FROMDIR}/', f'{TODIR}/'], FROMDIR, TODIR)