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 DESCRIPTION 37The 38.Fn clonefile 39function causes the named file 40.Fa src 41to be cloned to the named file 42.Fa dst . 43The cloned file 44.Fa dst 45shares its data blocks with the 46.Fa src 47file but has its own copy of attributes and extended attributes which are identical to 48those of the named file 49.Fa src 50with the exceptions listed below 51.Pp 52. 53.Bl -enum 54. 55.It 56ownership information is set as it would be if 57.Fa dst 58was created by 59.Xr openat 2 60or 61.Xr mkdirat 2 62or 63.Xr symlinkat 2 64if the current user does not have privileges to change ownership. If the optional 65flag CLONE_NOOWNERCOPY is passed, the ownership information is the same as if the 66the current user does not have privileges to change ownership 67 68. 69.It 70setuid and setgid bits are turned off in the mode bits for regular files. 71.El 72.Pp 73Subsequent writes to either the original or cloned file are private to the file being modified (copy-on-write). 74The named file 75.Fa dst 76must not exist for the call to be successful. Since the clonefile() system call might not 77allocate new storage for data blocks, it is possible for a subsequent overwrite of an existing data block to 78return ENOSPC. If 79.Fa src 80names a directory, the directory hierarchy is cloned as if each item was cloned individually. However, the use of 81.Xr copyfile 3 82is more appropriate for copying large directory hierarchies instead of 83.Xr clonefile 2 84.Pp 85The 86.Fn clonefileat 87function is equivalent to 88.Fn clonefile 89except in the case where either 90.Fa src 91or 92.Fa dst 93specifies a relative path. If src is a relative path, the file to be cloned is located relative to the directory associated 94with the file descriptor 95.Fa src_dirfd 96instead of the current working directory. If 97.Fa dst 98is a relative path, the same happens only relative to the directory associated with 99.Fa dst_dirfd . 100If 101.Fn clonefileat 102is passed the special value 103.Dv AT_FDCWD 104in either the 105.Fa src_dirfd 106or 107.Fa dst_dirfd 108parameters, the current working directory is used in the determination of the file for 109the respective path parameter. 110.Pp 111The 112.Fn fclonefileat 113function is similar to 114.Fn clonefileat 115except that the source is identified by file descriptor 116.Fa srcfd 117rather than a path (as in 118.Fn clonefile 119or 120.Fn clonefileat ) 121.Pp 122The 123.Fa flags 124parameter specifies the options that can be passed. Options are specified in the 125.Fa flags 126argument by or'ing the following values: 127. 128.Bl -tag -width CLONE_NOFOLLOW 129. 130.It CLONE_NOFOLLOW 131Don't follow the src file if it is a symbolic link (applicable only if the source is not a directory). 132The symbolic link is itself cloned if 133.Fa src 134names a symbolic link. 135. 136.El 137.Pp 138.Bl -tag -width CLONE_NOOWNERCOPY 139. 140.It CLONE_NOOWNERCOPY 141Don't copy ownership information from the source when run called with superuser privileges. 142The symbolic link is itself cloned if 143.Fa src 144names a symbolic link. 145. 146.El 147.Pp 148The 149The 150.Fn clonefile , 151.Fn clonefileat 152and 153.Fn fclonefileat 154functions are expected to be atomic i.e. the system call will result all new objects being created 155successfully or no new objects will be created. POSIX conforming applications cannot use 156.Fn clonefile . 157. 158.Sh RETURN VALUES 159Upon successful completion, 160.Fn clonefile 161returns 0. Otherwise, a value of -1 is returned and errno is set to indicate the error. 162.Pp 163.Sh COMPATIBILITY 164Not all volumes support 165.Fn clonefile . 166A volume can be tested for 167.Fn clonefile 168support by using 169.Xr getattrlist 2 170to get the volume capabilities attribute ATTR_VOL_CAPABILITIES, and then testing the VOL_CAP_INT_CLONE flag. 171.Pp 172.Sh ERRORS 173The 174.Fn clonefile 175function will fail if: 176.Bl -tag -width Er 177. 178.It Bq Er EACCES 179Read permissions are denied on the source or write permissions are on the destination parent. 180. 181.It Bq Er ENOTSUP 182The underlying filesystem does not support this call. 183. 184.It Bq Er EEXIST 185The named file 186.Fa dst 187exists. 188. 189.It Bq Er EXDEV 190.Fa src 191and 192.Fa dst 193are not on the same filesystem. 194. 195.It Bq Er EINVAL 196The value of the 197.Fa flags 198parameter is invalid. 199. 200.It Bq Er ENOSPC 201There is no free space remaining on the file system containing the file. 202. 203.It Bq Er EIO 204An I/O error occurred while reading from or writing to the file system. 205. 206.It Bq Er EPERM 207The calling process does not have appropriate privileges. 208. 209.It Bq Er EPERM 210.Fa src 211is the root of the Filesystem. 212. 213.It Bq Er ELOOP 214A loop exists in symbolic links encountered during in resolution 215of the 216.Fa src 217or 218.Fa dst 219path arguments. 220. 221.It Bq Er EROFS 222The requested operation requires writing in a directory on a read-only file system. 223. 224.It Bq Er ENAMETOOLONG 225The length of a component of a pathname is longer than {NAME_MAX}. 226. 227.It Bq Er ENOENT 228A component of path 229.Fa src 230or the path 231.Fa dst 232does not name an existing file or path is an empty string. 233. 234.It Bq Er ENOTDIR 235A component of path prefix of either 236.Fa src 237or 238.Fa dst 239names an existing file that is neither a directory nor a symbolic link to a directory, 240or the path argument contains at least one non <slash> character and ends with one or 241more trailing <slash> characters and the last pathname component names an existing file that 242is neither a directory nor a symbolic link to a directory. 243. 244.It Bq Er EDEADLK 245A component of either pathname refers to a 246.Dq dataless 247directory that requires materialization and the I/O policy of the current 248thread or process disallows dataless directory materialization 249.Po see 250.Xr getiopolicy_np 3 251.Pc . 252. 253.It Bq Er EDEADLK 254The 255.Fa src 256pathname refers to a 257.Dq dataless 258file that must be materialized before being cloned and the I/O policy of 259the current thread or process disallows file materialization 260.Po see 261.Xr getiopolicy_np 3 262.Pc . 263.El 264.Pp 265In addition, the 266.Fn clonefileat 267or 268.Fn fclonefileat 269functions may fail with the following errors 270.Bl -tag -width Er 271.It Bq Er EBADF 272The 273.Fa src 274or 275.Fa dst 276argument does not specify an absolute path and the 277.Fa src_dirfd 278or 279.Fa dst_dirfd 280argument is neither 281.Dv AT_FDCWD 282nor a valid file descriptor open for searching. 283. 284.It Bq Er ENOTDIR 285The 286.Fa src 287or 288.Fa dst 289argument is not an absolute path and 290.Fa src_dirfd 291or 292.Fa dst_dirfd 293is neither 294.Dv AT_FDCWD 295nor a file descriptor associated with a directory. 296. 297.El 298. 299.Pp 300. 301.Sh SEE ALSO 302. 303.Xr copyfile 3 304.Xr chown 2 305. 306.Sh HISTORY 307The 308.Fn clonefile , 309.Fn clonefileat 310and 311.Fn fclonefileat 312function calls appeared in OS X version 10.12 313. 314