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