| 1 |
|
| 2 |
"""--link-dest must fall back to a copy when the destination refuses to |
| 3 |
hard-link a symlink, whatever the errno, and must report it only once. |
| 4 |
|
| 5 |
CAN_HARDLINK_SYMLINK is decided by configure running linkat() on whatever |
| 6 |
filesystem the build tree sat on. The destination is free to disagree -- macOS |
| 7 |
builds on APFS and backs up to HFS+, which answers ENOTSUP -- and a build that |
| 8 |
says yes had no fallback left, so the transfer died with exit 23 even though the |
| 9 |
symlink was then created correctly. Every neighbouring case copes: a regular |
| 10 |
file whose link() fails falls back, and so does a build compiled without the |
| 11 |
capability. |
| 12 |
|
| 13 |
Forcing the errno with LD_PRELOAD reaches that arm on a filesystem that can |
| 14 |
hard-link symlinks perfectly well. |
| 15 |
""" |
| 16 |
|
| 17 |
import os |
| 18 |
import re |
| 19 |
import platform |
| 20 |
import subprocess |
| 21 |
|
| 22 |
from rsyncfns import ( |
| 23 |
SCRATCHDIR, makepath, rmtree, run_rsync, rsync_argv, test_fail, |
| 24 |
test_skipped, |
| 25 |
) |
| 26 |
|
| 27 |
if platform.system() != 'Linux': |
| 28 |
test_skipped('the LD_PRELOAD linkat hook is Linux-only') |
| 29 |
|
| 30 |
vv = run_rsync('-VV', check=True, capture_output=True).stdout |
| 31 |
if '"hardlink_symlinks": true' not in vv: |
| 32 |
test_skipped('build cannot hard-link symlinks, so it already falls back ' |
| 33 |
'at compile time and this arm is unreachable') |
| 34 |
|
| 35 |
hook_code = r''' |
| 36 |
#define _GNU_SOURCE |
| 37 |
#include <sys/stat.h> |
| 38 |
#include <sys/types.h> |
| 39 |
#include <dlfcn.h> |
| 40 |
#include <errno.h> |
| 41 |
#include <fcntl.h> |
| 42 |
#include <stdlib.h> |
| 43 |
#include <string.h> |
| 44 |
#include <unistd.h> |
| 45 |
|
| 46 |
static int (*real_link)(const char *, const char *); |
| 47 |
static int (*real_linkat)(int, const char *, int, const char *, int); |
| 48 |
|
| 49 |
/* Resolved on demand rather than in a constructor: link()/linkat() are not |
| 50 |
* called during loader initialisation, but resolving lazily costs nothing and |
| 51 |
* keeps this hook out of the constructor-ordering trap the partial-retry hook |
| 52 |
* documents. */ |
| 53 |
static void hook_resolve(void) |
| 54 |
{ |
| 55 |
if (!real_link) real_link = dlsym(RTLD_NEXT, "link"); |
| 56 |
if (!real_linkat) real_linkat = dlsym(RTLD_NEXT, "linkat"); |
| 57 |
} |
| 58 |
|
| 59 |
static void mark(const char *var) |
| 60 |
{ |
| 61 |
const char *path = getenv(var); |
| 62 |
int fd; |
| 63 |
if (!path) |
| 64 |
return; |
| 65 |
fd = open(path, O_WRONLY | O_CREAT, 0600); |
| 66 |
if (fd >= 0) |
| 67 |
close(fd); |
| 68 |
} |
| 69 |
|
| 70 |
static int refuse_errno(void) |
| 71 |
{ |
| 72 |
const char *e = getenv("RSYNC_LINK_REFUSE_ERRNO"); |
| 73 |
if (e && strcmp(e, "EPERM") == 0) |
| 74 |
return EPERM; |
| 75 |
if (e && strcmp(e, "ENOSYS") == 0) |
| 76 |
return ENOSYS; |
| 77 |
return ENOTSUP; |
| 78 |
} |
| 79 |
|
| 80 |
/* Only a SYMLINK source is refused: the regular files in the same transfer |
| 81 |
* must keep hard-linking, or the test could not tell "fell back for the |
| 82 |
* symlink" from "gave up on --link-dest entirely". */ |
| 83 |
static int is_symlink_at(int dfd, const char *path) |
| 84 |
{ |
| 85 |
struct stat st; |
| 86 |
if (!path) |
| 87 |
return 0; |
| 88 |
if (fstatat(dfd, path, &st, AT_SYMLINK_NOFOLLOW) < 0) |
| 89 |
return 0; |
| 90 |
return S_ISLNK(st.st_mode); |
| 91 |
} |
| 92 |
|
| 93 |
int linkat(int olddirfd, const char *oldpath, int newdirfd, |
| 94 |
const char *newpath, int flags) |
| 95 |
{ |
| 96 |
hook_resolve(); |
| 97 |
if (is_symlink_at(olddirfd, oldpath)) { |
| 98 |
mark("RSYNC_LINK_HOOK_MARKER"); |
| 99 |
errno = refuse_errno(); |
| 100 |
return -1; |
| 101 |
} |
| 102 |
return real_linkat(olddirfd, oldpath, newdirfd, newpath, flags); |
| 103 |
} |
| 104 |
|
| 105 |
int link(const char *oldpath, const char *newpath) |
| 106 |
{ |
| 107 |
hook_resolve(); |
| 108 |
if (is_symlink_at(AT_FDCWD, oldpath)) { |
| 109 |
mark("RSYNC_LINK_HOOK_MARKER"); |
| 110 |
errno = refuse_errno(); |
| 111 |
return -1; |
| 112 |
} |
| 113 |
return real_link(oldpath, newpath); |
| 114 |
} |
| 115 |
''' |
| 116 |
|
| 117 |
base = SCRATCHDIR / 'link-dest-symlink-enotsup' |
| 118 |
rmtree(base) |
| 119 |
src = base / 'src' |
| 120 |
ldest = base / 'ldest' |
| 121 |
makepath(src, ldest) |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
for d in (src, ldest): |
| 126 |
(d / 'reg').write_bytes(b'REGULAR-CONTENT') |
| 127 |
os.symlink('some-target', d / 'sym') |
| 128 |
|
| 129 |
hook_src = base / 'hook.c' |
| 130 |
hook_lib = base / 'hook.so' |
| 131 |
hook_src.write_text(hook_code) |
| 132 |
build = subprocess.run( |
| 133 |
['cc', '-shared', '-fPIC', '-o', str(hook_lib), str(hook_src), '-ldl'], |
| 134 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
| 135 |
if build.returncode != 0: |
| 136 |
test_skipped(f'cannot build the LD_PRELOAD linkat hook: {build.stdout!r}') |
| 137 |
|
| 138 |
|
| 139 |
def run(dest, refuse, args=('-a',)): |
| 140 |
marker = base / f'fired-{refuse}-{dest.name}' |
| 141 |
env = os.environ.copy() |
| 142 |
env.update({ |
| 143 |
'LD_PRELOAD': str(hook_lib), |
| 144 |
'RSYNC_LINK_REFUSE_ERRNO': refuse, |
| 145 |
'RSYNC_LINK_HOOK_MARKER': str(marker), |
| 146 |
}) |
| 147 |
r = subprocess.run( |
| 148 |
rsync_argv(*args, f'--link-dest={ldest}', f'{src}/', f'{dest}/'), |
| 149 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, env=env) |
| 150 |
if r.returncode is not None and r.returncode < 0: |
| 151 |
test_fail(f'rsync died by signal {-r.returncode} under the hook: ' |
| 152 |
f'output={r.stdout!r}') |
| 153 |
if not marker.exists(): |
| 154 |
test_skipped(f'the LD_PRELOAD hook never fired (rc={r.returncode}, ' |
| 155 |
f'output={r.stdout!r})') |
| 156 |
return r |
| 157 |
|
| 158 |
|
| 159 |
def check_fallback(dest, refuse): |
| 160 |
got = run(dest, refuse) |
| 161 |
ctx = f'refuse={refuse}, rc={got.returncode}, output={got.stdout!r}' |
| 162 |
assert got.returncode == 0, ( |
| 163 |
'a destination that refused to hard-link a symlink failed the transfer ' |
| 164 |
f'instead of falling back to a copy: {ctx}') |
| 165 |
assert 'failed to hard-link' not in got.stdout, ( |
| 166 |
f'the fallback still reported a hard-link failure: {ctx}') |
| 167 |
assert (dest / 'sym').is_symlink() and os.readlink(dest / 'sym') == 'some-target', ( |
| 168 |
f'the symlink was not recreated by the fallback: {ctx}') |
| 169 |
|
| 170 |
|
| 171 |
assert (dest / 'reg').stat().st_ino == (ldest / 'reg').stat().st_ino, ( |
| 172 |
f'the regular file stopped being hard-linked to the basis: {ctx}') |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
check_fallback(base / 'dest-enotsup', 'ENOTSUP') |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
check_fallback(base / 'dest-eperm', 'EPERM') |
| 184 |
check_fallback(base / 'dest-enosys', 'ENOSYS') |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
dest = base / 'dest-itemize' |
| 191 |
got = run(dest, 'ENOTSUP', args=('-ivvplrtH',)) |
| 192 |
ctx = f'rc={got.returncode}, output={got.stdout!r}' |
| 193 |
assert got.returncode == 0, ( |
| 194 |
f'the itemised run did not succeed, so its output proves nothing: {ctx}') |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
sym_lines = [ln for ln in got.stdout.splitlines() |
| 199 |
if re.match(r'^cL\S*\s+sym -> some-target$', ln)] |
| 200 |
assert len(sym_lines) == 1, ( |
| 201 |
f'expected exactly one "cL... sym -> some-target" itemisation, got ' |
| 202 |
f'{len(sym_lines)}: {sym_lines} ({ctx})') |