Blame man2/lseek.2

Packit 7cfc04
'\" t
Packit 7cfc04
.\" Copyright (c) 1980, 1991 Regents of the University of California.
Packit 7cfc04
.\" and Copyright (c) 2011, Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\" All rights reserved.
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
Packit 7cfc04
.\" Redistribution and use in source and binary forms, with or without
Packit 7cfc04
.\" modification, are permitted provided that the following conditions
Packit 7cfc04
.\" are met:
Packit 7cfc04
.\" 1. Redistributions of source code must retain the above copyright
Packit 7cfc04
.\"    notice, this list of conditions and the following disclaimer.
Packit 7cfc04
.\" 2. Redistributions in binary form must reproduce the above copyright
Packit 7cfc04
.\"    notice, this list of conditions and the following disclaimer in the
Packit 7cfc04
.\"    documentation and/or other materials provided with the distribution.
Packit 7cfc04
.\" 3. All advertising materials mentioning features or use of this software
Packit 7cfc04
.\"    must display the following acknowledgement:
Packit 7cfc04
.\"	This product includes software developed by the University of
Packit 7cfc04
.\"	California, Berkeley and its contributors.
Packit 7cfc04
.\" 4. Neither the name of the University nor the names of its contributors
Packit 7cfc04
.\"    may be used to endorse or promote products derived from this software
Packit 7cfc04
.\"    without specific prior written permission.
Packit 7cfc04
.\"
Packit 7cfc04
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 7cfc04
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 7cfc04
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 7cfc04
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 7cfc04
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 7cfc04
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 7cfc04
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 7cfc04
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 7cfc04
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 7cfc04
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 7cfc04
.\" SUCH DAMAGE.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\"     @(#)lseek.2	6.5 (Berkeley) 3/10/91
Packit 7cfc04
.\"
Packit 7cfc04
.\" Modified 1993-07-23 by Rik Faith <faith@cs.unc.edu>
Packit 7cfc04
.\" Modified 1995-06-10 by Andries Brouwer <aeb@cwi.nl>
Packit 7cfc04
.\" Modified 1996-10-31 by Eric S. Raymond <esr@thyrsus.com>
Packit 7cfc04
.\" Modified 1998-01-17 by Michael Haardt
Packit 7cfc04
.\"   <michael@cantor.informatik.rwth-aachen.de>
Packit 7cfc04
.\" Modified 2001-09-24 by Michael Haardt <michael@moria.de>
Packit 7cfc04
.\" Modified 2003-08-21 by Andries Brouwer <aeb@cwi.nl>
Packit 7cfc04
.\" 2011-09-18, mtk, Added SEEK_DATA + SEEK_HOLE
Packit 7cfc04
.\"
Packit 7cfc04
.TH LSEEK 2 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
lseek \- reposition read/write file offset
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.B #include <sys/types.h>
Packit 7cfc04
.br
Packit 7cfc04
.B #include <unistd.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "off_t lseek(int " fd ", off_t " offset ", int " whence );
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
.BR lseek ()
Packit 7cfc04
repositions the file offset of the open file description
Packit 7cfc04
associated with the file descriptor
Packit 7cfc04
.I fd
Packit 7cfc04
to the argument
Packit 7cfc04
.I offset
Packit 7cfc04
according to the directive
Packit 7cfc04
.I whence
Packit 7cfc04
as follows:
Packit 7cfc04
.TP
Packit 7cfc04
.B SEEK_SET
Packit 7cfc04
The file offset is set to
Packit 7cfc04
.I offset
Packit 7cfc04
bytes.
Packit 7cfc04
.TP
Packit 7cfc04
.B SEEK_CUR
Packit 7cfc04
The file offset is set to its current location plus
Packit 7cfc04
.I offset
Packit 7cfc04
bytes.
Packit 7cfc04
.TP
Packit 7cfc04
.B SEEK_END
Packit 7cfc04
The file offset is set to the size of the file plus
Packit 7cfc04
.I offset
Packit 7cfc04
bytes.
Packit 7cfc04
.PP
Packit 7cfc04
.BR lseek ()
Packit 7cfc04
allows the file offset to be set beyond the end
Packit 7cfc04
of the file (but this does not change the size of the file).
Packit 7cfc04
If data is later written at this point, subsequent reads of the data
Packit 7cfc04
in the gap (a "hole") return null bytes (\(aq\\0\(aq) until
Packit 7cfc04
data is actually written into the gap.
Packit 7cfc04
.SS Seeking file data and holes
Packit 7cfc04
Since version 3.1, Linux supports the following additional values for
Packit 7cfc04
.IR whence :
Packit 7cfc04
.TP
Packit 7cfc04
.B SEEK_DATA
Packit 7cfc04
Adjust the file offset to the next location
Packit 7cfc04
in the file greater than or equal to
Packit 7cfc04
.I offset
Packit 7cfc04
containing data.
Packit 7cfc04
If
Packit 7cfc04
.I offset
Packit 7cfc04
points to data,
Packit 7cfc04
then the file offset is set to
Packit 7cfc04
.IR offset .
Packit 7cfc04
.TP
Packit 7cfc04
.B SEEK_HOLE
Packit 7cfc04
Adjust the file offset to the next hole in the file
Packit 7cfc04
greater than or equal to
Packit 7cfc04
.IR offset .
Packit 7cfc04
If
Packit 7cfc04
.I offset
Packit 7cfc04
points into the middle of a hole,
Packit 7cfc04
then the file offset is set to
Packit 7cfc04
.IR offset .
Packit 7cfc04
If there is no hole past
Packit 7cfc04
.IR offset ,
Packit 7cfc04
then the file offset is adjusted to the end of the file
Packit 7cfc04
(i.e., there is an implicit hole at the end of any file).
Packit 7cfc04
.PP
Packit 7cfc04
In both of the above cases,
Packit 7cfc04
.BR lseek ()
Packit 7cfc04
fails if
Packit 7cfc04
.I offset
Packit 7cfc04
points past the end of the file.
Packit 7cfc04
.PP
Packit 7cfc04
These operations allow applications to map holes in a sparsely
Packit 7cfc04
allocated file.
Packit 7cfc04
This can be useful for applications such as file backup tools,
Packit 7cfc04
which can save space when creating backups and preserve holes,
Packit 7cfc04
if they have a mechanism for discovering holes.
Packit 7cfc04
.PP
Packit 7cfc04
For the purposes of these operations, a hole is a sequence of zeros that
Packit 7cfc04
(normally) has not been allocated in the underlying file storage.
Packit 7cfc04
However, a filesystem is not obliged to report holes,
Packit 7cfc04
so these operations are not a guaranteed mechanism for
Packit 7cfc04
mapping the storage space actually allocated to a file.
Packit 7cfc04
(Furthermore, a sequence of zeros that actually has been written
Packit 7cfc04
to the underlying storage may not be reported as a hole.)
Packit 7cfc04
In the simplest implementation,
Packit 7cfc04
a filesystem can support the operations by making
Packit 7cfc04
.BR SEEK_HOLE
Packit 7cfc04
always return the offset of the end of the file,
Packit 7cfc04
and making
Packit 7cfc04
.BR SEEK_DATA
Packit 7cfc04
always return
Packit 7cfc04
.IR offset
Packit 7cfc04
(i.e., even if the location referred to by
Packit 7cfc04
.I offset
Packit 7cfc04
is a hole,
Packit 7cfc04
it can be considered to consist of data that is a sequence of zeros).
Packit 7cfc04
.\" https://lkml.org/lkml/2011/4/22/79
Packit 7cfc04
.\" http://lwn.net/Articles/440255/
Packit 7cfc04
.\" http://blogs.oracle.com/bonwick/entry/seek_hole_and_seek_data
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR _GNU_SOURCE
Packit 7cfc04
feature test macro must be defined in order to obtain the definitions of
Packit 7cfc04
.BR SEEK_DATA
Packit 7cfc04
and
Packit 7cfc04
.BR SEEK_HOLE
Packit 7cfc04
from
Packit 7cfc04
.IR <unistd.h> .
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR SEEK_HOLE
Packit 7cfc04
and
Packit 7cfc04
.BR SEEK_DATA
Packit 7cfc04
operations are supported for the following filesystems:
Packit 7cfc04
.IP * 3
Packit 7cfc04
Btrfs (since Linux 3.1)
Packit 7cfc04
.IP * 3
Packit 7cfc04
OCFS (since Linux 3.2)
Packit 7cfc04
.\" commit 93862d5e1ab875664c6cc95254fc365028a48bb1
Packit 7cfc04
.IP *
Packit 7cfc04
XFS (since Linux 3.5)
Packit 7cfc04
.IP *
Packit 7cfc04
ext4 (since Linux 3.8)
Packit 7cfc04
.IP *
Packit 7cfc04
.BR tmpfs "(5) (since Linux 3.8)"
Packit 7cfc04
.IP *
Packit 7cfc04
NFS (since Linux 3.18)
Packit 7cfc04
.\" commit 1c6dcbe5ceff81c2cf8d929646af675cd59fe7c0
Packit 7cfc04
.\" commit 24bab491220faa446d945624086d838af41d616c
Packit 7cfc04
.IP *
Packit 7cfc04
FUSE (since Linux 4.5)
Packit 7cfc04
.\" commit 0b5da8db145bfd44266ac964a2636a0cf8d7c286
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
Upon successful completion,
Packit 7cfc04
.BR lseek ()
Packit 7cfc04
returns the resulting offset location as measured in bytes from the
Packit 7cfc04
beginning of the file.
Packit 7cfc04
On error, the value \fI(off_t)\ \-1\fP is returned and
Packit 7cfc04
.I errno
Packit 7cfc04
is set to indicate the error.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.TP
Packit 7cfc04
.B EBADF
Packit 7cfc04
.I fd
Packit 7cfc04
is not an open file descriptor.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
.I whence
Packit 7cfc04
is not valid.
Packit 7cfc04
Or: the resulting file offset would be negative,
Packit 7cfc04
or beyond the end of a seekable device.
Packit 7cfc04
.\" Some systems may allow negative offsets for character devices
Packit 7cfc04
.\" and/or for remote filesystems.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENXIO
Packit 7cfc04
.I whence
Packit 7cfc04
is
Packit 7cfc04
.B SEEK_DATA
Packit 7cfc04
or
Packit 7cfc04
.BR SEEK_HOLE ,
Packit 7cfc04
and the file offset is beyond the end of the file.
Packit 7cfc04
.TP
Packit 7cfc04
.B EOVERFLOW
Packit 7cfc04
.\" HP-UX 11 says EINVAL for this case (but POSIX.1 says EOVERFLOW)
Packit 7cfc04
The resulting file offset cannot be represented in an
Packit 7cfc04
.IR off_t .
Packit 7cfc04
.TP
Packit 7cfc04
.B ESPIPE
Packit 7cfc04
.I fd
Packit 7cfc04
is associated with a pipe, socket, or FIFO.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
Packit 7cfc04
.PP
Packit 7cfc04
.BR SEEK_DATA
Packit 7cfc04
and
Packit 7cfc04
.BR SEEK_HOLE
Packit 7cfc04
are nonstandard extensions also present in Solaris,
Packit 7cfc04
FreeBSD, and DragonFly BSD;
Packit 7cfc04
they are proposed for inclusion in the next POSIX revision (Issue 8).
Packit 7cfc04
.\" FIXME . Review http://austingroupbugs.net/view.php?id=415 in the future
Packit 7cfc04
.SH NOTES
Packit 7cfc04
See
Packit 7cfc04
.BR open (2)
Packit 7cfc04
for a discussion of the relationship between file descriptors,
Packit 7cfc04
open file descriptions, and files.
Packit 7cfc04
.PP
Packit 7cfc04
If the
Packit 7cfc04
.B O_APPEND
Packit 7cfc04
file status flag is set on the open file description,
Packit 7cfc04
then a
Packit 7cfc04
.BR write (2)
Packit 7cfc04
.I always
Packit 7cfc04
moves the file offset to the end of the file, regardless of the use of
Packit 7cfc04
.BR lseek ().
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I off_t
Packit 7cfc04
data type is a signed integer data type specified by POSIX.1.
Packit 7cfc04
.PP
Packit 7cfc04
Some devices are incapable of seeking and POSIX does not specify which
Packit 7cfc04
devices must support
Packit 7cfc04
.BR lseek ().
Packit 7cfc04
.PP
Packit 7cfc04
On Linux, using
Packit 7cfc04
.BR lseek ()
Packit 7cfc04
on a terminal device fails with the error
Packit 7cfc04
\fBESPIPE\fP.
Packit 7cfc04
.\" Other systems return the number of written characters,
Packit 7cfc04
.\" using SEEK_SET to set the counter. (Of written characters.)
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR dup (2),
Packit 7cfc04
.BR fallocate (2),
Packit 7cfc04
.BR fork (2),
Packit 7cfc04
.BR open (2),
Packit 7cfc04
.BR fseek (3),
Packit 7cfc04
.BR lseek64 (3),
Packit 7cfc04
.BR posix_fallocate (3)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.