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