]> git.saurik.com Git - apple/copyfile.git/blame - copyfile.3
copyfile-41.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"
28.Sh DESCRIPTION
29These functions are used to copy a file's data and/or metadata. (Metadata
30consists of permissions, extended attributes, access control lists, and so
31forth.)
32.Pp
33The
34.Fn copyfile_state_alloc
35function initializes a
36.Vt copyfile_state_t
37object (which is an opaque data type).
38This object can be passed to
39.Fn copyfile
40and
41.Fn fcopyfile ;
42.Fn copyfile_state_get
43and
44.Fn copyfile_state_set
45can be used to manipulate the state (see below).
46The
47.Fn copyfile_state_free
48function is used to deallocate the object and its contents.
49.Pp
50The
51.Fn copyfile
52function can copy the named
53.Va from
54file to the named
55.Va to
56file; the
57.Fn fcopyfile
58function does the same, but using the file descriptors of already-opened
59files.
60If the
61.Va state
62parameter is the return value from
63.Fn copyfile_state_alloc ,
64then
65.Fn copyfile
66and
67.Fn fcopyfile
68will use the information from the state object; if it is
69.Dv NULL ,
70then both functions will work normally, but less control will be available to the caller.
71The
72.Va flags
73parameter controls which contents are copied:
74.Bl -tag -width COPYFILE_XATTR
75.It Dv COPYFILE_ACL
76Copy the source file's access control lists.
77.It Dv COPYFILE_STAT
78Copy the source file's POSIX information (mode, modification time, etc.).
79.It Dv COPYFILE_XATTR
80Copy the source file's extended attributes.
81.It Dv COPYFILE_DATA
82Copy the source file's data.
83.El
84.Pp
85These values may be or'd together; several convenience macros are provided:
86.Bl -tag -width COPYFILE_SECURITY
87.It Dv COPYFILE_SECURITY
88Copy the source file's POSIX and ACL information; equivalent to
89.Dv (COPYFILE_STAT|COPYFILE_ACL) .
90.It Dv COPYFILE_METADATA
91Copy the metadata; equivalent to
92.Dv (COPYFILE_SECURITY|COPYFILE_XATTR) .
93.It Dv COPYFILE_ALL
94Copy the entire file; equivalent to
95.Dv (COPYFILE_METADATA|COPYFILE_DATA) .
96.El
97.Pp
98The
99.Fn copyfile
100and
101.Fn fcopyfile
102functions can also have their behavior modified by the following flags:
103.Bl -tag -width COPYFILE_NOFOLLOW_SRC
104.It Dv COPYFILE_CHECK
105Return a bitmask (corresponding to the
106.Va flags
107argument) indicating which contents would be copied; no data are actually
108copied. (E.g., if
109.Va flags
110was set to
111.Dv COPYFILE_CHECK|COPYFILE_METADATA ,
112and the
113.Va from
114file had extended attributes but no ACLs, the return value would be
115.Dv COPYFILE_XATTR .)
116.It Dv COPYFILE_PACK
117Serialize the
118.Va from
119file. The
120.Va to
121file is an AppleDouble-format file.
122.It Dv COPYFILE_UNPACK
123Unserialize the
124.Va from
125file. The
126.Va from
127file is an AppleDouble-format file; the
128.Va to
129file will have the extended attributes, ACLs, resource fork, and
130FinderInfo data from the
131.Va to
132file, regardless of the
133.Va flags
134argument passed in.
135.It Dv COPYFILE_EXCL
136Fail if the
137.Va to
138file already exists. (This is only applicable for the
139.Fn copyfile
140function.)
141.It Dv COPYFILE_NOFOLLOW_SRC
142Do not follow the
143.Va from
144file, if it is a symbolic link. (This is only applicable for the
145.Fn copyfile
146function.)
147.It Dv COPYFILE_NOFOLLOW_DST
148Do not follow the
149.Va to
150file, if it is a symbolic link. (This is only applicable for the
151.Fn copyfile
152function.)
153.It Dv COPYFILE_MOVE
154Unlink (remove) the
155.Fa from
156file. (This is only applicable for the
157.Fn copyfile
158function.)
159.It Dv COPYFILE_UNLINK
160Unlink the
161.Va to
162file before starting. (This is only applicable for the
163.Fn copyfile
164function.)
165.It Dv COPYFILE_NOFOLLOW
166This is a convenience macro, equivalent to
167.Dv (COPYFILE_NOFOLLOW_DST|COPYFILE_NOFOLLOW_SRC) .
168.El
169.Pp
170The
171.Fn copyfile_state_get
172and
173.Fn copyfile_state_set
174functions can be used to manipulate the
175.Ft copyfile_state_t
176object returned by
177.Fn copyfile_state_alloc .
178In both functions, the
179.Va dst
180parameter's type depends on the
181.Va flag
182parameter that is passed in.
183.Bl -tag -width COPYFILE_STATE_DST_FILENAME
184.It Dv COPYFILE_STATE_SRC_FD
185.It Dv COPYFILE_STATE_DST_FD
186Get or set the file descriptor associated with the source (or destination)
187file.
188If this has not been initialized yet, the value will be -2.
189The
190.Va dst
191(for
192.Fn copyfile_state_get )
193and
194.Va src
195(for
196.Fn copyfile_state_set )
197parameters are pointers to
198.Vt int .
199.It Dv COPYFILE_STATE_SRC_FILENAME
200.It Dv COPYFILE_STATE_DST_FILENAME
201Get or set the filename associated with the source (or destination)
202file. If it has not been initialized yet, the value will be
203.Dv NULL .
204For
205.Fn copyfile_state_set ,
206the
207.Va src
208parameter is a pointer to a C string
209(i.e.,
210.Vt char* );
211.Fn copyfile_state_set
212makes a private copy of this string.
213For
214.Fn copyfile_state_get
215function, the
216.Va dst
217parameter is a pointer to a pointer to a C string
218(i.e.,
219.Vt char** );
220the returned value is a pointer to the
221.Va state 's
222copy, and must not be modified or released.
223.It Dv COPYFILE_STATE_QUARANTINE
224Get or set the quarantine information with the source file.
225The
226.Va src
227parameter is a pointer to a
228.Vt qtn_file_t
229object (see
230.Pa <quarantine.h> ).
231In the case of
232.Dv COPYFILE_STATE_SET ,
233the object is cloned; in the case of
234.Dv COPYFILE_STATE_GET ,
235the object is simply returned, and it is up to the
236caller to clone it if desired.
237.El
238.Sh RETURN VALUES
239Except when given the
240.Dv COPYFILE_CHECK
241flag,
242.Fn copyfile
243and
244.Fn fcopyfile
245return less than 0 on error, and 0 on success.
246All of the other functions return 0 on success, and less than 0
247on error.
248.Sh ERRORS
249.Fn copyfile
250and
251.Fn fcopyfile
252will fail if:
253.Bl -tag -width Er
254.It Bq Er EINVAL
255Either the
256.Va from
257or
258.Va to
259parameter was a
260.Dv NULL
261pointer (
262.Fn copyfile ),
263or a negative number (
264.Fn fcopyfile ).
265.It Bq Er ENOMEM
266A memory allocation failed.
267.It Bq Er ENOTSUP
268The source file was not a directory, symbolic link, or regular file.
269.El
270In addition, both functions may set
271.Dv errno
272via an underlying library or system call.
273.Sh EXAMPLES
274.Bd -literal -offset indent
275/* Initialize a state variable */
276copyfile_state_t s;
277s = copyfile_state_alloc();
278/* Copy the data and extended attributes of one file to another */
279copyfile("/tmp/f1", "/tmp/f2", s, COPYFILE_DATA | COPYFILE_XATTR);
280/* Convert a file to an AppleDouble file for serialization */
281copyfile("/tmp/f2", "/tmp/tmpfile", NULL, COPYFILE_ALL | COPYFILE_PACK);
282/* Release the state variable */
283copyfile_state_free(s);
284/* A more complex way to call copyfile() */
285s = copyfile_state_alloc();
286copyfile_state_set(s, COPYFILE_STATE_SRC_FILENAME, "/tmp/foo");
287/* One of src or dst must be set... rest can come from the state */
288copyfile(NULL, "/tmp/bar", s, COPYFILE_ALL);
289/* Now copy the same source file to another destination file */
290copyfile(NULL, "/tmp/car", s, COPYFILE_ALL);
291copyfile_state_free(s);
292.Ed
293.Sh BUGS
294Both
295.Fn copyfile
296functions lack a way to set the input or output block size.
297.Sh HISTORY
298The
299.Fn copyfile
300API was introduced in Mac OS X 10.5.