xref: /openbsd-src/lib/libc/sys/rename.2 (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1.\"	$OpenBSD: rename.2,v 1.8 2000/10/18 05:12:11 aaron Exp $
2.\"	$NetBSD: rename.2,v 1.7 1995/02/27 12:36:15 cgd Exp $
3.\"
4.\" Copyright (c) 1983, 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. All advertising materials mentioning features or use of this software
16.\"    must display the following acknowledgement:
17.\"	This product includes software developed by the University of
18.\"	California, Berkeley and its contributors.
19.\" 4. Neither the name of the University nor the names of its contributors
20.\"    may be used to endorse or promote products derived from this software
21.\"    without specific prior written permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.\"     @(#)rename.2	8.1 (Berkeley) 6/4/93
36.\"
37.Dd June 4, 1993
38.Dt RENAME 2
39.Os
40.Sh NAME
41.Nm rename
42.Nd change the name of a file
43.Sh SYNOPSIS
44.Fd #include <stdio.h>
45.Ft int
46.Fn rename "const char *from" "const char *to"
47.Sh DESCRIPTION
48.Fn rename
49causes the link named
50.Fa from
51to be renamed as
52.Fa to .
53If
54.Fa to
55exists, it is first removed.
56Both
57.Fa from
58and
59.Fa to
60must be of the same type (that is, both directories or both
61non-directories), and must reside on the same file system.
62.Pp
63.Fn rename
64guarantees that an instance of
65.Fa to
66will always exist, even if the system should crash in
67the middle of the operation.
68.Pp
69If the final component of
70.Fa from
71is a symbolic link,
72the symbolic link is renamed,
73not the file or directory to which it points.
74.Sh RETURN VALUES
75A 0 value is returned if the operation succeeds, otherwise
76.Fn rename
77returns \-1 and the global variable
78.Va errno
79indicates the reason for the failure.
80.Sh ERRORS
81.Fn rename
82will fail and neither of the argument files will be
83affected if:
84.Bl -tag -width Er
85.It Bq Er ENAMETOOLONG
86A component of a pathname exceeded
87.Dv {NAME_MAX}
88characters, or an entire path name exceeded
89.Dv {PATH_MAX}
90characters.
91.It Bq Er ENOENT
92A component of the
93.Fa from
94path does not exist,
95or a path prefix of
96.Fa to
97does not exist.
98.It Bq Er EACCES
99A component of either path prefix denies search permission.
100.It Bq Er EACCES
101The requested link requires writing in a directory with a mode
102that denies write permission.
103.It Bq Er EPERM
104The directory containing
105.Fa from
106is marked sticky,
107and neither the containing directory nor
108.Fa from
109are owned by the effective user ID.
110.It Bq Er EPERM
111The
112.Fa to
113file exists,
114the directory containing
115.Fa to
116is marked sticky,
117and neither the containing directory nor
118.Fa to
119are owned by the effective user ID.
120.It Bq Er ELOOP
121Too many symbolic links were encountered in translating either pathname.
122.It Bq Er EMLINK
123The link count on the source file or destination directory is at the maximum.
124A rename cannot be completed under these conditions.
125.It Bq Er ENOTDIR
126A component of either path prefix is not a directory.
127.It Bq Er ENOTDIR
128.Fa from
129is a directory, but
130.Fa to
131is not a directory.
132.It Bq Er EISDIR
133.Fa to
134is a directory, but
135.Fa from
136is not a directory.
137.It Bq Er EXDEV
138The link named by
139.Fa to
140and the file named by
141.Fa from
142are on different logical devices (file systems).
143Note that this error code will not be returned if the implementation
144permits cross-device links.
145.It Bq Er ENOSPC
146The directory in which the entry for the new name is being placed
147cannot be extended because there is no space left on the file
148system containing the directory.
149.It Bq Er EDQUOT
150The directory in which the entry for the new name
151is being placed cannot be extended because the
152user's quota of disk blocks on the file system
153containing the directory has been exhausted.
154.It Bq Er EIO
155An I/O error occurred while making or updating a directory entry.
156.It Bq Er EROFS
157The requested link requires writing in a directory on a read-only file
158system.
159.It Bq Er EFAULT
160.Em Path
161points outside the process's allocated address space.
162.It Bq Er EINVAL
163.Fa from
164is a parent directory of
165.Fa to ,
166or an attempt is made to rename
167.Ql \&.
168or
169.Ql \&.. .
170.It Bq Er ENOTEMPTY
171.Fa to
172is a directory and is not empty.
173.El
174.Sh SEE ALSO
175.Xr open 2 ,
176.Xr symlink 7
177.Sh STANDARDS
178The
179.Fn rename
180function conforms to
181.St -p1003.1-88 .
182.Sh CAVEATS
183The system can deadlock if a loop in the file system graph is present.
184This loop takes the form of an entry in directory
185.Ql Pa a ,
186say
187.Ql Pa a/foo ,
188being a hard link to directory
189.Ql Pa b ,
190and an entry in
191directory
192.Ql Pa b ,
193say
194.Ql Pa b/bar ,
195being a hard link
196to directory
197.Ql Pa a .
198When such a loop exists and two separate processes attempt to
199perform
200.Ql rename a/foo b/bar
201and
202.Ql rename b/bar a/foo ,
203respectively,
204the system may deadlock attempting to lock
205both directories for modification.
206Hard links to directories should be
207replaced by symbolic links by the system administrator.
208