]> git.saurik.com Git - apple/copyfile.git/blame - copyfile.3
copyfile-85.1.tar.gz
[apple/copyfile.git] / copyfile.3
CommitLineData
b3aa0442
A
1.\"
2.\" Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
3.\"
4.Dd April 27, 2006
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.)
181.It Dv COPYFILE_NOFOLLOW
182This is a convenience macro, equivalent to
183.Dv (COPYFILE_NOFOLLOW_DST|COPYFILE_NOFOLLOW_SRC) .
184.El
185.Pp
186The
187.Fn copyfile_state_get
188and
189.Fn copyfile_state_set
190functions can be used to manipulate the
191.Ft copyfile_state_t
192object returned by
193.Fn copyfile_state_alloc .
194In both functions, the
195.Va dst
196parameter's type depends on the
197.Va flag
198parameter that is passed in.
199.Bl -tag -width COPYFILE_STATE_DST_FILENAME
200.It Dv COPYFILE_STATE_SRC_FD
201.It Dv COPYFILE_STATE_DST_FD
202Get or set the file descriptor associated with the source (or destination)
203file.
204If this has not been initialized yet, the value will be -2.
205The
206.Va dst
207(for
208.Fn copyfile_state_get )
209and
210.Va src
211(for
212.Fn copyfile_state_set )
213parameters are pointers to
214.Vt int .
215.It Dv COPYFILE_STATE_SRC_FILENAME
216.It Dv COPYFILE_STATE_DST_FILENAME
217Get or set the filename associated with the source (or destination)
218file. If it has not been initialized yet, the value will be
219.Dv NULL .
220For
221.Fn copyfile_state_set ,
222the
223.Va src
224parameter is a pointer to a C string
225(i.e.,
226.Vt char* );
227.Fn copyfile_state_set
228makes a private copy of this string.
229For
230.Fn copyfile_state_get
231function, the
232.Va dst
233parameter is a pointer to a pointer to a C string
234(i.e.,
235.Vt char** );
236the returned value is a pointer to the
237.Va state 's
238copy, and must not be modified or released.
659640e4
A
239.It Dv COPYFILE_STATE_STATUS_CB
240Get or set the callback status function (currently
241only used for recursive copies; see below for details).
242The
243.Va src
244parameter is a pointer to a function of type
245.Vt copyfile_callback_t
246(see above).
247.It Dv COPYFILE_STATE_STATUS_CTX
248Get or set the context parameter for the status
249call-back function (see below for details).
250The
251.Va src
252parameter is a
253.Vt void\ * .
b3aa0442
A
254.It Dv COPYFILE_STATE_QUARANTINE
255Get or set the quarantine information with the source file.
256The
257.Va src
659640e4
A
258parameter is a pointer to an opaque
259object (type
260.Vt void\ *
261).
262.It Dv COPYFILE_STATE_COPIED
263Get the number of data bytes copied so far.
264(Only valid for
265.Fn copyfile_state_get ;
266see below for more details about callbacks.)
267The
268.Va dst
269parameter is a pointer to
270.Vt off_t
271(type
272.Vt off_t\ * ).
3f81d1c4
A
273.It Dv COPYFILE_STATE_XATTRNAME
274Get the name of the extended attribute during a callback
275for
276.Dv COPYFILE_COPY_XATTR
277(see below for details). This field cannot be set,
278and may be
279.Dv NULL .
659640e4
A
280.El
281.Sh Recursive Copies
282When given the
283.Dv COPYFILE_RECURSIVE
284flag,
285.Fn copyfile
286(but not
287.Fn fcopyfile )
288will use the
289.Xr fts 3
290functions to recursively descend into the source file-system object.
291It then calls
292.Fn copyfile
293on each of the entries it finds that way.
294If a call-back function is given (using
295.Fn copyfile_state_set
296and
297.Dv COPYFILE_STATE_STATUS_CB ),
298the call-back function will be called four times for each directory
299object, and twice for all other objects. (Each directory will
300be examined twice, once on entry -- before copying each of the
301objects contained in the directory -- and once on exit -- after
302copying each object contained in the directory, in order to perform
303some final cleanup.)
304.Pp
305The call-back function will have one of the following values
306as the first argument, indicating what is being copied:
307.Bl -tag -width COPYFILE_RECURSE_DIR_CLEANUP
308.It Dv COPYFILE_RECURSE_FILE
309The object being copied is a file (or, rather,
310something other than a directory).
311.It Dv COPYFILE_RECURSE_DIR
312The object being copied is a directory, and is being
313entered. (That is, none of the filesystem objects contained
314within the directory have been copied yet.)
315.It Dv COPYFILE_RECURSE_DIR_CLEANUP
316The object being copied is a directory, and all of the
317objects contained have been copied. At this stage, the destination directory
318being copied will have any extra permissions that were added to
319allow the copying will be removed.
320.It Dv COPYFILE_RECURSE_ERROR
321There was an error in processing an element of the source hierarchy;
322this happens when
323.Xr fts 3
324returns an error or unknown file type.
325(Currently, the second argument to the call-back function will always
326be
327.Dv COPYFILE_ERR
328in this case.)
329.El
330.Pp
331The second argument to the call-back function will indicate
332the stage of the copy, and will be one of the following values:
333.Bl -tag -width COPYFILE_FINISH
334.It Dv COPYFILE_START
335Before copying has begun. The third
336parameter will be a newly-created
337.Vt copyfile_state_t
338object with the call-back function and context pre-loaded.
339.It Dv COPYFILE_FINISH
340After copying has successfully finished.
341.It Dv COPYFILE_ERR
342Indicates an error has happened at some stage. If the
343first argument to the call-back function is
344.Dv COPYFILE_RECURSE_ERROR ,
345then an error occurred while processing the source hierarchy;
346otherwise, it will indicate what type of object was being copied,
347and
348.Dv errno
349will be set to indicate the error.
350.El
351.Pp
352The fourth and fifth
353parameters are the source and destination paths that
354are to be copied (or have been copied, or failed to copy, depending on
355the second argument).
356.Pp
357The last argument to the call-back function will be the value
358set by
359.Dv COPYFILE_STATE_STATUS_CTX ,
360if any.
361.Pp
362The call-back function is required to return one of the following
363values:
364.Bl -tag -width COPYFILE_CONTINUE
365.It Dv COPYFILE_CONTINUE
366The copy will continue as expected.
367.It Dv COPYFILE_SKIP
368This object will be skipped, and the next object will
369be processed. (Note that, when entering a directory.
370returning
371.Dv COPYFILE_SKIP
372from the call-back function will prevent the contents
373of the directory from being copied.)
374.It Dv COPYFILE_QUIT
375The entire copy is aborted at this stage. Any filesystem
376objects created up to this point will remain.
377.Fn copyfile
378will return -1, but
379.Dv errno
380will be unmodified.
381.El
382.Pp
383The call-back function must always return one of the values listed
384above; if not, the results are undefined.
385.Pp
386The call-back function will be called twice for each object
387(and an additional two times for directory cleanup); the first
388call will have a
389.Ar stage
390parameter of
391.Dv COPYFILE_START ;
392the second time, that value will be either
393.Dv COPYFILE_FINISH
394or
395.Dv COPYFILE_ERR
396to indicate a successful completion, or an error during
397processing.
398In the event of an error, the
399.Dv errno
400value will be set appropriately.
401.Pp
402The
403.Dv COPYFILE_PACK ,
404.Dv COPYFILE_UNPACK ,
405.Dv COPYFILE_MOVE ,
406and
407.Dv COPYFILE_UNLINK
408flags are not used during a recursive copy, and will result
409in an error being returned.
410.Sh Progress Callback
411In addition to the recursive callbacks described above,
412.Fn copyfile
413and
414.Fn fcopyfile
3f81d1c4 415will also use a callback to report data (e.g.,
659640e4
A
416.Dv COPYFILE_DATA )
417progress. If given, the callback will be invoked on each
418.Xr write 2
419call. The first argument to the callback function will be
420.Dv COPYFILE_COPY_DATA .
421The second argument will either be
3f81d1c4 422.Dv COPYFILE_PROGRESS
659640e4
A
423(indicating that the write was successful), or
424.Dv COPYFILE_ERR
425(indicating that there was an error of some sort).
426.Pp
427The amount of data bytes copied so far can be retrieved using
428.Fn copyfile_state_get ,
429with the
430.Dv COPYFILE_STATE_COPIED
431requestor (the argument type is a pointer to
432.Vt off_t ).
433.Pp
3f81d1c4
A
434When copying extended attributes, the first argument to the
435callback function will be
436.Dv COPYFILE_COPY_XATTR .
437The other arguments will be as described for
438.Dv COPYFILE_COPY_DATA ;
439the name of the extended attribute being copied may be
440retrieved using
441.Fn copyfile_state_get
442and the parameter
443.Dv COPYFILE_STATE_XATTRNAME .
444When using
445.Dv COPYFILE_PACK ,
446the callback may be called with
447.Dv COPYFILE_START
448for each of the extended attributes first, followed by
449.Dv COPYFILE_PROGRESS
450before getting and packing the data for each
451individual attribute, and then
452.Dv COPYFILE_FINISH
453when finished with each individual attribute.
454(That is,
455.Dv COPYFILE_START
456may be called for all of the extended attributes, before
457the first callback with
458.Dv COPYFILE_PROGRESS
459is invoked.) Any attribute skipped by returning
460.Dv COPYFILE_SKIP
461from the
462.Dv COPYFILE_START
463callback will not be placed into the packed output file.
464.Pp
659640e4
A
465The return value for the data callback must be one of
466.Bl -tag -width COPYFILE_CONTINUE
467.It Dv COPYFILE_CONTINUE
468The copy will continue as expected.
469(In the case of error, it will attempt to write the data again.)
470.It Dv COPYFILE_SKIP
471The data copy will be aborted, but without error.
472.It Dv COPYFILE_QUIT
473The data copy will be aborted; in the case of
3f81d1c4 474.Dv COPYFILE_PROGRESS ,
659640e4
A
475.Dv errno
476will be set to
477.Dv ECANCELED .
b3aa0442 478.El
659640e4
A
479.Pp
480While the
481.Va src
482and
483.Va dst
484parameters will be passed in, they may be
485.Dv NULL
486in the case of
487.Fn fcopyfile .
b3aa0442
A
488.Sh RETURN VALUES
489Except when given the
490.Dv COPYFILE_CHECK
491flag,
492.Fn copyfile
493and
494.Fn fcopyfile
495return less than 0 on error, and 0 on success.
496All of the other functions return 0 on success, and less than 0
497on error.
659640e4
A
498.Sh WARNING
499Both
500.Fn copyfile
501and
502.Fn fcopyfile
503can copy symbolic links; there is a gap between when the source
3f81d1c4 504link is examined and the actual copy is started, and this can
659640e4
A
505be a potential security risk, especially if the process has
506elevated privileges.
507.Pp
508When performing a recursive copy, if the source hierarchy
509changes while the copy is occurring, the results are undefined.
3f81d1c4
A
510.Pp
511.Fn fcopyfile
512does not reset the seek position for either source or destination.
513This can result in the destination file being a different size
514than the source file.
b3aa0442
A
515.Sh ERRORS
516.Fn copyfile
517and
518.Fn fcopyfile
519will fail if:
520.Bl -tag -width Er
521.It Bq Er EINVAL
659640e4
A
522An invalid flag was passed in with
523.Dv COPYFILE_RECURSIVE .
524.It Bq Er EINVAL
525The
b3aa0442
A
526.Va from
527or
528.Va to
659640e4
A
529parameter to
530.Fn copyfile
531was a
b3aa0442 532.Dv NULL
659640e4
A
533pointer.
534.It Bq Er EINVAL
535The
536.Va from
537or
538.Va to
539parameter to
540.Fn copyfile
541was a negative number.
b3aa0442
A
542.It Bq Er ENOMEM
543A memory allocation failed.
544.It Bq Er ENOTSUP
545The source file was not a directory, symbolic link, or regular file.
659640e4
A
546.It Bq Er ECANCELED
547The copy was cancelled by callback.
b3aa0442
A
548.El
549In addition, both functions may set
550.Dv errno
551via an underlying library or system call.
552.Sh EXAMPLES
553.Bd -literal -offset indent
554/* Initialize a state variable */
555copyfile_state_t s;
556s = copyfile_state_alloc();
557/* Copy the data and extended attributes of one file to another */
558copyfile("/tmp/f1", "/tmp/f2", s, COPYFILE_DATA | COPYFILE_XATTR);
559/* Convert a file to an AppleDouble file for serialization */
560copyfile("/tmp/f2", "/tmp/tmpfile", NULL, COPYFILE_ALL | COPYFILE_PACK);
561/* Release the state variable */
562copyfile_state_free(s);
563/* A more complex way to call copyfile() */
564s = copyfile_state_alloc();
565copyfile_state_set(s, COPYFILE_STATE_SRC_FILENAME, "/tmp/foo");
566/* One of src or dst must be set... rest can come from the state */
567copyfile(NULL, "/tmp/bar", s, COPYFILE_ALL);
568/* Now copy the same source file to another destination file */
569copyfile(NULL, "/tmp/car", s, COPYFILE_ALL);
570copyfile_state_free(s);
659640e4
A
571/* Remove extended attributes from a file */
572copyfile("/dev/null", "/tmp/bar", NULL, COPYFILE_XATTR);
b3aa0442 573.Ed
659640e4
A
574.Sh SEE ALSO
575.Xr listxattr 2 ,
576.Xr getxattr 2 ,
577.Xr setxattr 2 ,
578.Xr acl 3
b3aa0442
A
579.Sh BUGS
580Both
581.Fn copyfile
582functions lack a way to set the input or output block size.
659640e4
A
583.Pp
584Recursive copies do not honor hard links.
b3aa0442
A
585.Sh HISTORY
586The
587.Fn copyfile
588API was introduced in Mac OS X 10.5.