diff --git a/compat.c b/compat.c index aae467ea8..5aebf29ce 100644 --- a/compat.c +++ b/compat.c @@ -33,6 +33,9 @@ extern int allow_inc_recurse; extern int preallocate_files; extern int append_mode; extern int fuzzy_basis; +extern int relative_paths; +extern int implied_dirs; +extern int ignore_non_existing_dirs; extern int read_batch; extern int write_batch; extern int delay_updates; @@ -664,6 +667,14 @@ void setup_protocol(int f_out,int f_in) #endif if (protocol_version < 30) { + /* Older senders omit implied parents instead of marking them. */ + if (ignore_non_existing_dirs && relative_paths && !implied_dirs) { + rprintf(FERROR, + "--ignore-non-existing-directory with --relative --no-implied-dirs" + " requires protocol 30 or higher (negotiated %d).\n", + protocol_version); + exit_cleanup(RERR_PROTOCOL); + } if (append_mode == 1) append_mode = 2; if (preserve_acls && !local_server) { diff --git a/generator.c b/generator.c index e5cf7c170..3c4d416c1 100644 --- a/generator.c +++ b/generator.c @@ -64,6 +64,7 @@ extern int update_only; extern int human_readable; extern int ignore_existing; extern int ignore_non_existing; +extern int ignore_non_existing_dirs; extern int want_xattr_optim; extern int modify_window; extern int inplace; @@ -1967,14 +1968,21 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx, exit_cleanup(RERR_PROTOCOL); } } - if (relative_paths && !implied_dirs && file->mode != 0 - && do_stat_at(dn, &sx.st) < 0) { - if (dry_run) - goto parent_is_dry_missing; - if (make_path(fname, MKP_DROP_NAME | MKP_SKIP_SLASH) < 0) { - rsyserr(FERROR_XFER, errno, - "recv_generator: mkdir %s failed", - full_fname(dn)); + if (relative_paths && !implied_dirs && file->mode != 0) { + int parent_stat = do_stat_at(dn, &sx.st); + if (ignore_non_existing_dirs > 0 + && (parent_stat < 0 || !S_ISDIR(sx.st.st_mode))) { + if (parent_stat < 0 && errno != ENOENT && errno != ENOTDIR) + rsyserr(FERROR_XFER, errno, "recv_generator: stat %s failed", + full_fname(dn)); + goto skipping_non_existing; + } + if (parent_stat < 0) { + if (dry_run) + goto parent_is_dry_missing; + if (make_path(fname, MKP_DROP_NAME | MKP_SKIP_SLASH) < 0) + rsyserr(FERROR_XFER, errno, + "recv_generator: mkdir %s failed", full_fname(dn)); } } if (fuzzy_basis) { @@ -2029,7 +2037,13 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx, return; } - if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) { + if ((ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) + || (ignore_non_existing_dirs > 0 && is_dir + && (statret != 0 || !S_ISDIR(sx.st.st_mode)))) { + if (is_dir > 0 && statret != 0 && stat_errno != ENOENT && stat_errno != ENOTDIR) + rsyserr(FERROR_XFER, stat_errno, "recv_generator: stat %s failed", + full_fname(fname)); + skipping_non_existing: if (is_dir) { if (is_dir < 0) return; diff --git a/options.c b/options.c index 240b65f98..637dcd9e8 100644 --- a/options.c +++ b/options.c @@ -145,6 +145,7 @@ int fuzzy_basis = 0; size_t bwlimit_writemax = 0; int ignore_existing = 0; int ignore_non_existing = 0; +int ignore_non_existing_dirs = 0; int need_messages_from_generator = 0; int max_delete = INT_MIN; OFF_T max_size = -1; @@ -726,6 +727,7 @@ static struct poptOption long_options[] = { {"update", 'u', POPT_ARG_NONE, &update_only, 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 }, {"ignore-existing", 0, POPT_ARG_NONE, &ignore_existing, 0, 0, 0 }, {"max-size", 0, POPT_ARG_STRING, &max_size_arg, OPT_MAX_SIZE, 0, 0 }, {"min-size", 0, POPT_ARG_STRING, &min_size_arg, OPT_MIN_SIZE, 0, 0 }, @@ -3223,6 +3225,9 @@ void server_options(char **args, int *argc_p) if (ignore_non_existing) ADD_SERVER_ARG("--existing"); + if (ignore_non_existing_dirs) + ADD_SERVER_ARG("--ignore-non-existing-directory"); + if (tmpdir) { ADD_SERVER_ARG("--temp-dir"); ADD_SERVER_ARG(safe_arg("", tmpdir)); diff --git a/rsync.1.md b/rsync.1.md index cd323d3f1..8b74d4a77 100644 --- a/rsync.1.md +++ b/rsync.1.md @@ -601,6 +601,7 @@ has its own detailed description later in this manpage. --rsync-path=PROGRAM specify the rsync to run on remote machine --existing skip creating new files on receiver --ignore-non-existing skip creating new files on receiver +--ignore-non-existing-directory skip new destination directories --ignore-existing skip updating files that exist on receiver --remove-source-files sender removes synchronized files (non-directory) --del an alias for --delete-during @@ -2132,6 +2133,24 @@ sign) if you want the local shell to expand it. This option is a [TRANSFER RULE](#TRANSFER_RULES), so don't expect any exclude side effects. +0. `--ignore-non-existing-directory` + + Skip a source directory and its contents unless the corresponding + destination directory already exists. A non-directory entry at that path + is left in place. This also applies to implied parent directories in a + [`--relative`](#opt) transfer. + + New files can still be copied into existing directories. Use + [`--existing`](#opt) as well to skip new files. This option is a + [TRANSFER RULE](#TRANSFER_RULES) and does not alter deletion in existing + directories. It does not change how rsync creates the destination path + itself. + + The receiver must support this option. Older senders can still be used. + Combining this option with `--relative --no-implied-dirs` requires + protocol 30 or higher because older senders omit the implied parents + from the file list. + 0. `--ignore-existing` This tells rsync to skip updating files that already exist on the diff --git a/testsuite/tests/transfer/ignore-missing-directories.py b/testsuite/tests/transfer/ignore-missing-directories.py new file mode 100644 index 000000000..cd3ef36f5 --- /dev/null +++ b/testsuite/tests/transfer/ignore-missing-directories.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python3 + +from harness import TestContext, requires, run +from harness.daemon import start_test_daemon +from harness.daemon_config import write_daemon_conf +from harness.filesystem import assert_same +from harness.results import test_fail +from harness.rsync import forced_protocol, rsh_cmd, rsync_path_arg, run_rsync + + +DIRECTORIES_ONLY = '--ignore-non-existing-directory' + + +@requires(features={'daemon', 'remote-shell'}, transports={'pipe', 'tcp'}, + min_peer='4.0.0', mutates={'environment', 'filesystem', 'process', 'socket'}, + tags={'transfer'}) +def test(context: TestContext): + source = context.source + for directory in ('present/new-child', 'absent', 'blocked'): + (source / directory).mkdir(parents=True) + for name in ('present/update', 'present/new', 'present/new-child/item', + 'absent/item', 'blocked/item'): + (source / name).write_text(f'{name}\n') + + def destination(name): + path = context.scratch / name + (path / 'present').mkdir(parents=True) + (path / 'present' / 'update').write_text('old\n') + (path / 'present' / 'extra').write_text('extra\n') + (path / 'blocked').write_text('keep this file\n') + return path + + def transfer(label, *args): + result = run_rsync('-rt', *args, capture_output=True, check=False) + if result.returncode: + test_fail(f'{label}: transfer failed with exit {result.returncode}: {result.stderr}') + + def check_missing(path): + for name in ('absent', 'present/new-child'): + if (path / name).exists(): + test_fail(f'{path.name}: unexpectedly created {name}') + if not (path / 'blocked').is_file() or (path / 'blocked').read_text() != 'keep this file\n': + test_fail(f'{path.name}: replaced the existing non-directory entry') + + def check(path, *, deleted=False, existing=False): + assert_same(source / 'present' / 'update', path / 'present' / 'update', label='updated file') + if existing: + if (path / 'present' / 'new').exists(): + test_fail(f'{path.name}: --existing allowed a new file') + else: + assert_same(source / 'present' / 'new', path / 'present' / 'new', label='new file') + if (path / 'present' / 'extra').exists() == deleted: + test_fail(f'{path.name}: wrong deletion result in an existing directory') + check_missing(path) + + for label, options in ( + ('local', []), + ('delete-during', ['--delete']), + ('delete-before', ['--delete-before']), + ('existing', ['--existing']), + ): + target = destination(label) + transfer(label, DIRECTORIES_ONLY, *options, f'{source}/', f'{target}/') + check(target, deleted=label.startswith('delete-'), existing=label == 'existing') + + protocol = forced_protocol() + relative_operands = (f'{source}/./present/update', f'{source}/./absent/item', + f'{source}/./blocked/item') + for label, extra in (('relative', []), ('no-implied-dirs', ['--no-implied-dirs'])): + target = destination(label) + args = (DIRECTORIES_ONLY, '--relative', *extra, *relative_operands, f'{target}/') + if extra and protocol is not None and protocol < 30: + result = run_rsync('-rt', *args, capture_output=True, check=False) + if result.returncode != 2 or 'requires protocol 30 or higher' not in result.stderr: + test_fail(f'{label}: expected a protocol refusal, got {result.returncode}: {result.stderr}') + if (target / 'present' / 'update').read_text() != 'old\n': + test_fail(f'{label}: updated a file despite the protocol refusal') + check_missing(target) + continue + transfer(label, *args) + assert_same(source / 'present' / 'update', target / 'present' / 'update', label=label) + check_missing(target) + + for label, sender, receiver in (('push', '', 'lh:'), ('pull', 'lh:', '')): + target = destination(label) + transfer(label, DIRECTORIES_ONLY, '-e', rsh_cmd(), f'--rsync-path={rsync_path_arg()}', + f'{sender}{source}/', f'{receiver}{target}/') + check(target) + + target = destination('daemon') + config = write_daemon_conf([('files', {'path': str(target), 'read only': 'no', 'use chroot': 'no'})]) + url = start_test_daemon(config, 12985) + transfer('daemon', DIRECTORIES_ONLY, f'{source}/', f'{url}files/') + check(target) + + +if __name__ == '__main__': + run(test)