1truncate(2)                   System Calls Manual                  truncate(2)
2
3
4

NAME

6       truncate, ftruncate - truncate a file to a specified length
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <unistd.h>
13
14       int truncate(const char *path, off_t length);
15       int ftruncate(int fd, off_t length);
16
17   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19       truncate():
20           _XOPEN_SOURCE >= 500
21               || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
22               || /* glibc <= 2.19: */ _BSD_SOURCE
23
24       ftruncate():
25           _XOPEN_SOURCE >= 500
26               || /* Since glibc 2.3.5: */ _POSIX_C_SOURCE >= 200112L
27               || /* glibc <= 2.19: */ _BSD_SOURCE
28

DESCRIPTION

30       The  truncate()  and ftruncate() functions cause the regular file named
31       by path or referenced by fd to be truncated  to  a  size  of  precisely
32       length bytes.
33
34       If  the  file  previously  was larger than this size, the extra data is
35       lost.  If the file previously was shorter, it is extended, and the  ex‐
36       tended part reads as null bytes ('\0').
37
38       The file offset is not changed.
39
40       If  the  size  changed,  then the st_ctime and st_mtime fields (respec‐
41       tively, time of last status change and time of last  modification;  see
42       inode(7))  for the file are updated, and the set-user-ID and set-group-
43       ID mode bits may be cleared.
44
45       With ftruncate(), the file must be open for writing;  with  truncate(),
46       the file must be writable.
47

RETURN VALUE

49       On  success,  zero is returned.  On error, -1 is returned, and errno is
50       set to indicate the error.
51

ERRORS

53       For truncate():
54
55       EACCES Search permission is denied for a component of the path  prefix,
56              or  the  named  file  is  not  writable  by the user.  (See also
57              path_resolution(7).)
58
59       EFAULT The argument path points outside the process's allocated address
60              space.
61
62       EFBIG  The argument length is larger than the maximum file size. (XSI)
63
64       EINTR  While blocked waiting to complete, the call was interrupted by a
65              signal handler; see fcntl(2) and signal(7).
66
67       EINVAL The argument length is negative or larger than the maximum  file
68              size.
69
70       EIO    An I/O error occurred updating the inode.
71
72       EISDIR The named file is a directory.
73
74       ELOOP  Too  many  symbolic  links  were  encountered in translating the
75              pathname.
76
77       ENAMETOOLONG
78              A component of a pathname exceeded 255 characters, or an  entire
79              pathname exceeded 1023 characters.
80
81       ENOENT The named file does not exist.
82
83       ENOTDIR
84              A component of the path prefix is not a directory.
85
86       EPERM  The  underlying filesystem does not support extending a file be‐
87              yond its current size.
88
89       EPERM  The operation was prevented by a file seal; see fcntl(2).
90
91       EROFS  The named file resides on a read-only filesystem.
92
93       ETXTBSY
94              The file is an executable file that is being executed.
95
96       For ftruncate() the same errors apply, but instead of things  that  can
97       be  wrong with path, we now have things that can be wrong with the file
98       descriptor, fd:
99
100       EBADF  fd is not a valid file descriptor.
101
102       EBADF or EINVAL
103              fd is not open for writing.
104
105       EINVAL fd does not reference a regular file or a  POSIX  shared  memory
106              object.
107
108       EINVAL or EBADF
109              The  file descriptor fd is not open for writing.  POSIX permits,
110              and portable applications should handle, either error  for  this
111              case.  (Linux produces EINVAL.)
112

VERSIONS

114       The details in DESCRIPTION are for XSI-compliant systems.  For non-XSI-
115       compliant systems, the POSIX standard allows two behaviors  for  ftrun‐
116       cate() when length exceeds the file length (note that truncate() is not
117       specified at all in such an environment): either returning an error, or
118       extending  the file.  Like most UNIX implementations, Linux follows the
119       XSI requirement when dealing with native  filesystems.   However,  some
120       nonnative  filesystems  do  not permit truncate() and ftruncate() to be
121       used to extend a file beyond its current length: a notable  example  on
122       Linux is VFAT.
123
124       On  some  32-bit  architectures, the calling signature for these system
125       calls differ, for the reasons described in syscall(2).
126

STANDARDS

128       POSIX.1-2008.
129

HISTORY

131       POSIX.1-2001, 4.4BSD, SVr4 (first appeared in 4.2BSD).
132
133       The original Linux truncate() and ftruncate() system calls were not de‐
134       signed  to  handle  large  file offsets.  Consequently, Linux 2.4 added
135       truncate64() and ftruncate64() system calls that  handle  large  files.
136       However,  these  details  can  be  ignored by applications using glibc,
137       whose wrapper functions transparently employ  the  more  recent  system
138       calls where they are available.
139

NOTES

141       ftruncate()  can  also be used to set the size of a POSIX shared memory
142       object; see shm_open(3).
143

BUGS

145       A header file bug in  glibc  2.12  meant  that  the  minimum  value  of
146       _POSIX_C_SOURCE  required  to expose the declaration of ftruncate() was
147       200809L instead of 200112L.  This has been fixed in  later  glibc  ver‐
148       sions.
149

SEE ALSO

151       truncate(1), open(2), stat(2), path_resolution(7)
152
153
154
155Linux man-pages 6.04              2023-03-30                       truncate(2)
Impressum