]>
Commit | Line | Data |
---|---|---|
b3aa0442 A |
1 | .\" |
2 | .\" Copyright (c) 2002 Apple Computer, Inc. All rights reserved. | |
3 | .\" | |
62b275d9 | 4 | .Dd July 22, 2019 |
b3aa0442 A |
5 | .Dt COPYFILE 3 |
6 | .Os | |
7 | .Sh NAME | |
8 | .Nm copyfile , fcopyfile , | |
9 | .Nm copyfile_state_alloc , copyfile_state_free , | |
10 | .Nm copyfile_state_get , copyfile_state_set | |
11 | .Nd copy a file | |
12 | .Sh LIBRARY | |
13 | .Lb libc | |
14 | .Sh SYNOPSIS | |
15 | .In copyfile.h | |
16 | .Ft int | |
17 | .Fn copyfile "const char *from" "const char *to" "copyfile_state_t state" "copyfile_flags_t flags" | |
18 | .Ft int | |
19 | .Fn fcopyfile "int from" "int to" "copyfile_state_t state" "copyfile_flags_t flags" | |
20 | .Ft copyfile_state_t | |
21 | .Fn copyfile_state_alloc "void" | |
22 | .Ft int | |
23 | .Fn copyfile_state_free "copyfile_state_t state" | |
24 | .Ft int | |
25 | .Fn copyfile_state_get "copyfile_state_t state" "uint32_t flag" "void * dst" | |
26 | .Ft int | |
27 | .Fn copyfile_state_set "copyfile_state_t state" "uint32_t flag" "const void * src" | |
659640e4 A |
28 | .Ft typedef int |
29 | .Fn (*copyfile_callback_t) "int what" "int stage" "copyfile_state_t state" "const char * src" "const char * dst" "void * ctx" | |
b3aa0442 A |
30 | .Sh DESCRIPTION |
31 | These functions are used to copy a file's data and/or metadata. (Metadata | |
32 | consists of permissions, extended attributes, access control lists, and so | |
33 | forth.) | |
34 | .Pp | |
35 | The | |
36 | .Fn copyfile_state_alloc | |
37 | function initializes a | |
38 | .Vt copyfile_state_t | |
39 | object (which is an opaque data type). | |
40 | This object can be passed to | |
41 | .Fn copyfile | |
42 | and | |
43 | .Fn fcopyfile ; | |
44 | .Fn copyfile_state_get | |
45 | and | |
46 | .Fn copyfile_state_set | |
47 | can be used to manipulate the state (see below). | |
48 | The | |
49 | .Fn copyfile_state_free | |
50 | function is used to deallocate the object and its contents. | |
51 | .Pp | |
52 | The | |
53 | .Fn copyfile | |
54 | function can copy the named | |
55 | .Va from | |
56 | file to the named | |
57 | .Va to | |
58 | file; the | |
59 | .Fn fcopyfile | |
60 | function does the same, but using the file descriptors of already-opened | |
61 | files. | |
62 | If the | |
63 | .Va state | |
64 | parameter is the return value from | |
65 | .Fn copyfile_state_alloc , | |
66 | then | |
67 | .Fn copyfile | |
68 | and | |
69 | .Fn fcopyfile | |
70 | will use the information from the state object; if it is | |
71 | .Dv NULL , | |
72 | then both functions will work normally, but less control will be available to the caller. | |
73 | The | |
74 | .Va flags | |
75 | parameter controls which contents are copied: | |
76 | .Bl -tag -width COPYFILE_XATTR | |
77 | .It Dv COPYFILE_ACL | |
78 | Copy the source file's access control lists. | |
79 | .It Dv COPYFILE_STAT | |
80 | Copy the source file's POSIX information (mode, modification time, etc.). | |
81 | .It Dv COPYFILE_XATTR | |
82 | Copy the source file's extended attributes. | |
83 | .It Dv COPYFILE_DATA | |
84 | Copy the source file's data. | |
85 | .El | |
86 | .Pp | |
87 | These values may be or'd together; several convenience macros are provided: | |
88 | .Bl -tag -width COPYFILE_SECURITY | |
89 | .It Dv COPYFILE_SECURITY | |
90 | Copy the source file's POSIX and ACL information; equivalent to | |
91 | .Dv (COPYFILE_STAT|COPYFILE_ACL) . | |
92 | .It Dv COPYFILE_METADATA | |
93 | Copy the metadata; equivalent to | |
94 | .Dv (COPYFILE_SECURITY|COPYFILE_XATTR) . | |
95 | .It Dv COPYFILE_ALL | |
96 | Copy the entire file; equivalent to | |
97 | .Dv (COPYFILE_METADATA|COPYFILE_DATA) . | |
98 | .El | |
99 | .Pp | |
100 | The | |
101 | .Fn copyfile | |
102 | and | |
103 | .Fn fcopyfile | |
104 | functions can also have their behavior modified by the following flags: | |
105 | .Bl -tag -width COPYFILE_NOFOLLOW_SRC | |
659640e4 A |
106 | .It Dv COPYFILE_RECURSIVE |
107 | Causes | |
108 | .Fn copyfile | |
3f81d1c4 | 109 | to recursively copy a hierarchy. |
659640e4 A |
110 | This flag is not used by |
111 | .Fn fcopyfile ; | |
112 | see below for more information. | |
b3aa0442 A |
113 | .It Dv COPYFILE_CHECK |
114 | Return a bitmask (corresponding to the | |
115 | .Va flags | |
116 | argument) indicating which contents would be copied; no data are actually | |
117 | copied. (E.g., if | |
118 | .Va flags | |
119 | was set to | |
120 | .Dv COPYFILE_CHECK|COPYFILE_METADATA , | |
121 | and the | |
122 | .Va from | |
123 | file had extended attributes but no ACLs, the return value would be | |
124 | .Dv COPYFILE_XATTR .) | |
125 | .It Dv COPYFILE_PACK | |
126 | Serialize the | |
127 | .Va from | |
85b8a2cb A |
128 | file. |
129 | The | |
b3aa0442 A |
130 | .Va to |
131 | file is an AppleDouble-format file. | |
132 | .It Dv COPYFILE_UNPACK | |
133 | Unserialize the | |
134 | .Va from | |
85b8a2cb A |
135 | file. |
136 | The | |
b3aa0442 A |
137 | .Va from |
138 | file is an AppleDouble-format file; the | |
139 | .Va to | |
140 | file will have the extended attributes, ACLs, resource fork, and | |
141 | FinderInfo data from the | |
142 | .Va to | |
143 | file, regardless of the | |
144 | .Va flags | |
145 | argument passed in. | |
146 | .It Dv COPYFILE_EXCL | |
147 | Fail if the | |
148 | .Va to | |
149 | file already exists. (This is only applicable for the | |
150 | .Fn copyfile | |
151 | function.) | |
152 | .It Dv COPYFILE_NOFOLLOW_SRC | |
153 | Do not follow the | |
154 | .Va from | |
155 | file, if it is a symbolic link. (This is only applicable for the | |
156 | .Fn copyfile | |
157 | function.) | |
158 | .It Dv COPYFILE_NOFOLLOW_DST | |
159 | Do not follow the | |
160 | .Va to | |
161 | file, if it is a symbolic link. (This is only applicable for the | |
162 | .Fn copyfile | |
163 | function.) | |
164 | .It Dv COPYFILE_MOVE | |
3f81d1c4 A |
165 | Unlink (using |
166 | .Xr remove 3 ) | |
167 | the | |
b3aa0442 A |
168 | .Fa from |
169 | file. (This is only applicable for the | |
170 | .Fn copyfile | |
3f81d1c4 A |
171 | function.) No error is returned if |
172 | .Xr remove 3 | |
85b8a2cb A |
173 | fails. |
174 | Note that | |
3f81d1c4 A |
175 | .Xr remove 3 |
176 | removes a symbolic link itself, not the | |
177 | target of the link. | |
b3aa0442 A |
178 | .It Dv COPYFILE_UNLINK |
179 | Unlink the | |
180 | .Va to | |
181 | file before starting. (This is only applicable for the | |
182 | .Fn copyfile | |
183 | function.) | |
1f4df596 | 184 | .It Dv COPYFILE_CLONE_FORCE |
f475fe66 | 185 | Clone the file instead. |
1f4df596 | 186 | This is a force flag i.e. if cloning fails, an error is returned. |
dcb8fd50 A |
187 | This flag is equivalent to (COPYFILE_EXCL | COPYFILE_ACL | COPYFILE_STAT | COPYFILE_XATTR | COPYFILE_DATA |
188 | | COPYFILE_NOFOLLOW_SRC). | |
189 | Note that if cloning is successful, progress callbacks will not be invoked. | |
f475fe66 | 190 | Note also that there is no support for cloning directories: if a directory is provided as the source, |
85b8a2cb A |
191 | an error will be returned. |
192 | Since this flag implies COPYFILE_NOFOLLOW_SRC, symbolic links themselves will be cloned instead of their targets. | |
f475fe66 A |
193 | (This is only applicable for the |
194 | .Fn copyfile | |
195 | function.) | |
1f4df596 | 196 | .It Dv COPYFILE_CLONE |
f475fe66 | 197 | Try to clone the file instead. |
1f4df596 | 198 | This is a best try flag i.e. if cloning fails, fallback to copying the file. |
dcb8fd50 A |
199 | This flag is equivalent to (COPYFILE_EXCL | COPYFILE_ACL | COPYFILE_STAT | COPYFILE_XATTR | COPYFILE_DATA |
200 | | COPYFILE_NOFOLLOW_SRC). | |
201 | Note that if cloning is successful, progress callbacks will not be invoked. | |
202 | Note also that there is no support for cloning directories: if a directory is provided as the source and | |
85b8a2cb A |
203 | COPYFILE_CLONE_FORCE is not passed, this will instead copy the directory. |
204 | Since this flag implies COPYFILE_NOFOLLOW_SRC, symbolic links themselves will be cloned instead of their targets. | |
205 | Recursive copying however is supported, see below for more information. | |
f475fe66 A |
206 | (This is only applicable for the |
207 | .Fn copyfile | |
208 | function.) | |
937356ff A |
209 | .It Dv COPYFILE_DATA_SPARSE |
210 | Copy a file sparsely. | |
211 | This requires that the source and destination file systems support sparse files with hole sizes | |
212 | at least as large as their block sizes. | |
213 | This also requires that the source file is sparse, and for | |
214 | .Fn fcopyfile | |
215 | the source file descriptor's offset be a multiple of the minimum hole size. | |
216 | If COPYFILE_DATA is also specified, this will fall back to a full copy | |
217 | if sparse copying cannot be performed for any reason; otherwise, an error is returned. | |
b3aa0442 A |
218 | .It Dv COPYFILE_NOFOLLOW |
219 | This is a convenience macro, equivalent to | |
f475fe66 | 220 | .Dv (COPYFILE_NOFOLLOW_DST | COPYFILE_NOFOLLOW_SRC) . |
1f4df596 | 221 | .It Dv COPYFILE_RUN_IN_PLACE |
85b8a2cb A |
222 | If the src file has quarantine information, add the QTN_FLAG_DO_NOT_TRANSLOCATE flag to the quarantine information of the dst file. |
223 | This allows a bundle to run in place instead of being translocated. | |
62b275d9 A |
224 | .It Dv COPYFILE_PRESERVE_DST_TRACKED |
225 | Preserve the UF_TRACKED flag at | |
226 | .Va to | |
227 | when copying metadata, regardless of whether | |
228 | .Va from | |
85b8a2cb A |
229 | has it set. |
230 | This flag is used in conjunction with COPYFILE_STAT, or COPYFILE_CLONE (for its fallback case). | |
b3aa0442 A |
231 | .El |
232 | .Pp | |
85b8a2cb A |
233 | Copying files into a directory is supported. |
234 | If | |
23896e53 A |
235 | .Va to |
236 | is a directory, | |
237 | .Va from | |
238 | will be copied into | |
239 | .Va to | |
240 | (if | |
241 | .Va from | |
242 | is a directory, | |
243 | copying its contents requires use of the COPYFILE_RECURSIVE parameter, | |
244 | which is documented below). | |
245 | .Pp | |
b3aa0442 A |
246 | The |
247 | .Fn copyfile_state_get | |
248 | and | |
249 | .Fn copyfile_state_set | |
250 | functions can be used to manipulate the | |
251 | .Ft copyfile_state_t | |
252 | object returned by | |
253 | .Fn copyfile_state_alloc . | |
254 | In both functions, the | |
255 | .Va dst | |
256 | parameter's type depends on the | |
257 | .Va flag | |
258 | parameter that is passed in. | |
259 | .Bl -tag -width COPYFILE_STATE_DST_FILENAME | |
260 | .It Dv COPYFILE_STATE_SRC_FD | |
261 | .It Dv COPYFILE_STATE_DST_FD | |
262 | Get or set the file descriptor associated with the source (or destination) | |
263 | file. | |
264 | If this has not been initialized yet, the value will be -2. | |
265 | The | |
266 | .Va dst | |
267 | (for | |
268 | .Fn copyfile_state_get ) | |
269 | and | |
270 | .Va src | |
271 | (for | |
272 | .Fn copyfile_state_set ) | |
273 | parameters are pointers to | |
274 | .Vt int . | |
275 | .It Dv COPYFILE_STATE_SRC_FILENAME | |
276 | .It Dv COPYFILE_STATE_DST_FILENAME | |
277 | Get or set the filename associated with the source (or destination) | |
85b8a2cb A |
278 | file. |
279 | If it has not been initialized yet, the value will be | |
b3aa0442 A |
280 | .Dv NULL . |
281 | For | |
282 | .Fn copyfile_state_set , | |
283 | the | |
284 | .Va src | |
285 | parameter is a pointer to a C string | |
286 | (i.e., | |
287 | .Vt char* ); | |
288 | .Fn copyfile_state_set | |
289 | makes a private copy of this string. | |
290 | For | |
291 | .Fn copyfile_state_get | |
292 | function, the | |
293 | .Va dst | |
294 | parameter is a pointer to a pointer to a C string | |
295 | (i.e., | |
296 | .Vt char** ); | |
297 | the returned value is a pointer to the | |
298 | .Va state 's | |
299 | copy, and must not be modified or released. | |
659640e4 A |
300 | .It Dv COPYFILE_STATE_STATUS_CB |
301 | Get or set the callback status function (currently | |
302 | only used for recursive copies; see below for details). | |
303 | The | |
304 | .Va src | |
305 | parameter is a pointer to a function of type | |
306 | .Vt copyfile_callback_t | |
307 | (see above). | |
308 | .It Dv COPYFILE_STATE_STATUS_CTX | |
309 | Get or set the context parameter for the status | |
310 | call-back function (see below for details). | |
311 | The | |
312 | .Va src | |
313 | parameter is a | |
314 | .Vt void\ * . | |
b3aa0442 A |
315 | .It Dv COPYFILE_STATE_QUARANTINE |
316 | Get or set the quarantine information with the source file. | |
317 | The | |
318 | .Va src | |
659640e4 A |
319 | parameter is a pointer to an opaque |
320 | object (type | |
321 | .Vt void\ * | |
322 | ). | |
323 | .It Dv COPYFILE_STATE_COPIED | |
324 | Get the number of data bytes copied so far. | |
325 | (Only valid for | |
326 | .Fn copyfile_state_get ; | |
327 | see below for more details about callbacks.) | |
1f4df596 A |
328 | If a |
329 | .Dv COPYFILE_CLONE | |
330 | or | |
331 | .Dv COPYFILE_CLONE_FORCE | |
332 | operation successfully cloned the requested objects, then this value will be 0. | |
659640e4 A |
333 | The |
334 | .Va dst | |
335 | parameter is a pointer to | |
336 | .Vt off_t | |
337 | (type | |
338 | .Vt off_t\ * ). | |
3f81d1c4 A |
339 | .It Dv COPYFILE_STATE_XATTRNAME |
340 | Get the name of the extended attribute during a callback | |
341 | for | |
342 | .Dv COPYFILE_COPY_XATTR | |
343 | (see below for details). This field cannot be set, | |
344 | and may be | |
345 | .Dv NULL . | |
1f4df596 A |
346 | .It Dv COPYFILE_STATE_WAS_CLONED |
347 | True if a | |
348 | .Dv COPYFILE_CLONE | |
349 | or | |
350 | .Dv COPYFILE_CLONE_FORCE | |
351 | operation successfully cloned the requested objects. | |
352 | The | |
353 | .Va dst | |
354 | parameter is a pointer to | |
355 | .Vt bool | |
356 | (type | |
357 | .Vt bool\ * ). | |
659640e4 A |
358 | .El |
359 | .Sh Recursive Copies | |
360 | When given the | |
361 | .Dv COPYFILE_RECURSIVE | |
362 | flag, | |
363 | .Fn copyfile | |
364 | (but not | |
365 | .Fn fcopyfile ) | |
366 | will use the | |
367 | .Xr fts 3 | |
368 | functions to recursively descend into the source file-system object. | |
369 | It then calls | |
370 | .Fn copyfile | |
371 | on each of the entries it finds that way. | |
372 | If a call-back function is given (using | |
373 | .Fn copyfile_state_set | |
374 | and | |
375 | .Dv COPYFILE_STATE_STATUS_CB ), | |
376 | the call-back function will be called four times for each directory | |
377 | object, and twice for all other objects. (Each directory will | |
378 | be examined twice, once on entry -- before copying each of the | |
379 | objects contained in the directory -- and once on exit -- after | |
380 | copying each object contained in the directory, in order to perform | |
381 | some final cleanup.) | |
382 | .Pp | |
383 | The call-back function will have one of the following values | |
384 | as the first argument, indicating what is being copied: | |
385 | .Bl -tag -width COPYFILE_RECURSE_DIR_CLEANUP | |
386 | .It Dv COPYFILE_RECURSE_FILE | |
387 | The object being copied is a file (or, rather, | |
388 | something other than a directory). | |
389 | .It Dv COPYFILE_RECURSE_DIR | |
390 | The object being copied is a directory, and is being | |
391 | entered. (That is, none of the filesystem objects contained | |
392 | within the directory have been copied yet.) | |
393 | .It Dv COPYFILE_RECURSE_DIR_CLEANUP | |
394 | The object being copied is a directory, and all of the | |
85b8a2cb A |
395 | objects contained have been copied. |
396 | At this stage, the destination directory | |
659640e4 A |
397 | being copied will have any extra permissions that were added to |
398 | allow the copying will be removed. | |
399 | .It Dv COPYFILE_RECURSE_ERROR | |
400 | There was an error in processing an element of the source hierarchy; | |
401 | this happens when | |
402 | .Xr fts 3 | |
403 | returns an error or unknown file type. | |
404 | (Currently, the second argument to the call-back function will always | |
405 | be | |
406 | .Dv COPYFILE_ERR | |
407 | in this case.) | |
408 | .El | |
409 | .Pp | |
410 | The second argument to the call-back function will indicate | |
411 | the stage of the copy, and will be one of the following values: | |
412 | .Bl -tag -width COPYFILE_FINISH | |
413 | .It Dv COPYFILE_START | |
85b8a2cb A |
414 | Before copying has begun. |
415 | The third parameter will be a newly-created | |
659640e4 A |
416 | .Vt copyfile_state_t |
417 | object with the call-back function and context pre-loaded. | |
418 | .It Dv COPYFILE_FINISH | |
419 | After copying has successfully finished. | |
420 | .It Dv COPYFILE_ERR | |
85b8a2cb A |
421 | Indicates an error has happened at some stage. |
422 | If the first argument to the call-back function is | |
659640e4 A |
423 | .Dv COPYFILE_RECURSE_ERROR , |
424 | then an error occurred while processing the source hierarchy; | |
425 | otherwise, it will indicate what type of object was being copied, | |
426 | and | |
427 | .Dv errno | |
428 | will be set to indicate the error. | |
429 | .El | |
430 | .Pp | |
431 | The fourth and fifth | |
432 | parameters are the source and destination paths that | |
433 | are to be copied (or have been copied, or failed to copy, depending on | |
434 | the second argument). | |
435 | .Pp | |
436 | The last argument to the call-back function will be the value | |
437 | set by | |
438 | .Dv COPYFILE_STATE_STATUS_CTX , | |
439 | if any. | |
440 | .Pp | |
441 | The call-back function is required to return one of the following | |
442 | values: | |
443 | .Bl -tag -width COPYFILE_CONTINUE | |
444 | .It Dv COPYFILE_CONTINUE | |
445 | The copy will continue as expected. | |
446 | .It Dv COPYFILE_SKIP | |
447 | This object will be skipped, and the next object will | |
85b8a2cb A |
448 | be processed. |
449 | (Note that, when entering a directory, returning | |
659640e4 A |
450 | .Dv COPYFILE_SKIP |
451 | from the call-back function will prevent the contents | |
452 | of the directory from being copied.) | |
453 | .It Dv COPYFILE_QUIT | |
85b8a2cb A |
454 | The entire copy is aborted at this stage. |
455 | Any filesystem | |
659640e4 A |
456 | objects created up to this point will remain. |
457 | .Fn copyfile | |
458 | will return -1, but | |
459 | .Dv errno | |
460 | will be unmodified. | |
461 | .El | |
462 | .Pp | |
463 | The call-back function must always return one of the values listed | |
464 | above; if not, the results are undefined. | |
465 | .Pp | |
466 | The call-back function will be called twice for each object | |
467 | (and an additional two times for directory cleanup); the first | |
468 | call will have a | |
469 | .Ar stage | |
470 | parameter of | |
471 | .Dv COPYFILE_START ; | |
472 | the second time, that value will be either | |
473 | .Dv COPYFILE_FINISH | |
474 | or | |
475 | .Dv COPYFILE_ERR | |
476 | to indicate a successful completion, or an error during | |
477 | processing. | |
478 | In the event of an error, the | |
479 | .Dv errno | |
480 | value will be set appropriately. | |
481 | .Pp | |
dcb8fd50 A |
482 | Note that recursive cloning is also supported with the |
483 | .Dv COPYFILE_CLONE | |
484 | flag (but not the | |
485 | .Dv COPYFILE_CLONE_FORCE | |
85b8a2cb A |
486 | flag). |
487 | A recursive clone operation invokes | |
dcb8fd50 A |
488 | .Fn copyfile |
489 | with | |
490 | .Dv COPYFILE_CLONE | |
85b8a2cb A |
491 | on every entry found in the source file-system object. |
492 | Because | |
dcb8fd50 A |
493 | .Fn copyfile |
494 | does not allow the cloning of directories, a recursive clone will | |
495 | instead copy any directory it finds (while cloning its contents). | |
496 | As symbolic links may point to directories, they are not followed | |
497 | during recursive clones even if the source is a symbolic link. | |
498 | Additionally, because the | |
499 | .Dv COPYFILE_CLONE | |
500 | flag implies the | |
501 | .Dv COPYFILE_EXCL | |
502 | flag, recursive clones require a nonexistent destination. | |
503 | .Pp | |
659640e4 A |
504 | The |
505 | .Dv COPYFILE_PACK , | |
506 | .Dv COPYFILE_UNPACK , | |
507 | .Dv COPYFILE_MOVE , | |
508 | and | |
509 | .Dv COPYFILE_UNLINK | |
510 | flags are not used during a recursive copy, and will result | |
511 | in an error being returned. | |
23896e53 A |
512 | .Pp |
513 | Note that if the source path ends in a | |
514 | .Va / | |
515 | its contents are copied rather than the directory itself (like cp(1)). | |
516 | The behavior of a recursive copy on a directory hierarchy also depends | |
85b8a2cb A |
517 | on the contents of the destination. |
518 | If the destination is a directory, the source directory (or its contents, | |
519 | if the source path ends in a | |
23896e53 | 520 | .Va / |
85b8a2cb A |
521 | ) will be copied into it. |
522 | If the destination exists but is not a | |
23896e53 A |
523 | directory, and the source is a non-empty directory, the copy will fail; |
524 | the exact error set depends on the flags provided to | |
525 | .Fn copyfile | |
526 | initially. | |
659640e4 A |
527 | .Sh Progress Callback |
528 | In addition to the recursive callbacks described above, | |
529 | .Fn copyfile | |
530 | and | |
531 | .Fn fcopyfile | |
3f81d1c4 | 532 | will also use a callback to report data (e.g., |
659640e4 | 533 | .Dv COPYFILE_DATA ) |
85b8a2cb A |
534 | progress. |
535 | If given, the callback will be invoked on each | |
659640e4 | 536 | .Xr write 2 |
85b8a2cb A |
537 | call. |
538 | The first argument to the callback function will be | |
659640e4 A |
539 | .Dv COPYFILE_COPY_DATA . |
540 | The second argument will either be | |
3f81d1c4 | 541 | .Dv COPYFILE_PROGRESS |
659640e4 A |
542 | (indicating that the write was successful), or |
543 | .Dv COPYFILE_ERR | |
544 | (indicating that there was an error of some sort). | |
545 | .Pp | |
546 | The amount of data bytes copied so far can be retrieved using | |
547 | .Fn copyfile_state_get , | |
548 | with the | |
549 | .Dv COPYFILE_STATE_COPIED | |
550 | requestor (the argument type is a pointer to | |
551 | .Vt off_t ). | |
552 | .Pp | |
3f81d1c4 A |
553 | When copying extended attributes, the first argument to the |
554 | callback function will be | |
555 | .Dv COPYFILE_COPY_XATTR . | |
556 | The other arguments will be as described for | |
557 | .Dv COPYFILE_COPY_DATA ; | |
558 | the name of the extended attribute being copied may be | |
559 | retrieved using | |
560 | .Fn copyfile_state_get | |
561 | and the parameter | |
562 | .Dv COPYFILE_STATE_XATTRNAME . | |
563 | When using | |
564 | .Dv COPYFILE_PACK , | |
565 | the callback may be called with | |
566 | .Dv COPYFILE_START | |
567 | for each of the extended attributes first, followed by | |
568 | .Dv COPYFILE_PROGRESS | |
569 | before getting and packing the data for each | |
570 | individual attribute, and then | |
571 | .Dv COPYFILE_FINISH | |
572 | when finished with each individual attribute. | |
573 | (That is, | |
574 | .Dv COPYFILE_START | |
575 | may be called for all of the extended attributes, before | |
576 | the first callback with | |
577 | .Dv COPYFILE_PROGRESS | |
85b8a2cb A |
578 | is invoked.) |
579 | Any attribute skipped by returning | |
3f81d1c4 A |
580 | .Dv COPYFILE_SKIP |
581 | from the | |
582 | .Dv COPYFILE_START | |
583 | callback will not be placed into the packed output file. | |
584 | .Pp | |
659640e4 A |
585 | The return value for the data callback must be one of |
586 | .Bl -tag -width COPYFILE_CONTINUE | |
587 | .It Dv COPYFILE_CONTINUE | |
588 | The copy will continue as expected. | |
589 | (In the case of error, it will attempt to write the data again.) | |
590 | .It Dv COPYFILE_SKIP | |
591 | The data copy will be aborted, but without error. | |
592 | .It Dv COPYFILE_QUIT | |
593 | The data copy will be aborted; in the case of | |
3f81d1c4 | 594 | .Dv COPYFILE_PROGRESS , |
659640e4 A |
595 | .Dv errno |
596 | will be set to | |
597 | .Dv ECANCELED . | |
b3aa0442 | 598 | .El |
659640e4 A |
599 | .Pp |
600 | While the | |
601 | .Va src | |
602 | and | |
603 | .Va dst | |
604 | parameters will be passed in, they may be | |
605 | .Dv NULL | |
606 | in the case of | |
607 | .Fn fcopyfile . | |
dcb8fd50 A |
608 | .Pp |
609 | Note that progress callbacks are not invoked when a clone is requested | |
610 | (e.g. | |
611 | .Dv COPYFILE_CLONE ) | |
612 | unless the clone cannot be performed and a copy is performed instead. | |
b3aa0442 A |
613 | .Sh RETURN VALUES |
614 | Except when given the | |
615 | .Dv COPYFILE_CHECK | |
616 | flag, | |
617 | .Fn copyfile | |
618 | and | |
619 | .Fn fcopyfile | |
620 | return less than 0 on error, and 0 on success. | |
621 | All of the other functions return 0 on success, and less than 0 | |
622 | on error. | |
659640e4 A |
623 | .Sh WARNING |
624 | Both | |
625 | .Fn copyfile | |
626 | and | |
627 | .Fn fcopyfile | |
628 | can copy symbolic links; there is a gap between when the source | |
3f81d1c4 | 629 | link is examined and the actual copy is started, and this can |
659640e4 A |
630 | be a potential security risk, especially if the process has |
631 | elevated privileges. | |
632 | .Pp | |
633 | When performing a recursive copy, if the source hierarchy | |
634 | changes while the copy is occurring, the results are undefined. | |
3f81d1c4 A |
635 | .Pp |
636 | .Fn fcopyfile | |
637 | does not reset the seek position for either source or destination. | |
638 | This can result in the destination file being a different size | |
639 | than the source file. | |
85b8a2cb A |
640 | .Sh EXAMPLES |
641 | .Bd -literal -offset indent | |
642 | /* Initialize a state variable */ | |
643 | copyfile_state_t s; | |
644 | s = copyfile_state_alloc(); | |
645 | /* Copy the data and extended attributes of one file to another */ | |
646 | copyfile("/tmp/f1", "/tmp/f2", s, COPYFILE_DATA | COPYFILE_XATTR); | |
647 | /* Convert a file to an AppleDouble file for serialization */ | |
648 | copyfile("/tmp/f2", "/tmp/tmpfile", NULL, COPYFILE_ALL | COPYFILE_PACK); | |
649 | /* Release the state variable */ | |
650 | copyfile_state_free(s); | |
651 | /* A more complex way to call copyfile() */ | |
652 | s = copyfile_state_alloc(); | |
653 | copyfile_state_set(s, COPYFILE_STATE_SRC_FILENAME, "/tmp/foo"); | |
654 | /* One of src or dst must be set... rest can come from the state */ | |
655 | copyfile(NULL, "/tmp/bar", s, COPYFILE_ALL); | |
656 | /* Now copy the same source file to another destination file */ | |
657 | copyfile(NULL, "/tmp/car", s, COPYFILE_ALL); | |
658 | copyfile_state_free(s); | |
659 | /* Remove extended attributes from a file */ | |
660 | copyfile("/dev/null", "/tmp/bar", NULL, COPYFILE_XATTR); | |
661 | .Ed | |
b3aa0442 A |
662 | .Sh ERRORS |
663 | .Fn copyfile | |
664 | and | |
665 | .Fn fcopyfile | |
666 | will fail if: | |
667 | .Bl -tag -width Er | |
668 | .It Bq Er EINVAL | |
659640e4 A |
669 | An invalid flag was passed in with |
670 | .Dv COPYFILE_RECURSIVE . | |
671 | .It Bq Er EINVAL | |
672 | The | |
b3aa0442 A |
673 | .Va from |
674 | or | |
675 | .Va to | |
659640e4 A |
676 | parameter to |
677 | .Fn copyfile | |
678 | was a | |
b3aa0442 | 679 | .Dv NULL |
659640e4 A |
680 | pointer. |
681 | .It Bq Er EINVAL | |
682 | The | |
683 | .Va from | |
684 | or | |
685 | .Va to | |
686 | parameter to | |
f475fe66 | 687 | .Fn fcopyfile |
659640e4 | 688 | was a negative number. |
b3aa0442 A |
689 | .It Bq Er ENOMEM |
690 | A memory allocation failed. | |
691 | .It Bq Er ENOTSUP | |
692 | The source file was not a directory, symbolic link, or regular file. | |
937356ff A |
693 | .It Bq Er ENOTSUP |
694 | COPYFILE_CLONE_FORCE was specified and file cloning is not supported. | |
695 | .It Bq Er ENOTSUP | |
696 | COPYFILE_DATA_SPARSE was specified, sparse copying is not supported, | |
697 | and COPYFILE_DATA was not specified. | |
659640e4 A |
698 | .It Bq Er ECANCELED |
699 | The copy was cancelled by callback. | |
f475fe66 A |
700 | .It Bq Er EEXIST |
701 | The | |
702 | .Va to | |
703 | parameter to | |
704 | .Fn copyfile | |
705 | already existed and was passed in with | |
706 | .Dv COPYFILE_EXCL . | |
707 | .It Bq Er ENOENT | |
708 | The | |
709 | .Va from | |
710 | parameter to | |
711 | .Fn copyfile | |
712 | did not exist. | |
713 | .It Bq Er EACCES | |
714 | Search permission is denied for a component of the path prefix for | |
715 | the | |
716 | .Va from | |
717 | or | |
718 | .Va to | |
719 | parameters. | |
720 | .It Bq Er EACCES | |
721 | Write permission is denied for a component of the path prefix for the | |
722 | .Va to | |
723 | parameter. | |
b3aa0442 | 724 | .El |
f475fe66 | 725 | .Pp |
b3aa0442 A |
726 | In addition, both functions may set |
727 | .Dv errno | |
728 | via an underlying library or system call. | |
659640e4 | 729 | .Sh SEE ALSO |
659640e4 | 730 | .Xr getxattr 2 , |
85b8a2cb | 731 | .Xr listxattr 2 , |
659640e4 A |
732 | .Xr setxattr 2 , |
733 | .Xr acl 3 | |
85b8a2cb A |
734 | .Sh HISTORY |
735 | The | |
736 | .Fn copyfile | |
737 | API was introduced in Mac OS X 10.5. | |
b3aa0442 A |
738 | .Sh BUGS |
739 | Both | |
740 | .Fn copyfile | |
741 | functions lack a way to set the input or output block size. | |
659640e4 A |
742 | .Pp |
743 | Recursive copies do not honor hard links. |