]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fallocate: add --verbose, clean up usage()
authorKarel Zak <kzak@redhat.com>
Fri, 14 Feb 2014 13:37:51 +0000 (14:37 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 14 Feb 2014 13:37:51 +0000 (14:37 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
bash-completion/fallocate
sys-utils/fallocate.1
sys-utils/fallocate.c

index ce5f4f14cd1d58c0749505a47f8840b23f71609a..1a98eb085778996bc96429db47b7dffee7f6d71b 100644 (file)
@@ -15,7 +15,7 @@ _fallocate_module()
        esac
        case $cur in
                -*)
-                       OPTS="--keep-size --punch-hole --dig-holes --offset --length --help --version"
+                       OPTS="--keep-size --punch-hole --dig-holes --offset --length --help --verbose --version"
                        COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) )
                        return 0
                        ;;
index 04c426375970def50677f915c9bbcfd0d25ce916..8f862ab61ec514093060a6160113fba4d893455c 100644 (file)
@@ -59,6 +59,8 @@ Specifies the beginning offset of the allocation, in bytes.
 Specifies the length of the allocation, in bytes.
 .IP "\fB\-h, \-\-help\fP"
 Display help text and exit.
+.IP "\fB-v, \-\-verbose"
+Enable verbose mode.
 .IP "\fB-V, \-\-version"
 Display version information and exit.
 .SH AUTHORS
index b03d123724aa84792ddecdcd304be9caa13f52a2..64be20de856314e75e52d752066f911d0b34dfe8 100644 (file)
 #include "closestream.h"
 #include "xalloc.h"
 
+static int verbose;
+static char *filename;
+
 static void __attribute__((__noreturn__)) usage(FILE *out)
 {
        fputs(USAGE_HEADER, out);
        fprintf(out,
              _(" %s [options] <filename>\n"), program_invocation_short_name);
        fputs(USAGE_OPTIONS, out);
-       fputs(_(" -n, --keep-size     don't modify the length of the file\n"
-               " -p, --punch-hole    punch holes in the file\n"
-               " -d, --dig-holes     detect and dig holes\n"
-               " -o, --offset <num>  offset of the (de)allocation, in bytes\n"
-               " -l, --length <num>  length of the (de)allocation, in bytes\n"), out);
+       fputs(_(" -d, --dig-holes     detect and dig holes\n"), out);
+       fputs(_(" -l, --length <num>  length of the (de)allocation, in bytes\n"), out);
+       fputs(_(" -n, --keep-size     don't modify the length of the file\n"), out);
+       fputs(_(" -o, --offset <num>  offset of the (de)allocation, in bytes\n"), out);
+       fputs(_(" -p, --punch-hole    punch holes in the file\n"), out);
+       fputs(_(" -v, --verbose       verbose mode\n"), out);
+
        fputs(USAGE_SEPARATOR, out);
        fputs(USAGE_HELP, out);
        fputs(USAGE_VERSION, out);
@@ -140,6 +145,10 @@ static void detect_holes(int fd, size_t hole_size)
                if (memcmp(buf, zeros, bufsz))
                        continue;
 
+               if (verbose)
+                       fprintf(stdout, "%s: detected hole at offset %ju (size %ju)\n",
+                               filename, (uintmax_t) offset, (uintmax_t) bufsz);
+
                xfallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE,
                                offset, bufsz);
        }
@@ -151,7 +160,6 @@ static void detect_holes(int fd, size_t hole_size)
 
 int main(int argc, char **argv)
 {
-       char    *fname;
        int     c;
        int     fd;
        int     mode = 0;
@@ -167,6 +175,7 @@ int main(int argc, char **argv)
            { "dig-holes",  0, 0, 'd' },
            { "offset",     1, 0, 'o' },
            { "length",     1, 0, 'l' },
+           { "verbose",    0, 0, 'v' },
            { NULL,         0, 0, 0 }
        };
 
@@ -175,7 +184,7 @@ int main(int argc, char **argv)
        textdomain(PACKAGE);
        atexit(close_stdout);
 
-       while ((c = getopt_long(argc, argv, "hVnpdl:o:", longopts, NULL)) != -1) {
+       while ((c = getopt_long(argc, argv, "hvVnpdl:o:", longopts, NULL)) != -1) {
                switch(c) {
                case 'h':
                        usage(stdout);
@@ -198,6 +207,9 @@ int main(int argc, char **argv)
                case 'o':
                        offset = cvtnum(optarg);
                        break;
+               case 'v':
+                       verbose++;
+                       break;
                default:
                        usage(stderr);
                        break;
@@ -218,24 +230,29 @@ int main(int argc, char **argv)
        if (optind == argc)
                errx(EXIT_FAILURE, _("no filename specified."));
 
-       fname = argv[optind++];
+       filename = argv[optind++];
 
        if (optind != argc) {
                warnx(_("unexpected number of arguments"));
                usage(stderr);
        }
 
-       fd = open(fname, O_RDWR|O_CREAT, 0644);
+       fd = open(filename, O_RDWR|O_CREAT, 0644);
        if (fd < 0)
-               err(EXIT_FAILURE, _("cannot open %s"), fname);
+               err(EXIT_FAILURE, _("cannot open %s"), filename);
 
        if (dig_holes)
                detect_holes(fd, length);
-       else
+       else {
+               if (verbose)
+                       fprintf(stdout, "%s: fallocate offset=%ju, length=%ju\n",
+                                       filename, (uintmax_t) offset,
+                                       (uintmax_t) length);
                xfallocate(fd, mode, offset, length);
+       }
 
        if (close_fd(fd) != 0)
-               err(EXIT_FAILURE, _("write failed: %s"), fname);
+               err(EXIT_FAILURE, _("write failed: %s"), filename);
 
        return EXIT_SUCCESS;
 }