1TRUNCATE(2) Linux Programmer's Manual TRUNCATE(2)
2
3
4
6 truncate, ftruncate - truncate a file to a specified length
7
9 #include <unistd.h>
10
11 int truncate(const char *path, off_t length);
12 int ftruncate(int fd, off_t length);
13
14 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
15
16 truncate():
17 _XOPEN_SOURCE >= 500
18 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
19 || /* Glibc <= 2.19: */ _BSD_SOURCE
20
21 ftruncate():
22 _XOPEN_SOURCE >= 500
23 || /* Since glibc 2.3.5: */ _POSIX_C_SOURCE >= 200112L
24 || /* Glibc <= 2.19: */ _BSD_SOURCE
25
27 The truncate() and ftruncate() functions cause the regular file named
28 by path or referenced by fd to be truncated to a size of precisely
29 length bytes.
30
31 If the file previously was larger than this size, the extra data is
32 lost. If the file previously was shorter, it is extended, and the ex‐
33 tended part reads as null bytes ('\0').
34
35 The file offset is not changed.
36
37 If the size changed, then the st_ctime and st_mtime fields (respec‐
38 tively, time of last status change and time of last modification; see
39 inode(7)) for the file are updated, and the set-user-ID and set-group-
40 ID mode bits may be cleared.
41
42 With ftruncate(), the file must be open for writing; with truncate(),
43 the file must be writable.
44
46 On success, zero is returned. On error, -1 is returned, and errno is
47 set to indicate the error.
48
50 For truncate():
51
52 EACCES Search permission is denied for a component of the path prefix,
53 or the named file is not writable by the user. (See also
54 path_resolution(7).)
55
56 EFAULT The argument path points outside the process's allocated address
57 space.
58
59 EFBIG The argument length is larger than the maximum file size. (XSI)
60
61 EINTR While blocked waiting to complete, the call was interrupted by a
62 signal handler; see fcntl(2) and signal(7).
63
64 EINVAL The argument length is negative or larger than the maximum file
65 size.
66
67 EIO An I/O error occurred updating the inode.
68
69 EISDIR The named file is a directory.
70
71 ELOOP Too many symbolic links were encountered in translating the
72 pathname.
73
74 ENAMETOOLONG
75 A component of a pathname exceeded 255 characters, or an entire
76 pathname exceeded 1023 characters.
77
78 ENOENT The named file does not exist.
79
80 ENOTDIR
81 A component of the path prefix is not a directory.
82
83 EPERM The underlying filesystem does not support extending a file be‐
84 yond its current size.
85
86 EPERM The operation was prevented by a file seal; see fcntl(2).
87
88 EROFS The named file resides on a read-only filesystem.
89
90 ETXTBSY
91 The file is an executable file that is being executed.
92
93 For ftruncate() the same errors apply, but instead of things that can
94 be wrong with path, we now have things that can be wrong with the file
95 descriptor, fd:
96
97 EBADF fd is not a valid file descriptor.
98
99 EBADF or EINVAL
100 fd is not open for writing.
101
102 EINVAL fd does not reference a regular file or a POSIX shared memory
103 object.
104
105 EINVAL or EBADF
106 The file descriptor fd is not open for writing. POSIX permits,
107 and portable applications should handle, either error for this
108 case. (Linux produces EINVAL.)
109
111 POSIX.1-2001, POSIX.1-2008, 4.4BSD, SVr4 (these calls first appeared in
112 4.2BSD).
113
115 ftruncate() can also be used to set the size of a POSIX shared memory
116 object; see shm_open(3).
117
118 The details in DESCRIPTION are for XSI-compliant systems. For non-XSI-
119 compliant systems, the POSIX standard allows two behaviors for ftrun‐
120 cate() when length exceeds the file length (note that truncate() is not
121 specified at all in such an environment): either returning an error, or
122 extending the file. Like most UNIX implementations, Linux follows the
123 XSI requirement when dealing with native filesystems. However, some
124 nonnative filesystems do not permit truncate() and ftruncate() to be
125 used to extend a file beyond its current length: a notable example on
126 Linux is VFAT.
127
128 The original Linux truncate() and ftruncate() system calls were not de‐
129 signed to handle large file offsets. Consequently, Linux 2.4 added
130 truncate64() and ftruncate64() system calls that handle large files.
131 However, these details can be ignored by applications using glibc,
132 whose wrapper functions transparently employ the more recent system
133 calls where they are available.
134
135 On some 32-bit architectures, the calling signature for these system
136 calls differ, for the reasons described in syscall(2).
137
139 A header file bug in glibc 2.12 meant that the minimum value of
140 _POSIX_C_SOURCE required to expose the declaration of ftruncate() was
141 200809L instead of 200112L. This has been fixed in later glibc ver‐
142 sions.
143
145 truncate(1), open(2), stat(2), path_resolution(7)
146
148 This page is part of release 5.13 of the Linux man-pages project. A
149 description of the project, information about reporting bugs, and the
150 latest version of this page, can be found at
151 https://www.kernel.org/doc/man-pages/.
152
153
154
155Linux 2021-03-22 TRUNCATE(2)