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