lseek file-descriptor offset whence=> resulting-offset, error
file-descriptor -- A non-negative fixnum.
offset -- An integer.
whence -- A non-negative fixnum.
resulting-offset -- An integer.
error -- A fixnum.
This function repositions the offset of the open file associated with the file descriptor file-descriptor to the argument offset according to the directive whence as follows:
+seek-set+ |
The offset is set to offset bytes. |
+seek-cur+ |
The offset is set to its current location plus offset bytes. |
+seek-end+ |
The offset is set to the size of the file plus offset bytes. |
+seek-data+ |
Adjust the file offset to the next location in the file greater than or equal to offset containing data. If offset points to data, then the file offset is set to offset. |
+seek-hole+ |
Adjust the file offset to the next hole in the file greater than or equal to offset. If offset points into the middle of a hole, then the file offset is set to offset. If there is no hole past offset, then the file offset is adjusted to the end of the file (i.e., there is an implicit hole at the end of any file). |
In both of the above cases, the call fails if offset points past the end of the file.
These operations allow applications to map holes in a sparsely allocated file. This can be useful for applications such as file backup tools, which can save space when creating backups and preserve holes, if they have a mechanism for discovering holes.
For the purposes of these operations, a hole is a sequence of zeros that (normally) has not been allocated in the underlying file storage. However, a filesystem is not obliged to report holes, so these operations are not a guaranteed mechanism for mapping the storage space actually allocated to a file. (Furthermore, a sequence of zeros that actually has been written to the underlying storage may not be reported as a hole.) In the simplest implementation, a filesystem can support the operations by making +seek-hole+ always return the offset of the end of the file, and making +seek-data+ always return offset (i.e., even if the location referred to by offset is a hole, it can be considered to consist of data that is a sequence of zeros).
+ebadf+ | file-descriptor is not an open file descriptor. |
+einval+ | whence is not valid, or the resulting file offset would be negative, or beyond the end of a seekable device. |
+eoverflow+ | The resulting file offset cannot be represented in the fixed-precision data type of the underlying system. |
+espipe+ | file-descriptor is associated with a pipe, socket, or FIFO. |
+enxio+ | whence is +seek-data+ or +seek-hole+, and the current file offset is beyond the end of the file. |
Some devices are incapable of seeking and POSIX does not specify which devices must support this system call.
On Linux, using this call on a terminal device returns +espipe+.
Note that file descriptors created by dup or fork share the current file position pointer, so seeking on such files may be subject to race conditions.