xref: /xnu-11417.140.69/bsd/man/man2/clonefile.2 (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1.\" Copyright (c) 2015 Apple Computer, Inc. All rights reserved.
2.\"
3.\" The contents of this file constitute Original Code as defined in and
4.\" are subject to the Apple Public Source License Version 1.1 (the
5.\" "License").  You may not use this file except in compliance with the
6.\" License.  Please obtain a copy of the License at
7.\" http://www.apple.com/publicsource and read it before using this file.
8.\"
9.\" This Original Code and all software distributed under the License are
10.\" distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
11.\" EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
12.\" INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
13.\" FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
14.\" License for the specific language governing rights and limitations
15.\" under the License.
16.\"
17.\"     @(#)clonefile.2
18.
19.Dd June 3, 2021
20.Dt CLONEFILE 2
21.Os Darwin
22.Sh NAME
23.Nm clonefile
24.Nd create copy on write clones of files
25.Sh SYNOPSIS
26.Fd #include <sys/attr.h>
27.Fd #include <sys/clonefile.h>
28.Pp
29.Ft int
30.Fn clonefile  "const char * src" "const char * dst" "int flags"
31.
32.Fn clonefileat  "int src_dirfd" "const char * src" "int dst_dirfd" "const char * dst" "int flags"
33.
34.Fn fclonefileat "int srcfd" "int dst_dirfd" "const char * dst" "int flags"
35.
36.Sh LIMITATIONS
37Cloning directories with these functions is strongly discouraged.  Use
38.Xr copyfile 3
39to clone directories instead.
40.
41.Sh DESCRIPTION
42The
43.Fn clonefile
44function causes the named file
45.Fa src
46to be cloned to the named file
47.Fa dst .
48The cloned file
49.Fa dst
50shares its data blocks with the
51.Fa src
52file but has its own copy of attributes and extended attributes which are identical to
53those of the named file
54.Fa src
55with the exceptions listed below:
56.Pp
57.
58.Bl -enum
59.
60.It
61ownership information is set as it would be if
62.Fa dst
63was created by
64.Xr openat 2
65or
66.Xr mkdirat 2
67or
68.Xr symlinkat 2
69if the current user does not have privileges to change ownership. If the optional
70flag CLONE_NOOWNERCOPY is passed, the ownership information is the same as if the
71the current user does not have privileges to change ownership.
72.
73.It
74setuid and setgid bits are turned off in the mode bits for regular files.
75.It
76inherit target directory's access control lists. If the optional flag CLONE_ACL is passed, it will additionally
77copy the ACLs from the source file and also apply the inherited ACLs from the target directory.
78.El
79.Pp
80Subsequent writes to either the original or cloned file are private to the file being modified (copy-on-write).
81The named file
82.Fa dst
83must not exist for the call to be successful. Since the clonefile() system call might not
84allocate new storage for data blocks, it is possible for a subsequent overwrite of an existing data block to
85return ENOSPC.  If
86.Fa src
87names a directory, the directory hierarchy is cloned as if each item was cloned individually.  However, the use of
88.Xr clonefile 2
89to clone directory hierarchies is strongly discouraged.  Use
90.Xr copyfile 3
91instead for copying directories.
92.Pp
93The
94.Fn clonefileat
95function is equivalent to
96.Fn clonefile
97except in the case where either
98.Fa src
99or
100.Fa dst
101specifies a relative path. If src is a relative path, the file to be cloned is located relative to the directory associated
102with the file descriptor
103.Fa src_dirfd
104instead of the current working directory. If
105.Fa dst
106is a relative path, the same happens only relative to the directory associated with
107.Fa dst_dirfd .
108If
109.Fn clonefileat
110is passed the special value
111.Dv AT_FDCWD
112in either the
113.Fa src_dirfd
114or
115.Fa dst_dirfd
116parameters, the current working directory is used in the determination of the file for
117the respective path parameter.
118.Pp
119The
120.Fn fclonefileat
121function is similar to
122.Fn clonefileat
123except that the source is identified by file descriptor
124.Fa srcfd
125rather than a path (as in
126.Fn clonefile
127or
128.Fn clonefileat
129).
130.Pp
131The
132.Fa flags
133parameter specifies the options that can be passed. Options are specified in the
134.Fa flags
135argument by or'ing the following values:
136.
137.Bl -tag -width CLONE_NOFOLLOW
138.
139.It CLONE_NOFOLLOW
140Don't follow the src file if it is a symbolic link (applicable only if the source is not a directory).
141The symbolic link is itself cloned if
142.Fa src
143names a symbolic link.
144.
145.El
146.Pp
147.Bl -tag -width CLONE_NOOWNERCOPY
148.
149.It CLONE_NOOWNERCOPY
150Don't copy ownership information from the source when run called with superuser privileges.
151The symbolic link is itself cloned if
152.Fa src
153names a symbolic link.
154.
155.El
156.Pp
157.Bl -tag -width CLONE_ACL
158.
159.It CLONE_ACL
160Copy ACLs from the source file.
161.El
162.Pp
163The
164.Fn clonefile ,
165.Fn clonefileat
166and
167.Fn fclonefileat
168functions are expected to be atomic i.e. the system call will result all new objects being created
169successfully or no new objects will be created. POSIX conforming applications cannot use
170.Fn clonefile .
171.
172.Sh RETURN VALUES
173Upon successful completion,
174.Fn clonefile
175returns 0. Otherwise, a value of -1 is returned and errno is set to indicate the error.
176.Pp
177.Sh COMPATIBILITY
178Not all volumes support
179.Fn clonefile .
180A volume can be tested for
181.Fn clonefile
182support by using
183.Xr getattrlist 2
184to get the volume capabilities attribute ATTR_VOL_CAPABILITIES, and then testing the VOL_CAP_INT_CLONE flag.
185.Pp
186.Sh ERRORS
187The
188.Fn clonefile
189function will fail if:
190.Bl -tag -width Er
191.
192.It Bq Er EACCES
193Read permissions are denied on the source or write permissions are on the destination parent.
194.
195.It Bq Er ENOTSUP
196The underlying filesystem does not support this call.
197.
198.It Bq Er EEXIST
199The named file
200.Fa dst
201exists.
202.
203.It Bq Er EXDEV
204.Fa src
205and
206.Fa dst
207are not on the same filesystem.
208.
209.It Bq Er EINVAL
210The value of the
211.Fa flags
212parameter is invalid.
213.
214.It Bq Er ENOSPC
215There is no free space remaining on the file system containing the file.
216.
217.It Bq Er EIO
218An I/O error occurred while reading from or writing to the file system.
219.
220.It Bq Er EPERM
221The calling process does not have appropriate privileges.
222.
223.It Bq Er EPERM
224.Fa src
225is the root of the Filesystem.
226.
227.It Bq Er ELOOP
228A loop exists in symbolic links encountered during in resolution
229of the
230.Fa src
231or
232.Fa dst
233path arguments.
234.
235.It Bq Er EROFS
236The requested operation requires writing in a directory on a read-only file system.
237.
238.It Bq Er ENAMETOOLONG
239The length of a component of a pathname is longer than {NAME_MAX}.
240.
241.It Bq Er ENOENT
242A component of path
243.Fa src
244or the path
245.Fa dst
246does not name an existing file or path is an empty string.
247.
248.It Bq Er ENOTDIR
249A component of path prefix of either
250.Fa src
251or
252.Fa dst
253names an  existing file that is  neither a directory nor a symbolic link to a directory,
254or the path argument contains at least one non <slash> character and ends with one or
255more trailing <slash> characters and the last pathname component names an existing file that
256is neither a directory nor a symbolic link to a directory.
257.
258.It Bq Er EDEADLK
259A component of either pathname refers to a
260.Dq dataless
261directory that requires materialization and the I/O policy of the current
262thread or process disallows dataless directory materialization
263.Po see
264.Xr getiopolicy_np 3
265.Pc .
266.
267.It Bq Er EDEADLK
268The
269.Fa src
270pathname refers to a
271.Dq dataless
272file that must be materialized before being cloned and the I/O policy of
273the current thread or process disallows file materialization
274.Po see
275.Xr getiopolicy_np 3
276.Pc .
277.El
278.Pp
279In addition, the
280.Fn clonefileat
281or
282.Fn fclonefileat
283functions may fail with the following errors:
284.Bl -tag -width Er
285.It Bq Er EBADF
286The
287.Fa src
288or
289.Fa dst
290argument does not specify an absolute path and the
291.Fa src_dirfd
292or
293.Fa dst_dirfd
294argument is neither
295.Dv AT_FDCWD
296nor a valid file descriptor open for searching.
297.
298.It Bq Er ENOTDIR
299The
300.Fa src
301or
302.Fa dst
303argument is not an absolute path and
304.Fa src_dirfd
305or
306.Fa dst_dirfd
307is neither
308.Dv AT_FDCWD
309nor a file descriptor associated with a directory.
310.
311.El
312.
313.Pp
314.
315.Sh SEE ALSO
316.
317.Xr copyfile 3
318.Xr chown 2
319.
320.Sh HISTORY
321The
322.Fn clonefile ,
323.Fn clonefileat
324and
325.Fn fclonefileat
326function calls appeared in OS X version 10.12
327.
328