1 /* truncate -- truncate or extend the length of files.
2 Copyright (C) 2008-2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Pádraig Brady
19 This is backwards compatible with the FreeBSD utility, but is more
20 flexible wrt the size specifications and the use of long options,
21 to better fit the "GNU" environment. */
23 #include <config.h> /* sets _FILE_OFFSET_BITS=64 etc. */
26 #include <sys/types.h>
34 /* The official name of this program (e.g., no `g' prefix). */
35 #define PROGRAM_NAME "truncate"
37 #define AUTHORS proper_name_utf8 ("Padraig Brady", "P\303\241draig Brady")
39 /* (-c) If true, don't create if not already there */
40 static bool no_create;
42 /* (-o) If true, --size refers to blocks not bytes */
43 static bool block_mode;
45 /* (-r) Reference file to use size from */
46 static char const *ref_file;
48 static struct option const longopts[] =
50 {"no-create", no_argument, NULL, 'c'},
51 {"io-blocks", no_argument, NULL, 'o'},
52 {"reference", required_argument, NULL, 'r'},
53 {"size", required_argument, NULL, 's'},
54 {GETOPT_HELP_OPTION_DECL},
55 {GETOPT_VERSION_OPTION_DECL},
60 { rm_abs = 0, rm_rel, rm_min, rm_max, rm_rdn, rm_rup } rel_mode_t;
62 /* Set size to the value of STR, interpreted as a decimal integer,
63 optionally multiplied by various values.
64 Return -1 on error, 0 on success.
66 This supports dd BLOCK size suffixes + lowercase g,t,m for bsd compat
67 Note we don't support dd's b=512, c=1, w=2 or 21x512MiB formats. */
69 parse_len (char const *str, off_t *size)
73 e = xstrtoimax (str, NULL, 10, &tmp_size, "EgGkKmMPtTYZ0");
75 && !(OFF_T_MIN <= tmp_size && tmp_size <= OFF_T_MAX))
85 errno = (e == LONGINT_OVERFLOW ? EOVERFLOW : 0);
92 if (status != EXIT_SUCCESS)
93 fprintf (stderr, _("Try `%s --help' for more information.\n"),
97 printf (_("Usage: %s OPTION... FILE...\n"), program_name);
99 Shrink or extend the size of each FILE to the specified size\n\
101 A FILE argument that does not exist is created.\n\
103 If a FILE is larger than the specified size, the extra data is lost.\n\
104 If a FILE is shorter, it is extended and the extended part (hole)\n\
105 reads as zero bytes.\n\
109 Mandatory arguments to long options are mandatory for short options too.\n\
112 -c, --no-create do not create any files\n\
115 -o, --io-blocks treat SIZE as number of IO blocks instead of bytes\n\
118 -r, --reference=RFILE base size on RFILE\n\
119 -s, --size=SIZE set or adjust the file size by SIZE\n"), stdout);
120 fputs (HELP_OPTION_DESCRIPTION, stdout);
121 fputs (VERSION_OPTION_DESCRIPTION, stdout);
124 SIZE may also be prefixed by one of the following modifying characters:\n\
125 `+' extend by, `-' reduce by, `<' at most, `>' at least,\n\
126 `/' round down to multiple of, `%' round up to multiple of.\n"), stdout);
127 emit_ancillary_info ();
132 /* return true on success, false on error. */
134 do_ftruncate (int fd, char const *fname, off_t ssize, off_t rsize,
140 if ((block_mode || (rel_mode && rsize < 0)) && fstat (fd, &sb) != 0)
142 error (0, errno, _("cannot fstat %s"), quote (fname));
147 off_t const blksize = ST_BLKSIZE (sb);
148 if (ssize < OFF_T_MIN / blksize || ssize > OFF_T_MAX / blksize)
151 _("overflow in %" PRIdMAX
152 " * %" PRIdMAX " byte blocks for file %s"),
153 (intmax_t) ssize, (intmax_t) blksize,
161 uintmax_t const fsize = rsize < 0 ? sb.st_size : rsize;
163 if (rsize < 0) /* fstat used above to get size. */
165 if (!S_ISREG (sb.st_mode) && !S_TYPEISSHM (&sb))
167 error (0, 0, _("cannot get the size of %s"), quote (fname));
172 /* Sanity check. Overflow is the only reason I can think
173 this would ever go negative. */
174 error (0, 0, _("%s has unusable, apparently negative size"),
180 if (rel_mode == rm_min)
181 nsize = MAX (fsize, (uintmax_t) ssize);
182 else if (rel_mode == rm_max)
183 nsize = MIN (fsize, (uintmax_t) ssize);
184 else if (rel_mode == rm_rdn)
185 /* 0..ssize-1 -> 0 */
186 nsize = (fsize / ssize) * ssize;
187 else if (rel_mode == rm_rup)
188 /* 1..ssize -> ssize */
190 /* Here ssize>=1 && fsize>=0 */
191 uintmax_t const overflow = ((fsize + ssize - 1) / ssize) * ssize;
192 if (overflow > OFF_T_MAX)
194 error (0, 0, _("overflow rounding up size of file %s"),
202 if (ssize > OFF_T_MAX - (off_t)fsize)
204 error (0, 0, _("overflow extending size of file %s"),
208 nsize = fsize + ssize;
216 if (ftruncate (fd, nsize) == -1) /* note updates mtime & ctime */
219 _("failed to truncate %s at %" PRIdMAX " bytes"), quote (fname),
228 main (int argc, char **argv)
230 bool got_size = false;
232 off_t size IF_LINT ( = 0);
234 rel_mode_t rel_mode = rm_abs;
236 int c, fd = -1, oflags;
239 initialize_main (&argc, &argv);
240 set_program_name (argv[0]);
241 setlocale (LC_ALL, "");
242 bindtextdomain (PACKAGE, LOCALEDIR);
243 textdomain (PACKAGE);
245 atexit (close_stdout);
247 while ((c = getopt_long (argc, argv, "cor:s:", longopts, NULL)) != -1)
264 /* skip any whitespace */
265 while (isspace (to_uchar (*optarg)))
286 /* skip any whitespace */
287 while (isspace (to_uchar (*optarg)))
289 if (*optarg == '+' || *optarg == '-')
293 error (0, 0, _("multiple relative modifiers specified"));
294 /* Note other combinations are flagged as invalid numbers */
295 usage (EXIT_FAILURE);
299 if (parse_len (optarg, &size) == -1)
300 error (EXIT_FAILURE, errno, _("invalid number %s"),
302 /* Rounding to multiple of 0 is nonsensical */
303 if ((rel_mode == rm_rup || rel_mode == rm_rdn) && size == 0)
304 error (EXIT_FAILURE, 0, _("division by zero"));
308 case_GETOPT_HELP_CHAR;
310 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
313 usage (EXIT_FAILURE);
320 /* must specify either size or reference file */
321 if (!ref_file && !got_size)
323 error (0, 0, _("you must specify either %s or %s"),
324 quote_n (0, "--size"), quote_n (1, "--reference"));
325 usage (EXIT_FAILURE);
327 /* must specify a relative size with a reference file */
328 if (ref_file && got_size && !rel_mode)
330 error (0, 0, _("you must specify a relative %s with %s"),
331 quote_n (0, "--size"), quote_n (1, "--reference"));
332 usage (EXIT_FAILURE);
334 /* block_mode without size is not valid */
335 if (block_mode && !got_size)
337 error (0, 0, _("%s was specified but %s was not"),
338 quote_n (0, "--io-blocks"), quote_n (1, "--size"));
339 usage (EXIT_FAILURE);
341 /* must specify at least 1 file */
344 error (0, 0, _("missing file operand"));
345 usage (EXIT_FAILURE);
350 /* FIXME: Maybe support getting size of block devices. */
352 if (stat (ref_file, &sb) != 0)
353 error (EXIT_FAILURE, errno, _("cannot stat %s"), quote (ref_file));
354 if (!S_ISREG (sb.st_mode) && !S_TYPEISSHM (&sb))
355 error (EXIT_FAILURE, 0, _("cannot get the size of %s"),
363 oflags = O_WRONLY | (no_create ? 0 : O_CREAT) | O_NONBLOCK;
364 omode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
366 while ((fname = *argv++) != NULL)
368 if ((fd = open (fname, oflags, omode)) == -1)
370 /* `truncate -s0 -c no-such-file` shouldn't gen error
371 `truncate -s0 no-such-dir/file` should gen ENOENT error
372 `truncate -s0 no-such-dir/` should gen EISDIR error
373 `truncate -s0 .` should gen EISDIR error */
374 if (!(no_create && errno == ENOENT))
376 error (0, errno, _("cannot open %s for writing"),
386 errors |= !do_ftruncate (fd, fname, size, rsize, rel_mode);
389 error (0, errno, _("closing %s"), quote (fname));
395 return errors ? EXIT_FAILURE : EXIT_SUCCESS;