]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/fallocate.c
swaplabel: add --version and align with howto-usage-function.txt
[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
fac8b4bd
MF
39#ifdef HAVE_LINUX_FALLOC_H
40# include <linux/falloc.h> /* for FALLOC_FL_* flags */
f75b8e5c
KZ
41#endif
42
43#ifndef FALLOC_FL_KEEP_SIZE
fac8b4bd 44# define FALLOC_FL_KEEP_SIZE 1
f75b8e5c
KZ
45#endif
46
47#ifndef FALLOC_FL_PUNCH_HOLE
411fd3c2 48# define FALLOC_FL_PUNCH_HOLE 2
fac8b4bd 49#endif
d46a5499
KZ
50
51#include "nls.h"
8abcf290 52#include "strutils.h"
eb76ca98 53#include "c.h"
d46a5499
KZ
54
55
56static void __attribute__((__noreturn__)) usage(FILE *out)
57{
d46a5499 58
130bf041
KZ
59 fputs(_("\nUsage:\n"), out);
60 fprintf(out,
61 _(" %s [options] <filename>\n"), program_invocation_short_name);
62
63 fputs(_("\nOptions:\n"), out);
64 fputs(_(" -h, --help this help\n"
d46a5499 65 " -n, --keep-size don't modify the length of the file\n"
411fd3c2 66 " -p, --punch-hole punch holes in the file\n"
d46a5499 67 " -o, --offset <num> offset of the allocation, in bytes\n"
130bf041 68 " -l, --length <num> length of the allocation, in bytes\n"), out);
d46a5499
KZ
69
70 fprintf(out, _("\nFor more information see fallocate(1).\n"));
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
85int main(int argc, char **argv)
86{
87 char *fname;
88 int c;
89 int error;
90 int fd;
91 int mode = 0;
92 loff_t length = -2LL;
93 loff_t offset = 0;
94
6c7d5ae9 95 static const struct option longopts[] = {
d46a5499
KZ
96 { "help", 0, 0, 'h' },
97 { "keep-size", 0, 0, 'n' },
411fd3c2 98 { "punch-hole", 0, 0, 'p' },
d46a5499 99 { "offset", 1, 0, 'o' },
042154d7 100 { "length", 1, 0, 'l' },
d46a5499
KZ
101 { NULL, 0, 0, 0 }
102 };
103
104 setlocale(LC_ALL, "");
105 bindtextdomain(PACKAGE, LOCALEDIR);
106 textdomain(PACKAGE);
107
411fd3c2 108 while ((c = getopt_long(argc, argv, "hnpl:o:", longopts, NULL)) != -1) {
d46a5499
KZ
109 switch(c) {
110 case 'h':
111 usage(stdout);
112 break;
411fd3c2
CW
113 case 'p':
114 mode |= FALLOC_FL_PUNCH_HOLE;
115 /* fall through */
d46a5499
KZ
116 case 'n':
117 mode |= FALLOC_FL_KEEP_SIZE;
118 break;
119 case 'l':
120 length = cvtnum(optarg);
121 break;
122 case 'o':
123 offset = cvtnum(optarg);
124 break;
125 default:
126 usage(stderr);
127 break;
128 }
129 }
130
131 if (length == -2LL)
132 errx(EXIT_FAILURE, _("no length argument specified"));
133 if (length <= 0)
134 errx(EXIT_FAILURE, _("invalid length value specified"));
135 if (offset < 0)
136 errx(EXIT_FAILURE, _("invalid offset value specified"));
137 if (optind == argc)
138 errx(EXIT_FAILURE, _("no filename specified."));
139
140 fname = argv[optind++];
141
fd1ee3b9
KZ
142 if (optind != argc) {
143 warnx(_("unexpected number of arguments"));
144 usage(stderr);
145 }
146
d46a5499
KZ
147 fd = open(fname, O_WRONLY|O_CREAT, 0644);
148 if (fd < 0)
149 err(EXIT_FAILURE, _("%s: open failed"), fname);
150
151#ifdef HAVE_FALLOCATE
152 error = fallocate(fd, mode, offset, length);
153#else
154 error = syscall(SYS_fallocate, fd, mode, offset, length);
155#endif
156 /*
157 * EOPNOTSUPP: The FALLOC_FL_KEEP_SIZE is unsupported
158 * ENOSYS: The filesystem does not support sys_fallocate
159 */
160 if (error < 0) {
161 if ((mode & FALLOC_FL_KEEP_SIZE) && errno == EOPNOTSUPP)
162 errx(EXIT_FAILURE,
163 _("keep size mode (-n option) unsupported"));
164 err(EXIT_FAILURE, _("%s: fallocate failed"), fname);
165 }
166
167 close(fd);
168 return EXIT_SUCCESS;
169}