vi: fixes to string search in colon commands, closes 10321
[oweals/busybox.git] / util-linux / fstrim.c
index 51400ef0b0d228f4c8ef557c74545bddfdd2ed27..48948e839c1f476a81c60a6ee8935c44cfee907c 100644 (file)
@@ -7,15 +7,14 @@
  *
  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
-
 //config:config FSTRIM
-//config:      bool "fstrim"
+//config:      bool "fstrim (4.4 kb)"
 //config:      default y
 //config:      select PLATFORM_LINUX
 //config:      help
-//config:        Discard unused blocks on a mounted filesystem.
+//config:      Discard unused blocks on a mounted filesystem.
 
-//applet:IF_FSTRIM(APPLET(fstrim, BB_DIR_SBIN, BB_SUID_DROP))
+//applet:IF_FSTRIM(APPLET_NOEXEC(fstrim, fstrim, BB_DIR_SBIN, BB_SUID_DROP, fstrim))
 
 //kbuild:lib-$(CONFIG_FSTRIM) += fstrim.o
 
@@ -23,9 +22,9 @@
 //usage:       "[OPTIONS] MOUNTPOINT"
 //usage:#define fstrim_full_usage "\n\n"
 //usage:       IF_LONG_OPTS(
-//usage:       "       -o,--offset=OFFSET      Offset in bytes to discard from"
-//usage:     "\n       -l,--length=LEN         Bytes to discard"
-//usage:     "\n       -m,--minimum=MIN        Minimum extent length"
+//usage:       "       -o,--offset OFFSET      Offset in bytes to discard from"
+//usage:     "\n       -l,--length LEN         Bytes to discard"
+//usage:     "\n       -m,--minimum MIN        Minimum extent length"
 //usage:     "\n       -v,--verbose            Print number of discarded bytes"
 //usage:       )
 //usage:       IF_NOT_LONG_OPTS(
@@ -47,25 +46,6 @@ struct fstrim_range {
 #define FITRIM         _IOWR('X', 121, struct fstrim_range)
 #endif
 
-static const struct suffix_mult fstrim_sfx[] = {
-       { "KiB", 1024 },
-       { "kiB", 1024 },
-       { "K", 1024 },
-       { "k", 1024 },
-       { "MiB", 1048576 },
-       { "miB", 1048576 },
-       { "M", 1048576 },
-       { "m", 1048576 },
-       { "GiB", 1073741824 },
-       { "giB", 1073741824 },
-       { "G", 1073741824 },
-       { "g", 1073741824 },
-       { "KB", 1000 },
-       { "MB", 1000000 },
-       { "GB", 1000000000 },
-       { "", 0 }
-};
-
 int fstrim_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int fstrim_main(int argc UNUSED_PARAM, char **argv)
 {
@@ -82,38 +62,46 @@ int fstrim_main(int argc UNUSED_PARAM, char **argv)
        };
 
 #if ENABLE_LONG_OPTS
-       static const char getopt_longopts[] ALIGN1 =
+       static const char fstrim_longopts[] ALIGN1 =
                "offset\0"    Required_argument    "o"
                "length\0"    Required_argument    "l"
                "minimum\0"   Required_argument    "m"
                "verbose\0"   No_argument          "v"
                ;
-       applet_long_options = getopt_longopts;
 #endif
 
-       opt_complementary = "=1"; /* exactly one non-option arg: the mountpoint */
-       opts = getopt32(argv, "o:l:m:v", &arg_o, &arg_l, &arg_m);
+       opts = getopt32long(argv, "^"
+                       "o:l:m:v"
+                       "\0" "=1", fstrim_longopts,
+                       &arg_o, &arg_l, &arg_m
+       );
 
        memset(&range, 0, sizeof(range));
        range.len = ULLONG_MAX;
 
        if (opts & OPT_o)
-               range.start = xatoull_sfx(arg_o, fstrim_sfx);
+               range.start = xatoull_sfx(arg_o, kmg_i_suffixes);
        if (opts & OPT_l)
-               range.len = xatoull_sfx(arg_l, fstrim_sfx);
+               range.len = xatoull_sfx(arg_l, kmg_i_suffixes);
        if (opts & OPT_m)
-               range.minlen = xatoull_sfx(arg_m, fstrim_sfx);
+               range.minlen = xatoull_sfx(arg_m, kmg_i_suffixes);
 
        mp = argv[optind];
-       if (find_block_device(mp)) {
+//Wwhy bother checking that it's a blockdev?
+//     if (find_block_device(mp)) {
                fd = xopen_nonblocking(mp);
+
+               /* On ENOTTY error, util-linux 2.31 says:
+                * "fstrim: FILE: the discard operation is not supported"
+                */
                xioctl(fd, FITRIM, &range);
+
                if (ENABLE_FEATURE_CLEAN_UP)
                        close(fd);
 
                if (opts & OPT_v)
                        printf("%s: %llu bytes trimmed\n", mp, (unsigned long long)range.len);
                return EXIT_SUCCESS;
-       }
+//     }
        return EXIT_FAILURE;
 }