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