fallocate: support suffixes for --offset and --lenght
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
parent
20543e618a
commit
3b6b039ae8
|
@ -25,6 +25,7 @@ info_TEXINFOS = ipc.texi
|
|||
|
||||
if BUILD_FALLOCATE
|
||||
usrbin_exec_PROGRAMS += fallocate
|
||||
fallocate_SOURCES = fallocate.c ../lib/strtosize.c
|
||||
dist_man_MANS += fallocate.1
|
||||
endif
|
||||
|
||||
|
|
|
@ -25,17 +25,18 @@ The exit code returned by
|
|||
is 0 on success and 1 on failure.
|
||||
.PP
|
||||
.SH OPTIONS
|
||||
The \fIlength\fR and \fIoffset\fR arguments may be followed by binary (2^N)
|
||||
suffixes KiB, MiB, GiB, TiB, PiB and EiB (the "iB" is optional, e.g. "K" has the
|
||||
same meaning as "KiB") or decimal (10^N) suffixes KB, MB, GB, PB and EB.
|
||||
.IP "\fB\-h, \-\-help\fP"
|
||||
Print help and exit.
|
||||
.IP "\fB\-n, \-\-keep-size\fP"
|
||||
Do not modify the apparent length of the file. This may effectively allocate
|
||||
blocks past EOF, which can be removed with a truncate.
|
||||
.IP "\fB\-o, \-\-offset\fP \fIoffset\fP
|
||||
Specifies the beginning offset of the allocation, in bytes. Suffixes of k, m,
|
||||
g, t, p, e may be specified to denote KiB, MiB, GiB, etc.
|
||||
Specifies the beginning offset of the allocation, in bytes.
|
||||
.IP "\fB\-l, \-\-length\fP \fIlength\fP
|
||||
Specifies the length of the allocation, in bytes. Suffixes of k, m, g, t, p, e
|
||||
may be specified to denote KiB, MiB, GiB, etc.
|
||||
Specifies the length of the allocation, in bytes.
|
||||
.SH AUTHORS
|
||||
.nf
|
||||
Eric Sandeen <sandeen@redhat.com>
|
||||
|
|
|
@ -40,6 +40,7 @@
|
|||
#include <linux/falloc.h> /* for FALLOC_FL_* flags */
|
||||
|
||||
#include "nls.h"
|
||||
#include "strtosize.h"
|
||||
|
||||
|
||||
static void __attribute__((__noreturn__)) usage(FILE *out)
|
||||
|
@ -58,47 +59,14 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
|
|||
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
#define EXABYTES(x) ((x) << 60)
|
||||
#define PETABYTES(x) ((x) << 50)
|
||||
#define TERABYTES(x) ((x) << 40)
|
||||
#define GIGABYTES(x) ((x) << 30)
|
||||
#define MEGABYTES(x) ((x) << 20)
|
||||
#define KILOBYTES(x) ((x) << 10)
|
||||
|
||||
static loff_t cvtnum(char *s)
|
||||
{
|
||||
loff_t i;
|
||||
char *sp;
|
||||
uintmax_t x;
|
||||
|
||||
errno = 0;
|
||||
i = strtoll(s, &sp, 0);
|
||||
|
||||
if ((errno == ERANGE && (i == LLONG_MAX || i == LLONG_MIN)) ||
|
||||
(errno != 0 && i == 0))
|
||||
return -1LL;
|
||||
if (i == 0 && sp == s)
|
||||
return -1LL;
|
||||
if (*sp == '\0')
|
||||
return i;
|
||||
if (sp[1] != '\0')
|
||||
if (strtosize(s, &x))
|
||||
return -1LL;
|
||||
|
||||
switch (tolower(*sp)) {
|
||||
case 'k':
|
||||
return KILOBYTES(i);
|
||||
case 'm':
|
||||
return MEGABYTES(i);
|
||||
case 'g':
|
||||
return GIGABYTES(i);
|
||||
case 't':
|
||||
return TERABYTES(i);
|
||||
case 'p':
|
||||
return PETABYTES(i);
|
||||
case 'e':
|
||||
return EXABYTES(i);
|
||||
}
|
||||
|
||||
return -1LL;
|
||||
return x;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
|
|
Loading…
Reference in New Issue