]> git.saurik.com Git - apple/xnu.git/blame - bsd/man/man2/rename.2
xnu-2782.30.5.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
A
40.Nm rename ,
41.Nm renameat
9bccf70c
A
42.Nd change the name of a file
43.Sh SYNOPSIS
44.Fd #include <stdio.h>
45.Ft int
2d21ac55
A
46.Fo rename
47.Fa "const char *old"
48.Fa "const char *new"
49.Fc
fe8ab488
A
50.Ft int
51.Fn renameat "int fromfd" "const char *from" "int tofd" "const char *to"
9bccf70c 52.Sh DESCRIPTION
2d21ac55
A
53The
54.Fn rename
55system call causes the link named
56.Fa old
9bccf70c 57to be renamed as
2d21ac55 58.Fa new .
9bccf70c 59If
2d21ac55 60.Fa new
9bccf70c
A
61exists, it is first removed.
62Both
2d21ac55 63.Fa old
9bccf70c 64and
2d21ac55
A
65.Fa new
66must be of the same type
67(that is, both must be either directories or non-directories)
68and must reside on the same file system.
9bccf70c 69.Pp
2d21ac55
A
70The
71.Fn rename
72system call guarantees that an instance of
73.Fa new
74will always exist,
75even if the system should crash in the middle of the operation.
9bccf70c
A
76.Pp
77If the final component of
2d21ac55 78.Fa old
9bccf70c
A
79is a symbolic link,
80the symbolic link is renamed,
81not the file or directory to which it points.
fe8ab488
A
82.Pp
83The
84.Fn renameat
85system call is equivalent to
86.Fn rename
87except in the case where either
88.Fa from
89or
90.Fa to
91specifies a relative path.
92If
93.Fa from
94is a relative path, the file to be renamed is located
95relative to the directory associated with the file descriptor
96.Fa fromfd
97instead of the current working directory.
98If the
99.Fa to
100is a relative path, the same happens only relative to the directory associated
101with
102.Fa tofd .
103If the
104.Fn renameat
105is passed the special value
106.Dv AT_FDCWD
107in the
108.Fa fromfd
109or
110.Fa tofd
111parameter, the current working directory is used in the determination
112of the file for the respective path parameter.
b0d623f7 113.Sh CAVEATS
2d21ac55 114The system can deadlock if a loop is present in the file system graph.
9bccf70c
A
115This loop takes the form of an entry in directory
116.Ql Pa a ,
117say
118.Ql Pa a/foo ,
119being a hard link to directory
120.Ql Pa b ,
121and an entry in
122directory
123.Ql Pa b ,
124say
125.Ql Pa b/bar ,
126being a hard link
127to directory
128.Ql Pa a .
129When such a loop exists and two separate processes attempt to
130perform
131.Ql rename a/foo b/bar
132and
133.Ql rename b/bar a/foo ,
134respectively,
135the system may deadlock attempting to lock
136both directories for modification.
2d21ac55
A
137.Pp
138Whether or not hard links to directories are supported is specific to
139the underlying filesystem implementation.
140.Pp
141It is recommended that any hard links to directories in an underlying
142filesystem should be replaced by symbolic links by the system administrator
143to avoid the possibility of deadlocks.
b0d623f7
A
144.Pp
145Moving 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
146.Xr acl 3
147in conjunction with
148.Fn rename
149to set ACLs on the file or directory.
9bccf70c
A
150.Sh RETURN VALUES
151A 0 value is returned if the operation succeeds, otherwise
152.Fn rename
153returns -1 and the global variable
154.Va errno
155indicates the reason for the failure.
156.Sh ERRORS
2d21ac55
A
157The
158.Fn rename
159system call will fail and neither of the argument files will be
9bccf70c
A
160affected if:
161.Bl -tag -width Er
2d21ac55
A
162.\" ===========
163.It Bq Er EACCES
164A component of either path prefix denies search permission.
165.\" ===========
166.It Bq Er EACCES
167The requested operation requires writing in a directory
168(e.g.,
169.Fa new ,
170new/.., or old/..) whose modes disallow this.
171.\" ===========
172.It Bq Er EDQUOT
173The directory in which the entry for the new name
174is being placed cannot be extended because the
175user's quota of disk blocks on the file system
176containing the directory has been exhausted.
177.\" ===========
178.It Bq Er EFAULT
179.Em Path
180points outside the process's allocated address space.
181.\" ===========
182.It Bq Er EINVAL
183.Fa Old
184is a parent directory of
185.Fa new ,
186or an attempt is made to rename
187.Ql \&.
188or
189.Ql \&.. .
190.\" ===========
191.It Bq Er EIO
192An I/O error occurs while making or updating a directory entry.
193.\" ===========
194.It Bq Er EISDIR
195.Fa new
196is a directory, but
197.Fa old
198is not a directory.
199.\" ===========
200.It Bq Er ELOOP
201Too many symbolic links are encountered in translating either pathname.
202This is taken to be indicative of a looping symbolic link.
203.\" ===========
9bccf70c 204.It Bq Er ENAMETOOLONG
2d21ac55 205A component of a pathname exceeds
9bccf70c 206.Dv {NAME_MAX}
2d21ac55 207characters, or an entire path name exceeds
9bccf70c
A
208.Dv {PATH_MAX}
209characters.
2d21ac55 210.\" ===========
9bccf70c
A
211.It Bq Er ENOENT
212A component of the
2d21ac55 213.Fa old
9bccf70c
A
214path does not exist,
215or a path prefix of
2d21ac55 216.Fa new
9bccf70c 217does not exist.
2d21ac55
A
218.\" ===========
219.It Bq Er ENOSPC
220The directory in which the entry for the new name is being placed
221cannot be extended because there is no space left on the file
222system containing the directory.
223.\" ===========
224.It Bq Er ENOTDIR
225A component of either path prefix is not a directory.
226.\" ===========
227.It Bq Er ENOTDIR
228.Fa old
229is a directory, but
230.Fa new
231is not a directory.
232.\" ===========
233.It Bq Er ENOTEMPTY
234.Fa New
235is a directory and is not empty.
236.\" ===========
9bccf70c
A
237.It Bq Er EPERM
238The directory containing
2d21ac55 239.Fa old
9bccf70c
A
240is marked sticky,
241and neither the containing directory nor
2d21ac55 242.Fa old
9bccf70c 243are owned by the effective user ID.
2d21ac55 244.\" ===========
9bccf70c
A
245.It Bq Er EPERM
246The
2d21ac55 247.Fa new
9bccf70c
A
248file exists,
249the directory containing
2d21ac55 250.Fa new
9bccf70c
A
251is marked sticky,
252and neither the containing directory nor
2d21ac55 253.Fa new
9bccf70c 254are owned by the effective user ID.
2d21ac55 255.\" ===========
9bccf70c
A
256.It Bq Er EROFS
257The requested link requires writing in a directory on a read-only file
258system.
2d21ac55
A
259.\" ===========
260.It Bq Er EXDEV
261The link named by
262.Fa new
263and the file named by
264.Fa old
265are on different logical devices (file systems).
266Note that this error code will not be returned
267if the implementation permits cross-device links.
9bccf70c 268.El
fe8ab488
A
269.Pp
270In addition to the errors returned by the
271.Fn rename ,
272the
273.Fn renameat
274may fail if:
275.Bl -tag -width Er
276.It Bq Er EBADF
277The
278.Fa from
279argument does not specify an absolute path and the
280.Fa fromfd
281argument is neither
282.Dv AT_FDCWD
283nor a valid file descriptor open for searching, or the
284.Fa to
285argument does not specify an absolute path and the
286.Fa tofd
287argument is neither
288.Dv AT_FDCWD
289nor a valid file descriptor open for searching.
290.It Bq Er ENOTDIR
291The
292.Fa from
293argument is not an absolute path and
294.Fa fromfd
295is neither
296.Dv AT_FDCWD
297nor a file descriptor associated with a directory, or the
298.Fa to
299argument is not an absolute path and
300.Fa tofd
301is neither
302.Dv AT_FDCWD
303nor a file descriptor associated with a directory.
2d21ac55
A
304.Sh CONFORMANCE
305The restriction on renaming a directory whose permissions disallow writing
306is based on the fact that UFS directories contain a ".." entry.
307If renaming a directory would move it to another parent directory,
308this entry needs to be changed.
309.Pp
310This restriction has been generalized to disallow renaming
311of any write-disabled directory,
312even when this would not require a change to the ".." entry.
313For consistency, HFS+ directories emulate this behavior.
9bccf70c 314.Sh SEE ALSO
2d21ac55 315.Xr open 2 ,
9bccf70c
A
316.Xr symlink 7
317.Sh STANDARDS
318The
319.Fn rename
320function conforms to
321.St -p1003.1-88 .
fe8ab488
A
322The
323.Fn renameat
324system call is expected to conform to POSIX.1-2008 .