1COPY_FILE_RANGE(2) Linux Programmer's Manual COPY_FILE_RANGE(2)
2
3
4
6 copy_file_range - Copy a range of data from one file to another
7
9 #define _GNU_SOURCE
10 #include <unistd.h>
11
12 ssize_t copy_file_range(int fd_in, off64_t *off_in,
13 int fd_out, off64_t *off_out,
14 size_t len, unsigned int flags);
15
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 the source file descriptor fd_in to the
21 target file descriptor fd_out, overwriting any data that exists within
22 the requested range of 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 fd_in and fd_out can refer to the same file. If they refer to the same
37 file, then the source and target ranges are not allowed to overlap.
38
39 The flags argument is provided to allow for future extensions and cur‐
40 rently must be set to 0.
41
43 Upon successful completion, copy_file_range() will return the number of
44 bytes copied between files. This could be less than the length origi‐
45 nally requested. If the file offset of fd_in is at or past the end of
46 file, no bytes are copied, and copy_file_range() returns zero.
47
48 On error, copy_file_range() returns -1 and errno is set to indicate the
49 error.
50
52 EBADF One or more file descriptors are not valid.
53
54 EBADF fd_in is not open for reading; or fd_out is not open for writ‐
55 ing.
56
57 EBADF The O_APPEND flag is set for the open file description (see
58 open(2)) referred to by the file descriptor fd_out.
59
60 EFBIG An attempt was made to write at a position past the maximum file
61 offset the kernel supports.
62
63 EFBIG An attempt was made to write a range that exceeds the allowed
64 maximum file size. The maximum file size differs between
65 filesystem implementations and can be different from the maximum
66 allowed file offset.
67
68 EFBIG An attempt was made to write beyond the process's file size re‐
69 source limit. This may also result in the process receiving a
70 SIGXFSZ signal.
71
72 EINVAL The flags argument is not 0.
73
74 EINVAL fd_in and fd_out refer to the same file and the source and tar‐