2 * fallocate - utility to use the fallocate system call
4 * Copyright (C) 2008-2009 Red Hat, Inc. All rights reserved.
5 * Written by Eric Sandeen <sandeen@redhat.com>
6 * Karel Zak <kzak@redhat.com>
8 * cvtnum routine taken from xfsprogs,
9 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it would be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/types.h>
36 #ifndef HAVE_FALLOCATE
37 # include <sys/syscall.h>
40 #ifdef HAVE_LINUX_FALLOC_H
41 # include <linux/falloc.h> /* for FALLOC_FL_* flags */
43 # define FALLOC_FL_KEEP_SIZE 1
47 #include "strtosize.h"
50 static void __attribute__((__noreturn__
)) usage(FILE *out
)
52 fprintf(out
, _("Usage: %s [options] <filename>\n\nOptions:\n"),
53 program_invocation_short_name
);
56 " -h, --help this help\n"
57 " -n, --keep-size don't modify the length of the file\n"
58 " -o, --offset <num> offset of the allocation, in bytes\n"
59 " -l, --length <num> length of the allocation, in bytes\n"));
61 fprintf(out
, _("\nFor more information see fallocate(1).\n"));
63 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
66 static loff_t
cvtnum(char *s
)
76 int main(int argc
, char **argv
)
86 struct option longopts
[] = {
87 { "help", 0, 0, 'h' },
88 { "keep-size", 0, 0, 'n' },
89 { "offset", 1, 0, 'o' },
90 { "lenght", 1, 0, 'l' },
94 setlocale(LC_ALL
, "");
95 bindtextdomain(PACKAGE
, LOCALEDIR
);
98 while ((c
= getopt_long(argc
, argv
, "hnl:o:", longopts
, NULL
)) != -1) {
104 mode
|= FALLOC_FL_KEEP_SIZE
;
107 length
= cvtnum(optarg
);
110 offset
= cvtnum(optarg
);
119 errx(EXIT_FAILURE
, _("no length argument specified"));
121 errx(EXIT_FAILURE
, _("invalid length value specified"));
123 errx(EXIT_FAILURE
, _("invalid offset value specified"));
125 errx(EXIT_FAILURE
, _("no filename specified."));
127 fname
= argv
[optind
++];
129 if (optind
!= argc
) {
130 warnx(_("unexpected number of arguments"));
134 fd
= open(fname
, O_WRONLY
|O_CREAT
, 0644);
136 err(EXIT_FAILURE
, _("%s: open failed"), fname
);
138 #ifdef HAVE_FALLOCATE
139 error
= fallocate(fd
, mode
, offset
, length
);
141 error
= syscall(SYS_fallocate
, fd
, mode
, offset
, length
);
144 * EOPNOTSUPP: The FALLOC_FL_KEEP_SIZE is unsupported
145 * ENOSYS: The filesystem does not support sys_fallocate
148 if ((mode
& FALLOC_FL_KEEP_SIZE
) && errno
== EOPNOTSUPP
)
150 _("keep size mode (-n option) unsupported"));
151 err(EXIT_FAILURE
, _("%s: fallocate failed"), fname
);