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