]>
git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - contrib/fallocate.c
2 * fallocate - utility to use the fallocate system call
4 * Copyright (C) 2008 Red Hat, Inc. All rights reserved.
5 * Written by Eric Sandeen <sandeen@redhat.com>
7 * cvtnum routine taken from xfsprogs,
8 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it would be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #ifndef _LARGEFILE_SOURCE
25 #define _LARGEFILE_SOURCE
27 #ifndef _LARGEFILE64_SOURCE
28 #define _LARGEFILE64_SOURCE
32 #include <sys/syscall.h>
33 #include <sys/types.h>
40 // #include <linux/falloc.h>
41 #define FALLOC_FL_KEEP_SIZE 0x01
42 #define FALLOC_FL_PUNCH_HOLE 0x02 /* de-allocates range */
43 #define FALLOC_FL_COLLAPSE_RANGE 0x08
44 #define FALLOC_FL_ZERO_RANGE 0x10
48 printf("Usage: fallocate [-npt] [-o offset] -l length filename\n");
52 #define EXABYTES(x) ((long long)(x) << 60)
53 #define PETABYTES(x) ((long long)(x) << 50)
54 #define TERABYTES(x) ((long long)(x) << 40)
55 #define GIGABYTES(x) ((long long)(x) << 30)
56 #define MEGABYTES(x) ((long long)(x) << 20)
57 #define KILOBYTES(x) ((long long)(x) << 10)
66 i
= strtoll(s
, &sp
, 0);
67 if (i
== 0 && sp
== s
)
93 int main(int argc
, char **argv
)
104 while ((opt
= getopt(argc
, argv
, "npl:o:tzc")) != -1) {
107 /* do not change filesize */
108 falloc_mode
= FALLOC_FL_KEEP_SIZE
;
112 falloc_mode
= (FALLOC_FL_PUNCH_HOLE
|
113 FALLOC_FL_KEEP_SIZE
);
116 /* collapse range mode */
117 falloc_mode
= (FALLOC_FL_COLLAPSE_RANGE
|
118 FALLOC_FL_KEEP_SIZE
);
121 /* zero range mode */
122 falloc_mode
= (FALLOC_FL_ZERO_RANGE
|
123 FALLOC_FL_KEEP_SIZE
);
126 length
= cvtnum(optarg
);
129 offset
= cvtnum(optarg
);
139 if (length
== -2LL) {
140 printf("Error: no length argument specified\n");
145 printf("Error: invalid length value specified\n");
150 printf("Error: invalid offset value specified\n");
154 if (tflag
&& (falloc_mode
& FALLOC_FL_KEEP_SIZE
)) {
155 printf("-n and -t options incompatible\n");
159 if (tflag
&& offset
) {
160 printf("-n and -o options incompatible\n");
164 if (optind
== argc
) {
165 printf("Error: no filename specified\n");
169 fname
= argv
[optind
++];
171 /* Should we create the file if it doesn't already exist? */
172 fd
= open(fname
, O_WRONLY
|O_LARGEFILE
);
174 perror("Error opening file");
179 error
= ftruncate(fd
, length
);
181 error
= syscall(SYS_fallocate
, fd
, falloc_mode
, offset
, length
);
184 perror("fallocate failed");