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