]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - sys-utils/blkdiscard.c
su: change error message
[thirdparty/util-linux.git] / sys-utils / blkdiscard.c
index f36e504927938a526ec47d7b97eb07d8fb8a21a8..c19b67bf4d9d13bdd7b94d4fd532a3abcdd4b46d 100644 (file)
 #include "monotonic.h"
 
 #ifndef BLKDISCARD
-#define BLKDISCARD     _IO(0x12,119)
+# define BLKDISCARD    _IO(0x12,119)
 #endif
 
 #ifndef BLKSECDISCARD
-#define BLKSECDISCARD  _IO(0x12,125)
+# define BLKSECDISCARD _IO(0x12,125)
 #endif
 
-#define print_stats(path, stats) \
-       printf(_("%s: Discarded %" PRIu64 " bytes from the " \
-                "offset %" PRIu64"\n"), path, stats[1], stats[0]);
+#ifndef BLKZEROOUT
+# define BLKZEROOUT    _IO(0x12,127)
+#endif
+
+enum {
+       ACT_DISCARD = 0,        /* default */
+       ACT_ZEROOUT,
+       ACT_SECURE
+};
 
-static void __attribute__((__noreturn__)) usage(FILE *out)
+static void print_stats(int act, char *path, uint64_t stats[])
 {
+       switch (act) {
+       case ACT_ZEROOUT:
+               printf(_("%s: Zero-filled %" PRIu64 " bytes from the offset %" PRIu64"\n"), \
+                       path, stats[1], stats[0]);
+               break;
+       case ACT_SECURE:
+       case ACT_DISCARD:
+               printf(_("%s: Discarded %" PRIu64 " bytes from the offset %" PRIu64"\n"), \
+                       path, stats[1], stats[0]);
+               break;
+       }
+}
+
+static void __attribute__((__noreturn__)) usage(void)
+{
+       FILE *out = stdout;
        fputs(USAGE_HEADER, out);
        fprintf(out,
              _(" %s [options] <device>\n"), program_invocation_short_name);
@@ -66,36 +88,40 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
        fputs(_("Discard the content of sectors on a device.\n"), out);
 
        fputs(USAGE_OPTIONS, out);
-       fputs(_(" -o, --offset <num>  offset in bytes to discard from\n"
-               " -l, --length <num>  length of bytes to discard from the offset\n"
-               " -p, --step <num>    size of the discard iterations within the offset\n"
-               " -s, --secure        perform secure discard\n"
-               " -v, --verbose       print aligned length and offset\n"),
-               out);
+       fputs(_(" -o, --offset <num>  offset in bytes to discard from\n"), out);
+       fputs(_(" -l, --length <num>  length of bytes to discard from the offset\n"), out);
+       fputs(_(" -p, --step <num>    size of the discard iterations within the offset\n"), out);
+       fputs(_(" -s, --secure        perform secure discard\n"), out);
+       fputs(_(" -z, --zeroout       zero-fill rather than discard\n"), out);
+       fputs(_(" -v, --verbose       print aligned length and offset\n"), out);
+
        fputs(USAGE_SEPARATOR, out);
-       fputs(USAGE_HELP, out);
-       fputs(USAGE_VERSION, out);
-       fprintf(out, USAGE_MAN_TAIL("blkdiscard(8)"));
-       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+       printf(USAGE_HELP_OPTIONS(21));
+
+       printf(USAGE_MAN_TAIL("blkdiscard(8)"));
+       exit(EXIT_SUCCESS);
 }
 
+
 int main(int argc, char **argv)
 {
        char *path;
-       int c, fd, verbose = 0, secure = 0, secsize;
+       int c, fd, verbose = 0, secsize;
        uint64_t end, blksize, step, range[2], stats[2];
        struct stat sb;
        struct timeval now, last;
+       int act = ACT_DISCARD;
 
        static const struct option longopts[] = {
-           { "help",      0, 0, 'h' },
-           { "version",   0, 0, 'V' },
-           { "offset",    1, 0, 'o' },
-           { "length",    1, 0, 'l' },
-           { "step",      1, 0, 'p' },
-           { "secure",    0, 0, 's' },
-           { "verbose",   0, 0, 'v' },
-           { NULL,        0, 0, 0 }
+           { "help",      no_argument,       NULL, 'h' },
+           { "version",   no_argument,       NULL, 'V' },
+           { "offset",    required_argument, NULL, 'o' },
+           { "length",    required_argument, NULL, 'l' },
+           { "step",      required_argument, NULL, 'p' },
+           { "secure",    no_argument,       NULL, 's' },
+           { "verbose",   no_argument,       NULL, 'v' },
+           { "zeroout",   no_argument,       NULL, 'z' },
+           { NULL, 0, NULL, 0 }
        };
 
        setlocale(LC_ALL, "");
@@ -107,10 +133,10 @@ int main(int argc, char **argv)
        range[1] = ULLONG_MAX;
        step = 0;
 
-       while ((c = getopt_long(argc, argv, "hVsvo:l:p:", longopts, NULL)) != -1) {
+       while ((c = getopt_long(argc, argv, "hVsvo:l:p:z", longopts, NULL)) != -1) {
                switch(c) {
                case 'h':
-                       usage(stdout);
+                       usage();
                        break;
                case 'V':
                        printf(UTIL_LINUX_VERSION);
@@ -128,14 +154,16 @@ int main(int argc, char **argv)
                                        _("failed to parse step"));
                        break;
                case 's':
-                       secure = 1;
+                       act = ACT_SECURE;
                        break;
                case 'v':
                        verbose = 1;
                        break;
-               default:
-                       usage(stderr);
+               case 'z':
+                       act = ACT_ZEROOUT;
                        break;
+               default:
+                       errtryhelp(EXIT_FAILURE);
                }
        }
 
@@ -146,7 +174,7 @@ int main(int argc, char **argv)
 
        if (optind != argc) {
                warnx(_("unexpected number of arguments"));
-               usage(stderr);
+               errtryhelp(EXIT_FAILURE);
        }
 
        fd = open(path, O_WRONLY);
@@ -189,12 +217,19 @@ int main(int argc, char **argv)
                if (range[0] + range[1] > end)
                        range[1] = end - range[0];
 
-               if (secure) {
+               switch (act) {
+               case ACT_ZEROOUT:
+                       if (ioctl(fd, BLKZEROOUT, &range))
+                                err(EXIT_FAILURE, _("%s: BLKZEROOUT ioctl failed"), path);
+                       break;
+               case ACT_SECURE:
                        if (ioctl(fd, BLKSECDISCARD, &range))
                                err(EXIT_FAILURE, _("%s: BLKSECDISCARD ioctl failed"), path);
-               } else {
+                       break;
+               case ACT_DISCARD:
                        if (ioctl(fd, BLKDISCARD, &range))
                                err(EXIT_FAILURE, _("%s: BLKDISCARD ioctl failed"), path);
+                       break;
                }
 
                stats[1] += range[1];
@@ -204,7 +239,7 @@ int main(int argc, char **argv)
                        gettime_monotonic(&now);
                        if (now.tv_sec > last.tv_sec &&
                            (now.tv_usec >= last.tv_usec || now.tv_sec > last.tv_sec + 1)) {
-                               print_stats(path, stats);
+                               print_stats(act, path, stats);
                                stats[0] += stats[1], stats[1] = 0;
                                last = now;
                        }
@@ -212,7 +247,7 @@ int main(int argc, char **argv)
        }
 
        if (verbose && stats[1])
-               print_stats(path, stats);
+               print_stats(act, path, stats);
 
        close(fd);
        return EXIT_SUCCESS;