]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/fallocate.c
fallocate: Hide #ifdef tricks to call fallocate in a function
[thirdparty/util-linux.git] / sys-utils / fallocate.c
CommitLineData
d46a5499
KZ
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 *
7cebf0bb
SK
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
d46a5499
KZ
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>
6264af59 33#include <limits.h>
d46a5499
KZ
34
35#ifndef HAVE_FALLOCATE
36# include <sys/syscall.h>
37#endif
38
bc5ddf0c
KZ
39#if defined(HAVE_LINUX_FALLOC_H) && \
40 (!defined(FALLOC_FL_KEEP_SIZE) || !defined(FALLOC_FL_PUNCH_HOLE))
41# include <linux/falloc.h> /* non-libc fallback for FALLOC_FL_* flags */
f75b8e5c
KZ
42#endif
43
44#ifndef FALLOC_FL_KEEP_SIZE
fac8b4bd 45# define FALLOC_FL_KEEP_SIZE 1
f75b8e5c
KZ
46#endif
47
48#ifndef FALLOC_FL_PUNCH_HOLE
411fd3c2 49# define FALLOC_FL_PUNCH_HOLE 2
fac8b4bd 50#endif
d46a5499
KZ
51
52#include "nls.h"
8abcf290 53#include "strutils.h"
eb76ca98 54#include "c.h"
efb8854f 55#include "closestream.h"
d46a5499
KZ
56
57static void __attribute__((__noreturn__)) usage(FILE *out)
58{
584aed6b 59 fputs(USAGE_HEADER, out);
130bf041
KZ
60 fprintf(out,
61 _(" %s [options] <filename>\n"), program_invocation_short_name);
584aed6b
SK
62 fputs(USAGE_OPTIONS, out);
63 fputs(_(" -n, --keep-size don't modify the length of the file\n"
411fd3c2 64 " -p, --punch-hole punch holes in the file\n"
a8d10d1c
RC
65 " -o, --offset <num> offset of the (de)allocation, in bytes\n"
66 " -l, --length <num> length of the (de)allocation, in bytes\n"), out);
584aed6b
SK
67 fputs(USAGE_SEPARATOR, out);
68 fputs(USAGE_HELP, out);
69 fputs(USAGE_VERSION, out);
70 fprintf(out, USAGE_MAN_TAIL("fallocate(1)"));
d46a5499
KZ
71
72 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
73}
74
d46a5499
KZ
75static loff_t cvtnum(char *s)
76{
3b6b039a 77 uintmax_t x;
6264af59 78
3b6b039a 79 if (strtosize(s, &x))
d46a5499 80 return -1LL;
d46a5499 81
3b6b039a 82 return x;
d46a5499
KZ
83}
84
bcd9315d
RC
85static int xfallocate(int fd, int mode, off_t offset, off_t length)
86{
87 int error;
88
89#ifdef HAVE_FALLOCATE
90 error = fallocate(fd, mode, offset, length);
91#else
92 error = syscall(SYS_fallocate, fd, mode, offset, length);
93#endif
94 /*
95 * EOPNOTSUPP: The FALLOC_FL_KEEP_SIZE is unsupported
96 * ENOSYS: The filesystem does not support sys_fallocate
97 */
98 if (error < 0) {
99 if ((mode & FALLOC_FL_KEEP_SIZE) && errno == EOPNOTSUPP) {
100 fputs(_("keep size mode (-n option) unsupported\n"),
101 stderr);
102 } else {
103 fputs(_("fallocate failed\n"), stderr);
104 }
105 }
106 return error;
107}
108
d46a5499
KZ
109int main(int argc, char **argv)
110{
111 char *fname;
112 int c;
113 int error;
114 int fd;
115 int mode = 0;
116 loff_t length = -2LL;
117 loff_t offset = 0;
118
6c7d5ae9 119 static const struct option longopts[] = {
d46a5499 120 { "help", 0, 0, 'h' },
584aed6b 121 { "version", 0, 0, 'V' },
d46a5499 122 { "keep-size", 0, 0, 'n' },
411fd3c2 123 { "punch-hole", 0, 0, 'p' },
d46a5499 124 { "offset", 1, 0, 'o' },
042154d7 125 { "length", 1, 0, 'l' },
d46a5499
KZ
126 { NULL, 0, 0, 0 }
127 };
128
129 setlocale(LC_ALL, "");
130 bindtextdomain(PACKAGE, LOCALEDIR);
131 textdomain(PACKAGE);
efb8854f 132 atexit(close_stdout);
d46a5499 133
584aed6b 134 while ((c = getopt_long(argc, argv, "hVnpl:o:", longopts, NULL)) != -1) {
d46a5499
KZ
135 switch(c) {
136 case 'h':
137 usage(stdout);
138 break;
584aed6b
SK
139 case 'V':
140 printf(UTIL_LINUX_VERSION);
141 return EXIT_SUCCESS;
411fd3c2
CW
142 case 'p':
143 mode |= FALLOC_FL_PUNCH_HOLE;
144 /* fall through */
d46a5499
KZ
145 case 'n':
146 mode |= FALLOC_FL_KEEP_SIZE;
147 break;
148 case 'l':
149 length = cvtnum(optarg);
150 break;
151 case 'o':
152 offset = cvtnum(optarg);
153 break;
154 default:
155 usage(stderr);
156 break;
157 }
158 }
159
160 if (length == -2LL)
161 errx(EXIT_FAILURE, _("no length argument specified"));
162 if (length <= 0)
163 errx(EXIT_FAILURE, _("invalid length value specified"));
164 if (offset < 0)
165 errx(EXIT_FAILURE, _("invalid offset value specified"));
166 if (optind == argc)
167 errx(EXIT_FAILURE, _("no filename specified."));
168
169 fname = argv[optind++];
170
fd1ee3b9
KZ
171 if (optind != argc) {
172 warnx(_("unexpected number of arguments"));
173 usage(stderr);
174 }
175
d46a5499
KZ
176 fd = open(fname, O_WRONLY|O_CREAT, 0644);
177 if (fd < 0)
289dcc90 178 err(EXIT_FAILURE, _("cannot open %s"), fname);
d46a5499 179
bcd9315d
RC
180 error = xfallocate(fd, mode, offset, length);
181
182 if (error < 0)
183 exit(EXIT_FAILURE);
d46a5499 184
5f52af50
SK
185 if (close_fd(fd) != 0)
186 err(EXIT_FAILURE, _("write failed: %s"), fname);
d46a5499
KZ
187 return EXIT_SUCCESS;
188}