From 82ecafa141821f782b27905d84d46f1f56517adc Mon Sep 17 00:00:00 2001 From: claudio Date: Fri, 29 Oct 2021 08:00:59 +0000 Subject: [PATCH] Properly implement --max-size and --min-size. This uses scan_scaled(3) from libutil which is very similar to how rsync behaves. Not implemented are the +/-1 math and the 1000 vs 1024 multipliers. OK benno@ job@ --- usr.bin/rsync/Makefile | 6 +++--- usr.bin/rsync/extern.h | 4 +++- usr.bin/rsync/fargs.c | 6 +++++- usr.bin/rsync/main.c | 13 ++++++++++--- usr.bin/rsync/rsync.1 | 22 ++++++++++++++++++++-- usr.bin/rsync/uploader.c | 11 ++++++++++- 6 files changed, 51 insertions(+), 11 deletions(-) diff --git a/usr.bin/rsync/Makefile b/usr.bin/rsync/Makefile index 74d15d29a64..3c60f18e07f 100644 --- a/usr.bin/rsync/Makefile +++ b/usr.bin/rsync/Makefile @@ -1,11 +1,11 @@ -# $OpenBSD: Makefile,v 1.12 2021/10/22 11:10:34 claudio Exp $ +# $OpenBSD: Makefile,v 1.13 2021/10/29 08:00:59 claudio Exp $ PROG= openrsync SRCS= blocks.c client.c copy.c downloader.c fargs.c flist.c hash.c ids.c \ io.c log.c main.c misc.c mkpath.c mktemp.c receiver.c rmatch.c \ rules.c sender.c server.c session.c socket.c symlinks.c uploader.c -LDADD+= -lcrypto -lm -DPADD+= ${LIBCRYPTO} ${LIBM} +LDADD+= -lcrypto -lm -lutil +DPADD+= ${LIBCRYPTO} ${LIBM} ${LIBUTIL} MAN= openrsync.1 CFLAGS+= -Wall -Wextra diff --git a/usr.bin/rsync/extern.h b/usr.bin/rsync/extern.h index cd7006a1737..3a612722b9a 100644 --- a/usr.bin/rsync/extern.h +++ b/usr.bin/rsync/extern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: extern.h,v 1.42 2021/10/22 11:10:34 claudio Exp $ */ +/* $OpenBSD: extern.h,v 1.43 2021/10/29 08:00:59 claudio Exp $ */ /* * Copyright (c) 2019 Kristaps Dzonsons * @@ -141,6 +141,8 @@ struct opts { int numeric_ids; /* --numeric-ids */ int one_file_system; /* -x */ int alt_base_mode; + off_t max_size; /* --max-size */ + off_t min_size; /* --min-size */ char *rsync_path; /* --rsync-path */ char *ssh_prog; /* --rsh or -e */ char *port; /* --port */ diff --git a/usr.bin/rsync/fargs.c b/usr.bin/rsync/fargs.c index a69955c4332..437ec61ce04 100644 --- a/usr.bin/rsync/fargs.c +++ b/usr.bin/rsync/fargs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fargs.c,v 1.20 2021/10/22 11:10:34 claudio Exp $ */ +/* $OpenBSD: fargs.c,v 1.21 2021/10/29 08:00:59 claudio Exp $ */ /* * Copyright (c) 2019 Kristaps Dzonsons * @@ -131,6 +131,10 @@ fargs_cmdline(struct sess *sess, const struct fargs *f, size_t *skip) if (!sess->opts->specials && sess->opts->devices) /* --devices is sent as -D --no-specials */ addargs(&args, "--no-specials"); + if (sess->opts->max_size >= 0) + addargs(&args, "--max-size=%lld", sess->opts->max_size); + if (sess->opts->min_size >= 0) + addargs(&args, "--min-size=%lld", sess->opts->min_size); /* only add --compare-dest, etc if this is the sender */ if (sess->opts->alt_base_mode != 0 && diff --git a/usr.bin/rsync/main.c b/usr.bin/rsync/main.c index eeeabec8fb7..d1214a4d25f 100644 --- a/usr.bin/rsync/main.c +++ b/usr.bin/rsync/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.61 2021/10/28 13:07:43 claudio Exp $ */ +/* $OpenBSD: main.c,v 1.62 2021/10/29 08:00:59 claudio Exp $ */ /* * Copyright (c) 2019 Kristaps Dzonsons * @@ -26,6 +26,7 @@ #include #include #include +#include #include "extern.h" @@ -341,7 +342,7 @@ main(int argc, char *argv[]) pid_t child; int fds[2], sd = -1, rc, c, st, i, lidx; size_t basedir_cnt = 0; - struct sess sess; + struct sess sess; struct fargs *fargs; char **args; const char *errstr; @@ -352,6 +353,8 @@ main(int argc, char *argv[]) NULL) == -1) err(ERR_IPC, "pledge"); + opts.max_size = opts.min_size = -1; + while ((c = getopt_long(argc, argv, "Dae:ghlnoprtvxz", lopts, &lidx)) != -1) { switch (c) { @@ -472,8 +475,12 @@ basedir: opts.basedir[basedir_cnt++] = optarg; break; case OP_MAX_SIZE: + if (scan_scaled(optarg, &opts.max_size) == -1) + err(1, "bad max-size"); + break; case OP_MIN_SIZE: - /* for now simply ignore */ + if (scan_scaled(optarg, &opts.min_size) == -1) + err(1, "bad min-size"); break; case OP_VERSION: fprintf(stderr, "openrsync: protocol version %u\n", diff --git a/usr.bin/rsync/rsync.1 b/usr.bin/rsync/rsync.1 index 37f054a9b52..a1b75295e11 100644 --- a/usr.bin/rsync/rsync.1 +++ b/usr.bin/rsync/rsync.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: rsync.1,v 1.27 2021/10/22 16:42:28 jmc Exp $ +.\" $OpenBSD: rsync.1,v 1.28 2021/10/29 08:00:59 claudio Exp $ .\" .\" Copyright (c) 2019 Kristaps Dzonsons .\" @@ -14,7 +14,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: October 22 2021 $ +.Dd $Mdocdate: October 29 2021 $ .Dt OPENRSYNC 1 .Os .Sh NAME @@ -31,6 +31,8 @@ .Op Fl -exclude-from Ns = Ns Ar file .Op Fl -include Ar pattern .Op Fl -include-from Ns = Ns Ar file +.Op Fl -max-size Ns = Ns size +.Op Fl -min-size Ns = Ns size .Op Fl -no-motd .Op Fl -numeric-ids .Op Fl -port Ns = Ns Ar service @@ -127,6 +129,22 @@ set the numeric group ID to match the source instead. Also transfer symbolic links. The link is transferred as a standalone file: if the destination does not exist, it will be broken. +.It Fl -max-size Ar size +Don't transfer any file that is larger than +.Ar size +bytes. +Alternatively +.Ar size +may instead use a multiplier, as documented in +.Xr scan_scaled 3 , +to specify the size. +.It Fl -min-size Ar size +Don't transfer any file that is smaller than +.Ar size +bytes. +See +.Fl -max-size +on the definiton of size. .It Fl n , -dry-run Do not actually modify the destination. Mainly useful in combination with diff --git a/usr.bin/rsync/uploader.c b/usr.bin/rsync/uploader.c index 32cf4ec0ff6..38d1ebdbe89 100644 --- a/usr.bin/rsync/uploader.c +++ b/usr.bin/rsync/uploader.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uploader.c,v 1.31 2021/10/24 21:24:17 deraadt Exp $ */ +/* $OpenBSD: uploader.c,v 1.32 2021/10/29 08:00:59 claudio Exp $ */ /* * Copyright (c) 2019 Kristaps Dzonsons * Copyright (c) 2019 Florian Obser @@ -704,6 +704,15 @@ pre_file(const struct upload *p, int *filefd, off_t *size, return 0; } + if (sess->opts->max_size >= 0 && f->st.size > sess->opts->max_size) { + WARNX("skipping over max-size file %s", f->path); + return 0; + } + if (sess->opts->min_size >= 0 && f->st.size < sess->opts->min_size) { + WARNX("skipping under min-size file %s", f->path); + return 0; + } + /* * For non dry-run cases, we'll write the acknowledgement later * in the rsync_uploader() function. -- 2.20.1