Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Enhancements

- Add `--update-links` to skip newer destination symlinks independently
of `--update`.
- Allow daemon modules to apply PAM account policy after rsync authentication.
Both build-time support and the module setting are disabled by default.
- Let rrsync administrators require a server-side transfer log outside the
Expand Down
29 changes: 29 additions & 0 deletions generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ extern int remove_source_files;
extern int delay_updates;
extern int delay_symlinks;
extern int update_only;
extern int update_links;
extern int human_readable;
extern int ignore_existing;
extern int ignore_non_existing;
Expand Down Expand Up @@ -449,6 +450,21 @@ static inline int mtime_differs(STRUCT_STAT *stp, struct file_struct *file)
#endif
}

#ifdef SUPPORT_LINKS
static BOOL destination_link_is_newer(const STRUCT_STAT *dest, const struct file_struct *source)
{
if (dest->st_mtime > source->modtime) {
uint64_t difference = (uint64_t)dest->st_mtime - (uint64_t)source->modtime;
return difference > (uint64_t)MAX(modify_window, 0);
}
#ifdef ST_MTIME_NSEC
if (dest->st_mtime == source->modtime && modify_window < 0 && protocol_version >= 31)
return dest->ST_MTIME_NSEC > F_MOD_NSEC_or_0(source);
#endif
return False;
}
#endif

static inline int any_time_differs(stat_x *sxp, struct file_struct *file, UNUSED(const char *fname))
{
int differs = mtime_differs(&sxp->st, file);
Expand Down Expand Up @@ -2093,6 +2109,19 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
goto cleanup;
}

#ifdef SUPPORT_LINKS
if (update_links && preserve_links && statret == 0
&& ftype == FT_SYMLINK && stype == FT_SYMLINK && destination_link_is_newer(&sx.st, file)) {
if (INFO_GTE(SKIP, 1))
rprintf(FINFO, "%s is newer\n", fname);
#ifdef SUPPORT_HARD_LINKS
if (!replacing_hlink && F_IS_HLINKED(file))
handle_skipped_hlink(file, itemizing, code, f_out);
#endif
goto cleanup;
}
#endif

fnamecmp = fname;

if (is_dir) {
Expand Down
4 changes: 4 additions & 0 deletions options.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ int omit_dir_times = 0;
int omit_link_times = 0;
int trust_sender = 0;
int update_only = 0;
int update_links = 0;
int open_noatime = 0;
int cvs_exclude = 0;
int dry_run = 0;
Expand Down Expand Up @@ -726,6 +727,7 @@ static struct poptOption long_options[] = {
{"no-one-file-system",0, POPT_ARG_VAL, &one_file_system, 0, 0, 0 },
{"no-x", 0, POPT_ARG_VAL, &one_file_system, 0, 0, 0 },
{"update", 'u', POPT_ARG_NONE, &update_only, 0, 0, 0 },
{"update-links", 0, POPT_ARG_NONE, &update_links, 0, 0, 0 },
{"existing", 0, POPT_ARG_NONE, &ignore_non_existing, 0, 0, 0 },
{"ignore-non-existing",0,POPT_ARG_NONE, &ignore_non_existing, 0, 0, 0 },
{"ignore-non-existing-directory",0,POPT_ARG_NONE,&ignore_non_existing_dirs, 0, 0, 0 },
Expand Down Expand Up @@ -3238,6 +3240,8 @@ void server_options(char **args, int *argc_p)

if (ignore_non_existing_dirs)
ADD_SERVER_ARG("--ignore-non-existing-directory");
if (update_links)
ADD_SERVER_ARG("--update-links");

if (tmpdir) {
ADD_SERVER_ARG("--temp-dir");
Expand Down
14 changes: 14 additions & 0 deletions rsync.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ has its own detailed description later in this manpage.
--backup-dir=DIR make backups into hierarchy based in DIR
--suffix=SUFFIX backup suffix (default ~ w/o --backup-dir)
--update, -u skip files that are newer on the receiver
--update-links skip newer destination symlinks
--inplace update destination files in-place
--append append data onto shorter files
--append-verify --append w/old data in file checksum
Expand Down Expand Up @@ -1216,6 +1217,19 @@ sign) if you want the local shell to expand it.
is usually best to avoid combining this with[ `--inplace`](#opt) unless you
have implemented manual steps to handle any interrupted in-progress files.

0. `--update-links`

Skip an existing destination symlink if it is newer than the source
symlink. The comparison uses the links' own modification times and
respects [`--modify-window`](#opt). Subsecond comparison also requires
protocol 31 or higher.

This [transfer rule](#TRANSFER_RULES) applies only to matching symlink
types. It does not enable [`--links`](#opt), change how
[`--copy-links`](#opt) reads the source or alter file-type replacement.
It can be used independently of [`--update`](#opt). The receiving rsync
must support this option.

0. `--inplace`

This option changes how rsync transfers a file when its data needs to be
Expand Down
122 changes: 122 additions & 0 deletions testsuite/tests/transfer/update-links.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/usr/bin/env python3

import errno
import json
import os

from harness import TestContext, requires, run
from harness.filesystem import hardlink_symlinks_supported
from harness.results import test_fail, test_skipped
from harness.rsync import forced_protocol, rsh_cmd, rsync_path_arg, run_rsync


STAMP = 1_600_000_000


def symlink(path, target, seconds, nanoseconds=0):
path.symlink_to(target)
stamp = seconds * 1_000_000_000 + nanoseconds
os.utime(path, ns=(stamp, stamp), follow_symlinks=False)


def check_link(path, target):
if not path.is_symlink() or os.readlink(path) != target:
test_fail(f'{path.parent.name}/{path.name}: expected a symlink to {target}')


@requires(features={'remote-shell', 'symlink', 'symlink_timestamps'}, transports={'pipe'},
min_peer='4.0.0', mutates={'filesystem', 'process'}, tags={'transfer'})
def test(context: TestContext):
probe = context.scratch / 'timestamp-probe'
try:
symlink(probe, 'unneeded', STAMP)
except NotImplementedError:
test_skipped('Python cannot set a symbolic link timestamp', capability='symlink_timestamps')
except OSError as error:
if error.errno not in (errno.ENOSYS, errno.EOPNOTSUPP):
raise
test_skipped('The fixture filesystem cannot set symbolic link timestamps',
capability='symlink_timestamps')
if probe.lstat().st_mtime != STAMP:
test_fail('the symbolic link timestamp fixture did not retain its time')

source = context.source
source.mkdir()
(source / 'incoming').write_bytes(b'source data\n')
(source / 'regular').write_bytes(b'incoming regular file\n')
os.utime(source / 'regular', (STAMP, STAMP))
for name in ('newer', 'older', 'equal', 'missing', 'type-change'):
symlink(source / name, 'incoming', STAMP)

def destination(label):
target = context.scratch / label
target.mkdir()
for name, stamp in (('newer', STAMP + 60), ('older', STAMP - 60), ('equal', STAMP)):
symlink(target / name, 'existing', stamp)
(target / 'type-change').write_bytes(b'existing regular file\n')
(target / 'regular').write_bytes(b'old\n')
os.utime(target / 'type-change', (STAMP + 60, STAMP + 60))
os.utime(target / 'regular', (STAMP + 60, STAMP + 60))
return target

def transfer(label, options, *, push=False, pull=False):
target = destination(label)
shell = ('-e', rsh_cmd(), f'--rsync-path={rsync_path_arg()}') if push or pull else ()
run_rsync('-rlt', *options, *shell,
f'{"lh:" if pull else ""}{source}/', f'{"lh:" if push else ""}{target}/')
return target

check_link(transfer('default', ()) / 'newer', 'incoming')
check_link(transfer('regular-update', ('--update',)) / 'newer', 'incoming')
for label, push, pull in (('local', False, False), ('push', True, False), ('pull', False, True)):
target = transfer(label, ('--update-links',), push=push, pull=pull)
check_link(target / 'newer', 'existing')
for name in ('older', 'equal', 'missing', 'type-change'):
check_link(target / name, 'incoming')
if (target / 'regular').read_bytes() != b'incoming regular file\n':
test_fail(f'{label}: update-links affected regular file selection')

target = transfer('window', ('--update-links', '--modify-window=120'))
check_link(target / 'newer', 'incoming')

target = transfer('copy-links', ('--update-links', '--copy-links'))
if (target / 'newer').is_symlink() or (target / 'newer').read_bytes() != b'source data\n':
test_fail('update-links overrode copy-links')

target = context.scratch / 'no-links'
run_rsync('-r', '--update-links', f'{source}/', f'{target}/')
if os.path.lexists(target / 'newer'):
test_fail('update-links enabled copying symbolic links')

src, dst = context.scratch / 'subsecond-source', context.scratch / 'subsecond-dest'
src.mkdir()
dst.mkdir()
symlink(src / 'link', 'incoming', STAMP, 100_000_000)
symlink(dst / 'link', 'existing', STAMP, 900_000_000)
link_times = [(tree / 'link').lstat().st_mtime_ns for tree in (src, dst)]
# Some filesystems round both times to one second.
if link_times[0] < link_times[1] and all(value // 1_000_000_000 == STAMP for value in link_times):
run_rsync('-rl', '--update-links', '--modify-window=-1', f'{src}/', f'{dst}/')
protocol = forced_protocol()
check_link(dst / 'link', 'existing' if protocol is None or protocol >= 31 else 'incoming')

capabilities = json.loads(run_rsync('-VV', capture_output=True).stdout)['capabilities']
if capabilities['hardlink_symlinks'] and hardlink_symlinks_supported(context.scratch):
src, dst = context.scratch / 'hardlink-source', context.scratch / 'hardlink-dest'
src.mkdir()
dst.mkdir()
symlink(src / 'first', 'incoming', STAMP)
os.link(src / 'first', src / 'second', follow_symlinks=False)
os.link(src / 'first', src / 'third', follow_symlinks=False)
symlink(dst / 'first', 'existing', STAMP + 60)
run_rsync('-rlH', '--update-links', f'{src}/', f'{dst}/')
check_link(dst / 'first', 'existing')
check_link(dst / 'second', 'incoming')
check_link(dst / 'third', 'incoming')
inodes = [(dst / name).lstat().st_ino for name in ('first', 'second', 'third')]
if inodes[0] == inodes[1] or inodes[1] != inodes[2]:
test_fail('the accepted symbolic links did not form their own hard-link group')


if __name__ == '__main__':
run(test)
Loading