2 .\" Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
8 .Nm copyfile , fcopyfile ,
9 .Nm copyfile_state_alloc , copyfile_state_free ,
10 .Nm copyfile_state_get , copyfile_state_set
17 .Fn copyfile "const char *from" "const char *to" "copyfile_state_t state" "copyfile_flags_t flags"
19 .Fn fcopyfile "int from" "int to" "copyfile_state_t state" "copyfile_flags_t flags"
21 .Fn copyfile_state_alloc "void"
23 .Fn copyfile_state_free "copyfile_state_t state"
25 .Fn copyfile_state_get "copyfile_state_t state" "uint32_t flag" "void * dst"
27 .Fn copyfile_state_set "copyfile_state_t state" "uint32_t flag" "const void * src"
29 .Fn (*copyfile_callback_t) "int what" "int stage" "copyfile_state_t state" "const char * src" "const char * dst" "void * ctx"
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
36 .Fn copyfile_state_alloc
37 function initializes a
39 object (which is an opaque data type).
40 This object can be passed to
44 .Fn copyfile_state_get
46 .Fn copyfile_state_set
47 can be used to manipulate the state (see below).
49 .Fn copyfile_state_free
50 function is used to deallocate the object and its contents.
54 function can copy the named
60 function does the same, but using the file descriptors of already-opened
64 parameter is the return value from
65 .Fn copyfile_state_alloc ,
70 will use the information from the state object; if it is
72 then both functions will work normally, but less control will be available to the caller.
75 parameter controls which contents are copied:
76 .Bl -tag -width COPYFILE_XATTR
78 Copy the source file's access control lists.
80 Copy the source file's POSIX information (mode, modification time, etc.).
82 Copy the source file's extended attributes.
84 Copy the source file's data.
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) .
96 Copy the entire file; equivalent to
97 .Dv (COPYFILE_METADATA|COPYFILE_DATA) .
104 functions can also have their behavior modified by the following flags:
105 .Bl -tag -width COPYFILE_NOFOLLOW_SRC
106 .It Dv COPYFILE_RECURSIVE
109 to recursively copy a hierarchy.
110 This flag is not used by
112 see below for more information.
113 .It Dv COPYFILE_CHECK
114 Return a bitmask (corresponding to the
116 argument) indicating which contents would be copied; no data are actually
120 .Dv COPYFILE_CHECK|COPYFILE_METADATA ,
123 file had extended attributes but no ACLs, the return value would be
124 .Dv COPYFILE_XATTR .)
130 file is an AppleDouble-format file.
131 .It Dv COPYFILE_UNPACK
136 file is an AppleDouble-format file; the
138 file will have the extended attributes, ACLs, resource fork, and
139 FinderInfo data from the
141 file, regardless of the
147 file already exists. (This is only applicable for the
150 .It Dv COPYFILE_NOFOLLOW_SRC
153 file, if it is a symbolic link. (This is only applicable for the
156 .It Dv COPYFILE_NOFOLLOW_DST
159 file, if it is a symbolic link. (This is only applicable for the
167 file. (This is only applicable for the
169 function.) No error is returned if
173 removes a symbolic link itself, not the
175 .It Dv COPYFILE_UNLINK
178 file before starting. (This is only applicable for the
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 Note that if cloning is successful, callbacks will not be invoked.
186 Note also that there is no support for cloning directories: if a directory is provided as the source,
187 an error will be returned.
188 (This is only applicable for the
191 .It Dv COPYFILE_CLONE
192 Try to clone the file instead.
193 This is a best try flag i.e. if cloning fails, fallback to copying the file.
194 This flag is equivalent to (COPYFILE_EXCL | COPYFILE_ACL | COPYFILE_STAT | COPYFILE_XATTR | COPYFILE_DATA).
195 Note that if cloning is successful, callbacks will not be invoked.
196 Note also that there is no support for cloning directories: if a directory is provided as the source,
197 this will instead copy the directory (recursively if COPYFILE_RECURSIVE is also passed).
198 (This is only applicable for the
201 .It Dv COPYFILE_NOFOLLOW
202 This is a convenience macro, equivalent to
203 .Dv (COPYFILE_NOFOLLOW_DST | COPYFILE_NOFOLLOW_SRC) .
204 .It Dv COPYFILE_RUN_IN_PLACE
205 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 .Fn copyfile_state_get
211 .Fn copyfile_state_set
212 functions can be used to manipulate the
215 .Fn copyfile_state_alloc .
216 In both functions, the
218 parameter's type depends on the
220 parameter that is passed in.
221 .Bl -tag -width COPYFILE_STATE_DST_FILENAME
222 .It Dv COPYFILE_STATE_SRC_FD
223 .It Dv COPYFILE_STATE_DST_FD
224 Get or set the file descriptor associated with the source (or destination)
226 If this has not been initialized yet, the value will be -2.
230 .Fn copyfile_state_get )
234 .Fn copyfile_state_set )
235 parameters are pointers to
237 .It Dv COPYFILE_STATE_SRC_FILENAME
238 .It Dv COPYFILE_STATE_DST_FILENAME
239 Get or set the filename associated with the source (or destination)
240 file. If it has not been initialized yet, the value will be
243 .Fn copyfile_state_set ,
246 parameter is a pointer to a C string
249 .Fn copyfile_state_set
250 makes a private copy of this string.
252 .Fn copyfile_state_get
255 parameter is a pointer to a pointer to a C string
258 the returned value is a pointer to the
260 copy, and must not be modified or released.
261 .It Dv COPYFILE_STATE_STATUS_CB
262 Get or set the callback status function (currently
263 only used for recursive copies; see below for details).
266 parameter is a pointer to a function of type
267 .Vt copyfile_callback_t
269 .It Dv COPYFILE_STATE_STATUS_CTX
270 Get or set the context parameter for the status
271 call-back function (see below for details).
276 .It Dv COPYFILE_STATE_QUARANTINE
277 Get or set the quarantine information with the source file.
280 parameter is a pointer to an opaque
284 .It Dv COPYFILE_STATE_COPIED
285 Get the number of data bytes copied so far.
287 .Fn copyfile_state_get ;
288 see below for more details about callbacks.)
292 .Dv COPYFILE_CLONE_FORCE
293 operation successfully cloned the requested objects, then this value will be 0.
296 parameter is a pointer to
300 .It Dv COPYFILE_STATE_XATTRNAME
301 Get the name of the extended attribute during a callback
303 .Dv COPYFILE_COPY_XATTR
304 (see below for details). This field cannot be set,
307 .It Dv COPYFILE_STATE_WAS_CLONED
311 .Dv COPYFILE_CLONE_FORCE
312 operation successfully cloned the requested objects.
315 parameter is a pointer to
322 .Dv COPYFILE_RECURSIVE
329 functions to recursively descend into the source file-system object.
332 on each of the entries it finds that way.
333 If a call-back function is given (using
334 .Fn copyfile_state_set
336 .Dv COPYFILE_STATE_STATUS_CB ),
337 the call-back function will be called four times for each directory
338 object, and twice for all other objects. (Each directory will
339 be examined twice, once on entry -- before copying each of the
340 objects contained in the directory -- and once on exit -- after
341 copying each object contained in the directory, in order to perform
344 The call-back function will have one of the following values
345 as the first argument, indicating what is being copied:
346 .Bl -tag -width COPYFILE_RECURSE_DIR_CLEANUP
347 .It Dv COPYFILE_RECURSE_FILE
348 The object being copied is a file (or, rather,
349 something other than a directory).
350 .It Dv COPYFILE_RECURSE_DIR
351 The object being copied is a directory, and is being
352 entered. (That is, none of the filesystem objects contained
353 within the directory have been copied yet.)
354 .It Dv COPYFILE_RECURSE_DIR_CLEANUP
355 The object being copied is a directory, and all of the
356 objects contained have been copied. At this stage, the destination directory
357 being copied will have any extra permissions that were added to
358 allow the copying will be removed.
359 .It Dv COPYFILE_RECURSE_ERROR
360 There was an error in processing an element of the source hierarchy;
363 returns an error or unknown file type.
364 (Currently, the second argument to the call-back function will always
370 The second argument to the call-back function will indicate
371 the stage of the copy, and will be one of the following values:
372 .Bl -tag -width COPYFILE_FINISH
373 .It Dv COPYFILE_START
374 Before copying has begun. The third
375 parameter will be a newly-created
377 object with the call-back function and context pre-loaded.
378 .It Dv COPYFILE_FINISH
379 After copying has successfully finished.
381 Indicates an error has happened at some stage. If the
382 first argument to the call-back function is
383 .Dv COPYFILE_RECURSE_ERROR ,
384 then an error occurred while processing the source hierarchy;
385 otherwise, it will indicate what type of object was being copied,
388 will be set to indicate the error.
392 parameters are the source and destination paths that
393 are to be copied (or have been copied, or failed to copy, depending on
394 the second argument).
396 The last argument to the call-back function will be the value
398 .Dv COPYFILE_STATE_STATUS_CTX ,
401 The call-back function is required to return one of the following
403 .Bl -tag -width COPYFILE_CONTINUE
404 .It Dv COPYFILE_CONTINUE
405 The copy will continue as expected.
407 This object will be skipped, and the next object will
408 be processed. (Note that, when entering a directory.
411 from the call-back function will prevent the contents
412 of the directory from being copied.)
414 The entire copy is aborted at this stage. Any filesystem
415 objects created up to this point will remain.
422 The call-back function must always return one of the values listed
423 above; if not, the results are undefined.
425 The call-back function will be called twice for each object
426 (and an additional two times for directory cleanup); the first
431 the second time, that value will be either
435 to indicate a successful completion, or an error during
437 In the event of an error, the
439 value will be set appropriately.
443 .Dv COPYFILE_UNPACK ,
447 flags are not used during a recursive copy, and will result
448 in an error being returned.
449 .Sh Progress Callback
450 In addition to the recursive callbacks described above,
454 will also use a callback to report data (e.g.,
456 progress. If given, the callback will be invoked on each
458 call. The first argument to the callback function will be
459 .Dv COPYFILE_COPY_DATA .
460 The second argument will either be
461 .Dv COPYFILE_PROGRESS
462 (indicating that the write was successful), or
464 (indicating that there was an error of some sort).
466 The amount of data bytes copied so far can be retrieved using
467 .Fn copyfile_state_get ,
469 .Dv COPYFILE_STATE_COPIED
470 requestor (the argument type is a pointer to
473 When copying extended attributes, the first argument to the
474 callback function will be
475 .Dv COPYFILE_COPY_XATTR .
476 The other arguments will be as described for
477 .Dv COPYFILE_COPY_DATA ;
478 the name of the extended attribute being copied may be
480 .Fn copyfile_state_get
482 .Dv COPYFILE_STATE_XATTRNAME .
485 the callback may be called with
487 for each of the extended attributes first, followed by
488 .Dv COPYFILE_PROGRESS
489 before getting and packing the data for each
490 individual attribute, and then
492 when finished with each individual attribute.
495 may be called for all of the extended attributes, before
496 the first callback with
497 .Dv COPYFILE_PROGRESS
498 is invoked.) Any attribute skipped by returning
502 callback will not be placed into the packed output file.
504 The return value for the data callback must be one of
505 .Bl -tag -width COPYFILE_CONTINUE
506 .It Dv COPYFILE_CONTINUE
507 The copy will continue as expected.
508 (In the case of error, it will attempt to write the data again.)
510 The data copy will be aborted, but without error.
512 The data copy will be aborted; in the case of
513 .Dv COPYFILE_PROGRESS ,
523 parameters will be passed in, they may be
528 Except when given the
534 return less than 0 on error, and 0 on success.
535 All of the other functions return 0 on success, and less than 0
542 can copy symbolic links; there is a gap between when the source
543 link is examined and the actual copy is started, and this can
544 be a potential security risk, especially if the process has
547 When performing a recursive copy, if the source hierarchy
548 changes while the copy is occurring, the results are undefined.
551 does not reset the seek position for either source or destination.
552 This can result in the destination file being a different size
553 than the source file.
561 An invalid flag was passed in with
562 .Dv COPYFILE_RECURSIVE .
580 was a negative number.
582 A memory allocation failed.
584 The source file was not a directory, symbolic link, or regular file.
586 The copy was cancelled by callback.
592 already existed and was passed in with
601 Search permission is denied for a component of the path prefix for
608 Write permission is denied for a component of the path prefix for the
613 In addition, both functions may set
615 via an underlying library or system call.
617 .Bd -literal -offset indent
618 /* Initialize a state variable */
620 s = copyfile_state_alloc();
621 /* Copy the data and extended attributes of one file to another */
622 copyfile("/tmp/f1", "/tmp/f2", s, COPYFILE_DATA | COPYFILE_XATTR);
623 /* Convert a file to an AppleDouble file for serialization */
624 copyfile("/tmp/f2", "/tmp/tmpfile", NULL, COPYFILE_ALL | COPYFILE_PACK);
625 /* Release the state variable */
626 copyfile_state_free(s);
627 /* A more complex way to call copyfile() */
628 s = copyfile_state_alloc();
629 copyfile_state_set(s, COPYFILE_STATE_SRC_FILENAME, "/tmp/foo");
630 /* One of src or dst must be set... rest can come from the state */
631 copyfile(NULL, "/tmp/bar", s, COPYFILE_ALL);
632 /* Now copy the same source file to another destination file */
633 copyfile(NULL, "/tmp/car", s, COPYFILE_ALL);
634 copyfile_state_free(s);
635 /* Remove extended attributes from a file */
636 copyfile("/dev/null", "/tmp/bar", NULL, COPYFILE_XATTR);
646 functions lack a way to set the input or output block size.
648 Recursive copies do not honor hard links.
652 API was introduced in Mac OS X 10.5.