1COPY_FILE_RANGE(2)         Linux Programmer's Manual        COPY_FILE_RANGE(2)
2
3
4

NAME

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

SYNOPSIS

9       #define _GNU_SOURCE
10       #include <unistd.h>
11
12       ssize_t copy_file_range(int fd_in, loff_t *off_in,
13                               int fd_out, loff_t *off_out,
14                               size_t len, unsigned int flags);
15

DESCRIPTION

17       The  copy_file_range()  system  call performs an in-kernel copy between
18       two file descriptors without the additional cost of  transferring  data
19       from the kernel to user space and then back into the kernel.  It copies
20       up to len bytes of data from file descriptor fd_in to  file  descriptor
21       fd_out,  overwriting any data that exists within the requested range of
22       the target file.
23
24       The following semantics apply for off_in, and similar statements  apply
25       to off_out:
26
27       *  If  off_in is NULL, then bytes are read from fd_in starting from the
28          file offset, and the file offset is adjusted by the number of  bytes
29          copied.
30
31       *  If off_in is not NULL, then off_in must point to a buffer that spec‐
32          ifies the starting offset where bytes from fd_in will be read.   The
33          file  offset  of fd_in is not changed, but off_in is adjusted appro‐
34          priately.
35
36       The flags argument is provided to allow for future extensions and  cur‐
37       rently must be to 0.
38

RETURN VALUE

40       Upon successful completion, copy_file_range() will return the number of
41       bytes copied between files.  This could be less than the length  origi‐
42       nally requested.
43
44       On error, copy_file_range() returns -1 and errno is set to indicate the
45       error.
46

ERRORS

48       EBADF  One or more file descriptors are not valid; or fd_in is not open
49              for  reading; or fd_out is not open for writing; or the O_APPEND
50              flag is set for the open file description referred to by fd_out.
51
52       EFBIG  An attempt was made to write a file that exceeds the implementa‐
53              tion-defined maximum file size or the process's file size limit,
54              or to write at a position past the maximum allowed offset.
55
56       EINVAL Requested range extends beyond the end of the  source  file;  or
57              the flags argument is not 0.
58
59       EIO    A low-level I/O error occurred while copying.
60
61       EISDIR fd_in or fd_out refers to a directory.
62
63       ENOMEM Out of memory.
64
65       ENOSPC There  is  not enough space on the target filesystem to complete
66              the copy.
67
68       EXDEV  The files referred to by file_in and file_out  are  not  on  the
69              same mounted filesystem.
70

VERSIONS

72       The  copy_file_range()  system  call  first  appeared in Linux 4.5, but
73       glibc 2.27 provides a user-space emulation when it is not available.
74

CONFORMING TO

76       The copy_file_range() system call is a nonstandard Linux and GNU exten‐
77       sion.
78

NOTES

80       If  file_in  is  a  sparse  file, then copy_file_range() may expand any
81       holes existing in the requested range.  Users may benefit from  calling
82       copy_file_range()  in  a  loop,  and  using  the lseek(2) SEEK_DATA and
83       SEEK_HOLE operations to find the locations of data segments.
84
85       copy_file_range() gives filesystems an opportunity to  implement  "copy
86       acceleration"  techniques,  such  as  the use of reflinks (i.e., two or
87       more inodes that share pointers to the same copy-on-write disk  blocks)
88       or server-side-copy (in the case of NFS).
89

EXAMPLE

91       #define _GNU_SOURCE
92       #include <fcntl.h>
93       #include <stdio.h>
94       #include <stdlib.h>
95       #include <sys/stat.h>
96       #include <sys/syscall.h>
97       #include <unistd.h>
98
99       /* On versions of glibc before 2.27, we must invoke copy_file_range()
100          using syscall(2) */
101
102       static loff_t
103       copy_file_range(int fd_in, loff_t *off_in, int fd_out,
104                       loff_t *off_out, size_t len, unsigned int flags)
105       {
106           return syscall(__NR_copy_file_range, fd_in, off_in, fd_out,
107                          off_out, len, flags);
108       }
109
110       int
111       main(int argc, char **argv)
112       {
113           int fd_in, fd_out;
114           struct stat stat;
115           loff_t len, ret;
116
117           if (argc != 3) {
118               fprintf(stderr, "Usage: %s <source> <destination>\n", argv[0]);
119               exit(EXIT_FAILURE);
120           }
121
122           fd_in = open(argv[1], O_RDONLY);
123           if (fd_in == -1) {
124               perror("open (argv[1])");
125               exit(EXIT_FAILURE);
126           }
127
128           if (fstat(fd_in, &stat) == -1) {
129               perror("fstat");
130               exit(EXIT_FAILURE);
131           }
132
133           len = stat.st_size;
134
135           fd_out = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0644);
136           if (fd_out == -1) {
137               perror("open (argv[2])");
138               exit(EXIT_FAILURE);
139           }
140
141           do {
142               ret = copy_file_range(fd_in, NULL, fd_out, NULL, len, 0);
143               if (ret == -1) {
144                   perror("copy_file_range");
145                   exit(EXIT_FAILURE);
146               }
147
148               len -= ret;
149           } while (len > 0);
150
151           close(fd_in);
152           close(fd_out);
153           exit(EXIT_SUCCESS);
154       }
155

SEE ALSO

157       lseek(2), sendfile(2), splice(2)
158

COLOPHON

160       This  page  is  part of release 4.16 of the Linux man-pages project.  A
161       description of the project, information about reporting bugs,  and  the
162       latest     version     of     this    page,    can    be    found    at
163       https://www.kernel.org/doc/man-pages/.
164
165
166
167Linux                             2018-02-02                COPY_FILE_RANGE(2)
Impressum