418 lines
11 KiB
HTML
418 lines
11 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of COPY_FILE_RANGE</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>COPY_FILE_RANGE</H1>
|
|
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-10-10<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
copy_file_range - Copy a range of data from one file to another
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#define _GNU_SOURCE</B>
|
|
<B>#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>></B>
|
|
|
|
<B>ssize_t copy_file_range(int </B><I>fd_in</I><B>, loff_t *</B><I>off_in</I><B>,</B>
|
|
<B> int </B><I>fd_out</I><B>, loff_t *</B><I>off_out</I><B>,</B>
|
|
<B> size_t </B><I>len</I><B>, unsigned int </B><I>flags</I><B>);</B>
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>copy_file_range</B>()
|
|
|
|
system call performs an in-kernel copy between two file descriptors
|
|
without the additional cost of transferring data from the kernel to user space
|
|
and then back into the kernel.
|
|
It copies up to
|
|
<I>len</I>
|
|
|
|
bytes of data from the source file descriptor
|
|
<I>fd_in</I>
|
|
|
|
to the target file descriptor
|
|
<I>fd_out</I>,
|
|
|
|
overwriting any data that exists within the requested range of the target file.
|
|
<P>
|
|
|
|
The following semantics apply for
|
|
<I>off_in</I>,
|
|
|
|
and similar statements apply to
|
|
<I>off_out</I>:
|
|
|
|
<DL COMPACT>
|
|
<DT id="1">*<DD>
|
|
If
|
|
<I>off_in</I>
|
|
|
|
is NULL, then bytes are read from
|
|
<I>fd_in</I>
|
|
|
|
starting from the file offset, and the file offset is
|
|
adjusted by the number of bytes copied.
|
|
<DT id="2">*<DD>
|
|
If
|
|
<I>off_in</I>
|
|
|
|
is not NULL, then
|
|
<I>off_in</I>
|
|
|
|
must point to a buffer that specifies the starting
|
|
offset where bytes from
|
|
<I>fd_in</I>
|
|
|
|
will be read.
|
|
The file offset of
|
|
<I>fd_in</I>
|
|
|
|
is not changed, but
|
|
<I>off_in</I>
|
|
|
|
is adjusted appropriately.
|
|
</DL>
|
|
<P>
|
|
|
|
<I>fd_in</I>
|
|
|
|
and
|
|
<I>fd_out</I>
|
|
|
|
can refer to the same file.
|
|
If they refer to the same file, then the source and target ranges are not
|
|
allowed to overlap.
|
|
<P>
|
|
|
|
The
|
|
<I>flags</I>
|
|
|
|
argument is provided to allow for future extensions
|
|
and currently must be set to 0.
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
Upon successful completion,
|
|
<B>copy_file_range</B>()
|
|
|
|
will return the number of bytes copied between files.
|
|
This could be less than the length originally requested.
|
|
If the file offset of
|
|
<I>fd_in</I>
|
|
|
|
is at or past the end of file, no bytes are copied, and
|
|
<B>copy_file_range</B>()
|
|
|
|
returns zero.
|
|
<P>
|
|
|
|
On error,
|
|
<B>copy_file_range</B>()
|
|
|
|
returns -1 and
|
|
<I>errno</I>
|
|
|
|
is set to indicate the error.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
<DL COMPACT>
|
|
<DT id="3"><B>EBADF</B>
|
|
|
|
<DD>
|
|
One or more file descriptors are not valid.
|
|
<DT id="4"><B>EBADF</B>
|
|
|
|
<DD>
|
|
<I>fd_in</I>
|
|
|
|
is not open for reading; or
|
|
<I>fd_out</I>
|
|
|
|
is not open for writing.
|
|
<DT id="5"><B>EBADF</B>
|
|
|
|
<DD>
|
|
The
|
|
<B>O_APPEND</B>
|
|
|
|
flag is set for the open file description (see
|
|
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2))
|
|
|
|
referred to by the file descriptor
|
|
<I>fd_out</I>.
|
|
|
|
<DT id="6"><B>EFBIG</B>
|
|
|
|
<DD>
|
|
An attempt was made to write at a position past the maximum file offset the
|
|
kernel supports.
|
|
<DT id="7"><B>EFBIG</B>
|
|
|
|
<DD>
|
|
An attempt was made to write a range that exceeds the allowed maximum file size.
|
|
The maximum file size differs between filesystem implementations and can be
|
|
different from the maximum allowed file offset.
|
|
<DT id="8"><B>EFBIG</B>
|
|
|
|
<DD>
|
|
An attempt was made to write beyond the process's file size resource limit.
|
|
This may also result in the process receiving a
|
|
<I>SIGXFSZ</I>
|
|
|
|
signal.
|
|
<DT id="9"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
The
|
|
<I>flags</I>
|
|
|
|
argument is not 0.
|
|
<DT id="10"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
<I>fd_in</I>
|
|
|
|
and
|
|
<I>fd_out</I>
|
|
|
|
refer to the same file and the source and target ranges overlap.
|
|
<DT id="11"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
Either
|
|
<I>fd_in</I>
|
|
|
|
or
|
|
<I>fd_out</I>
|
|
|
|
is not a regular file.
|
|
<DT id="12"><B>EIO</B>
|
|
|
|
<DD>
|
|
A low-level I/O error occurred while copying.
|
|
<DT id="13"><B>EISDIR</B>
|
|
|
|
<DD>
|
|
Either
|
|
<I>fd_in</I>
|
|
|
|
or
|
|
<I>fd_out</I>
|
|
|
|
refers to a directory.
|
|
<DT id="14"><B>ENOMEM</B>
|
|
|
|
<DD>
|
|
Out of memory.
|
|
<DT id="15"><B>ENOSPC</B>
|
|
|
|
<DD>
|
|
There is not enough space on the target filesystem to complete the copy.
|
|
<DT id="16"><B>EOVERFLOW</B>
|
|
|
|
<DD>
|
|
The requested source or destination range is too large to represent in the
|
|
specified data types.
|
|
<DT id="17"><B>EPERM</B>
|
|
|
|
<DD>
|
|
<I>fd_out</I>
|
|
|
|
refers to an immutable file.
|
|
<DT id="18"><B>ETXTBSY</B>
|
|
|
|
<DD>
|
|
Either
|
|
<I>fd_in</I>
|
|
|
|
or
|
|
<I>fd_out</I>
|
|
|
|
refers to an active swap file.
|
|
<DT id="19"><B>EXDEV</B>
|
|
|
|
<DD>
|
|
The files referred to by
|
|
<I>file_in</I> and <I>file_out</I>
|
|
|
|
are not on the same mounted filesystem (pre Linux 5.3).
|
|
</DL>
|
|
<A NAME="lbAG"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
The
|
|
<B>copy_file_range</B>()
|
|
|
|
system call first appeared in Linux 4.5, but glibc 2.27 provides a user-space
|
|
emulation when it is not available.
|
|
|
|
<P>
|
|
|
|
A major rework of the kernel implementation occurred in 5.3.
|
|
Areas of the API that weren't clearly defined were clarified and the API bounds
|
|
are much more strictly checked than on earlier kernels.
|
|
Applications should target the behaviour and requirements of 5.3 kernels.
|
|
<P>
|
|
|
|
First support for cross-filesystem copies was introduced in Linux 5.3.
|
|
Older kernels will return -EXDEV when cross-filesystem copies are attempted.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
The
|
|
<B>copy_file_range</B>()
|
|
|
|
system call is a nonstandard Linux and GNU extension.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
If
|
|
<I>file_in</I>
|
|
|
|
is a sparse file, then
|
|
<B>copy_file_range</B>()
|
|
|
|
may expand any holes existing in the requested range.
|
|
Users may benefit from calling
|
|
<B>copy_file_range</B>()
|
|
|
|
in a loop, and using the
|
|
<B><A HREF="/cgi-bin/man/man2html?2+lseek">lseek</A></B>(2)
|
|
|
|
<B>SEEK_DATA</B>
|
|
|
|
and
|
|
<B>SEEK_HOLE</B>
|
|
|
|
operations to find the locations of data segments.
|
|
<P>
|
|
|
|
<B>copy_file_range</B>()
|
|
|
|
gives filesystems an opportunity to implement "copy acceleration" techniques,
|
|
such as the use of reflinks (i.e., two or more inodes that share
|
|
pointers to the same copy-on-write disk blocks)
|
|
or server-side-copy (in the case of NFS).
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
#include <<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/stat.h">sys/stat.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/syscall.h">sys/syscall.h</A>>
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
<P>
|
|
/* On versions of glibc before 2.27, we must invoke copy_file_range()
|
|
<BR> using <A HREF="/cgi-bin/man/man2html?2+syscall">syscall</A>(2) */
|
|
<P>
|
|
static loff_t
|
|
copy_file_range(int fd_in, loff_t *off_in, int fd_out,
|
|
<BR> loff_t *off_out, size_t len, unsigned int flags)
|
|
{
|
|
<BR> return syscall(__NR_copy_file_range, fd_in, off_in, fd_out,
|
|
<BR> off_out, len, flags);
|
|
}
|
|
<P>
|
|
int
|
|
main(int argc, char **argv)
|
|
{
|
|
<BR> int fd_in, fd_out;
|
|
<BR> struct stat stat;
|
|
<BR> loff_t len, ret;
|
|
<P>
|
|
<BR> if (argc != 3) {
|
|
<BR> fprintf(stderr, "Usage: %s <source> <destination>\n", argv[0]);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> fd_in = open(argv[1], O_RDONLY);
|
|
<BR> if (fd_in == -1) {
|
|
<BR> perror("open (argv[1])");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> if (fstat(fd_in, &stat) == -1) {
|
|
<BR> perror("fstat");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> len = stat.st_size;
|
|
<P>
|
|
<BR> fd_out = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0644);
|
|
<BR> if (fd_out == -1) {
|
|
<BR> perror("open (argv[2])");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> do {
|
|
<BR> ret = copy_file_range(fd_in, NULL, fd_out, NULL, len, 0);
|
|
<BR> if (ret == -1) {
|
|
<BR> perror("copy_file_range");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> len -= ret;
|
|
<BR> } while (len > 0 && ret > 0);
|
|
<P>
|
|
<BR> close(fd_in);
|
|
<BR> close(fd_out);
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+lseek">lseek</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+sendfile">sendfile</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+splice">splice</A></B>(2)
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="20"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="21"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="22"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="23"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="24"><A HREF="#lbAF">ERRORS</A><DD>
|
|
<DT id="25"><A HREF="#lbAG">VERSIONS</A><DD>
|
|
<DT id="26"><A HREF="#lbAH">CONFORMING TO</A><DD>
|
|
<DT id="27"><A HREF="#lbAI">NOTES</A><DD>
|
|
<DT id="28"><A HREF="#lbAJ">EXAMPLE</A><DD>
|
|
<DT id="29"><A HREF="#lbAK">SEE ALSO</A><DD>
|
|
<DT id="30"><A HREF="#lbAL">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:32 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|