]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
hardlink: cleanup --minimum-size stuff
authorKarel Zak <kzak@redhat.com>
Thu, 4 Feb 2021 12:06:56 +0000 (13:06 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 17 Feb 2021 10:50:21 +0000 (11:50 +0100)
* use uintmax_t
* use strtosize_or_err()
* add info about suffixes to man page

Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/hardlink.1
misc-utils/hardlink.c

index dbb0b6372059bc188bab798c4c7edb149db22c4d..34e8a30c05a6393c828866d218548bf24a548fbf 100644 (file)
@@ -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.
index 3b696f531b061a883965dc081b7e39fa3e2c6cf8..87201b17314a33c211439971f68797db4a6e8a18 100644 (file)
@@ -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':