ubase

suckless linux base utils
git clone git://git.suckless.org/ubase
Log | Files | Refs | README | LICENSE

commit e3b20bbda0a7a23141702b3250dc64f4fd2d8f87
parent 90c75840894958edb000a1091e13d5dac6b86279
Author: FRIGN <dev@frign.de>
Date:   Fri, 11 Sep 2015 00:27:29 +0200

Refactor fallocate(1)

1) Simplify the manpage. Just refer to fallocate(2) and stop trying
   to list supported file systems. This can change and everbody
   with common sense can bring up the relevant manpages of a given
   operating system himself.
   Use the num-semantics.
2) Use estrtonum() instead of estrtol().
3) Allow multiple arguments.

Diffstat:
Mfallocate.1 | 34++++++++++++++++++++--------------
Mfallocate.c | 37+++++++++++++++++++++++--------------
Mutil.h | 8++++++++
3 files changed, 51 insertions(+), 28 deletions(-)

diff --git a/fallocate.1 b/fallocate.1 @@ -1,27 +1,33 @@ -.Dd February 2, 2015 +.Dd September 11, 2015 .Dt FALLOCATE 1 .Os ubase .Sh NAME .Nm fallocate -.Nd preallocate blocks to a file +.Nd reallocate files .Sh SYNOPSIS .Nm -.Op Fl o Ar offset -.Fl l Ar length Ar file +.Op Fl o Ar num +.Fl l Ar num +.Ar file ... .Sh DESCRIPTION .Nm -preallocates blocks to a file. Only certain filesystems support the -.Xr fallocate 2 -system call. This is a very fast operation to allocate uninitialized blocks -in a file without doing any IO. As of the Linux kernel v2.6.31, the -.Xr fallocate 2 -system call is supported by the btrfs, ext4, ocfs2, and xfs filesystems. +if necessary creates and reallocates each +.Ar file +resulting in expansion or truncation. +.sp +Given the filesystem supports +.Xr fallocate 2 , +it is a very fast method of reallocation. .Sh OPTIONS .Bl -tag -width Ds -.It Fl l Ar length -Specifies the length of the allocation, in bytes. -.It Fl o -Specifies the beginning offset of the allocation, in bytes. +.It Fl l Ar num +Allocate +.Ar num +bytes. +.It Fl o Ar num +Offset allocation by +.Ar num +bytes. .El .Sh SEE ALSO .Xr fallocate 2 diff --git a/fallocate.c b/fallocate.c @@ -2,6 +2,8 @@ #include <sys/stat.h> #include <fcntl.h> +#include <limits.h> +#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> @@ -11,36 +13,43 @@ static void usage(void) { - eprintf("usage: %s [-o offset] -l length file\n", argv0); + eprintf("usage: %s [-o num] -l num file ...\n", argv0); } int main(int argc, char *argv[]) { - int fd; + int fd, ret = 0; off_t size = 0, offset = 0; ARGBEGIN { case 'l': - size = estrtol(EARGF(usage()), 10); + size = estrtonum(EARGF(usage()), 1, MIN(LLONG_MAX, SIZE_MAX)); break; case 'o': - offset = estrtol(EARGF(usage()), 10); + offset = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX)); break; default: usage(); } ARGEND; - if (argc != 1 || !size) + if (!argc || !size) usage(); - fd = open(argv[0], O_RDWR | O_CREAT, 0644); - if (fd < 0) - eprintf("open %s:", argv[0]); - - if (posix_fallocate(fd, offset, size) < 0) - eprintf("posix_fallocate:"); - - close(fd); - return 0; + for (; *argv; argc--, argv++) { + if ((fd = open(*argv, O_RDWR | O_CREAT, 0644)) < 0) { + weprintf("open %s:", *argv); + ret = 1; + } else if (posix_fallocate(fd, offset, size) < 0) { + weprintf("posix_fallocate %s:", *argv); + ret = 1; + } + + if (fd >= 0 && close(fd) < 0) { + weprintf("close %s:", *argv); + ret = 1; + } + } + + return ret; } diff --git a/util.h b/util.h @@ -2,6 +2,14 @@ #include "arg.h" #define UTF8_POINT(c) (((c) & 0xc0) != 0x80) + +#undef MIN +#define MIN(x,y) ((x) < (y) ? (x) : (y)) +#undef MAX +#define MAX(x,y) ((x) > (y) ? (x) : (y)) +#undef LIMIT +#define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x) + #define LEN(x) (sizeof (x) / sizeof *(x)) /* eprintf.c */