]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/fallocate.c
fsfreeze: new command
[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 *
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>
6264af59 34#include <limits.h>
d46a5499
KZ
35
36#ifndef HAVE_FALLOCATE
37# include <sys/syscall.h>
38#endif
39
40#include <linux/falloc.h> /* for FALLOC_FL_* flags */
41
42#include "nls.h"
3b6b039a 43#include "strtosize.h"
d46a5499
KZ
44
45
46static void __attribute__((__noreturn__)) usage(FILE *out)
47{
48 fprintf(out, _("Usage: %s [options] <filename>\n\nOptions:\n"),
49 program_invocation_short_name);
50
51 fprintf(out, _(
52 " -h, --help this help\n"
53 " -n, --keep-size don't modify the length of the file\n"
54 " -o, --offset <num> offset of the allocation, in bytes\n"
55 " -l, --length <num> length of the allocation, in bytes\n"));
56
57 fprintf(out, _("\nFor more information see fallocate(1).\n"));
58
59 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
60}
61
d46a5499
KZ
62static loff_t cvtnum(char *s)
63{
3b6b039a 64 uintmax_t x;
6264af59 65
3b6b039a 66 if (strtosize(s, &x))
d46a5499 67 return -1LL;
d46a5499 68
3b6b039a 69 return x;
d46a5499
KZ
70}
71
72int main(int argc, char **argv)
73{
74 char *fname;
75 int c;
76 int error;
77 int fd;
78 int mode = 0;
79 loff_t length = -2LL;
80 loff_t offset = 0;
81
82 struct option longopts[] = {
83 { "help", 0, 0, 'h' },
84 { "keep-size", 0, 0, 'n' },
85 { "offset", 1, 0, 'o' },
86 { "lenght", 1, 0, 'l' },
87 { NULL, 0, 0, 0 }
88 };
89
90 setlocale(LC_ALL, "");
91 bindtextdomain(PACKAGE, LOCALEDIR);
92 textdomain(PACKAGE);
93
94 while ((c = getopt_long(argc, argv, "hnl:o:", longopts, NULL)) != -1) {
95 switch(c) {
96 case 'h':
97 usage(stdout);
98 break;
99 case 'n':
100 mode |= FALLOC_FL_KEEP_SIZE;
101 break;
102 case 'l':
103 length = cvtnum(optarg);
104 break;
105 case 'o':
106 offset = cvtnum(optarg);
107 break;
108 default:
109 usage(stderr);
110 break;
111 }
112 }
113
114 if (length == -2LL)
115 errx(EXIT_FAILURE, _("no length argument specified"));
116 if (length <= 0)
117 errx(EXIT_FAILURE, _("invalid length value specified"));
118 if (offset < 0)
119 errx(EXIT_FAILURE, _("invalid offset value specified"));
120 if (optind == argc)
121 errx(EXIT_FAILURE, _("no filename specified."));
122
123 fname = argv[optind++];
124
125 fd = open(fname, O_WRONLY|O_CREAT, 0644);
126 if (fd < 0)
127 err(EXIT_FAILURE, _("%s: open failed"), fname);
128
129#ifdef HAVE_FALLOCATE
130 error = fallocate(fd, mode, offset, length);
131#else
132 error = syscall(SYS_fallocate, fd, mode, offset, length);
133#endif
134 /*
135 * EOPNOTSUPP: The FALLOC_FL_KEEP_SIZE is unsupported
136 * ENOSYS: The filesystem does not support sys_fallocate
137 */
138 if (error < 0) {
139 if ((mode & FALLOC_FL_KEEP_SIZE) && errno == EOPNOTSUPP)
140 errx(EXIT_FAILURE,
141 _("keep size mode (-n option) unsupported"));
142 err(EXIT_FAILURE, _("%s: fallocate failed"), fname);
143 }
144
145 close(fd);
146 return EXIT_SUCCESS;
147}