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