]> git.saurik.com Git - apple/xnu.git/blame - bsd/man/man2/rename.2
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / man / man2 / rename.2
CommitLineData
9bccf70c
A
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.\"
b0d623f7 36.Dd September 18, 2008
9bccf70c
A
37.Dt RENAME 2
38.Os BSD 4.2
39.Sh NAME
fe8ab488 40.Nm rename ,
39037602
A
41.Nm renameat ,
42.Nm renamex_np ,
43.Nm renameatx_np
9bccf70c
A
44.Nd change the name of a file
45.Sh SYNOPSIS
46.Fd #include <stdio.h>
47.Ft int
2d21ac55
A
48.Fo rename
49.Fa "const char *old"
50.Fa "const char *new"
51.Fc
fe8ab488
A
52.Ft int
53.Fn renameat "int fromfd" "const char *from" "int tofd" "const char *to"
39037602
A
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"
9bccf70c 58.Sh DESCRIPTION
2d21ac55
A
59The
60.Fn rename
61system call causes the link named
62.Fa old
9bccf70c 63to be renamed as
2d21ac55 64.Fa new .
9bccf70c 65If
2d21ac55 66.Fa new
9bccf70c
A
67exists, it is first removed.
68Both
2d21ac55 69.Fa old
9bccf70c 70and
2d21ac55
A
71.Fa new
72must be of the same type
73(that is, both must be either directories or non-directories)
74and must reside on the same file system.
9bccf70c 75.Pp
2d21ac55
A
76The
77.Fn rename
78system call guarantees that an instance of
79.Fa new
80will always exist,
81even if the system should crash in the middle of the operation.
9bccf70c
A
82.Pp
83If the final component of
2d21ac55 84.Fa old
9bccf70c
A
85is a symbolic link,
86the symbolic link is renamed,
87not the file or directory to which it points.
fe8ab488
A
88.Pp
89The
90.Fn renameat
91system call is equivalent to
92.Fn rename
93except in the case where either
94.Fa from
95or
96.Fa to
97specifies a relative path.
98If
99.Fa from
100is a relative path, the file to be renamed is located
101relative to the directory associated with the file descriptor
102.Fa fromfd
103instead of the current working directory.
104If the
105.Fa to
106is a relative path, the same happens only relative to the directory associated
107with
108.Fa tofd .
109If the
110.Fn renameat
111is passed the special value
112.Dv AT_FDCWD
113in the
114.Fa fromfd
115or
116.Fa tofd
117parameter, the current working directory is used in the determination
118of the file for the respective path parameter.
39037602
A
119.Pp
120The
121.Fn renamex_np
122and
123.Fn renameatx_np
124system calls are similar to their counterparts except that they take a
125.Fa flags
126argument.
127Values for
128.Fa flags
129are constructed with below bits set:
130.Bl -tag -offset indent
131.It Dv RENAME_SWAP
132On file systems that support it (see
133.Xr getattrlist 2
134.Dv VOL_CAP_INT_RENAME_SWAP Ns ),
135it will cause the source and target to be atomically swapped. Source and target need not be of
136the same type, i.e. it is possible to swap a file with a directory.
137EINVAL is returned in case of bitwise-inclusive OR with
138.Dv RENAME_EXCL .
139.It Dv RENAME_EXCL
140On file systems that support it (see
141.Xr getattrlist 2
142.Dv VOL_CAP_INT_RENAME_EXCL Ns ),
143it will cause
144.Dv EEXIST
145to be returned if the destination already exists. EINVAL is returned in case of bitwise-inclusive OR with
146.Dv RENAME_SWAP .
147.El
b0d623f7 148.Sh CAVEATS
2d21ac55 149The system can deadlock if a loop is present in the file system graph.
9bccf70c
A
150This loop takes the form of an entry in directory
151.Ql Pa a ,
152say
153.Ql Pa a/foo ,
154being a hard link to directory
155.Ql Pa b ,
156and an entry in
157directory
158.Ql Pa b ,
159say
160.Ql Pa b/bar ,
161being a hard link
162to directory
163.Ql Pa a .
164When such a loop exists and two separate processes attempt to
165perform
166.Ql rename a/foo b/bar
167and
168.Ql rename b/bar a/foo ,
169respectively,
170the system may deadlock attempting to lock
171both directories for modification.
2d21ac55
A
172.Pp
173Whether or not hard links to directories are supported is specific to
174the underlying filesystem implementation.
175.Pp
176It is recommended that any hard links to directories in an underlying
177filesystem should be replaced by symbolic links by the system administrator
178to avoid the possibility of deadlocks.
b0d623f7
A
179.Pp
180Moving 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
182in conjunction with
183.Fn rename
184to set ACLs on the file or directory.
9bccf70c
A
185.Sh RETURN VALUES
186A 0 value is returned if the operation succeeds, otherwise
187.Fn rename
188returns -1 and the global variable
189.Va errno
190indicates the reason for the failure.
191.Sh ERRORS
2d21ac55
A
192The
193.Fn rename
194system call will fail and neither of the argument files will be
9bccf70c
A
195affected if:
196.Bl -tag -width Er
2d21ac55
A
197.\" ===========
198.It Bq Er EACCES
199A component of either path prefix denies search permission.
200.\" ===========
201.It Bq Er EACCES
202The requested operation requires writing in a directory
203(e.g.,
204.Fa new ,
205new/.., or old/..) whose modes disallow this.
206.\" ===========
cb323159
A
207.It Bq Er EACCES
208.Fa old
209is a directory and it, or some descendent in the namespace, is open
210and the file system format does does not support renaming a directory
211with open descendents (see
212.Xr getattrlist 2
213.Dv VOL_CAP_INT_RENAME_OPENFAIL Ns ).
214.\" ===========
2d21ac55
A
215.It Bq Er EDQUOT
216The directory in which the entry for the new name
217is being placed cannot be extended because the
218user's quota of disk blocks on the file system
219containing the directory has been exhausted.
220.\" ===========
39037602
A
221.It Bq Er EEXIST
222.Fa flags
223has
224.Dv RENAME_EXCL
225set but
226.Fa new
227already exists.
228.\" ===========
2d21ac55
A
229.It Bq Er EFAULT
230.Em Path
231points outside the process's allocated address space.
232.\" ===========
233.It Bq Er EINVAL
234.Fa Old
235is a parent directory of
236.Fa new ,
237or an attempt is made to rename
238.Ql \&.
239or
240.Ql \&.. .
39037602
A
241If
242.Dv RENAME_SWAP
243is used, then
244.Dv EINVAL
245will also be returned if
246.Fa new
247is a parent directory of
248.Fa old .
249If both RENAME_SWAP and RENAME_EXCL bits are set in
250.Fa flags ,
251then
252.Dv EINVAL
253will be returned.
254.\" ===========
255.It Bq Er EINVAL
256.Fa flags
257has an invalid value.
2d21ac55
A
258.\" ===========
259.It Bq Er EIO
260An I/O error occurs while making or updating a directory entry.
261.\" ===========
262.It Bq Er EISDIR
263.Fa new
264is a directory, but
265.Fa old
266is not a directory.
267.\" ===========
268.It Bq Er ELOOP
269Too many symbolic links are encountered in translating either pathname.
270This is taken to be indicative of a looping symbolic link.
271.\" ===========
9bccf70c 272.It Bq Er ENAMETOOLONG
2d21ac55 273A component of a pathname exceeds
9bccf70c 274.Dv {NAME_MAX}
2d21ac55 275characters, or an entire path name exceeds
9bccf70c
A
276.Dv {PATH_MAX}
277characters.
2d21ac55 278.\" ===========
9bccf70c
A
279.It Bq Er ENOENT
280A component of the
2d21ac55 281.Fa old
9bccf70c
A
282path does not exist,
283or a path prefix of
2d21ac55 284.Fa new
9bccf70c 285does not exist.
2d21ac55 286.\" ===========
39037602
A
287.It Bq Er ENOENT
288.Fa flags
289has
290.Dv RENAME_SWAP
291set but
292.Fa new
293does not exist.
294.\" ===========
2d21ac55
A
295.It Bq Er ENOSPC
296The directory in which the entry for the new name is being placed
297cannot be extended because there is no space left on the file
298system containing the directory.
299.\" ===========
300.It Bq Er ENOTDIR
301A component of either path prefix is not a directory.
302.\" ===========
303.It Bq Er ENOTDIR
304.Fa old
305is a directory, but
306.Fa new
307is not a directory.
308.\" ===========
309.It Bq Er ENOTEMPTY
310.Fa New
311is a directory and is not empty.
312.\" ===========
39037602
A
313.It Bq Er ENOTSUP
314.Fa flags
315has a value that is not supported by the file system.
316.\" ===========
9bccf70c
A
317.It Bq Er EPERM
318The directory containing
2d21ac55 319.Fa old
9bccf70c
A
320is marked sticky,
321and neither the containing directory nor
2d21ac55 322.Fa old
9bccf70c 323are owned by the effective user ID.
2d21ac55 324.\" ===========
9bccf70c
A
325.It Bq Er EPERM
326The
2d21ac55 327.Fa new
9bccf70c
A
328file exists,
329the directory containing
2d21ac55 330.Fa new
9bccf70c
A
331is marked sticky,
332and neither the containing directory nor
2d21ac55 333.Fa new
9bccf70c 334are owned by the effective user ID.
2d21ac55 335.\" ===========
9bccf70c
A
336.It Bq Er EROFS
337The requested link requires writing in a directory on a read-only file
338system.
2d21ac55
A
339.\" ===========
340.It Bq Er EXDEV
341The link named by
342.Fa new
343and the file named by
344.Fa old
345are on different logical devices (file systems).
346Note that this error code will not be returned
347if the implementation permits cross-device links.
9bccf70c 348.El
fe8ab488 349.Pp
39037602 350The
fe8ab488 351.Fn renameat
39037602
A
352and
353.Fn renameatx_np
354calls may also fail with:
fe8ab488
A
355.Bl -tag -width Er
356.It Bq Er EBADF
357The
358.Fa from
359argument does not specify an absolute path and the
360.Fa fromfd
361argument is neither
362.Dv AT_FDCWD
363nor a valid file descriptor open for searching, or the
364.Fa to
365argument does not specify an absolute path and the
366.Fa tofd
367argument is neither
368.Dv AT_FDCWD
369nor a valid file descriptor open for searching.
370.It Bq Er ENOTDIR
371The
372.Fa from
373argument is not an absolute path and
374.Fa fromfd
375is neither
376.Dv AT_FDCWD
377nor a file descriptor associated with a directory, or the
378.Fa to
379argument is not an absolute path and
380.Fa tofd
381is neither
382.Dv AT_FDCWD
383nor a file descriptor associated with a directory.
39037602 384.El
2d21ac55
A
385.Sh CONFORMANCE
386The restriction on renaming a directory whose permissions disallow writing
387is based on the fact that UFS directories contain a ".." entry.
388If renaming a directory would move it to another parent directory,
389this entry needs to be changed.
390.Pp
391This restriction has been generalized to disallow renaming
392of any write-disabled directory,
393even when this would not require a change to the ".." entry.
394For consistency, HFS+ directories emulate this behavior.
9bccf70c 395.Sh SEE ALSO
2d21ac55 396.Xr open 2 ,
9bccf70c
A
397.Xr symlink 7
398.Sh STANDARDS
399The
400.Fn rename
401function conforms to
402.St -p1003.1-88 .
fe8ab488
A
403The
404.Fn renameat
405system call is expected to conform to POSIX.1-2008 .