| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 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 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 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 |
| 68 |
|
| 69 |
|
| 70 |
checkit(['-avv', '--partial', '--delay-updates', f'{FROMDIR}/', f'{TODIR}/'], FROMDIR, TODIR) |