seccomp_unotify.2: tfix
[man-pages.git] / man2 / truncate.2
blobebe3c4ef4fa50ef47099a78fb734fc5421ec8266
1 .\" Copyright (c) 1983, 1991 The Regents of the University of California.
2 .\" All rights reserved.
3 .\"
4 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
7 .\" are met:
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\"    notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\"    notice, this list of conditions and the following disclaimer in the
12 .\"    documentation and/or other materials provided with the distribution.
13 .\" 3. All advertising materials mentioning features or use of this software
14 .\"    must display the following acknowledgement:
15 .\"     This product includes software developed by the University of
16 .\"     California, Berkeley and its contributors.
17 .\" 4. Neither the name of the University nor the names of its contributors
18 .\"    may be used to endorse or promote products derived from this software
19 .\"    without specific prior written permission.
20 .\"
21 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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 .\" %%%LICENSE_END
33 .\"
34 .\"     @(#)truncate.2  6.9 (Berkeley) 3/10/91
35 .\"
36 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
37 .\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
38 .\" Modified 1998-12-21 by Andries Brouwer <aeb@cwi.nl>
39 .\" Modified 2002-01-07 by Michael Kerrisk <mtk.manpages@gmail.com>
40 .\" Modified 2002-04-06 by Andries Brouwer <aeb@cwi.nl>
41 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
42 .\"
43 .TH TRUNCATE 2 2021-03-22 "Linux" "Linux Programmer's Manual"
44 .SH NAME
45 truncate, ftruncate \- truncate a file to a specified length
46 .SH SYNOPSIS
47 .nf
48 .B #include <unistd.h>
49 .PP
50 .BI "int truncate(const char *" path ", off_t " length );
51 .BI "int ftruncate(int " fd ", off_t " length );
52 .fi
53 .PP
54 .RS -4
55 Feature Test Macro Requirements for glibc (see
56 .BR feature_test_macros (7)):
57 .RE
58 .PP
59 .BR truncate ():
60 .nf
61     _XOPEN_SOURCE >= 500
62 .\"    || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
63         || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
64         || /* Glibc <= 2.19: */ _BSD_SOURCE
65 .fi
66 .PP
67 .BR ftruncate ():
68 .nf
69     _XOPEN_SOURCE >= 500
70 .\"    || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
71         || /* Since glibc 2.3.5: */ _POSIX_C_SOURCE >= 200112L
72         || /* Glibc <= 2.19: */ _BSD_SOURCE
73 .fi
74 .SH DESCRIPTION
75 The
76 .BR truncate ()
77 and
78 .BR ftruncate ()
79 functions cause the regular file named by
80 .I path
81 or referenced by
82 .I fd
83 to be truncated to a size of precisely
84 .I length
85 bytes.
86 .PP
87 If the file previously was larger than this size, the extra data is lost.
88 If the file previously was shorter, it is extended, and
89 the extended part reads as null bytes (\(aq\e0\(aq).
90 .PP
91 The file offset is not changed.
92 .PP
93 If the size changed, then the st_ctime and st_mtime fields
94 (respectively, time of last status change and
95 time of last modification; see
96 .BR inode (7))
97 for the file are updated,
98 and the set-user-ID and set-group-ID mode bits may be cleared.
99 .PP
100 With
101 .BR ftruncate (),
102 the file must be open for writing; with
103 .BR truncate (),
104 the file must be writable.
105 .SH RETURN VALUE
106 On success, zero is returned.
107 On error, \-1 is returned, and
108 .I errno
109 is set to indicate the error.
110 .SH ERRORS
112 .BR truncate ():
114 .B EACCES
115 Search permission is denied for a component of the path prefix,
116 or the named file is not writable by the user.
117 (See also
118 .BR path_resolution (7).)
120 .B EFAULT
121 The argument
122 .I path
123 points outside the process's allocated address space.
125 .B EFBIG
126 The argument
127 .I length
128 is larger than the maximum file size. (XSI)
130 .B EINTR
131 While blocked waiting to complete,
132 the call was interrupted by a signal handler; see
133 .BR fcntl (2)
135 .BR signal (7).
137 .B EINVAL
138 The argument
139 .I length
140 is negative or larger than the maximum file size.
142 .B EIO
143 An I/O error occurred updating the inode.
145 .B EISDIR
146 The named file is a directory.
148 .B ELOOP
149 Too many symbolic links were encountered in translating the pathname.
151 .B ENAMETOOLONG
152 A component of a pathname exceeded 255 characters,
153 or an entire pathname exceeded 1023 characters.
155 .B ENOENT
156 The named file does not exist.
158 .B ENOTDIR
159 A component of the path prefix is not a directory.
161 .B EPERM
162 .\" This happens for at least MSDOS and VFAT filesystems
163 .\" on kernel 2.6.13
164 The underlying filesystem does not support extending
165 a file beyond its current size.
167 .B EPERM
168 The operation was prevented by a file seal; see
169 .BR fcntl (2).
171 .B EROFS
172 The named file resides on a read-only filesystem.
174 .B ETXTBSY
175 The file is an executable file that is being executed.
178 .BR ftruncate ()
179 the same errors apply, but instead of things that can be wrong with
180 .IR path ,
181 we now have things that can be wrong with the file descriptor,
182 .IR fd :
184 .B EBADF
185 .I fd
186 is not a valid file descriptor.
188 .BR EBADF " or " EINVAL
189 .I fd
190 is not open for writing.
192 .B EINVAL
193 .I fd
194 does not reference a regular file or a POSIX shared memory object.
196 .BR EINVAL " or " EBADF
197 The file descriptor
198 .I fd
199 is not open for writing.
200 POSIX permits, and portable applications should handle,
201 either error for this case.
202 (Linux produces
203 .BR EINVAL .)
204 .SH CONFORMING TO
205 POSIX.1-2001, POSIX.1-2008,
206 4.4BSD, SVr4 (these calls first appeared in 4.2BSD).
207 .\" POSIX.1-1996 has
208 .\" .BR ftruncate ().
209 .\" POSIX.1-2001 also has
210 .\" .BR truncate (),
211 .\" as an XSI extension.
212 .\" .LP
213 .\" SVr4 documents additional
214 .\" .BR truncate ()
215 .\" error conditions EMFILE, EMULTIHP, ENFILE, ENOLINK.  SVr4 documents for
216 .\" .BR ftruncate ()
217 .\" an additional EAGAIN error condition.
218 .SH NOTES
219 .BR ftruncate ()
220 can also be used to set the size of a POSIX shared memory object; see
221 .BR shm_open (3).
223 The details in DESCRIPTION are for XSI-compliant systems.
224 For non-XSI-compliant systems, the POSIX standard allows
225 two behaviors for
226 .BR ftruncate ()
227 when
228 .I length
229 exceeds the file length
230 (note that
231 .BR truncate ()
232 is not specified at all in such an environment):
233 either returning an error, or extending the file.
234 Like most UNIX implementations, Linux follows the XSI requirement
235 when dealing with native filesystems.
236 However, some nonnative filesystems do not permit
237 .BR truncate ()
239 .BR ftruncate ()
240 to be used to extend a file beyond its current length:
241 a notable example on Linux is VFAT.
242 .\" At the very least: OSF/1, Solaris 7, and FreeBSD conform, mtk, Jan 2002
244 The original Linux
245 .BR truncate ()
247 .BR ftruncate ()
248 system calls were not designed to handle large file offsets.
249 Consequently, Linux 2.4 added
250 .BR truncate64 ()
252 .BR ftruncate64 ()
253 system calls that handle large files.
254 However, these details can be ignored by applications using glibc, whose
255 wrapper functions transparently employ the more recent system calls
256 where they are available.
258 On some 32-bit architectures,
259 the calling signature for these system calls differ,
260 for the reasons described in
261 .BR syscall (2).
262 .SH BUGS
263 A header file bug in glibc 2.12 meant that the minimum value of
264 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=12037
265 .BR _POSIX_C_SOURCE
266 required to expose the declaration of
267 .BR ftruncate ()
268 was 200809L instead of 200112L.
269 This has been fixed in later glibc versions.
270 .SH SEE ALSO
271 .BR truncate (1),
272 .BR open (2),
273 .BR stat (2),
274 .BR path_resolution (7)