From 8e159ab86dfc962fed1866b1a0638147cd328822 Mon Sep 17 00:00:00 2001 From: "Robin H. Johnson" Date: Sat, 28 Nov 2015 13:48:34 -0800 Subject: [PATCH 1/2] ignore-non-existing-directory: add variant of ignore-non-existing Add new option --ignore-non-existing-directory, that is a variant of --ignore-non-existing, but applies ONLY to directories. This was previously proposed in bug #8366, but I independently had a use case for it in the Gentoo infrastructure. Reference: https://bugzilla.samba.org/show_bug.cgi?id=8366 Reference: http://superuser.com/questions/316561/rsync-synchronizing-files-only-without-creating-folders-on-destination Reference: https://github.com/RsyncProject/rsync-patches/issues/8 Reference: https://lists.samba.org/archive/rsync/2015-November/030455.html Signed-off-by: Robin H. Johnson --- generator.c | 21 ++++++--- options.c | 5 +++ rsync.1.md | 10 +++++ testsuite/ignore-non-existing-directory.test | 47 ++++++++++++++++++++ 4 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 testsuite/ignore-non-existing-directory.test diff --git a/generator.c b/generator.c index 110db28fc..4220b27dc 100644 --- a/generator.c +++ b/generator.c @@ -60,6 +60,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; @@ -1353,22 +1354,28 @@ 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 || ignore_non_existing_dirs > 0) && statret == -1 && stat_errno == ENOENT) { + int actually_ignore = 0; if (is_dir) { if (is_dir < 0) return; + actually_ignore = 1; skip_dir = file; file->flags |= FLAG_MISSING_DIR; } #ifdef SUPPORT_HARD_LINKS - else if (F_IS_HLINKED(file)) + else if (ignore_non_existing > 0 && F_IS_HLINKED(file)) { + actually_ignore = 1; handle_skipped_hlink(file, itemizing, code, f_out); + } #endif - if (INFO_GTE(SKIP, 1)) { - rprintf(FINFO, "not creating new %s \"%s\"\n", - is_dir ? "directory" : "file", fname); + if(actually_ignore == 1) { + if (INFO_GTE(SKIP, 1)) { + rprintf(FINFO, "not creating new %s \"%s\"\n", + is_dir ? "directory" : "file", fname); + } + return; } - return; } if (statret == 0 && !(sx.st.st_mode & S_IWUSR) @@ -2161,6 +2168,7 @@ void check_for_finished_files(int itemizing, enum logcode code, int check_redo) min_size = -1; ignore_existing = -ignore_existing; ignore_non_existing = -ignore_non_existing; + ignore_non_existing_dirs = -ignore_non_existing_dirs; update_only = -update_only; always_checksum = -always_checksum; size_only = -size_only; @@ -2186,6 +2194,7 @@ void check_for_finished_files(int itemizing, enum logcode code, int check_redo) min_size = save_min_size; ignore_existing = -ignore_existing; ignore_non_existing = -ignore_non_existing; + ignore_non_existing_dirs = -ignore_non_existing_dirs; update_only = -update_only; always_checksum = -always_checksum; size_only = -size_only; diff --git a/options.c b/options.c index fd674754c..ee3097ac3 100644 --- a/options.c +++ b/options.c @@ -125,6 +125,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; @@ -693,6 +694,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 }, + {"ignore-non-existing-directory",0,POPT_ARG_NONE, &ignore_non_existing_dirs, 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-existing", 0, POPT_ARG_NONE, &ignore_existing, 0, 0, 0 }, @@ -2904,6 +2906,9 @@ void server_options(char **args, int *argc_p) if (ignore_non_existing) args[ac++] = "--existing"; + if (ignore_non_existing_dirs) + args[ac++] = "--ignore-non-existing-directory"; + if (tmpdir) { args[ac++] = "--temp-dir"; args[ac++] = safe_arg("", tmpdir); diff --git a/rsync.1.md b/rsync.1.md index afaf1de89..c20e98dcd 100644 --- a/rsync.1.md +++ b/rsync.1.md @@ -1844,6 +1844,16 @@ expand it. This option is a [TRANSFER RULE](#TRANSFER_RULES), so don't expect any exclude side effects. +0. `--ignore-non-existing-directory` + + This tells rsync to skip creating directories that do not + exist yet on the destination. It is a variation of the + [`--ignore-non-existing`](#opt) option that is applied only for + directories. + + This option is a [TRANSFER RULE](#TRANSFER_RULES), so don't expect any + exclude side effects. + 0. `--ignore-existing` This tells rsync to skip updating files that already exist on the diff --git a/testsuite/ignore-non-existing-directory.test b/testsuite/ignore-non-existing-directory.test new file mode 100644 index 000000000..971ee2050 --- /dev/null +++ b/testsuite/ignore-non-existing-directory.test @@ -0,0 +1,47 @@ +#! /bin/sh + +# This program is distributable under the terms of the GNU GPL (see +# COPYING). + +. $suitedir/rsync.fns + +makepath "$fromdir/subdir1" "$fromdir/subdir2" "$todir/subdir1" +echo data >"$fromdir/subdir1/file" +echo data >"$todir/subdir1/file2" +echo data >"$fromdir/subdir2/file" + +# Test 1: Ensure subdir2 and content under it are not created +$RSYNC -r --ignore-non-existing-directory -vv "$fromdir/" "$todir/" | tee "$scratchdir/out" +if [ ! -d "$todir/subdir1" ]; then + test_fail 'test 1 failed: subdir1 should have been created' +fi +if [ ! -f "$todir/subdir1/file" ]; then + test_fail 'test 1 failed: subdir1/file should have been created' +fi +if [ ! -f "$todir/subdir1/file2" ]; then + test_fail 'test 1 failed: subdir1/file2 should not have been removed' +fi +if [ -d "$todir/subdir2" ]; then + test_fail 'test 1 failed: subdir2 should not have been created' +fi +if [ -f "$todir/subdir2/file" ]; then + test_fail 'test 1 failed: subdir2/file should not have been created' +fi + +# Test 2: Also ensure that other delete handling was not impacted +$RSYNC -r --delete --ignore-non-existing-directory -vv "$fromdir/" "$todir/" | tee "$scratchdir/out" +if [ ! -d "$todir/subdir1" ]; then + test_fail 'test 2 failed: subdir1 should have been created' +fi +if [ ! -f "$todir/subdir1/file" ]; then + test_fail 'test 2 failed: subdir1/file should have been created' +fi +if [ -f "$todir/subdir1/file2" ]; then + test_fail 'test 2 failed: subdir1/file2 should have been removed' +fi +if [ -d "$todir/subdir2" ]; then + test_fail 'test 2 failed: subdir2 should not have been created' +fi +if [ -f "$todir/subdir2/file" ]; then + test_fail 'test 2 failed: subdir2/file should not have been created' +fi From acfeffc1c8aa15ca8316479bc376b7316ba33831 Mon Sep 17 00:00:00 2001 From: Zen Dodd Date: Mon, 23 Nov 2026 00:33:21 +1100 Subject: [PATCH 2/2] compat: reject unsupported implied-directory mode before transfer --- compat.c | 11 ++++++++++ rsync.1.md | 3 +++ .../transfer/ignore-missing-directories.py | 20 +++++++++++++++---- 3 files changed, 30 insertions(+), 4 deletions(-) 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/rsync.1.md b/rsync.1.md index 004169e79..d4e582fb5 100644 --- a/rsync.1.md +++ b/rsync.1.md @@ -2125,6 +2125,9 @@ sign) if you want the local shell to expand it. 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` diff --git a/testsuite/tests/transfer/ignore-missing-directories.py b/testsuite/tests/transfer/ignore-missing-directories.py index 370e4befe..cd3ef36f5 100644 --- a/testsuite/tests/transfer/ignore-missing-directories.py +++ b/testsuite/tests/transfer/ignore-missing-directories.py @@ -5,14 +5,15 @@ from harness.daemon_config import write_daemon_conf from harness.filesystem import assert_same from harness.results import test_fail -from harness.rsync import rsh_cmd, rsync_path_arg, run_rsync +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'}, - mutates={'environment', 'filesystem', 'process', 'socket'}, tags={'transfer'}) + 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'): @@ -62,10 +63,21 @@ def check(path, *, deleted=False, existing=False): 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) - transfer(label, DIRECTORIES_ONLY, '--relative', *extra, f'{source}/./present/update', - f'{source}/./absent/item', f'{source}/./blocked/item', f'{target}/') + 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)