]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/truncate.2
getdents.2, open.2, stat.2, statx.2: SEE ALSO: add inode(7)
[thirdparty/man-pages.git] / man2 / truncate.2
CommitLineData
fea681da
MK
1.\" Copyright (c) 1983, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
a9cd9cb7 4.\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
fea681da
MK
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.
8c9302dc 32.\" %%%LICENSE_END
fea681da
MK
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>
c11b1abf 39.\" Modified 2002-01-07 by Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 40.\" Modified 2002-04-06 by Andries Brouwer <aeb@cwi.nl>
c11b1abf 41.\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 42.\"
31a1b45e 43.TH TRUNCATE 2 2017-03-13 "Linux" "Linux Programmer's Manual"
fea681da
MK
44.SH NAME
45truncate, ftruncate \- truncate a file to a specified length
46.SH SYNOPSIS
47.B #include <unistd.h>
48.br
49.B #include <sys/types.h>
50.sp
51.BI "int truncate(const char *" path ", off_t " length );
52.br
53.BI "int ftruncate(int " fd ", off_t " length );
cc4615cc
MK
54.sp
55.in -4n
56Feature Test Macro Requirements for glibc (see
57.BR feature_test_macros (7)):
58.in
59.ad l
1e2ba090 60.PD 0
cc4615cc 61.sp
3ca7341f 62.BR truncate ():
729166ed 63.RS 4
f8619b6a 64_XOPEN_SOURCE\ >=\ 500
cf7fa0a1 65.\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
3ba63d80 66.br
cf7fa0a1 67 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
f8619b6a 68 || /* Glibc versions <= 2.19: */ _BSD_SOURCE
729166ed 69.RE
1e2ba090 70.sp
3ca7341f 71.BR ftruncate ():
729166ed 72.RS 4
f8619b6a 73_XOPEN_SOURCE\ >=\ 500
cf7fa0a1 74.\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
cf7fa0a1 75 || /* Since glibc 2.3.5: */ _POSIX_C_SOURCE\ >=\ 200112L
f8619b6a 76 || /* Glibc versions <= 2.19: */ _BSD_SOURCE
729166ed 77.RE
1e2ba090 78.PD
cc4615cc 79.ad b
fea681da
MK
80.SH DESCRIPTION
81The
310b7919 82.BR truncate ()
fea681da 83and
310b7919 84.BR ftruncate ()
fea681da
MK
85functions cause the regular file named by
86.I path
87or referenced by
88.I fd
89to be truncated to a size of precisely
90.I length
91bytes.
92.LP
c13182ef 93If the file previously was larger than this size, the extra data is lost.
fea681da 94If the file previously was shorter, it is extended, and
f81fb444 95the extended part reads as null bytes (\(aq\\0\(aq).
fea681da 96.LP
1e568304 97The file offset is not changed.
fea681da 98.LP
c13182ef 99If the size changed, then the st_ctime and st_mtime fields
310b7919
MK
100(respectively, time of last status change and
101time of last modification; see
102.BR stat (2))
c13182ef 103for the file are updated,
ed948c28 104and the set-user-ID and set-group-ID mode bits may be cleared.
fea681da
MK
105.LP
106With
310b7919 107.BR ftruncate (),
fea681da 108the file must be open for writing; with
310b7919 109.BR truncate (),
fea681da 110the file must be writable.
47297adb 111.SH RETURN VALUE
c13182ef
MK
112On success, zero is returned.
113On error, \-1 is returned, and
fea681da
MK
114.I errno
115is set appropriately.
116.SH ERRORS
117For
e511ffb6 118.BR truncate ():
fea681da
MK
119.TP
120.B EACCES
121Search permission is denied for a component of the path prefix,
122or the named file is not writable by the user.
123(See also
ad7cc990 124.BR path_resolution (7).)
fea681da
MK
125.TP
126.B EFAULT
2d1b2fbc
MK
127The argument
128.I path
fea681da
MK
129points outside the process's allocated address space.
130.TP
131.B EFBIG
132The argument
133.I length
134is larger than the maximum file size. (XSI)
135.TP
136.B EINTR
374ce867
SL
137While blocked waiting to complete,
138the call was interrupted by a signal handler; see
139.BR fcntl (2)
140and
141.BR signal (7).
fea681da
MK
142.TP
143.B EINVAL
144The argument
145.I length
146is negative or larger than the maximum file size.
147.TP
148.B EIO
149An I/O error occurred updating the inode.
150.TP
151.B EISDIR
152The named file is a directory.
153.TP
154.B ELOOP
155Too many symbolic links were encountered in translating the pathname.
156.TP
157.B ENAMETOOLONG
158A component of a pathname exceeded 255 characters,
2d7195b8 159or an entire pathname exceeded 1023 characters.
fea681da
MK
160.TP
161.B ENOENT
162The named file does not exist.
163.TP
164.B ENOTDIR
165A component of the path prefix is not a directory.
166.TP
5b548b31 167.B EPERM
9ee4a2b6 168.\" This happens for at least MSDOS and VFAT filesystems
5b548b31 169.\" on kernel 2.6.13
9ee4a2b6 170The underlying filesystem does not support extending
5b548b31
MK
171a file beyond its current size.
172.TP
fbab10e5
MK
173.B EPERM
174The operation was prevented by a file seal; see
175.BR fcntl (2).
176.TP
fea681da 177.B EROFS
9ee4a2b6 178The named file resides on a read-only filesystem.
fea681da
MK
179.TP
180.B ETXTBSY
3aa74315 181The file is an executable file that is being executed.
fea681da
MK
182.PP
183For
310b7919 184.BR ftruncate ()
fea681da
MK
185the same errors apply, but instead of things that can be wrong with
186.IR path ,
3ade1aa9 187we now have things that can be wrong with the file descriptor,
fea681da
MK
188.IR fd :
189.TP
190.B EBADF
fea681da 191.I fd
d9cb0d7d 192is not a valid file descriptor.
fea681da
MK
193.TP
194.BR EBADF " or " EINVAL
fea681da
MK
195.I fd
196is not open for writing.
197.TP
198.B EINVAL
fea681da 199.I fd
dc951c84 200does not reference a regular file or a POSIX shared memory object.
29218e62
MK
201.TP
202.BR EINVAL " or " EBADF
8cc4d071 203The file descriptor
29218e62
MK
204.I fd
205is not open for writing.
206POSIX permits, and portable applications should handle,
207either error for this case.
208(Linux produces
209.BR EINVAL .)
47297adb 210.SH CONFORMING TO
84d596e8
MK
211POSIX.1-2001, POSIX.1-2008,
2124.4BSD, SVr4 (these calls first appeared in 4.2BSD).
97c1eac8
MK
213.\" POSIX.1-1996 has
214.\" .BR ftruncate ().
215.\" POSIX.1-2001 also has
216.\" .BR truncate (),
217.\" as an XSI extension.
218.\" .LP
219.\" SVr4 documents additional
220.\" .BR truncate ()
221.\" error conditions EMFILE, EMULTIHP, ENFILE, ENOLINK. SVr4 documents for
222.\" .BR ftruncate ()
223.\" an additional EAGAIN error condition.
fea681da 224.SH NOTES
dc951c84
MK
225.BR ftruncate ()
226can also be used to set the size of a POSIX shared memory object; see
227.BR shm_open (7).
228
c0697399 229The details in DESCRIPTION are for XSI-compliant systems.
fea681da 230For non-XSI-compliant systems, the POSIX standard allows
d9bfdb9c 231two behaviors for
310b7919 232.BR ftruncate ()
fea681da
MK
233when
234.I length
235exceeds the file length
236(note that
310b7919 237.BR truncate ()
fea681da
MK
238is not specified at all in such an environment):
239either returning an error, or extending the file.
008f1ecc 240Like most UNIX implementations, Linux follows the XSI requirement
9ee4a2b6
MK
241when dealing with native filesystems.
242However, some nonnative filesystems do not permit
5b548b31 243.BR truncate ()
c13182ef 244and
5b548b31
MK
245.BR ftruncate ()
246to be used to extend a file beyond its current length:
247a notable example on Linux is VFAT.
fea681da 248.\" At the very least: OSF/1, Solaris 7, and FreeBSD conform, mtk, Jan 2002
0290f437
MK
249
250The original Linux
251.BR truncate ()
252and
253.BR ftruncate ()
254system calls were not designed to handle large file offsets.
255Consequently, Linux 2.4 added
256.BR truncate64 ()
257and
258.BR ftruncate64 ()
259system calls that handle large files.
260However, these details can be ignored by applications using glibc, whose
dd7f1928 261wrapper functions transparently employ the more recent system calls
0290f437 262where they are available.
63ec43ae
MK
263
264On some 32-bit architectures,
265the calling signature for these system calls differ,
266for the reasons described in
267.BR syscall (2).
9dcef0c0
MK
268.SH BUGS
269A header file bug in glibc 2.12 meant that the minimum value of
270.\" http://sourceware.org/bugzilla/show_bug.cgi?id=12037
271.BR _POSIX_C_SOURCE
272required to expose the declaration of
273.BR ftruncate ()
274was 200809L instead of 200112L.
275This has been fixed in later glibc versions.
47297adb 276.SH SEE ALSO
ef2c6228 277.BR truncate (1),
fea681da 278.BR open (2),
ad7cc990
MK
279.BR stat (2),
280.BR path_resolution (7)