1LSEEK(2) Linux Programmer's Manual LSEEK(2)
2
3
4
6 lseek - reposition read/write file offset
7
9 #include <unistd.h>
10
11 off_t lseek(int fd, off_t offset, int whence);
12
14 lseek() repositions the file offset of the open file description asso‐
15 ciated with the file descriptor fd to the argument offset according to
16 the directive whence as follows:
17
18 SEEK_SET
19 The file offset is set to offset bytes.
20
21 SEEK_CUR
22 The file offset is set to its current location plus offset
23 bytes.
24
25 SEEK_END
26 The file offset is set to the size of the file plus offset
27 bytes.
28
29 lseek() allows the file offset to be set beyond the end of the file
30 (but this does not change the size of the file). If data is later
31 written at this point, subsequent reads of the data in the gap (a
32 "hole") return null bytes ('\0') until data is actually written into
33 the gap.
34
35 Seeking file data and holes
36 Since version 3.1, Linux supports the following additional values for
37 whence:
38
39 SEEK_DATA
40 Adjust the file offset to the next location in the file greater
41 than or equal to offset containing data. If offset points to
42 data, then the file offset is set to offset.
43
44 SEEK_HOLE
45 Adjust the file offset to the next hole in the file greater than
46 or equal to offset. If offset points into the middle of a hole,
47 then the file offset is set to offset. If there is no hole past
48 offset, then the file offset is adjusted to the end of the file
49 (i.e., there is an implicit hole at the end of any file).
50
51 In both of the above cases, lseek() fails if offset points past the end
52 of the file.
53
54 These operations allow applications to map holes in a sparsely allo‐
55 cated file. This can be useful for applications such as file backup
56 tools, which can save space when creating backups and preserve holes,
57 if they have a mechanism for discovering holes.
58
59 For the purposes of these operations, a hole is a sequence of zeros
60 that (normally) has not been allocated in the underlying file storage.
61 However, a filesystem is not obliged to report holes, so these opera‐
62 tions are not a guaranteed mechanism for mapping the storage space ac‐
63 tually allocated to a file. (Furthermore, a sequence of zeros that ac‐
64 tually has been written to the underlying storage may not be reported
65 as a hole.) In the simplest implementation, a filesystem can support
66 the operations by making SEEK_HOLE always return the offset of the end
67 of the file, and making SEEK_DATA always return offset (i.e., even if
68 the location referred to by offset is a hole, it can be considered to
69 consist of data that is a sequence of zeros).
70
71 The _GNU_SOURCE feature test macro must be defined in order to obtain
72 the definitions of SEEK_DATA and SEEK_HOLE from <unistd.h>.
73
74 The SEEK_HOLE and SEEK_DATA operations are supported for the following
75 filesystems:
76
77 * Btrfs (since Linux 3.1)
78
79 * OCFS (since Linux 3.2)
80
81 * XFS (since Linux 3.5)
82
83 * ext4 (since Linux 3.8)
84
85 * tmpfs(5) (since Linux 3.8)
86
87 * NFS (since Linux 3.18)
88
89 * FUSE (since Linux 4.5)
90
91 * GFS2 (since Linux 4.15)
92
94 Upon successful completion, lseek() returns the resulting offset loca‐
95 tion as measured in bytes from the beginning of the file. On error,
96 the value (off_t) -1 is returned and errno is set to indicate the er‐
97 ror.
98
100 EBADF fd is not an open file descriptor.
101
102 EINVAL whence is not valid. Or: the resulting file offset would be
103 negative, or beyond the end of a seekable device.
104
105 ENXIO whence is SEEK_DATA or SEEK_HOLE, and offset is beyond the end
106 of the file, or whence is SEEK_DATA and offset is within a hole
107 at the end of the file.
108
109 EOVERFLOW
110 The resulting file offset cannot be represented in an off_t.
111
112 ESPIPE fd is associated with a pipe, socket, or FIFO.
113
115 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
116
117 SEEK_DATA and SEEK_HOLE are nonstandard extensions also present in So‐
118 laris, FreeBSD, and DragonFly BSD; they are proposed for inclusion in
119 the next POSIX revision (Issue 8).
120
122 See open(2) for a discussion of the relationship between file descrip‐
123 tors, open file descriptions, and files.
124
125 If the O_APPEND file status flag is set on the open file description,
126 then a write(2) always moves the file offset to the end of the file,
127 regardless of the use of lseek().
128
129 The off_t data type is a signed integer data type specified by POSIX.1.
130
131 Some devices are incapable of seeking and POSIX does not specify which
132 devices must support lseek().
133
134 On Linux, using lseek() on a terminal device fails with the error ES‐
135 PIPE.
136
138 dup(2), fallocate(2), fork(2), open(2), fseek(3), lseek64(3),
139 posix_fallocate(3)
140
142 This page is part of release 5.12 of the Linux man-pages project. A
143 description of the project, information about reporting bugs, and the
144 latest version of this page, can be found at
145 https://www.kernel.org/doc/man-pages/.
146
147
148
149Linux 2021-03-22 LSEEK(2)