OSDN Git Service

(split) LDP: Update original to LDP v3.40.
[linuxjm/LDP_man-pages.git] / original / man2 / rename.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
4 .\"                               1993 Michael Haardt;
5 .\"                          1993,1995 Ian Jackson.
6 .\"
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
10 .\"
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
15 .\"
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein.  The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
22 .\" professionally.
23 .\"
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
26 .\"
27 .\" Modified Sat Jul 24 00:35:52 1993 by Rik Faith <faith@cs.unc.edu>
28 .\" Modified Thu Jun  4 12:21:13 1998 by Andries Brouwer <aeb@cwi.nl>
29 .\" Modified Thu Mar  3 09:49:35 2005 by Michael Haardt <michael@moria.de>
30 .\" 2007-03-25, mtk, added various text to DESCRIPTION.
31 .\"
32 .TH RENAME 2 2009-03-30 "Linux" "Linux Programmer's Manual"
33 .SH NAME
34 rename \- change the name or location of a file
35 .SH SYNOPSIS
36 .B #include <stdio.h>
37 .sp
38 .BI "int rename(const char *" oldpath ", const char *" newpath );
39 .SH DESCRIPTION
40 .BR rename ()
41 renames a file, moving it between directories if required.
42 Any other hard links to the file (as created using
43 .BR link (2))
44 are unaffected.
45 Open file descriptors for
46 .I oldpath
47 are also unaffected.
48
49 If
50 .I newpath
51 already exists it will be atomically replaced (subject to
52 a few conditions; see ERRORS below), so that there is
53 no point at which another process attempting to access
54 .I newpath
55 will find it missing.
56
57 If
58 .I oldpath
59 and
60 .I newpath
61 are existing hard links referring to the same file, then
62 .BR rename ()
63 does nothing, and returns a success status.
64
65 If
66 .I newpath
67 exists but the operation fails for some reason
68 .BR rename ()
69 guarantees to leave an instance of
70 .I newpath
71 in place.
72
73 .I oldpath
74 can specify a directory.
75 In this case,
76 .I newpath
77 must either not exist, or it must specify an empty directory.
78
79 However, when overwriting there will probably be a window in which
80 both
81 .I oldpath
82 and
83 .I newpath
84 refer to the file being renamed.
85
86 If
87 .I oldpath
88 refers to a symbolic link the link is renamed; if
89 .I newpath
90 refers to a symbolic link the link will be overwritten.
91 .SH "RETURN VALUE"
92 On success, zero is returned.
93 On error, \-1 is returned, and
94 .I errno
95 is set appropriately.
96 .SH ERRORS
97 .TP
98 .B EACCES
99 Write permission is denied for the directory containing
100 .I oldpath
101 or
102 .IR newpath ,
103 or, search permission is denied for one of the directories
104 in the path prefix of
105 .I oldpath
106 or
107 .IR newpath ,
108 or
109 .I oldpath
110 is a directory and does not allow write permission (needed to update
111 the
112 .I ..
113 entry).
114 (See also
115 .BR path_resolution (7).)
116 .TP
117 .B EBUSY
118 The rename fails because
119 .IR oldpath " or " newpath
120 is a directory that is in use by some process (perhaps as
121 current working directory, or as root directory, or because
122 it was open for reading) or is in use by the system
123 (for example as mount point), while the system considers
124 this an error.
125 (Note that there is no requirement to return
126 .B EBUSY
127 in such
128 cases\(emthere is nothing wrong with doing the rename anyway\(embut
129 it is allowed to return
130 .B EBUSY
131 if the system cannot otherwise
132 handle such situations.)
133 .TP
134 .B EFAULT
135 .IR oldpath " or " newpath " points outside your accessible address space."
136 .TP
137 .B EINVAL
138 The new pathname contained a path prefix of the old, or, more generally,
139 an attempt was made to make a directory a subdirectory of itself.
140 .TP
141 .B EISDIR
142 .I newpath
143 is an existing directory, but
144 .I oldpath
145 is not a directory.
146 .TP
147 .B ELOOP
148 Too many symbolic links were encountered in resolving
149 .IR oldpath " or " newpath .
150 .TP
151 .B EMLINK
152 .I oldpath
153 already has the maximum number of links to it, or
154 it was a directory and the directory containing
155 .I newpath
156 has the maximum number of links.
157 .TP
158 .B ENAMETOOLONG
159 .IR oldpath " or " newpath " was too long."
160 .TP
161 .B ENOENT
162 The link named by
163 .I oldpath
164 does not exist;
165 or, a directory component in
166 .I newpath
167 does not exist;
168 or,
169 .I oldpath
170 or
171 .I newpath
172 is an empty string.
173 .TP
174 .B ENOMEM
175 Insufficient kernel memory was available.
176 .TP
177 .B ENOSPC
178 The device containing the file has no room for the new directory
179 entry.
180 .TP
181 .B ENOTDIR
182 A component used as a directory in
183 .IR oldpath " or " newpath
184 is not, in fact, a directory.
185 Or,
186 .I oldpath
187 is a directory, and
188 .I newpath
189 exists but is not a directory.
190 .TP
191 .BR ENOTEMPTY " or " EEXIST
192 .I newpath
193 is a nonempty directory, that is, contains entries other than "." and "..".
194 .TP
195 .BR EPERM " or " EACCES
196 The directory containing
197 .I oldpath
198 has the sticky bit
199 .RB ( S_ISVTX )
200 set and the process's effective user ID is neither
201 the user ID of the file to be deleted nor that of the directory
202 containing it, and the process is not privileged
203 (Linux: does not have the
204 .B CAP_FOWNER
205 capability);
206 or
207 .I newpath
208 is an existing file and the directory containing it has the sticky bit set
209 and the process's effective user ID is neither the user ID of the file
210 to be replaced nor that of the directory containing it,
211 and the process is not privileged
212 (Linux: does not have the
213 .B CAP_FOWNER
214 capability);
215 or the file system containing
216 .I pathname
217 does not support renaming of the type requested.
218 .TP
219 .B EROFS
220 The file is on a read-only file system.
221 .TP
222 .B EXDEV
223 .IR oldpath " and " newpath
224 are not on the same mounted file system.
225 (Linux permits a file system to be mounted at multiple points, but
226 .BR rename ()
227 does not work across different mount points,
228 even if the same file system is mounted on both.)
229 .SH "CONFORMING TO"
230 4.3BSD, C89, C99, POSIX.1-2001.
231 .SH BUGS
232 On NFS file systems, you can not assume that if the operation
233 failed the file was not renamed.
234 If the server does the rename operation
235 and then crashes, the retransmitted RPC which will be processed when the
236 server is up again causes a failure.
237 The application is expected to
238 deal with this.
239 See
240 .BR link (2)
241 for a similar problem.
242 .SH "SEE ALSO"
243 .BR mv (1),
244 .BR chmod (2),
245 .BR link (2),
246 .BR renameat (2),
247 .BR symlink (2),
248 .BR unlink (2),
249 .BR path_resolution (7),
250 .BR symlink (7)