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.
This commit is contained in:
FRIGN 2015-09-11 00:27:29 +02:00 committed by sin
parent 90c7584089
commit e3b20bbda0
3 changed files with 49 additions and 26 deletions

View File

@ -1,27 +1,33 @@
.Dd February 2, 2015 .Dd September 11, 2015
.Dt FALLOCATE 1 .Dt FALLOCATE 1
.Os ubase .Os ubase
.Sh NAME .Sh NAME
.Nm fallocate .Nm fallocate
.Nd preallocate blocks to a file .Nd reallocate files
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl o Ar offset .Op Fl o Ar num
.Fl l Ar length Ar file .Fl l Ar num
.Ar file ...
.Sh DESCRIPTION .Sh DESCRIPTION
.Nm .Nm
preallocates blocks to a file. Only certain filesystems support the if necessary creates and reallocates each
.Xr fallocate 2 .Ar file
system call. This is a very fast operation to allocate uninitialized blocks resulting in expansion or truncation.
in a file without doing any IO. As of the Linux kernel v2.6.31, the .sp
.Xr fallocate 2 Given the filesystem supports
system call is supported by the btrfs, ext4, ocfs2, and xfs filesystems. .Xr fallocate 2 ,
it is a very fast method of reallocation.
.Sh OPTIONS .Sh OPTIONS
.Bl -tag -width Ds .Bl -tag -width Ds
.It Fl l Ar length .It Fl l Ar num
Specifies the length of the allocation, in bytes. Allocate
.It Fl o .Ar num
Specifies the beginning offset of the allocation, in bytes. bytes.
.It Fl o Ar num
Offset allocation by
.Ar num
bytes.
.El .El
.Sh SEE ALSO .Sh SEE ALSO
.Xr fallocate 2 .Xr fallocate 2

View File

@ -2,6 +2,8 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
@ -11,36 +13,43 @@
static void static void
usage(void) usage(void)
{ {
eprintf("usage: %s [-o offset] -l length file\n", argv0); eprintf("usage: %s [-o num] -l num file ...\n", argv0);
} }
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int fd; int fd, ret = 0;
off_t size = 0, offset = 0; off_t size = 0, offset = 0;
ARGBEGIN { ARGBEGIN {
case 'l': case 'l':
size = estrtol(EARGF(usage()), 10); size = estrtonum(EARGF(usage()), 1, MIN(LLONG_MAX, SIZE_MAX));
break; break;
case 'o': case 'o':
offset = estrtol(EARGF(usage()), 10); offset = estrtonum(EARGF(usage()), 0, MIN(LLONG_MAX, SIZE_MAX));
break; break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;
if (argc != 1 || !size) if (!argc || !size)
usage(); usage();
fd = open(argv[0], O_RDWR | O_CREAT, 0644); for (; *argv; argc--, argv++) {
if (fd < 0) if ((fd = open(*argv, O_RDWR | O_CREAT, 0644)) < 0) {
eprintf("open %s:", argv[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) if (fd >= 0 && close(fd) < 0) {
eprintf("posix_fallocate:"); weprintf("close %s:", *argv);
ret = 1;
}
}
close(fd); return ret;
return 0;
} }

8
util.h
View File

@ -2,6 +2,14 @@
#include "arg.h" #include "arg.h"
#define UTF8_POINT(c) (((c) & 0xc0) != 0x80) #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)) #define LEN(x) (sizeof (x) / sizeof *(x))
/* eprintf.c */ /* eprintf.c */