]> git.saurik.com Git - apple/copyfile.git/blob - copyfile.3
4233ce9bcfdddf9d5864436ba4a4c7a2f8d597ee
[apple/copyfile.git] / copyfile.3
1 .\"
2 .\" Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
3 .\"
4 .Dd August 30, 2017
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"
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"
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
106 .It Dv COPYFILE_RECURSIVE
107 Causes
108 .Fn copyfile
109 to recursively copy a hierarchy.
110 This flag is not used by
111 .Fn fcopyfile ;
112 see below for more information.
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
128 file. The
129 .Va to
130 file is an AppleDouble-format file.
131 .It Dv COPYFILE_UNPACK
132 Unserialize the
133 .Va from
134 file. The
135 .Va from
136 file is an AppleDouble-format file; the
137 .Va to
138 file will have the extended attributes, ACLs, resource fork, and
139 FinderInfo data from the
140 .Va to
141 file, regardless of the
142 .Va flags
143 argument passed in.
144 .It Dv COPYFILE_EXCL
145 Fail if the
146 .Va to
147 file already exists. (This is only applicable for the
148 .Fn copyfile
149 function.)
150 .It Dv COPYFILE_NOFOLLOW_SRC
151 Do not follow the
152 .Va from
153 file, if it is a symbolic link. (This is only applicable for the
154 .Fn copyfile
155 function.)
156 .It Dv COPYFILE_NOFOLLOW_DST
157 Do not follow the
158 .Va to
159 file, if it is a symbolic link. (This is only applicable for the
160 .Fn copyfile
161 function.)
162 .It Dv COPYFILE_MOVE
163 Unlink (using
164 .Xr remove 3 )
165 the
166 .Fa from
167 file. (This is only applicable for the
168 .Fn copyfile
169 function.) No error is returned if
170 .Xr remove 3
171 fails. Note that
172 .Xr remove 3
173 removes a symbolic link itself, not the
174 target of the link.
175 .It Dv COPYFILE_UNLINK
176 Unlink the
177 .Va to
178 file before starting. (This is only applicable for the
179 .Fn copyfile
180 function.)
181 .It Dv COPYFILE_CLONE_FORCE
182 Clone the file instead.
183 This is a force flag i.e. if cloning fails, an error is returned.
184 This flag is equivalent to (COPYFILE_EXCL | COPYFILE_ACL | COPYFILE_STAT | COPYFILE_XATTR | COPYFILE_DATA
185 | COPYFILE_NOFOLLOW_SRC).
186 Note that if cloning is successful, progress callbacks will not be invoked.
187 Note also that there is no support for cloning directories: if a directory is provided as the source,
188 an error will be returned.
189 (This is only applicable for the
190 .Fn copyfile
191 function.)
192 .It Dv COPYFILE_CLONE
193 Try to clone the file instead.
194 This is a best try flag i.e. if cloning fails, fallback to copying the file.
195 This flag is equivalent to (COPYFILE_EXCL | COPYFILE_ACL | COPYFILE_STAT | COPYFILE_XATTR | COPYFILE_DATA
196 | COPYFILE_NOFOLLOW_SRC).
197 Note that if cloning is successful, progress callbacks will not be invoked.
198 Note also that there is no support for cloning directories: if a directory is provided as the source and
199 COPYFILE_CLONE_FORCE is not passed, this will instead copy the directory. Recursive copying however is
200 supported, see below for more information.
201 (This is only applicable for the
202 .Fn copyfile
203 function.)
204 .It Dv COPYFILE_NOFOLLOW
205 This is a convenience macro, equivalent to
206 .Dv (COPYFILE_NOFOLLOW_DST | COPYFILE_NOFOLLOW_SRC) .
207 .It Dv COPYFILE_RUN_IN_PLACE
208 If the src file has quarantine information, add the QTN_FLAG_DO_NOT_TRANSLOCATE flag to the quarantine information of the dst file. This allows a bundle to run in place instead of being translocated.
209 .El
210 .Pp
211 The
212 .Fn copyfile_state_get
213 and
214 .Fn copyfile_state_set
215 functions can be used to manipulate the
216 .Ft copyfile_state_t
217 object returned by
218 .Fn copyfile_state_alloc .
219 In both functions, the
220 .Va dst
221 parameter's type depends on the
222 .Va flag
223 parameter that is passed in.
224 .Bl -tag -width COPYFILE_STATE_DST_FILENAME
225 .It Dv COPYFILE_STATE_SRC_FD
226 .It Dv COPYFILE_STATE_DST_FD
227 Get or set the file descriptor associated with the source (or destination)
228 file.
229 If this has not been initialized yet, the value will be -2.
230 The
231 .Va dst
232 (for
233 .Fn copyfile_state_get )
234 and
235 .Va src
236 (for
237 .Fn copyfile_state_set )
238 parameters are pointers to
239 .Vt int .
240 .It Dv COPYFILE_STATE_SRC_FILENAME
241 .It Dv COPYFILE_STATE_DST_FILENAME
242 Get or set the filename associated with the source (or destination)
243 file. If it has not been initialized yet, the value will be
244 .Dv NULL .
245 For
246 .Fn copyfile_state_set ,
247 the
248 .Va src
249 parameter is a pointer to a C string
250 (i.e.,
251 .Vt char* );
252 .Fn copyfile_state_set
253 makes a private copy of this string.
254 For
255 .Fn copyfile_state_get
256 function, the
257 .Va dst
258 parameter is a pointer to a pointer to a C string
259 (i.e.,
260 .Vt char** );
261 the returned value is a pointer to the
262 .Va state 's
263 copy, and must not be modified or released.
264 .It Dv COPYFILE_STATE_STATUS_CB
265 Get or set the callback status function (currently
266 only used for recursive copies; see below for details).
267 The
268 .Va src
269 parameter is a pointer to a function of type
270 .Vt copyfile_callback_t
271 (see above).
272 .It Dv COPYFILE_STATE_STATUS_CTX
273 Get or set the context parameter for the status
274 call-back function (see below for details).
275 The
276 .Va src
277 parameter is a
278 .Vt void\ * .
279 .It Dv COPYFILE_STATE_QUARANTINE
280 Get or set the quarantine information with the source file.
281 The
282 .Va src
283 parameter is a pointer to an opaque
284 object (type
285 .Vt void\ *
286 ).
287 .It Dv COPYFILE_STATE_COPIED
288 Get the number of data bytes copied so far.
289 (Only valid for
290 .Fn copyfile_state_get ;
291 see below for more details about callbacks.)
292 If a
293 .Dv COPYFILE_CLONE
294 or
295 .Dv COPYFILE_CLONE_FORCE
296 operation successfully cloned the requested objects, then this value will be 0.
297 The
298 .Va dst
299 parameter is a pointer to
300 .Vt off_t
301 (type
302 .Vt off_t\ * ).
303 .It Dv COPYFILE_STATE_XATTRNAME
304 Get the name of the extended attribute during a callback
305 for
306 .Dv COPYFILE_COPY_XATTR
307 (see below for details). This field cannot be set,
308 and may be
309 .Dv NULL .
310 .It Dv COPYFILE_STATE_WAS_CLONED
311 True if a
312 .Dv COPYFILE_CLONE
313 or
314 .Dv COPYFILE_CLONE_FORCE
315 operation successfully cloned the requested objects.
316 The
317 .Va dst
318 parameter is a pointer to
319 .Vt bool
320 (type
321 .Vt bool\ * ).
322 .El
323 .Sh Recursive Copies
324 When given the
325 .Dv COPYFILE_RECURSIVE
326 flag,
327 .Fn copyfile
328 (but not
329 .Fn fcopyfile )
330 will use the
331 .Xr fts 3
332 functions to recursively descend into the source file-system object.
333 It then calls
334 .Fn copyfile
335 on each of the entries it finds that way.
336 If a call-back function is given (using
337 .Fn copyfile_state_set
338 and
339 .Dv COPYFILE_STATE_STATUS_CB ),
340 the call-back function will be called four times for each directory
341 object, and twice for all other objects. (Each directory will
342 be examined twice, once on entry -- before copying each of the
343 objects contained in the directory -- and once on exit -- after
344 copying each object contained in the directory, in order to perform
345 some final cleanup.)
346 .Pp
347 The call-back function will have one of the following values
348 as the first argument, indicating what is being copied:
349 .Bl -tag -width COPYFILE_RECURSE_DIR_CLEANUP
350 .It Dv COPYFILE_RECURSE_FILE
351 The object being copied is a file (or, rather,
352 something other than a directory).
353 .It Dv COPYFILE_RECURSE_DIR
354 The object being copied is a directory, and is being
355 entered. (That is, none of the filesystem objects contained
356 within the directory have been copied yet.)
357 .It Dv COPYFILE_RECURSE_DIR_CLEANUP
358 The object being copied is a directory, and all of the
359 objects contained have been copied. At this stage, the destination directory
360 being copied will have any extra permissions that were added to
361 allow the copying will be removed.
362 .It Dv COPYFILE_RECURSE_ERROR
363 There was an error in processing an element of the source hierarchy;
364 this happens when
365 .Xr fts 3
366 returns an error or unknown file type.
367 (Currently, the second argument to the call-back function will always
368 be
369 .Dv COPYFILE_ERR
370 in this case.)
371 .El
372 .Pp
373 The second argument to the call-back function will indicate
374 the stage of the copy, and will be one of the following values:
375 .Bl -tag -width COPYFILE_FINISH
376 .It Dv COPYFILE_START
377 Before copying has begun. The third
378 parameter will be a newly-created
379 .Vt copyfile_state_t
380 object with the call-back function and context pre-loaded.
381 .It Dv COPYFILE_FINISH
382 After copying has successfully finished.
383 .It Dv COPYFILE_ERR
384 Indicates an error has happened at some stage. If the
385 first argument to the call-back function is
386 .Dv COPYFILE_RECURSE_ERROR ,
387 then an error occurred while processing the source hierarchy;
388 otherwise, it will indicate what type of object was being copied,
389 and
390 .Dv errno
391 will be set to indicate the error.
392 .El
393 .Pp
394 The fourth and fifth
395 parameters are the source and destination paths that
396 are to be copied (or have been copied, or failed to copy, depending on
397 the second argument).
398 .Pp
399 The last argument to the call-back function will be the value
400 set by
401 .Dv COPYFILE_STATE_STATUS_CTX ,
402 if any.
403 .Pp
404 The call-back function is required to return one of the following
405 values:
406 .Bl -tag -width COPYFILE_CONTINUE
407 .It Dv COPYFILE_CONTINUE
408 The copy will continue as expected.
409 .It Dv COPYFILE_SKIP
410 This object will be skipped, and the next object will
411 be processed. (Note that, when entering a directory.
412 returning
413 .Dv COPYFILE_SKIP
414 from the call-back function will prevent the contents
415 of the directory from being copied.)
416 .It Dv COPYFILE_QUIT
417 The entire copy is aborted at this stage. Any filesystem
418 objects created up to this point will remain.
419 .Fn copyfile
420 will return -1, but
421 .Dv errno
422 will be unmodified.
423 .El
424 .Pp
425 The call-back function must always return one of the values listed
426 above; if not, the results are undefined.
427 .Pp
428 The call-back function will be called twice for each object
429 (and an additional two times for directory cleanup); the first
430 call will have a
431 .Ar stage
432 parameter of
433 .Dv COPYFILE_START ;
434 the second time, that value will be either
435 .Dv COPYFILE_FINISH
436 or
437 .Dv COPYFILE_ERR
438 to indicate a successful completion, or an error during
439 processing.
440 In the event of an error, the
441 .Dv errno
442 value will be set appropriately.
443 .Pp
444 Note that recursive cloning is also supported with the
445 .Dv COPYFILE_CLONE
446 flag (but not the
447 .Dv COPYFILE_CLONE_FORCE
448 flag). A recursive clone operation invokes
449 .Fn copyfile
450 with
451 .Dv COPYFILE_CLONE
452 on every entry found in the source file-system object. Because
453 .Fn copyfile
454 does not allow the cloning of directories, a recursive clone will
455 instead copy any directory it finds (while cloning its contents).
456 As symbolic links may point to directories, they are not followed
457 during recursive clones even if the source is a symbolic link.
458 Additionally, because the
459 .Dv COPYFILE_CLONE
460 flag implies the
461 .Dv COPYFILE_EXCL
462 flag, recursive clones require a nonexistent destination.
463 .Pp
464 The
465 .Dv COPYFILE_PACK ,
466 .Dv COPYFILE_UNPACK ,
467 .Dv COPYFILE_MOVE ,
468 and
469 .Dv COPYFILE_UNLINK
470 flags are not used during a recursive copy, and will result
471 in an error being returned.
472 .Sh Progress Callback
473 In addition to the recursive callbacks described above,
474 .Fn copyfile
475 and
476 .Fn fcopyfile
477 will also use a callback to report data (e.g.,
478 .Dv COPYFILE_DATA )
479 progress. If given, the callback will be invoked on each
480 .Xr write 2
481 call. The first argument to the callback function will be
482 .Dv COPYFILE_COPY_DATA .
483 The second argument will either be
484 .Dv COPYFILE_PROGRESS
485 (indicating that the write was successful), or
486 .Dv COPYFILE_ERR
487 (indicating that there was an error of some sort).
488 .Pp
489 The amount of data bytes copied so far can be retrieved using
490 .Fn copyfile_state_get ,
491 with the
492 .Dv COPYFILE_STATE_COPIED
493 requestor (the argument type is a pointer to
494 .Vt off_t ).
495 .Pp
496 When copying extended attributes, the first argument to the
497 callback function will be
498 .Dv COPYFILE_COPY_XATTR .
499 The other arguments will be as described for
500 .Dv COPYFILE_COPY_DATA ;
501 the name of the extended attribute being copied may be
502 retrieved using
503 .Fn copyfile_state_get
504 and the parameter
505 .Dv COPYFILE_STATE_XATTRNAME .
506 When using
507 .Dv COPYFILE_PACK ,
508 the callback may be called with
509 .Dv COPYFILE_START
510 for each of the extended attributes first, followed by
511 .Dv COPYFILE_PROGRESS
512 before getting and packing the data for each
513 individual attribute, and then
514 .Dv COPYFILE_FINISH
515 when finished with each individual attribute.
516 (That is,
517 .Dv COPYFILE_START
518 may be called for all of the extended attributes, before
519 the first callback with
520 .Dv COPYFILE_PROGRESS
521 is invoked.) Any attribute skipped by returning
522 .Dv COPYFILE_SKIP
523 from the
524 .Dv COPYFILE_START
525 callback will not be placed into the packed output file.
526 .Pp
527 The return value for the data callback must be one of
528 .Bl -tag -width COPYFILE_CONTINUE
529 .It Dv COPYFILE_CONTINUE
530 The copy will continue as expected.
531 (In the case of error, it will attempt to write the data again.)
532 .It Dv COPYFILE_SKIP
533 The data copy will be aborted, but without error.
534 .It Dv COPYFILE_QUIT
535 The data copy will be aborted; in the case of
536 .Dv COPYFILE_PROGRESS ,
537 .Dv errno
538 will be set to
539 .Dv ECANCELED .
540 .El
541 .Pp
542 While the
543 .Va src
544 and
545 .Va dst
546 parameters will be passed in, they may be
547 .Dv NULL
548 in the case of
549 .Fn fcopyfile .
550 .Pp
551 Note that progress callbacks are not invoked when a clone is requested
552 (e.g.
553 .Dv COPYFILE_CLONE )
554 unless the clone cannot be performed and a copy is performed instead.
555 .Sh RETURN VALUES
556 Except when given the
557 .Dv COPYFILE_CHECK
558 flag,
559 .Fn copyfile
560 and
561 .Fn fcopyfile
562 return less than 0 on error, and 0 on success.
563 All of the other functions return 0 on success, and less than 0
564 on error.
565 .Sh WARNING
566 Both
567 .Fn copyfile
568 and
569 .Fn fcopyfile
570 can copy symbolic links; there is a gap between when the source
571 link is examined and the actual copy is started, and this can
572 be a potential security risk, especially if the process has
573 elevated privileges.
574 .Pp
575 When performing a recursive copy, if the source hierarchy
576 changes while the copy is occurring, the results are undefined.
577 .Pp
578 .Fn fcopyfile
579 does not reset the seek position for either source or destination.
580 This can result in the destination file being a different size
581 than the source file.
582 .Sh ERRORS
583 .Fn copyfile
584 and
585 .Fn fcopyfile
586 will fail if:
587 .Bl -tag -width Er
588 .It Bq Er EINVAL
589 An invalid flag was passed in with
590 .Dv COPYFILE_RECURSIVE .
591 .It Bq Er EINVAL
592 The
593 .Va from
594 or
595 .Va to
596 parameter to
597 .Fn copyfile
598 was a
599 .Dv NULL
600 pointer.
601 .It Bq Er EINVAL
602 The
603 .Va from
604 or
605 .Va to
606 parameter to
607 .Fn fcopyfile
608 was a negative number.
609 .It Bq Er ENOMEM
610 A memory allocation failed.
611 .It Bq Er ENOTSUP
612 The source file was not a directory, symbolic link, or regular file.
613 .It Bq Er ECANCELED
614 The copy was cancelled by callback.
615 .It Bq Er EEXIST
616 The
617 .Va to
618 parameter to
619 .Fn copyfile
620 already existed and was passed in with
621 .Dv COPYFILE_EXCL .
622 .It Bq Er ENOENT
623 The
624 .Va from
625 parameter to
626 .Fn copyfile
627 did not exist.
628 .It Bq Er EACCES
629 Search permission is denied for a component of the path prefix for
630 the
631 .Va from
632 or
633 .Va to
634 parameters.
635 .It Bq Er EACCES
636 Write permission is denied for a component of the path prefix for the
637 .Va to
638 parameter.
639 .El
640 .Pp
641 In addition, both functions may set
642 .Dv errno
643 via an underlying library or system call.
644 .Sh EXAMPLES
645 .Bd -literal -offset indent
646 /* Initialize a state variable */
647 copyfile_state_t s;
648 s = copyfile_state_alloc();
649 /* Copy the data and extended attributes of one file to another */
650 copyfile("/tmp/f1", "/tmp/f2", s, COPYFILE_DATA | COPYFILE_XATTR);
651 /* Convert a file to an AppleDouble file for serialization */
652 copyfile("/tmp/f2", "/tmp/tmpfile", NULL, COPYFILE_ALL | COPYFILE_PACK);
653 /* Release the state variable */
654 copyfile_state_free(s);
655 /* A more complex way to call copyfile() */
656 s = copyfile_state_alloc();
657 copyfile_state_set(s, COPYFILE_STATE_SRC_FILENAME, "/tmp/foo");
658 /* One of src or dst must be set... rest can come from the state */
659 copyfile(NULL, "/tmp/bar", s, COPYFILE_ALL);
660 /* Now copy the same source file to another destination file */
661 copyfile(NULL, "/tmp/car", s, COPYFILE_ALL);
662 copyfile_state_free(s);
663 /* Remove extended attributes from a file */
664 copyfile("/dev/null", "/tmp/bar", NULL, COPYFILE_XATTR);
665 .Ed
666 .Sh SEE ALSO
667 .Xr listxattr 2 ,
668 .Xr getxattr 2 ,
669 .Xr setxattr 2 ,
670 .Xr acl 3
671 .Sh BUGS
672 Both
673 .Fn copyfile
674 functions lack a way to set the input or output block size.
675 .Pp
676 Recursive copies do not honor hard links.
677 .Sh HISTORY
678 The
679 .Fn copyfile
680 API was introduced in Mac OS X 10.5.