| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
import os |
| 11 |
import sys |
| 12 |
|
| 13 |
import rsyncfns |
| 14 |
from rsyncfns import ( |
| 15 |
FROMDIR, RSYNC_PREFIX, TODIR, |
| 16 |
checkit, test_skipped, xattr_set, xattrs_supported, |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
script_name = os.path.basename(sys.argv[0] if sys.argv[0] else __file__) |
| 23 |
fake_variant = 'fake' in script_name |
| 24 |
|
| 25 |
if fake_variant: |
| 26 |
|
| 27 |
if not xattrs_supported(): |
| 28 |
test_skipped("Rsync needs xattrs for fake device tests") |
| 29 |
|
| 30 |
|
| 31 |
rsyncfns.RSYNC = rsyncfns.RSYNC + ' --fake-super' |
| 32 |
rsyncfns.TLS_ARGS = (rsyncfns.TLS_ARGS + ' --fake-super').strip() |
| 33 |
|
| 34 |
def chown_or_fake(path, uid, gid): |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
mode = os.stat(path).st_mode |
| 39 |
xattr_set(f'{RSYNC_PREFIX}.%stat', f"{mode:o} 0,0 {uid}:{gid}", path) |
| 40 |
return True |
| 41 |
else: |
| 42 |
rsyncfns.RSYNC = rsyncfns.RSYNC + ' --super' |
| 43 |
|
| 44 |
my_uid = os.getuid() |
| 45 |
if my_uid != 0: |
| 46 |
|
| 47 |
|
| 48 |
fakeroot_path = os.environ.get('FAKEROOT_PATH') |
| 49 |
if fakeroot_path and os.access(fakeroot_path, os.X_OK): |
| 50 |
print("Let's try re-running the script under fakeroot...") |
| 51 |
os.execv(fakeroot_path, [fakeroot_path, sys.executable, __file__]) |
| 52 |
|
| 53 |
def chown_or_fake(path, uid, gid): |
| 54 |
try: |
| 55 |
os.chown(path, uid, gid) |
| 56 |
return True |
| 57 |
except (PermissionError, OSError): |
| 58 |
return False |
| 59 |
|
| 60 |
|
| 61 |
FROMDIR.mkdir(parents=True, exist_ok=True) |
| 62 |
name1 = FROMDIR / 'name1' |
| 63 |
name2 = FROMDIR / 'name2' |
| 64 |
name1.write_text("This is the file\n") |
| 65 |
name2.write_text("This is the other file\n") |
| 66 |
|
| 67 |
if not chown_or_fake(name1, 5000, 5002): |
| 68 |
test_skipped("Can't chown (probably need root)") |
| 69 |
if not chown_or_fake(name2, 5001, 5003): |
| 70 |
test_skipped("Can't chown (probably need root)") |
| 71 |
|
| 72 |
os.chdir(FROMDIR.parent) |
| 73 |
checkit(['-aHvv', 'from/', 'to/'], FROMDIR, TODIR) |