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 ac‐
64 tually allocated to a file. (Furthermore, a sequence of zeros that ac‐
65 tually 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