xref: /netbsd-src/lib/libc/sys/rename.2 (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1.\"	$NetBSD: rename.2,v 1.13 2001/05/17 23:45:24 matt 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.\"
36.Dd June 4, 1993
37.Dt RENAME 2
38.Os
39.Sh NAME
40.Nm rename
41.Nd change the name of a file
42.Sh LIBRARY
43.Lb libc
44.Sh SYNOPSIS
45.Fd #include <stdio.h>
46.Ft int
47.Fn rename "const char *from" "const char *to"
48.Sh DESCRIPTION
49.Fn rename
50causes the link named
51.Fa from
52to be renamed as
53.Fa to .
54If
55.Fa to
56exists, it is first removed.
57Both
58.Fa from
59and
60.Fa to
61must be of the same type (that is, both directories or both
62non-directories), and must reside on the same file system.
63.Pp
64.Fn rename
65guarantees that an instance of
66.Fa to
67will always exist, even if the system should crash in
68the middle of the operation.
69.Pp
70If the final component of
71.Fa from
72is a symbolic link,
73the symbolic link is renamed,
74not the file or directory to which it points.
75.Pp
76If both
77.Fa from
78and
79.Fa to
80are pathnames of the same existing file in the file system's name space,
81.Fn rename
82returns successfully and performs no other action.
83.Sh CAVEAT
84The system can deadlock if a loop in the file system graph is present.
85This loop takes the form of an entry in directory
86.Ql Pa a ,
87say
88.Ql Pa a/foo ,
89being a hard link to directory
90.Ql Pa b ,
91and an entry in
92directory
93.Ql Pa b ,
94say
95.Ql Pa b/bar ,
96being a hard link
97to directory
98.Ql Pa a .
99When such a loop exists and two separate processes attempt to
100perform
101.Ql rename a/foo b/bar
102and
103.Ql rename b/bar a/foo ,
104respectively,
105the system may deadlock attempting to lock
106both directories for modification.
107Hard links to directories should be
108replaced by symbolic links by the system administrator.
109.Sh RETURN VALUES
110A 0 value is returned if the operation succeeds, otherwise
111.Fn rename
112returns -1 and the global variable
113.Va errno
114indicates the reason for the failure.
115.Sh ERRORS
116.Fn rename
117will fail and neither of the argument files will be
118affected if:
119.Bl -tag -width Er
120.It Bq Er ENAMETOOLONG
121A component of a pathname exceeded
122.Dv {NAME_MAX}
123characters, or an entire path name exceeded
124.Dv {PATH_MAX}
125characters.
126.It Bq Er ENOENT
127A component of the
128.Fa from
129path does not exist,
130or a path prefix of
131.Fa to
132does not exist.
133.It Bq Er EACCES
134A component of either path prefix denies search permission, or
135the requested link requires writing in a directory with a mode
136that denies write permission.
137.It Bq Er EPERM
138The directory containing
139.Fa from
140is marked sticky,
141and neither the containing directory nor
142.Fa from
143are owned by the effective user ID.
144Or the
145.Fa to
146file exists,
147the directory containing
148.Fa to
149is marked sticky,
150and neither the containing directory nor
151.Fa to
152are owned by the effective user ID.
153.It Bq Er ELOOP
154Too many symbolic links were encountered in translating either pathname.
155.It Bq Er ENOTDIR
156A component of either path prefix is not a directory, or
157.Fa from
158is a directory, but
159.Fa to
160is not a directory.
161.It Bq Er EISDIR
162.Fa to
163is a directory, but
164.Fa from
165is not a directory.
166.It Bq Er EXDEV
167The link named by
168.Fa to
169and the file named by
170.Fa from
171are on different logical devices (file systems).  Note that this error
172code will not be returned if the implementation permits cross-device
173links.
174.It Bq Er ENOSPC
175The directory in which the entry for the new name is being placed
176cannot be extended because there is no space left on the file
177system containing the directory.
178.It Bq Er EDQUOT
179The directory in which the entry for the new name
180is being placed cannot be extended because the
181user's quota of disk blocks on the file system
182containing the directory has been exhausted.
183.It Bq Er EIO
184An I/O error occurred while making or updating a directory entry.
185.It Bq Er EROFS
186The requested link requires writing in a directory on a read-only file
187system.
188.It Bq Er EFAULT
189.Em Path
190points outside the process's allocated address space.
191.It Bq Er EINVAL
192.Fa from
193is a parent directory of
194.Fa to ,
195or an attempt is made to rename
196.Ql \&.
197or
198.Ql \&.. .
199.It Bq Er ENOTEMPTY
200.Fa to
201is a directory and is not empty.
202.El
203.Sh SEE ALSO
204.Xr open 2 ,
205.Xr symlink 7
206.Sh STANDARDS
207The
208.Fn rename
209function deviates from the semantics defined in
210.St -p1003.1-90 ,
211which specifies that if both
212.Fa from
213and
214.Fa to
215.Em link
216to the same existing file,
217.Fn rename
218shall return successfully and performs no further action, whereas this
219implementation will remove the file specified by
220.Fa from
221unless both
222.Fa from
223and
224.Fa to
225are pathnames of the same file in the file system's name space.
226.Pp
227To retain conformance, a compatibility interface is provided by the
228.Lb libposix
229which is also be brought into scope if any of the
230.Dv _POSIX_SOURCE ,
231.Dv _POSIX_C_SOURCE
232or
233.Dv _XOPEN_SOURCE
234preprocessor symbols are defined at compile-time:
235the
236.Fn rename
237function conforms to
238.St -p1003.1-90
239and
240.St -xpg4.2 .
241