Movatterモバイル変換


[0]ホーム

URL:


man7.org > Linux >man-pages

Linux/UNIX system programming training


copy_file_range(2) — Linux manual page

NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |VERSIONS |STANDARDS |HISTORY |NOTES |BUGS |EXAMPLES |SEE ALSO |COLOPHON

copy_file_range(2)         System Calls Manualcopy_file_range(2)

NAME        top

       copy_file_range - Copy a range of data from one file to another

LIBRARY        top

       Standard C library (libc,-lc)

SYNOPSIS        top

#define _GNU_SOURCE#define _FILE_OFFSET_BITS 64#include <unistd.h>ssize_t copy_file_range(intfd_in, off_t *_Nullableoff_in,intfd_out, off_t *_Nullableoff_out,size_tsize, unsigned intflags);

DESCRIPTION        top

       Thecopy_file_range() 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 tosize bytes of data from the source       file descriptorfd_in to the target file descriptorfd_out,       overwriting any data that exists within the requested range of the       target file.       The following semantics apply foroff_in, and similar statements       apply tooff_out:       •  Ifoff_in is NULL, then bytes are read fromfd_in starting from          the file offset, and the file offset is adjusted by the number          of bytes copied.       •  Ifoff_in is not NULL, thenoff_in must point to a buffer that          specifies the starting offset where bytes fromfd_in will be          read.  The file offset offd_in is not changed, butoff_in is          adjusted appropriately.fd_in andfd_out can refer to the same file.  If they refer to the       same file, then the source and target ranges are not allowed to       overlap.       Theflags argument is provided to allow for future extensions and       currently must be set to 0.

RETURN VALUE        top

       Upon successful completion,copy_file_range() will return the       number of bytes copied between files.  This could be less than the       size originally requested.  If the file offset offd_in is at or       past the end of file, no bytes are copied, andcopy_file_range()       returns zero.       On error,copy_file_range() returns -1 anderrno is set to       indicate the error.

ERRORS        top

EBADFOne or more file descriptors are not valid.EBADFfd_in is not open for reading; orfd_out is not open for              writing.EBADFTheO_APPENDflag is set for the open file description (seeopen(2)) referred to by the file descriptorfd_out.EFBIGAn attempt was made to write at a position past the maximum              file offset the kernel supports.EFBIGAn 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.EFBIGAn attempt was made to write beyond the process's file size              resource limit.  This may also result in the process              receiving aSIGXFSZsignal.EINVALTheflags argument is not 0.EINVALfd_in andfd_out refer to the same file and the source and              target ranges overlap.EINVALEitherfd_in orfd_out is not a regular file.EIOA low-level I/O error occurred while copying.EISDIREitherfd_in orfd_out refers to a directory.ENOMEMOut of memory.ENOSPCThere is not enough space on the target filesystem to              complete the copy.EOPNOTSUPP(since Linux 5.19)              The filesystem does not support this operation.EOVERFLOW              The requested source or destination range is too large to              represent in the specified data types.EPERMfd_out refers to an immutable file.ETXTBSY              Eitherfd_in orfd_out refers to an active swap file.EXDEV(before Linux 5.3)              The files referred to byfd_in andfd_out are not on the              same filesystem.EXDEV(since Linux 5.19)              The files referred to byfd_in andfd_out are not on the              same filesystem, and the source and target filesystems are              not of the same type, or do not support cross-filesystem              copy.

VERSIONS        top

       A major rework of the kernel implementation occurred in Linux 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.       Since Linux 5.19, cross-filesystem copies can be achieved when       both filesystems are of the same type, and that filesystem       implements support for it.  See BUGS for behavior prior to Linux       5.19.       Applications should target the behaviour and requirements of Linux       5.19, that was also backported to earlier stable kernels.

STANDARDS        top

       Linux, GNU.

HISTORY        top

       Linux 4.5, but glibc 2.27 provides a user-space emulation when it       is not available.

NOTES        top

       Iffd_in is a sparse file, thencopy_file_range() may expand any       holes existing in the requested range.  Users may benefit from       callingcopy_file_range() in a loop, and using thelseek(2)SEEK_DATAandSEEK_HOLEoperations to find the locations of data       segments.copy_file_range() 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)._FILE_OFFSET_BITSshould be defined to be 64 in code that uses       non-nulloff_in oroff_out or that takes the address ofcopy_file_range, if the code is intended to be portable to       traditional 32-bit x86 and ARM platforms whereoff_t's width       defaults to 32 bits.

BUGS        top

       In Linux 5.3 to Linux 5.18, cross-filesystem copies were       implemented by the kernel, if the operation was not supported by       individual filesystems.  However, on some virtual filesystems, the       call failed to copy, while still reporting success.

EXAMPLES        top

       #define _GNU_SOURCE       #define _FILE_OFFSET_BITS 64       #include <fcntl.h>       #include <stdio.h>       #include <stdlib.h>       #include <sys/stat.h>       #include <sys/types.h>       #include <unistd.h>       int       main(int argc, char *argv[])       {           int          fd_in, fd_out;           off_t        size, ret;           struct stat  stat;           if (argc != 3) {               fprintf(stderr, "Usage: %s <source> <destination>\n", argv[0]);               exit(EXIT_FAILURE);           }           fd_in = open(argv[1], O_RDONLY);           if (fd_in == -1) {               perror("open (argv[1])");               exit(EXIT_FAILURE);           }           if (fstat(fd_in, &stat) == -1) {               perror("fstat");               exit(EXIT_FAILURE);           }           size = stat.st_size;           fd_out = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0644);           if (fd_out == -1) {               perror("open (argv[2])");               exit(EXIT_FAILURE);           }           do {               ret = copy_file_range(fd_in, NULL, fd_out, NULL, size, 0);               if (ret == -1) {                   perror("copy_file_range");                   exit(EXIT_FAILURE);               }               size -= ret;           } while (size > 0 && ret > 0);           close(fd_in);           close(fd_out);           exit(EXIT_SUCCESS);       }

SEE ALSO        top

lseek(2),sendfile(2),splice(2)

COLOPHON        top

       This page is part of theman-pages (Linux kernel and C library       user-space interface documentation) project.  Information about       the project can be found at        ⟨https://www.kernel.org/doc/man-pages/⟩.  If you have a bug report       for this manual page, see       ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.       This page was obtained from the tarball man-pages-6.15.tar.gz       fetched from       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on       2025-08-11.  If you discover any rendering problems in this HTML       version of the page, or you believe there is a better or more up-       to-date source for the page, or you have corrections or       improvements to the information in this COLOPHON (which isnot       part of the original manual page), send a mail to       man-pages@man7.orgLinux man-pages 6.15            2025-05-17copy_file_range(2)

Pages that refer to this page:sendfile(2)splice(2)syscalls(2)off_t(3type)proc_pid_io(5)feature_test_macros(7)xfs_io(8)



HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface.

For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere.

Hosting byjambit GmbH.

Cover of TLPI


[8]ページ先頭

©2009-2025 Movatter.jp