diff --git a/fallocate.1 b/fallocate.1 index ecac614..2fad8d2 100644 --- 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 index 8e20cbb..47eb470 100644 --- a/fallocate.c +++ b/fallocate.c @@ -2,6 +2,8 @@ #include #include +#include +#include #include #include #include @@ -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]); + 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 (posix_fallocate(fd, offset, size) < 0) - eprintf("posix_fallocate:"); + if (fd >= 0 && close(fd) < 0) { + weprintf("close %s:", *argv); + ret = 1; + } + } - close(fd); - return 0; + return ret; } diff --git a/util.h b/util.h index 073156d..6ab609e 100644 --- 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 */