]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/fallocate.c
fallocate: fix build failure with old linux headers
[thirdparty/util-linux.git] / sys-utils / fallocate.c
1 /*
2 * fallocate - utility to use the fallocate system call
3 *
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>
7 *
8 * cvtnum routine taken from xfsprogs,
9 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
10 *
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.
14 *
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.
19 *
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
23 */
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <getopt.h>
33 #include <err.h>
34 #include <limits.h>
35
36 #ifndef HAVE_FALLOCATE
37 # include <sys/syscall.h>
38 #endif
39
40 #ifdef HAVE_LINUX_FALLOC_H
41 # include <linux/falloc.h> /* for FALLOC_FL_* flags */
42 #else
43 # define FALLOC_FL_KEEP_SIZE 1
44 #endif
45
46 #include "nls.h"
47 #include "strtosize.h"
48
49
50 static void __attribute__((__noreturn__)) usage(FILE *out)
51 {
52 fprintf(out, _("Usage: %s [options] <filename>\n\nOptions:\n"),
53 program_invocation_short_name);
54
55 fprintf(out, _(
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"));
60
61 fprintf(out, _("\nFor more information see fallocate(1).\n"));
62
63 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
64 }
65
66 static loff_t cvtnum(char *s)
67 {
68 uintmax_t x;
69
70 if (strtosize(s, &x))
71 return -1LL;
72
73 return x;
74 }
75
76 int main(int argc, char **argv)
77 {
78 char *fname;
79 int c;
80 int error;
81 int fd;
82 int mode = 0;
83 loff_t length = -2LL;
84 loff_t offset = 0;
85
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' },
91 { NULL, 0, 0, 0 }
92 };
93
94 setlocale(LC_ALL, "");
95 bindtextdomain(PACKAGE, LOCALEDIR);
96 textdomain(PACKAGE);
97
98 while ((c = getopt_long(argc, argv, "hnl:o:", longopts, NULL)) != -1) {
99 switch(c) {
100 case 'h':
101 usage(stdout);
102 break;
103 case 'n':
104 mode |= FALLOC_FL_KEEP_SIZE;
105 break;
106 case 'l':
107 length = cvtnum(optarg);
108 break;
109 case 'o':
110 offset = cvtnum(optarg);
111 break;
112 default:
113 usage(stderr);
114 break;
115 }
116 }
117
118 if (length == -2LL)
119 errx(EXIT_FAILURE, _("no length argument specified"));
120 if (length <= 0)
121 errx(EXIT_FAILURE, _("invalid length value specified"));
122 if (offset < 0)
123 errx(EXIT_FAILURE, _("invalid offset value specified"));
124 if (optind == argc)
125 errx(EXIT_FAILURE, _("no filename specified."));
126
127 fname = argv[optind++];
128
129 if (optind != argc) {
130 warnx(_("unexpected number of arguments"));
131 usage(stderr);
132 }
133
134 fd = open(fname, O_WRONLY|O_CREAT, 0644);
135 if (fd < 0)
136 err(EXIT_FAILURE, _("%s: open failed"), fname);
137
138 #ifdef HAVE_FALLOCATE
139 error = fallocate(fd, mode, offset, length);
140 #else
141 error = syscall(SYS_fallocate, fd, mode, offset, length);
142 #endif
143 /*
144 * EOPNOTSUPP: The FALLOC_FL_KEEP_SIZE is unsupported
145 * ENOSYS: The filesystem does not support sys_fallocate
146 */
147 if (error < 0) {
148 if ((mode & FALLOC_FL_KEEP_SIZE) && errno == EOPNOTSUPP)
149 errx(EXIT_FAILURE,
150 _("keep size mode (-n option) unsupported"));
151 err(EXIT_FAILURE, _("%s: fallocate failed"), fname);
152 }
153
154 close(fd);
155 return EXIT_SUCCESS;
156 }