1 /* truncate -- truncate or extend the length of files.
2 Copyright (C) 2008-2016 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>
32 #include "stat-size.h"
33 #include "xdectoint.h"
35 /* The official name of this program (e.g., no 'g' prefix). */
36 #define PROGRAM_NAME "truncate"
38 #define AUTHORS proper_name ("Padraig Brady")
40 /* (-c) If true, don't create if not already there */
41 static bool no_create
;
43 /* (-o) If true, --size refers to blocks not bytes */
44 static bool block_mode
;
46 /* (-r) Reference file to use size from */
47 static char const *ref_file
;
49 static struct option
const longopts
[] =
51 {"no-create", no_argument
, NULL
, 'c'},
52 {"io-blocks", no_argument
, NULL
, 'o'},
53 {"reference", required_argument
, NULL
, 'r'},
54 {"size", required_argument
, NULL
, 's'},
55 {GETOPT_HELP_OPTION_DECL
},
56 {GETOPT_VERSION_OPTION_DECL
},
61 { rm_abs
= 0, rm_rel
, rm_min
, rm_max
, rm_rdn
, rm_rup
} rel_mode_t
;
66 if (status
!= EXIT_SUCCESS
)
70 printf (_("Usage: %s OPTION... FILE...\n"), program_name
);
72 Shrink or extend the size of each FILE to the specified size\n\
74 A FILE argument that does not exist is created.\n\
76 If a FILE is larger than the specified size, the extra data is lost.\n\
77 If a FILE is shorter, it is extended and the extended part (hole)\n\
78 reads as zero bytes.\n\
81 emit_mandatory_arg_note ();
84 -c, --no-create do not create any files\n\
87 -o, --io-blocks treat SIZE as number of IO blocks instead of bytes\n\
90 -r, --reference=RFILE base size on RFILE\n\
91 -s, --size=SIZE set or adjust the file size by SIZE bytes\n"), stdout
);
92 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
93 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
96 SIZE may also be prefixed by one of the following modifying characters:\n\
97 '+' extend by, '-' reduce by, '<' at most, '>' at least,\n\
98 '/' round down to multiple of, '%' round up to multiple of.\n"), stdout
);
99 emit_ancillary_info (PROGRAM_NAME
);
104 /* return true on success, false on error. */
106 do_ftruncate (int fd
, char const *fname
, off_t ssize
, off_t rsize
,
112 if ((block_mode
|| (rel_mode
&& rsize
< 0)) && fstat (fd
, &sb
) != 0)
114 error (0, errno
, _("cannot fstat %s"), quoteaf (fname
));
119 off_t
const blksize
= ST_BLKSIZE (sb
);
120 if (ssize
< OFF_T_MIN
/ blksize
|| ssize
> OFF_T_MAX
/ blksize
)
123 _("overflow in %" PRIdMAX
124 " * %" PRIdMAX
" byte blocks for file %s"),
125 (intmax_t) ssize
, (intmax_t) blksize
,
140 if (usable_st_size (&sb
))
142 file_size
= sb
.st_size
;
145 /* Sanity check. Overflow is the only reason I can think
146 this would ever go negative. */
147 error (0, 0, _("%s has unusable, apparently negative size"),
154 file_size
= lseek (fd
, 0, SEEK_END
);
157 error (0, errno
, _("cannot get the size of %s"),
165 if (rel_mode
== rm_min
)
166 nsize
= MAX (fsize
, (uintmax_t) ssize
);
167 else if (rel_mode
== rm_max
)
168 nsize
= MIN (fsize
, (uintmax_t) ssize
);
169 else if (rel_mode
== rm_rdn
)
170 /* 0..ssize-1 -> 0 */
171 nsize
= (fsize
/ ssize
) * ssize
;
172 else if (rel_mode
== rm_rup
)
173 /* 1..ssize -> ssize */
175 /* Here ssize>=1 && fsize>=0 */
176 uintmax_t const overflow
= ((fsize
+ ssize
- 1) / ssize
) * ssize
;
177 if (overflow
> OFF_T_MAX
)
179 error (0, 0, _("overflow rounding up size of file %s"),
187 if (ssize
> OFF_T_MAX
- (off_t
)fsize
)
189 error (0, 0, _("overflow extending size of file %s"),
193 nsize
= fsize
+ ssize
;
201 if (ftruncate (fd
, nsize
) == -1) /* note updates mtime & ctime */
204 _("failed to truncate %s at %" PRIdMAX
" bytes"), quoteaf (fname
),
213 main (int argc
, char **argv
)
215 bool got_size
= false;
217 off_t size
IF_LINT ( = 0);
219 rel_mode_t rel_mode
= rm_abs
;
220 int c
, fd
= -1, oflags
;
223 initialize_main (&argc
, &argv
);
224 set_program_name (argv
[0]);
225 setlocale (LC_ALL
, "");
226 bindtextdomain (PACKAGE
, LOCALEDIR
);
227 textdomain (PACKAGE
);
229 atexit (close_stdout
);
231 while ((c
= getopt_long (argc
, argv
, "cor:s:", longopts
, NULL
)) != -1)
248 /* skip any whitespace */
249 while (isspace (to_uchar (*optarg
)))
270 /* skip any whitespace */
271 while (isspace (to_uchar (*optarg
)))
273 if (*optarg
== '+' || *optarg
== '-')
277 error (0, 0, _("multiple relative modifiers specified"));
278 /* Note other combinations are flagged as invalid numbers */
279 usage (EXIT_FAILURE
);
283 /* Support dd BLOCK size suffixes + lowercase g,t,m for bsd compat.
284 Note we don't support dd's b=512, c=1, w=2 or 21x512MiB formats. */
285 size
= xdectoimax (optarg
, OFF_T_MIN
, OFF_T_MAX
, "EgGkKmMPtTYZ0",
286 _("Invalid number"), 0);
287 /* Rounding to multiple of 0 is nonsensical */
288 if ((rel_mode
== rm_rup
|| rel_mode
== rm_rdn
) && size
== 0)
289 die (EXIT_FAILURE
, 0, _("division by zero"));
293 case_GETOPT_HELP_CHAR
;
295 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
298 usage (EXIT_FAILURE
);
305 /* must specify either size or reference file */
306 if (!ref_file
&& !got_size
)
308 error (0, 0, _("you must specify either %s or %s"),
309 quote_n (0, "--size"), quote_n (1, "--reference"));
310 usage (EXIT_FAILURE
);
312 /* must specify a relative size with a reference file */
313 if (ref_file
&& got_size
&& !rel_mode
)
315 error (0, 0, _("you must specify a relative %s with %s"),
316 quote_n (0, "--size"), quote_n (1, "--reference"));
317 usage (EXIT_FAILURE
);
319 /* block_mode without size is not valid */
320 if (block_mode
&& !got_size
)
322 error (0, 0, _("%s was specified but %s was not"),
323 quote_n (0, "--io-blocks"), quote_n (1, "--size"));
324 usage (EXIT_FAILURE
);
326 /* must specify at least 1 file */
329 error (0, 0, _("missing file operand"));
330 usage (EXIT_FAILURE
);
336 off_t file_size
= -1;
337 if (stat (ref_file
, &sb
) != 0)
338 die (EXIT_FAILURE
, errno
, _("cannot stat %s"), quoteaf (ref_file
));
339 if (usable_st_size (&sb
))
340 file_size
= sb
.st_size
;
343 int ref_fd
= open (ref_file
, O_RDONLY
);
346 off_t file_end
= lseek (ref_fd
, 0, SEEK_END
);
347 int saved_errno
= errno
;
348 close (ref_fd
); /* ignore failure */
350 file_size
= file_end
;
353 /* restore, in case close clobbered it. */
359 die (EXIT_FAILURE
, errno
, _("cannot get the size of %s"),
367 oflags
= O_WRONLY
| (no_create
? 0 : O_CREAT
) | O_NONBLOCK
;
369 while ((fname
= *argv
++) != NULL
)
371 if ((fd
= open (fname
, oflags
, MODE_RW_UGO
)) == -1)
373 /* 'truncate -s0 -c no-such-file' shouldn't gen error
374 'truncate -s0 no-such-dir/file' should gen ENOENT error
375 'truncate -s0 no-such-dir/' should gen EISDIR error
376 'truncate -s0 .' should gen EISDIR error */
377 if (!(no_create
&& errno
== ENOENT
))
379 error (0, errno
, _("cannot open %s for writing"),
389 errors
|= !do_ftruncate (fd
, fname
, size
, rsize
, rel_mode
);
392 error (0, errno
, _("failed to close %s"), quoteaf (fname
));
398 return errors
? EXIT_FAILURE
: EXIT_SUCCESS
;