]> git.saurik.com Git - apple/xnu.git/blob - bsd/man/man2/rename.2
xnu-3789.1.32.tar.gz
[apple/xnu.git] / bsd / man / man2 / rename.2
1 .\" $NetBSD: rename.2,v 1.7 1995/02/27 12:36:15 cgd Exp $
2 .\"
3 .\" Copyright (c) 1983, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
5 .\"
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. All advertising materials mentioning features or use of this software
15 .\" must display the following acknowledgement:
16 .\" This product includes software developed by the University of
17 .\" California, Berkeley and its contributors.
18 .\" 4. Neither the name of the University nor the names of its contributors
19 .\" may be used to endorse or promote products derived from this software
20 .\" without specific prior written permission.
21 .\"
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" SUCH DAMAGE.
33 .\"
34 .\" @(#)rename.2 8.1 (Berkeley) 6/4/93
35 .\"
36 .Dd September 18, 2008
37 .Dt RENAME 2
38 .Os BSD 4.2
39 .Sh NAME
40 .Nm rename ,
41 .Nm renameat ,
42 .Nm renamex_np ,
43 .Nm renameatx_np
44 .Nd change the name of a file
45 .Sh SYNOPSIS
46 .Fd #include <stdio.h>
47 .Ft int
48 .Fo rename
49 .Fa "const char *old"
50 .Fa "const char *new"
51 .Fc
52 .Ft int
53 .Fn renameat "int fromfd" "const char *from" "int tofd" "const char *to"
54 .Ft int
55 .Fn renamex_np "const char *from" "const char *to" "unsigned int flags"
56 .Ft int
57 .Fn renameatx_np "int fromfd" "const char *from" "int tofd" "const char *to" "unsigned int flags"
58 .Sh DESCRIPTION
59 The
60 .Fn rename
61 system call causes the link named
62 .Fa old
63 to be renamed as
64 .Fa new .
65 If
66 .Fa new
67 exists, it is first removed.
68 Both
69 .Fa old
70 and
71 .Fa new
72 must be of the same type
73 (that is, both must be either directories or non-directories)
74 and must reside on the same file system.
75 .Pp
76 The
77 .Fn rename
78 system call guarantees that an instance of
79 .Fa new
80 will always exist,
81 even if the system should crash in the middle of the operation.
82 .Pp
83 If the final component of
84 .Fa old
85 is a symbolic link,
86 the symbolic link is renamed,
87 not the file or directory to which it points.
88 .Pp
89 The
90 .Fn renameat
91 system call is equivalent to
92 .Fn rename
93 except in the case where either
94 .Fa from
95 or
96 .Fa to
97 specifies a relative path.
98 If
99 .Fa from
100 is a relative path, the file to be renamed is located
101 relative to the directory associated with the file descriptor
102 .Fa fromfd
103 instead of the current working directory.
104 If the
105 .Fa to
106 is a relative path, the same happens only relative to the directory associated
107 with
108 .Fa tofd .
109 If the
110 .Fn renameat
111 is passed the special value
112 .Dv AT_FDCWD
113 in the
114 .Fa fromfd
115 or
116 .Fa tofd
117 parameter, the current working directory is used in the determination
118 of the file for the respective path parameter.
119 .Pp
120 The
121 .Fn renamex_np
122 and
123 .Fn renameatx_np
124 system calls are similar to their counterparts except that they take a
125 .Fa flags
126 argument.
127 Values for
128 .Fa flags
129 are constructed with below bits set:
130 .Bl -tag -offset indent
131 .It Dv RENAME_SWAP
132 On file systems that support it (see
133 .Xr getattrlist 2
134 .Dv VOL_CAP_INT_RENAME_SWAP Ns ),
135 it will cause the source and target to be atomically swapped. Source and target need not be of
136 the same type, i.e. it is possible to swap a file with a directory.
137 EINVAL is returned in case of bitwise-inclusive OR with
138 .Dv RENAME_EXCL .
139 .It Dv RENAME_EXCL
140 On file systems that support it (see
141 .Xr getattrlist 2
142 .Dv VOL_CAP_INT_RENAME_EXCL Ns ),
143 it will cause
144 .Dv EEXIST
145 to be returned if the destination already exists. EINVAL is returned in case of bitwise-inclusive OR with
146 .Dv RENAME_SWAP .
147 .El
148 .Sh CAVEATS
149 The system can deadlock if a loop is present in the file system graph.
150 This loop takes the form of an entry in directory
151 .Ql Pa a ,
152 say
153 .Ql Pa a/foo ,
154 being a hard link to directory
155 .Ql Pa b ,
156 and an entry in
157 directory
158 .Ql Pa b ,
159 say
160 .Ql Pa b/bar ,
161 being a hard link
162 to directory
163 .Ql Pa a .
164 When such a loop exists and two separate processes attempt to
165 perform
166 .Ql rename a/foo b/bar
167 and
168 .Ql rename b/bar a/foo ,
169 respectively,
170 the system may deadlock attempting to lock
171 both directories for modification.
172 .Pp
173 Whether or not hard links to directories are supported is specific to
174 the underlying filesystem implementation.
175 .Pp
176 It is recommended that any hard links to directories in an underlying
177 filesystem should be replaced by symbolic links by the system administrator
178 to avoid the possibility of deadlocks.
179 .Pp
180 Moving or renaming a file or directory into a directory with inheritable ACLs does not result in ACLs being set on the file or directory. Use
181 .Xr acl 3
182 in conjunction with
183 .Fn rename
184 to set ACLs on the file or directory.
185 .Sh RETURN VALUES
186 A 0 value is returned if the operation succeeds, otherwise
187 .Fn rename
188 returns -1 and the global variable
189 .Va errno
190 indicates the reason for the failure.
191 .Sh ERRORS
192 The
193 .Fn rename
194 system call will fail and neither of the argument files will be
195 affected if:
196 .Bl -tag -width Er
197 .\" ===========
198 .It Bq Er EACCES
199 A component of either path prefix denies search permission.
200 .\" ===========
201 .It Bq Er EACCES
202 The requested operation requires writing in a directory
203 (e.g.,
204 .Fa new ,
205 new/.., or old/..) whose modes disallow this.
206 .\" ===========
207 .It Bq Er EDQUOT
208 The directory in which the entry for the new name
209 is being placed cannot be extended because the
210 user's quota of disk blocks on the file system
211 containing the directory has been exhausted.
212 .\" ===========
213 .It Bq Er EEXIST
214 .Fa flags
215 has
216 .Dv RENAME_EXCL
217 set but
218 .Fa new
219 already exists.
220 .\" ===========
221 .It Bq Er EFAULT
222 .Em Path
223 points outside the process's allocated address space.
224 .\" ===========
225 .It Bq Er EINVAL
226 .Fa Old
227 is a parent directory of
228 .Fa new ,
229 or an attempt is made to rename
230 .Ql \&.
231 or
232 .Ql \&.. .
233 If
234 .Dv RENAME_SWAP
235 is used, then
236 .Dv EINVAL
237 will also be returned if
238 .Fa new
239 is a parent directory of
240 .Fa old .
241 If both RENAME_SWAP and RENAME_EXCL bits are set in
242 .Fa flags ,
243 then
244 .Dv EINVAL
245 will be returned.
246 .\" ===========
247 .It Bq Er EINVAL
248 .Fa flags
249 has an invalid value.
250 .\" ===========
251 .It Bq Er EIO
252 An I/O error occurs while making or updating a directory entry.
253 .\" ===========
254 .It Bq Er EISDIR
255 .Fa new
256 is a directory, but
257 .Fa old
258 is not a directory.
259 .\" ===========
260 .It Bq Er ELOOP
261 Too many symbolic links are encountered in translating either pathname.
262 This is taken to be indicative of a looping symbolic link.
263 .\" ===========
264 .It Bq Er ENAMETOOLONG
265 A component of a pathname exceeds
266 .Dv {NAME_MAX}
267 characters, or an entire path name exceeds
268 .Dv {PATH_MAX}
269 characters.
270 .\" ===========
271 .It Bq Er ENOENT
272 A component of the
273 .Fa old
274 path does not exist,
275 or a path prefix of
276 .Fa new
277 does not exist.
278 .\" ===========
279 .It Bq Er ENOENT
280 .Fa flags
281 has
282 .Dv RENAME_SWAP
283 set but
284 .Fa new
285 does not exist.
286 .\" ===========
287 .It Bq Er ENOSPC
288 The directory in which the entry for the new name is being placed
289 cannot be extended because there is no space left on the file
290 system containing the directory.
291 .\" ===========
292 .It Bq Er ENOTDIR
293 A component of either path prefix is not a directory.
294 .\" ===========
295 .It Bq Er ENOTDIR
296 .Fa old
297 is a directory, but
298 .Fa new
299 is not a directory.
300 .\" ===========
301 .It Bq Er ENOTEMPTY
302 .Fa New
303 is a directory and is not empty.
304 .\" ===========
305 .It Bq Er ENOTSUP
306 .Fa flags
307 has a value that is not supported by the file system.
308 .\" ===========
309 .It Bq Er EPERM
310 The directory containing
311 .Fa old
312 is marked sticky,
313 and neither the containing directory nor
314 .Fa old
315 are owned by the effective user ID.
316 .\" ===========
317 .It Bq Er EPERM
318 The
319 .Fa new
320 file exists,
321 the directory containing
322 .Fa new
323 is marked sticky,
324 and neither the containing directory nor
325 .Fa new
326 are owned by the effective user ID.
327 .\" ===========
328 .It Bq Er EROFS
329 The requested link requires writing in a directory on a read-only file
330 system.
331 .\" ===========
332 .It Bq Er EXDEV
333 The link named by
334 .Fa new
335 and the file named by
336 .Fa old
337 are on different logical devices (file systems).
338 Note that this error code will not be returned
339 if the implementation permits cross-device links.
340 .El
341 .Pp
342 The
343 .Fn renameat
344 and
345 .Fn renameatx_np
346 calls may also fail with:
347 .Bl -tag -width Er
348 .It Bq Er EBADF
349 The
350 .Fa from
351 argument does not specify an absolute path and the
352 .Fa fromfd
353 argument is neither
354 .Dv AT_FDCWD
355 nor a valid file descriptor open for searching, or the
356 .Fa to
357 argument does not specify an absolute path and the
358 .Fa tofd
359 argument is neither
360 .Dv AT_FDCWD
361 nor a valid file descriptor open for searching.
362 .It Bq Er ENOTDIR
363 The
364 .Fa from
365 argument is not an absolute path and
366 .Fa fromfd
367 is neither
368 .Dv AT_FDCWD
369 nor a file descriptor associated with a directory, or the
370 .Fa to
371 argument is not an absolute path and
372 .Fa tofd
373 is neither
374 .Dv AT_FDCWD
375 nor a file descriptor associated with a directory.
376 .El
377 .Sh CONFORMANCE
378 The restriction on renaming a directory whose permissions disallow writing
379 is based on the fact that UFS directories contain a ".." entry.
380 If renaming a directory would move it to another parent directory,
381 this entry needs to be changed.
382 .Pp
383 This restriction has been generalized to disallow renaming
384 of any write-disabled directory,
385 even when this would not require a change to the ".." entry.
386 For consistency, HFS+ directories emulate this behavior.
387 .Sh SEE ALSO
388 .Xr open 2 ,
389 .Xr symlink 7
390 .Sh STANDARDS
391 The
392 .Fn rename
393 function conforms to
394 .St -p1003.1-88 .
395 The
396 .Fn renameat
397 system call is expected to conform to POSIX.1-2008 .