1TRUNCATE(2)                Linux Programmer's Manual               TRUNCATE(2)
2
3
4

NAME

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

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

47       On  success,  zero is returned.  On error, -1 is returned, and errno is
48       set appropriately.
49

ERRORS

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

CONFORMING TO

112       POSIX.1-2001, POSIX.1-2008, 4.4BSD, SVr4 (these calls first appeared in
113       4.2BSD).
114

NOTES

116       ftruncate() can also be used to set the size of a POSIX  shared  memory
117       object; see shm_open(7).
118
119       The details in DESCRIPTION are for XSI-compliant systems.  For non-XSI-
120       compliant systems, the POSIX standard allows two behaviors  for  ftrun‐
121       cate() when length exceeds the file length (note that truncate() is not
122       specified at all in such an environment): either returning an error, or
123       extending  the file.  Like most UNIX implementations, Linux follows the
124       XSI requirement when dealing with native  filesystems.   However,  some
125       nonnative  filesystems  do  not permit truncate() and ftruncate() to be
126       used to extend a file beyond its current length: a notable  example  on
127       Linux is VFAT.
128
129       The  original  Linux  truncate()  and ftruncate() system calls were not
130       designed to handle large file offsets.  Consequently, Linux  2.4  added
131       truncate64()  and  ftruncate64()  system calls that handle large files.
132       However, these details can be  ignored  by  applications  using  glibc,
133       whose  wrapper  functions  transparently  employ the more recent system
134       calls where they are available.
135
136       On some 32-bit architectures, the calling signature  for  these  system
137       calls differ, for the reasons described in syscall(2).
138

BUGS

140       A  header  file  bug  in  glibc  2.12  meant  that the minimum value of
141       _POSIX_C_SOURCE required to expose the declaration of  ftruncate()  was
142       200809L  instead  of  200112L.  This has been fixed in later glibc ver‐
143       sions.
144

SEE ALSO

146       truncate(1), open(2), stat(2), path_resolution(7)
147

COLOPHON

149       This page is part of release 5.07 of the Linux  man-pages  project.   A
150       description  of  the project, information about reporting bugs, and the
151       latest    version    of    this    page,    can     be     found     at
152       https://www.kernel.org/doc/man-pages/.
153
154
155
156Linux                             2019-03-06                       TRUNCATE(2)
Impressum