]>
Commit | Line | Data |
---|---|---|
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 | ||
56 | static void __attribute__((__noreturn__)) usage(FILE *out) | |
57 | { | |
584aed6b | 58 | fputs(USAGE_HEADER, out); |
130bf041 KZ |
59 | fprintf(out, |
60 | _(" %s [options] <filename>\n"), program_invocation_short_name); | |
584aed6b SK |
61 | fputs(USAGE_OPTIONS, out); |
62 | fputs(_(" -n, --keep-size don't modify the length of the file\n" | |
411fd3c2 | 63 | " -p, --punch-hole punch holes in the file\n" |
d46a5499 | 64 | " -o, --offset <num> offset of the allocation, in bytes\n" |
130bf041 | 65 | " -l, --length <num> length of the allocation, in bytes\n"), out); |
584aed6b SK |
66 | fputs(USAGE_SEPARATOR, out); |
67 | fputs(USAGE_HELP, out); | |
68 | fputs(USAGE_VERSION, out); | |
69 | fprintf(out, USAGE_MAN_TAIL("fallocate(1)")); | |
d46a5499 KZ |
70 | |
71 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
72 | } | |
73 | ||
d46a5499 KZ |
74 | static loff_t cvtnum(char *s) |
75 | { | |
3b6b039a | 76 | uintmax_t x; |
6264af59 | 77 | |
3b6b039a | 78 | if (strtosize(s, &x)) |
d46a5499 | 79 | return -1LL; |
d46a5499 | 80 | |
3b6b039a | 81 | return x; |
d46a5499 KZ |
82 | } |
83 | ||
84 | int main(int argc, char **argv) | |
85 | { | |
86 | char *fname; | |
87 | int c; | |
88 | int error; | |
89 | int fd; | |
90 | int mode = 0; | |
91 | loff_t length = -2LL; | |
92 | loff_t offset = 0; | |
93 | ||
6c7d5ae9 | 94 | static const struct option longopts[] = { |
d46a5499 | 95 | { "help", 0, 0, 'h' }, |
584aed6b | 96 | { "version", 0, 0, 'V' }, |
d46a5499 | 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 | ||
584aed6b | 108 | while ((c = getopt_long(argc, argv, "hVnpl:o:", longopts, NULL)) != -1) { |
d46a5499 KZ |
109 | switch(c) { |
110 | case 'h': | |
111 | usage(stdout); | |
112 | break; | |
584aed6b SK |
113 | case 'V': |
114 | printf(UTIL_LINUX_VERSION); | |
115 | return EXIT_SUCCESS; | |
411fd3c2 CW |
116 | case 'p': |
117 | mode |= FALLOC_FL_PUNCH_HOLE; | |
118 | /* fall through */ | |
d46a5499 KZ |
119 | case 'n': |
120 | mode |= FALLOC_FL_KEEP_SIZE; | |
121 | break; | |
122 | case 'l': | |
123 | length = cvtnum(optarg); | |
124 | break; | |
125 | case 'o': | |
126 | offset = cvtnum(optarg); | |
127 | break; | |
128 | default: | |
129 | usage(stderr); | |
130 | break; | |
131 | } | |
132 | } | |
133 | ||
134 | if (length == -2LL) | |
135 | errx(EXIT_FAILURE, _("no length argument specified")); | |
136 | if (length <= 0) | |
137 | errx(EXIT_FAILURE, _("invalid length value specified")); | |
138 | if (offset < 0) | |
139 | errx(EXIT_FAILURE, _("invalid offset value specified")); | |
140 | if (optind == argc) | |
141 | errx(EXIT_FAILURE, _("no filename specified.")); | |
142 | ||
143 | fname = argv[optind++]; | |
144 | ||
fd1ee3b9 KZ |
145 | if (optind != argc) { |
146 | warnx(_("unexpected number of arguments")); | |
147 | usage(stderr); | |
148 | } | |
149 | ||
d46a5499 KZ |
150 | fd = open(fname, O_WRONLY|O_CREAT, 0644); |
151 | if (fd < 0) | |
152 | err(EXIT_FAILURE, _("%s: open failed"), fname); | |
153 | ||
154 | #ifdef HAVE_FALLOCATE | |
155 | error = fallocate(fd, mode, offset, length); | |
156 | #else | |
157 | error = syscall(SYS_fallocate, fd, mode, offset, length); | |
158 | #endif | |
159 | /* | |
160 | * EOPNOTSUPP: The FALLOC_FL_KEEP_SIZE is unsupported | |
161 | * ENOSYS: The filesystem does not support sys_fallocate | |
162 | */ | |
163 | if (error < 0) { | |
164 | if ((mode & FALLOC_FL_KEEP_SIZE) && errno == EOPNOTSUPP) | |
165 | errx(EXIT_FAILURE, | |
166 | _("keep size mode (-n option) unsupported")); | |
167 | err(EXIT_FAILURE, _("%s: fallocate failed"), fname); | |
168 | } | |
169 | ||
170 | close(fd); | |
171 | return EXIT_SUCCESS; | |
172 | } |