From 631e6865e3b3b409ee4ee14c2f03ebf41d821a2e Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 4 Feb 2021 13:06:56 +0100 Subject: [PATCH] hardlink: cleanup --minimum-size stuff * use uintmax_t * use strtosize_or_err() * add info about suffixes to man page Signed-off-by: Karel Zak --- misc-utils/hardlink.1 | 8 ++++---- misc-utils/hardlink.c | 37 +++++++------------------------------ 2 files changed, 11 insertions(+), 34 deletions(-) diff --git a/misc-utils/hardlink.1 b/misc-utils/hardlink.1 index dbb0b63720..34e8a30c05 100644 --- a/misc-utils/hardlink.1 +++ b/misc-utils/hardlink.1 @@ -59,11 +59,11 @@ A regular expression to include files. If the option \-\-exclude has been given, this option re-includes files which would otherwise be excluded. If the option is used without \-\-exclude, only files matched by the pattern are included. .TP -.B \-s or \-\-minimum\-size +.B \-s or \-\-minimum\-size \fIsize\fP The minimum size to consider. By default this is 1, so empty files will not -be linked. An optional suffix of K,M,G,T may be provided, indicating that the -file size is KiB,MiB,GiB,TiB. - +be linked. The \fIsize\fR argument may be followed by the multiplicative +suffixes KiB (=1024), MiB (=1024*1024), and so on for GiB, TiB, PiB, EiB, ZiB +and YiB (the "iB" is optional, e.g., "K" has the same meaning as "KiB"). .SH ARGUMENTS .B hardlink takes one or more directories which will be searched for files to be linked. diff --git a/misc-utils/hardlink.c b/misc-utils/hardlink.c index 3b696f531b..87201b1731 100644 --- a/misc-utils/hardlink.c +++ b/misc-utils/hardlink.c @@ -40,6 +40,7 @@ #include "nls.h" #include "c.h" #include "xalloc.h" +#include "strutils.h" /* Use libpcre2posix if it's available */ #ifdef HAVE_PCRE2_POSIX @@ -152,7 +153,7 @@ static struct options { unsigned int minimise:1; unsigned int keep_oldest:1; unsigned int dry_run:1; - unsigned long long min_size; + uintmax_t min_size; } opts = { /* default setting */ .respect_mode = TRUE, @@ -770,7 +771,7 @@ static int inserter(const char *fpath, const struct stat *sb, int typeflag, stats.files++; - if (sb->st_size < opts.min_size) { + if ((uintmax_t) sb->st_size < opts.min_size) { jlog(JLOG_DEBUG1, "Skipped %s (smaller than configured size)", fpath); return 0; } @@ -972,13 +973,10 @@ static int parse_options(int argc, char *argv[]) {"content", no_argument, NULL, 'c'}, {NULL, 0, NULL, 0} }; + int c; - int opt; - char unit = '\0'; - - - while ((opt = getopt_long(argc, argv, optstr, long_options, NULL)) != -1) { - switch (opt) { + while ((c = getopt_long(argc, argv, optstr, long_options, NULL)) != -1) { + switch (c) { case 'p': opts.respect_mode = FALSE; break; @@ -1025,28 +1023,7 @@ static int parse_options(int argc, char *argv[]) return 1; break; case 's': - if (sscanf(optarg, "%llu%c", &opts.min_size, &unit) < 1) { - jlog(JLOG_ERROR, "Invalid option given to -s: %s", optarg); - return 1; - } - switch (tolower(unit)) { - case '\0': - break; - case 't': - opts.min_size *= 1024; - case 'g': - opts.min_size *= 1024; - case 'm': - opts.min_size *= 1024; - case 'k': - opts.min_size *= 1024; - break; - default: - jlog(JLOG_ERROR, "Unknown unit indicator %c.", unit); - return 1; - } - jlog(JLOG_DEBUG1, "Using minimum size of %lld bytes.", - opts.min_size); + opts.min_size = strtosize_or_err(optarg, _("failed to parse size")); break; case 'h': -- 2.47.3