2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2000 Sheldon Hearn <sheldonh@FreeBSD.org>.
7 * Copyright (c) 2021 The FreeBSD Foundation
9 * Portions of this software were developed by Ka Ho Ng <khng@FreeBSD.org>
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 static void usage(void);
50 main(int argc, char **argv)
54 off_t oflow, rsize, sz, tsize, round, off, len;
56 int ch, error, fd, oflags, r;
65 struct spacectl_range sr;
68 rsize = tsize = sz = off = 0;
70 do_dealloc = no_create = do_relative = do_round = do_refer =
75 while ((ch = getopt(argc, argv, "cdr:s:o:l:")) != -1)
89 if (*optarg == '+' || *optarg == '-') {
91 } else if (*optarg == '%' || *optarg == '/') {
94 if (expand_number(do_relative || do_round ?
96 &usz) == -1 || (off_t)usz < 0)
98 "invalid size argument `%s'", optarg);
100 sz = (*optarg == '-' || *optarg == '/') ?
101 -(off_t)usz : (off_t)usz;
105 if (expand_number(optarg, &usz) == -1 ||
108 "invalid offset argument `%s'", optarg);
113 if (expand_number(optarg, &usz) == -1 ||
116 "invalid length argument `%s'", optarg);
129 * Exactly one of do_refer, got_size or do_dealloc must be specified.
130 * Since do_relative implies got_size, do_relative, do_refer and
131 * do_dealloc are also mutually exclusive. If do_dealloc is specified,
132 * the length argument must be set. See usage() for allowed
135 if (argc < 1 || do_refer + got_size + do_dealloc != 1 ||
136 (do_dealloc == 1 && len == -1))
139 if (stat(rname, &sb) == -1)
140 err(EXIT_FAILURE, "%s", rname);
142 } else if (do_relative == 1 || do_round == 1)
144 else if (do_dealloc == 0)
150 oflags = O_WRONLY | O_CREAT;
151 omode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
153 while ((fname = *argv++) != NULL) {
156 if ((fd = open(fname, oflags, omode)) == -1) {
157 if (errno != ENOENT) {
163 if (do_relative == 1) {
164 if (fstat(fd, &sb) == -1) {
169 oflow = sb.st_size + rsize;
170 if (oflow < (sb.st_size + rsize)) {
179 if (fstat(fd, &sb) == -1) {
187 if (sb.st_size % sz) {
188 round = sb.st_size / sz;
189 if (round != sz && rsize < 0)
193 tsize = (round < 0 ? 0 : round) * sz;
200 if (do_dealloc == 1) {
203 r = fspacectl(fd, SPACECTL_DEALLOC, &sr, 0, &sr);
205 if (do_truncate == 1)
206 r = ftruncate(fd, tsize);
215 return (error ? EXIT_FAILURE : EXIT_SUCCESS);
221 fprintf(stderr, "%s\n%s\n%s\n",
222 "usage: truncate [-c] -s [+|-|%|/]size[K|k|M|m|G|g|T|t] file ...",
223 " truncate [-c] -r rfile file ...",
224 " truncate [-c] -d [-o offset[K|k|M|m|G|g|T|t]] -l length[K|k|M|m|G|g|T|t] file ...");