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 #include <sys/types.h>
11
12 int truncate(const char *path, off_t length);
13 int ftruncate(int fd, off_t length);
14
16 The truncate() and ftruncate() functions cause the regular file named
17 by path or referenced by fd to be truncated to a size of precisely
18 length bytes.
19
20 If the file previously was larger than this size, the extra data is
21 lost. If the file previously was shorter, it is extended, and the
22 extended part reads as null bytes ('\0').
23
24 The file offset is not changed.
25
26 If the size changed, then the st_ctime and st_mtime fields (respec‐
27 tively, time of last status change and time of last modification; see
28 stat(2)) for the file are updated, and the set-user-ID and set-group-ID
29 permission bits may be cleared.
30
31 With ftruncate(), the file must be open for writing; with truncate(),
32 the file must be writable.
33
35 On success, zero is returned. On error, -1 is returned, and errno is
36 set appropriately.
37
39 For truncate():
40
41 EACCES Search permission is denied for a component of the path prefix,
42 or the named file is not writable by the user. (See also
43 path_resolution(2).)
44
45 EFAULT Path points outside the process's allocated address space.
46
47 EFBIG The argument length is larger than the maximum file size. (XSI)
48
49 EINTR A signal was caught during execution.
50
51 EINVAL The argument length is negative or larger than the maximum file
52 size.
53
54 EIO An I/O error occurred updating the inode.
55
56 EISDIR The named file is a directory.
57
58 ELOOP Too many symbolic links were encountered in translating the
59 pathname.
60
61 ENAMETOOLONG
62 A component of a pathname exceeded 255 characters, or an entire
63 pathname exceeded 1023 characters.
64
65 ENOENT The named file does not exist.
66
67 ENOTDIR
68 A component of the path prefix is not a directory.
69
70 EPERM The underlying file system does not support extending a file
71 beyond its current size.
72
73 EROFS The named file resides on a read-only file system.
74
75 ETXTBSY
76 The file is a pure procedure (shared text) file that is being
77 executed.
78
79 For ftruncate() the same errors apply, but instead of things that can
80 be wrong with path, we now have things that can be wrong with fd:
81
82 EBADF The fd is not a valid descriptor.
83
84 EBADF or EINVAL
85 The fd is not open for writing.
86
87 EINVAL The fd does not reference a regular file.
88
90 4.4BSD, SVr4, POSIX.1-2001 (these calls first appeared in 4.2BSD).
91
93 The above description is for XSI-compliant systems. For non-XSI-com‐
94 pliant systems, the POSIX standard allows two behaviours for ftrun‐
95 cate() when length exceeds the file length (note that truncate() is not
96 specified at all in such an environment): either returning an error, or
97 extending the file. Like most Unix implementations, Linux follows the
98 XSI requirement when dealing with native file systems. However, some
99 non-native file systems do not permit truncate() and ftruncate() to be
100 used to extend a file beyond its current length: a notable example on
101 Linux is VFAT.
102
104 open(2), path_resolution(2), stat(2)
105
106
107
108Linux 2.6.7 2004-06-23 TRUNCATE(2)