OSDN Git Service

(split) LDP: Update original to LDP v3.65
[linuxjm/LDP_man-pages.git] / original / man2 / rename.2
1 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
2 .\"             and Copyright (C) 1993 Michael Haardt;
3 .\"             and Copyright (C) 1993,1995 Ian Jackson
4 .\"             and Copyright (C) 2006, 2014 Michael Kerrisk
5 .\"
6 .\" %%%LICENSE_START(VERBATIM)
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 .\" %%%LICENSE_END
27 .\"
28 .\" Modified Sat Jul 24 00:35:52 1993 by Rik Faith <faith@cs.unc.edu>
29 .\" Modified Thu Jun  4 12:21:13 1998 by Andries Brouwer <aeb@cwi.nl>
30 .\" Modified Thu Mar  3 09:49:35 2005 by Michael Haardt <michael@moria.de>
31 .\" 2007-03-25, mtk, added various text to DESCRIPTION.
32 .\"
33 .TH RENAME 2 2014-02-21 "Linux" "Linux Programmer's Manual"
34 .SH NAME
35 rename, renameat \- change the name or location of a file
36 .SH SYNOPSIS
37 .nf
38 .B #include <stdio.h>
39 .sp
40 .BI "int rename(const char *" oldpath ", const char *" newpath );
41 .sp
42 .BR "#include <fcntl.h>           " "/* Definition of AT_* constants */"
43 .B #include <stdio.h>
44 .sp
45 .BI "int renameat(int " olddirfd ", const char *" oldpath ,
46 .BI "             int " newdirfd ", const char *" newpath );
47 .fi
48 .sp
49 .in -4n
50 Feature Test Macro Requirements for glibc (see
51 .BR feature_test_macros (7)):
52 .in
53 .sp
54 .BR renameat ():
55 .PD 0
56 .ad l
57 .RS 4
58 .TP 4
59 Since glibc 2.10:
60 _XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
61 .TP
62 Before glibc 2.10:
63 _ATFILE_SOURCE
64 .RE
65 .ad
66 .PD
67 .SH DESCRIPTION
68 .BR rename ()
69 renames a file, moving it between directories if required.
70 Any other hard links to the file (as created using
71 .BR link (2))
72 are unaffected.
73 Open file descriptors for
74 .I oldpath
75 are also unaffected.
76
77 If
78 .I newpath
79 already exists, it will be atomically replaced (subject to
80 a few conditions; see ERRORS below), so that there is
81 no point at which another process attempting to access
82 .I newpath
83 will find it missing.
84
85 If
86 .I oldpath
87 and
88 .I newpath
89 are existing hard links referring to the same file, then
90 .BR rename ()
91 does nothing, and returns a success status.
92
93 If
94 .I newpath
95 exists but the operation fails for some reason,
96 .BR rename ()
97 guarantees to leave an instance of
98 .I newpath
99 in place.
100
101 .I oldpath
102 can specify a directory.
103 In this case,
104 .I newpath
105 must either not exist, or it must specify an empty directory.
106
107 However, when overwriting there will probably be a window in which
108 both
109 .I oldpath
110 and
111 .I newpath
112 refer to the file being renamed.
113
114 If
115 .I oldpath
116 refers to a symbolic link, the link is renamed; if
117 .I newpath
118 refers to a symbolic link, the link will be overwritten.
119 .SS renameat ()
120 The
121 .BR renameat ()
122 system call operates in exactly the same way as
123 .BR rename (),
124 except for the differences described here.
125
126 If the pathname given in
127 .I oldpath
128 is relative, then it is interpreted relative to the directory
129 referred to by the file descriptor
130 .I olddirfd
131 (rather than relative to the current working directory of
132 the calling process, as is done by
133 .BR rename ()
134 for a relative pathname).
135
136 If
137 .I oldpath
138 is relative and
139 .I olddirfd
140 is the special value
141 .BR AT_FDCWD ,
142 then
143 .I oldpath
144 is interpreted relative to the current working
145 directory of the calling process (like
146 .BR rename ()).
147
148 If
149 .I oldpath
150 is absolute, then
151 .I olddirfd
152 is ignored.
153
154 The interpretation of
155 .I newpath
156 is as for
157 .IR oldpath ,
158 except that a relative pathname is interpreted relative
159 to the directory referred to by the file descriptor
160 .IR newdirfd .
161
162 See
163 .BR openat (2)
164 for an explanation of the need for
165 .BR renameat ().
166 .SH RETURN VALUE
167 On success, zero is returned.
168 On error, \-1 is returned, and
169 .I errno
170 is set appropriately.
171 .SH ERRORS
172 .TP
173 .B EACCES
174 Write permission is denied for the directory containing
175 .I oldpath
176 or
177 .IR newpath ,
178 or, search permission is denied for one of the directories
179 in the path prefix of
180 .I oldpath
181 or
182 .IR newpath ,
183 or
184 .I oldpath
185 is a directory and does not allow write permission (needed to update
186 the
187 .I ..
188 entry).
189 (See also
190 .BR path_resolution (7).)
191 .TP
192 .B EBUSY
193 The rename fails because
194 .IR oldpath " or " newpath
195 is a directory that is in use by some process (perhaps as
196 current working directory, or as root directory, or because
197 it was open for reading) or is in use by the system
198 (for example as mount point), while the system considers
199 this an error.
200 (Note that there is no requirement to return
201 .B EBUSY
202 in such
203 cases\(emthere is nothing wrong with doing the rename anyway\(embut
204 it is allowed to return
205 .B EBUSY
206 if the system cannot otherwise
207 handle such situations.)
208 .TP
209 .B EDQUOT
210 The user's quota of disk blocks on the filesystem has been exhausted.
211 .TP
212 .B EFAULT
213 .IR oldpath " or " newpath " points outside your accessible address space."
214 .TP
215 .B EINVAL
216 The new pathname contained a path prefix of the old, or, more generally,
217 an attempt was made to make a directory a subdirectory of itself.
218 .TP
219 .B EISDIR
220 .I newpath
221 is an existing directory, but
222 .I oldpath
223 is not a directory.
224 .TP
225 .B ELOOP
226 Too many symbolic links were encountered in resolving
227 .IR oldpath " or " newpath .
228 .TP
229 .B EMLINK
230 .I oldpath
231 already has the maximum number of links to it, or
232 it was a directory and the directory containing
233 .I newpath
234 has the maximum number of links.
235 .TP
236 .B ENAMETOOLONG
237 .IR oldpath " or " newpath " was too long."
238 .TP
239 .B ENOENT
240 The link named by
241 .I oldpath
242 does not exist;
243 or, a directory component in
244 .I newpath
245 does not exist;
246 or,
247 .I oldpath
248 or
249 .I newpath
250 is an empty string.
251 .TP
252 .B ENOMEM
253 Insufficient kernel memory was available.
254 .TP
255 .B ENOSPC
256 The device containing the file has no room for the new directory
257 entry.
258 .TP
259 .B ENOTDIR
260 A component used as a directory in
261 .IR oldpath " or " newpath
262 is not, in fact, a directory.
263 Or,
264 .I oldpath
265 is a directory, and
266 .I newpath
267 exists but is not a directory.
268 .TP
269 .BR ENOTEMPTY " or " EEXIST
270 .I newpath
271 is a nonempty directory, that is, contains entries other than "." and "..".
272 .TP
273 .BR EPERM " or " EACCES
274 The directory containing
275 .I oldpath
276 has the sticky bit
277 .RB ( S_ISVTX )
278 set and the process's effective user ID is neither
279 the user ID of the file to be deleted nor that of the directory
280 containing it, and the process is not privileged
281 (Linux: does not have the
282 .B CAP_FOWNER
283 capability);
284 or
285 .I newpath
286 is an existing file and the directory containing it has the sticky bit set
287 and the process's effective user ID is neither the user ID of the file
288 to be replaced nor that of the directory containing it,
289 and the process is not privileged
290 (Linux: does not have the
291 .B CAP_FOWNER
292 capability);
293 or the filesystem containing
294 .I pathname
295 does not support renaming of the type requested.
296 .TP
297 .B EROFS
298 The file is on a read-only filesystem.
299 .TP
300 .B EXDEV
301 .IR oldpath " and " newpath
302 are not on the same mounted filesystem.
303 (Linux permits a filesystem to be mounted at multiple points, but
304 .BR rename ()
305 does not work across different mount points,
306 even if the same filesystem is mounted on both.)
307 .PP
308 The following additional errors can occur for
309 .BR renameat ():
310 .TP
311 .B EBADF
312 .I olddirfd
313 or
314 .I newdirfd
315 is not a valid file descriptor.
316 .TP
317 .B ENOTDIR
318 .I oldpath
319 is relative and
320 .I olddirfd
321 is a file descriptor referring to a file other than a directory;
322 or similar for
323 .I newpath
324 and
325 .I newdirfd
326 .SH VERSIONS
327 .BR renameat ()
328 was added to Linux in kernel 2.6.16;
329 library support was added to glibc in version 2.4.
330 .SH CONFORMING TO
331 .BR rename ():
332 4.3BSD, C89, C99, POSIX.1-2001, POSIX.1-2008.
333
334 .BR renameat ():
335 POSIX.1-2008.
336 .SH BUGS
337 On NFS filesystems, you can not assume that if the operation
338 failed, the file was not renamed.
339 If the server does the rename operation
340 and then crashes, the retransmitted RPC which will be processed when the
341 server is up again causes a failure.
342 The application is expected to
343 deal with this.
344 See
345 .BR link (2)
346 for a similar problem.
347 .SH SEE ALSO
348 .BR mv (1),
349 .BR chmod (2),
350 .BR link (2),
351 .BR symlink (2),
352 .BR unlink (2),
353 .BR path_resolution (7),
354 .BR symlink (7)
355 .SH COLOPHON
356 This page is part of release 3.65 of the Linux
357 .I man-pages
358 project.
359 A description of the project,
360 and information about reporting bugs,
361 can be found at
362 \%http://www.kernel.org/doc/man\-pages/.