-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_file_range.html
384 lines (286 loc) · 9.51 KB
/
copy_file_range.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<!-- Creator : groff version 1.22.4 -->
<!-- CreationDate: Wed Jan 29 11:26:55 2020 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="generator" content="groff -Thtml, see www.gnu.org">
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta name="Content-Style" content="text/css">
<style type="text/css">
p { margin-top: 0; margin-bottom: 0; vertical-align: top }
pre { margin-top: 0; margin-bottom: 0; vertical-align: top }
table { margin-top: 0; margin-bottom: 0; vertical-align: top }
h1 { text-align: center }
</style>
<title>COPY_FILE_RANGE</title>
</head>
<body>
<h1 align="center">COPY_FILE_RANGE</h1>
<a href="#NAME">NAME</a><br>
<a href="#SYNOPSIS">SYNOPSIS</a><br>
<a href="#DESCRIPTION">DESCRIPTION</a><br>
<a href="#RETURN VALUE">RETURN VALUE</a><br>
<a href="#ERRORS">ERRORS</a><br>
<a href="#VERSIONS">VERSIONS</a><br>
<a href="#CONFORMING TO">CONFORMING TO</a><br>
<a href="#NOTES">NOTES</a><br>
<a href="#EXAMPLE">EXAMPLE</a><br>
<a href="#SEE ALSO">SEE ALSO</a><br>
<a href="#COLOPHON">COLOPHON</a><br>
<hr>
<h2>NAME
<a name="NAME"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">copy_file_range
- Copy a range of data from one file to another</p>
<h2>SYNOPSIS
<a name="SYNOPSIS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>#define
_GNU_SOURCE <br>
#include <unistd.h></b></p>
<p style="margin-left:11%; margin-top: 1em"><b>ssize_t
copy_file_range(int</b> <i>fd_in</i><b>, loff_t
*</b><i>off_in</i><b>, <br>
int</b> <i>fd_out</i><b>, loff_t *</b><i>off_out</i><b>,
<br>
size_t</b> <i>len</i><b>, unsigned int</b>
<i>flags</i><b>);</b></p>
<h2>DESCRIPTION
<a name="DESCRIPTION"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">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 file descriptor <i>fd_in</i> to file descriptor
<i>fd_out</i>, overwriting any data that exists within the
requested range of the target file.</p>
<p style="margin-left:11%; margin-top: 1em">The following
semantics apply for <i>off_in</i>, and similar statements
apply to <i>off_out</i>:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="85%">
<p>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.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="1%">
<p>*</p></td>
<td width="3%"></td>
<td width="85%">
<p>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.</p> </td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em">The
<i>flags</i> argument is provided to allow for future
extensions and currently must be to 0.</p>
<h2>RETURN VALUE
<a name="RETURN VALUE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">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.</p>
<p style="margin-left:11%; margin-top: 1em">On error,
<b>copy_file_range</b>() returns -1 and <i>errno</i> is set
to indicate the error.</p>
<h2>ERRORS
<a name="ERRORS"></a>
</h2>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p style="margin-top: 1em"><b>EBADF</b></p></td>
<td width="2%"></td>
<td width="78%">
<p style="margin-top: 1em">One or more file descriptors are
not valid; or <i>fd_in</i> is not open for reading; or
<i>fd_out</i> is not open for writing; or the
<b>O_APPEND</b> flag is set for the open file description
(see <b>open</b>(2)) referred to by the file descriptor
<i>fd_out</i>.</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EFBIG</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>An attempt was made to write a file that exceeds the
implementation-defined maximum file size or the
process’s file size limit, or to write at a position
past the maximum allowed offset.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINVAL</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>Requested range extends beyond the end of the source
file; or the <i>flags</i> argument is not 0.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EIO</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>A low-level I/O error occurred while copying.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EISDIR</b></p></td>
<td width="2%"></td>
<td width="78%">
<p><i>fd_in</i> or <i>fd_out</i> refers to a directory.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ENOMEM</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>Out of memory.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ENOSPC</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>There is not enough space on the target filesystem to
complete the copy.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EXDEV</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The files referred to by <i>file_in</i> and
<i>file_out</i> are not on the same mounted filesystem.</p></td></tr>
</table>
<h2>VERSIONS
<a name="VERSIONS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">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>
<h2>CONFORMING TO
<a name="CONFORMING TO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">The
<b>copy_file_range</b>() system call is a nonstandard Linux
and GNU extension.</p>
<h2>NOTES
<a name="NOTES"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">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>lseek</b>(2) <b>SEEK_DATA</b> and <b>SEEK_HOLE</b>
operations to find the locations of data segments.</p>
<p style="margin-left:11%; margin-top: 1em"><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).</p>
<h2>EXAMPLE
<a name="EXAMPLE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">#define
_GNU_SOURCE <br>
#include <fcntl.h> <br>
#include <stdio.h> <br>
#include <stdlib.h> <br>
#include <sys/stat.h> <br>
#include <sys/syscall.h> <br>
#include <unistd.h></p>
<p style="margin-left:11%; margin-top: 1em">/* On versions
of glibc before 2.27, we must invoke copy_file_range() <br>
using syscall(2) */</p>
<p style="margin-left:11%; margin-top: 1em">static loff_t
<br>
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>
{ <br>
return syscall(__NR_copy_file_range, fd_in, off_in, fd_out,
<br>
off_out, len, flags); <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">int <br>
main(int argc, char **argv) <br>
{ <br>
int fd_in, fd_out; <br>
struct stat stat; <br>
loff_t len, ret;</p>
<p style="margin-left:11%; margin-top: 1em">if (argc != 3)
{ <br>
fprintf(stderr, "Usage: %s <source>
<destination>\n", argv[0]); <br>
exit(EXIT_FAILURE); <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">fd_in =
open(argv[1], O_RDONLY); <br>
if (fd_in == -1) { <br>
perror("open (argv[1])"); <br>
exit(EXIT_FAILURE); <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">if
(fstat(fd_in, &stat) == -1) { <br>
perror("fstat"); <br>
exit(EXIT_FAILURE); <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">len =
stat.st_size;</p>
<p style="margin-left:11%; margin-top: 1em">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>
<p style="margin-left:11%; margin-top: 1em">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>
<p style="margin-left:11%; margin-top: 1em">len -= ret;
<br>
} while (len > 0);</p>
<p style="margin-left:11%; margin-top: 1em">close(fd_in);
<br>
close(fd_out); <br>
exit(EXIT_SUCCESS); <br>
}</p>
<h2>SEE ALSO
<a name="SEE ALSO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>lseek</b>(2),
<b>sendfile</b>(2), <b>splice</b>(2)</p>
<h2>COLOPHON
<a name="COLOPHON"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">This page is
part of release 5.02 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
https://www.kernel.org/doc/man-pages/.</p>
<hr>
</body>
</html>