xref: /freebsd/usr.bin/truncate/truncate.c (revision eacc501eff52db16b7b784c89a3a4a03c9a3ef34)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2000 Sheldon Hearn <sheldonh@FreeBSD.org>.
5  * All rights reserved.
6  *
7  * Copyright (c) 2021 The FreeBSD Foundation
8  *
9  * Portions of this software were developed by Ka Ho Ng <khng@FreeBSD.org>
10  * under sponsorship from the FreeBSD Foundation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  *
33  */
34 
35 #include <sys/stat.h>
36 
37 #include <ctype.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 
45 #include <libutil.h>
46 
47 static void	usage(void);
48 
49 int
main(int argc,char ** argv)50 main(int argc, char **argv)
51 {
52 	struct stat sb;
53 	mode_t omode;
54 	off_t oflow, rsize, sz, tsize, round, off, len;
55 	uint64_t usz;
56 	int ch, error, fd, oflags, r;
57 	int do_dealloc;
58 	int do_truncate;
59 	int no_create;
60 	int do_relative;
61 	int do_round;
62 	int do_refer;
63 	int got_size;
64 	char *fname, *rname;
65 
66 	fd = -1;
67 	rsize = tsize = sz = off = 0;
68 	len = -1;
69 	do_dealloc = no_create = do_relative = do_round = do_refer =
70 	    got_size = 0;
71 	do_truncate = 1;
72 	error = r = 0;
73 	rname = NULL;
74 	while ((ch = getopt(argc, argv, "cdr:s:o:l:")) != -1)
75 		switch (ch) {
76 		case 'c':
77 			no_create = 1;
78 			break;
79 		case 'd':
80 			do_dealloc = 1;
81 			do_truncate = 0;
82 			break;
83 		case 'r':
84 			do_refer = 1;
85 			rname = optarg;
86 			break;
87 		case 's':
88 			if (*optarg == '+' || *optarg == '-') {
89 				do_relative = 1;
90 			} else if (*optarg == '%' || *optarg == '/') {
91 				do_round = 1;
92 			}
93 			if (expand_number(do_relative || do_round ?
94 			    optarg + 1 : optarg,
95 			    &usz) == -1 || (off_t)usz < 0)
96 				errx(EXIT_FAILURE,
97 				    "invalid size argument `%s'", optarg);
98 
99 			sz = (*optarg == '-' || *optarg == '/') ?
100 				-(off_t)usz : (off_t)usz;
101 			got_size = 1;
102 			break;
103 		case 'o':
104 			if (expand_number(optarg, &usz) == -1 ||
105 			    (off_t)usz < 0)
106 				errx(EXIT_FAILURE,
107 				    "invalid offset argument `%s'", optarg);
108 
109 			off = usz;
110 			break;
111 		case 'l':
112 			if (expand_number(optarg, &usz) == -1 ||
113 			    (off_t)usz <= 0)
114 				errx(EXIT_FAILURE,
115 				    "invalid length argument `%s'", optarg);
116 
117 			len = usz;
118 			break;
119 		default:
120 			usage();
121 			/* NOTREACHED */
122 		}
123 
124 	argv += optind;
125 	argc -= optind;
126 
127 	/*
128 	 * Exactly one of do_refer, got_size or do_dealloc must be specified.
129 	 * Since do_relative implies got_size, do_relative, do_refer and
130 	 * do_dealloc are also mutually exclusive.  If do_dealloc is specified,
131 	 * the length argument must be set.  See usage() for allowed
132 	 * invocations.
133 	 */
134 	if (argc < 1 || do_refer + got_size + do_dealloc != 1 ||
135 	    (do_dealloc == 1 && len == -1))
136 		usage();
137 	if (do_refer == 1) {
138 		if (stat(rname, &sb) == -1)
139 			err(EXIT_FAILURE, "%s", rname);
140 		tsize = sb.st_size;
141 	} else if (do_relative == 1 || do_round == 1)
142 		rsize = sz;
143 	else if (do_dealloc == 0)
144 		tsize = sz;
145 
146 	if (no_create)
147 		oflags = O_WRONLY;
148 	else
149 		oflags = O_WRONLY | O_CREAT;
150 	omode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
151 
152 	while ((fname = *argv++) != NULL) {
153 		if (fd != -1)
154 			close(fd);
155 		if ((fd = open(fname, oflags, omode)) == -1) {
156 			if (errno != ENOENT) {
157 				warn("%s", fname);
158 				error++;
159 			}
160 			continue;
161 		}
162 		if (do_relative == 1) {
163 			if (fstat(fd, &sb) == -1) {
164 				warn("%s", fname);
165 				error++;
166 				continue;
167 			}
168 			oflow = sb.st_size + rsize;
169 			if (oflow < (sb.st_size + rsize)) {
170 				errno = EFBIG;
171 				warn("%s", fname);
172 				error++;
173 				continue;
174 			}
175 			tsize = oflow;
176 		}
177 		if (do_round == 1) {
178 			if (fstat(fd, &sb) == -1) {
179 				warn("%s", fname);
180 				error++;
181 				continue;
182 			}
183 			sz = rsize;
184 			if (sz < 0)
185 				sz = -sz;
186 			if (sb.st_size % sz) {
187 				round = sb.st_size / sz;
188 				if (round != sz && rsize < 0)
189 					round--;
190 				else if (rsize > 0)
191 					round++;
192 				tsize = (round < 0 ? 0 : round) * sz;
193 			} else
194 				tsize = sb.st_size;
195 		}
196 		if (tsize < 0)
197 			tsize = 0;
198 
199 		if (do_dealloc == 1) {
200 			struct spacectl_range sr;
201 
202 			sr.r_offset = off;
203 			sr.r_len = len;
204 			r = fspacectl(fd, SPACECTL_DEALLOC, &sr, 0, &sr);
205 		}
206 		if (do_truncate == 1)
207 			r = ftruncate(fd, tsize);
208 		if (r == -1) {
209 			warn("%s", fname);
210 			error++;
211 		}
212 	}
213 	if (fd != -1)
214 		close(fd);
215 
216 	return (error ? EXIT_FAILURE : EXIT_SUCCESS);
217 }
218 
219 static void
usage(void)220 usage(void)
221 {
222 	fprintf(stderr, "%s\n%s\n%s\n",
223 	    "usage: truncate [-c] -s [+|-|%|/]size[K|k|M|m|G|g|T|t] file ...",
224 	    "       truncate [-c] -r rfile file ...",
225 	    "       truncate [-c] -d [-o offset[K|k|M|m|G|g|T|t]] -l length[K|k|M|m|G|g|T|t] file ...");
226 	exit(EXIT_FAILURE);
227 }
228