]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/rename.2
rename.2: glibc 2.28 adds library support for renameat2()
[thirdparty/man-pages.git] / 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 2017-09-15 "Linux" "Linux Programmer's Manual"
34 .SH NAME
35 rename, renameat, renameat2 \- change the name or location of a file
36 .SH SYNOPSIS
37 .nf
38 .B #include <stdio.h>
39 .PP
40 .BI "int rename(const char *" oldpath ", const char *" newpath );
41
42 .BR "#include <fcntl.h> " "/* Definition of AT_* constants */"
43 .B #include <stdio.h>
44 .PP
45 .BI "int renameat(int " olddirfd ", const char *" oldpath ,
46 .BI " int " newdirfd ", const char *" newpath );
47 .PP
48 .BI "int renameat2(int " olddirfd ", const char *" oldpath ,
49 .BI " int " newdirfd ", const char *" newpath \
50 ", unsigned int " flags );
51 .fi
52 .PP
53 .IR Note :
54 There is no glibc wrapper for
55 .BR renameat2 ();
56 see NOTES.
57 .PP
58 .in -4n
59 Feature Test Macro Requirements for glibc (see
60 .BR feature_test_macros (7)):
61 .in
62 .PP
63 .BR renameat ():
64 .PD 0
65 .ad l
66 .RS 4
67 .TP 4
68 Since glibc 2.10:
69 _POSIX_C_SOURCE\ >=\ 200809L
70 .TP
71 Before glibc 2.10:
72 _ATFILE_SOURCE
73 .\" FIXME . need to define FTMs for renameat2(), once it hits glibc
74 .RE
75 .ad
76 .PD
77 .SH DESCRIPTION
78 .BR rename ()
79 renames a file, moving it between directories if required.
80 Any other hard links to the file (as created using
81 .BR link (2))
82 are unaffected.
83 Open file descriptors for
84 .I oldpath
85 are also unaffected.
86 .PP
87 Various restrictions determine whether or not the rename operation succeeds:
88 see ERRORS below.
89 .PP
90 If
91 .I newpath
92 already exists, it will be atomically replaced, so that there is
93 no point at which another process attempting to access
94 .I newpath
95 will find it missing.
96 However, there will probably be a window in which both
97 .I oldpath
98 and
99 .I newpath
100 refer to the file being renamed.
101 .PP
102 If
103 .I oldpath
104 and
105 .I newpath
106 are existing hard links referring to the same file, then
107 .BR rename ()
108 does nothing, and returns a success status.
109 .PP
110 If
111 .I newpath
112 exists but the operation fails for some reason,
113 .BR rename ()
114 guarantees to leave an instance of
115 .I newpath
116 in place.
117 .PP
118 .I oldpath
119 can specify a directory.
120 In this case,
121 .I newpath
122 must either not exist, or it must specify an empty directory.
123 .PP
124 If
125 .I oldpath
126 refers to a symbolic link, the link is renamed; if
127 .I newpath
128 refers to a symbolic link, the link will be overwritten.
129 .SS renameat()
130 The
131 .BR renameat ()
132 system call operates in exactly the same way as
133 .BR rename (),
134 except for the differences described here.
135 .PP
136 If the pathname given in
137 .I oldpath
138 is relative, then it is interpreted relative to the directory
139 referred to by the file descriptor
140 .I olddirfd
141 (rather than relative to the current working directory of
142 the calling process, as is done by
143 .BR rename ()
144 for a relative pathname).
145 .PP
146 If
147 .I oldpath
148 is relative and
149 .I olddirfd
150 is the special value
151 .BR AT_FDCWD ,
152 then
153 .I oldpath
154 is interpreted relative to the current working
155 directory of the calling process (like
156 .BR rename ()).
157 .PP
158 If
159 .I oldpath
160 is absolute, then
161 .I olddirfd
162 is ignored.
163 .PP
164 The interpretation of
165 .I newpath
166 is as for
167 .IR oldpath ,
168 except that a relative pathname is interpreted relative
169 to the directory referred to by the file descriptor
170 .IR newdirfd .
171 .PP
172 See
173 .BR openat (2)
174 for an explanation of the need for
175 .BR renameat ().
176 .SS renameat2()
177 .BR renameat2 ()
178 has an additional
179 .I flags
180 argument.
181 A
182 .BR renameat2 ()
183 call with a zero
184 .I flags
185 argument is equivalent to
186 .BR renameat ().
187 .PP
188 The
189 .I flags
190 argument is a bit mask consisting of zero or more of the following flags:
191 .TP
192 .B RENAME_EXCHANGE
193 Atomically exchange
194 .IR oldpath
195 and
196 .IR newpath .
197 Both pathnames must exist
198 but may be of different types (e.g., one could be a non-empty directory
199 and the other a symbolic link).
200 .TP
201 .B RENAME_NOREPLACE
202 Don't overwrite
203 .IR newpath
204 of the rename.
205 Return an error if
206 .IR newpath
207 already exists.
208 .IP
209 .B RENAME_NOREPLACE
210 can't be employed together with
211 .BR RENAME_EXCHANGE .
212 .TP
213 .BR RENAME_WHITEOUT " (since Linux 3.18)"
214 .\" commit 0d7a855526dd672e114aff2ac22b60fc6f155b08
215 .\" commit 787fb6bc9682ec7c05fb5d9561b57100fbc1cc41
216 This operation makes sense only for overlay/union
217 filesystem implementations.
218 .IP
219 Specifying
220 .B RENAME_WHITEOUT
221 creates a "whiteout" object at the source of
222 the rename at the same time as performing the rename.
223 The whole operation is atomic,
224 so that if the rename succeeds then the whiteout will also have been created.
225 .IP
226 A "whiteout" is an object that has special meaning in union/overlay
227 filesystem constructs.
228 In these constructs,
229 multiple layers exist and only the top one is ever modified.
230 A whiteout on an upper layer will effectively hide a
231 matching file in the lower layer,
232 making it appear as if the file didn't exist.
233 .IP
234 When a file that exists on the lower layer is renamed,
235 the file is first copied up (if not already on the upper layer)
236 and then renamed on the upper, read-write layer.
237 At the same time, the source file needs to be "whiteouted"
238 (so that the version of the source file in the lower layer
239 is rendered invisible).
240 The whole operation needs to be done atomically.
241 .IP
242 When not part of a union/overlay,
243 the whiteout appears as a character device with a {0,0} device number.
244 .IP
245 .B RENAME_WHITEOUT
246 requires the same privileges as creating a device node (i.e., the
247 .BR CAP_MKNOD
248 capability).
249 .IP
250 .B RENAME_WHITEOUT
251 can't be employed together with
252 .BR RENAME_EXCHANGE .
253 .IP
254 .B RENAME_WHITEOUT
255 requires support from the underlying filesystem.
256 Among the filesystems that provide that support are
257 shmem (since Linux 3.18),
258 .\" shmem: commit 46fdb794e3f52ef18b859ebc92f0a9d7db21c5df
259 ext4 (since Linux 3.18),
260 .\" ext4: commit cd808deced431b66b5fa4e5c193cb7ec0059eaff
261 and XFS (since Linux 4.1).
262 .\" XFS: commit 7dcf5c3e4527cfa2807567b00387cf2ed5e07f00
263 .SH RETURN VALUE
264 On success, zero is returned.
265 On error, \-1 is returned, and
266 .I errno
267 is set appropriately.
268 .SH ERRORS
269 .TP
270 .B EACCES
271 Write permission is denied for the directory containing
272 .I oldpath
273 or
274 .IR newpath ,
275 or, search permission is denied for one of the directories
276 in the path prefix of
277 .I oldpath
278 or
279 .IR newpath ,
280 or
281 .I oldpath
282 is a directory and does not allow write permission (needed to update
283 the
284 .I ..
285 entry).
286 (See also
287 .BR path_resolution (7).)
288 .TP
289 .B EBUSY
290 The rename fails because
291 .IR oldpath " or " newpath
292 is a directory that is in use by some process (perhaps as
293 current working directory, or as root directory, or because
294 it was open for reading) or is in use by the system
295 (for example as mount point), while the system considers
296 this an error.
297 (Note that there is no requirement to return
298 .B EBUSY
299 in such
300 cases\(emthere is nothing wrong with doing the rename anyway\(embut
301 it is allowed to return
302 .B EBUSY
303 if the system cannot otherwise
304 handle such situations.)
305 .TP
306 .B EDQUOT
307 The user's quota of disk blocks on the filesystem has been exhausted.
308 .TP
309 .B EFAULT
310 .IR oldpath " or " newpath " points outside your accessible address space."
311 .TP
312 .B EINVAL
313 The new pathname contained a path prefix of the old, or, more generally,
314 an attempt was made to make a directory a subdirectory of itself.
315 .TP
316 .B EISDIR
317 .I newpath
318 is an existing directory, but
319 .I oldpath
320 is not a directory.
321 .TP
322 .B ELOOP
323 Too many symbolic links were encountered in resolving
324 .IR oldpath " or " newpath .
325 .TP
326 .B EMLINK
327 .I oldpath
328 already has the maximum number of links to it, or
329 it was a directory and the directory containing
330 .I newpath
331 has the maximum number of links.
332 .TP
333 .B ENAMETOOLONG
334 .IR oldpath " or " newpath " was too long."
335 .TP
336 .B ENOENT
337 The link named by
338 .I oldpath
339 does not exist;
340 or, a directory component in
341 .I newpath
342 does not exist;
343 or,
344 .I oldpath
345 or
346 .I newpath
347 is an empty string.
348 .TP
349 .B ENOMEM
350 Insufficient kernel memory was available.
351 .TP
352 .B ENOSPC
353 The device containing the file has no room for the new directory
354 entry.
355 .TP
356 .B ENOTDIR
357 A component used as a directory in
358 .IR oldpath " or " newpath
359 is not, in fact, a directory.
360 Or,
361 .I oldpath
362 is a directory, and
363 .I newpath
364 exists but is not a directory.
365 .TP
366 .BR ENOTEMPTY " or " EEXIST
367 .I newpath
368 is a nonempty directory, that is, contains entries other than "." and "..".
369 .TP
370 .BR EPERM " or " EACCES
371 The directory containing
372 .I oldpath
373 has the sticky bit
374 .RB ( S_ISVTX )
375 set and the process's effective user ID is neither
376 the user ID of the file to be deleted nor that of the directory
377 containing it, and the process is not privileged
378 (Linux: does not have the
379 .B CAP_FOWNER
380 capability);
381 or
382 .I newpath
383 is an existing file and the directory containing it has the sticky bit set
384 and the process's effective user ID is neither the user ID of the file
385 to be replaced nor that of the directory containing it,
386 and the process is not privileged
387 (Linux: does not have the
388 .B CAP_FOWNER
389 capability);
390 or the filesystem containing
391 .I pathname
392 does not support renaming of the type requested.
393 .TP
394 .B EROFS
395 The file is on a read-only filesystem.
396 .TP
397 .B EXDEV
398 .IR oldpath " and " newpath
399 are not on the same mounted filesystem.
400 (Linux permits a filesystem to be mounted at multiple points, but
401 .BR rename ()
402 does not work across different mount points,
403 even if the same filesystem is mounted on both.)
404 .PP
405 The following additional errors can occur for
406 .BR renameat ()
407 and
408 .BR renameat2 ():
409 .TP
410 .B EBADF
411 .I olddirfd
412 or
413 .I newdirfd
414 is not a valid file descriptor.
415 .TP
416 .B ENOTDIR
417 .I oldpath
418 is relative and
419 .I olddirfd
420 is a file descriptor referring to a file other than a directory;
421 or similar for
422 .I newpath
423 and
424 .I newdirfd
425 .PP
426 The following additional errors can occur for
427 .BR renameat2 ():
428 .TP
429 .B EEXIST
430 .I flags
431 contains
432 .B RENAME_NOREPLACE
433 and
434 .I newpath
435 already exists.
436 .TP
437 .B EINVAL
438 An invalid flag was specified in
439 .IR flags .
440 .TP
441 .B EINVAL
442 Both
443 .B RENAME_NOREPLACE
444 and
445 .B RENAME_EXCHANGE
446 were specified in
447 .IR flags .
448 .TP
449 .B EINVAL
450 Both
451 .B RENAME_WHITEOUT
452 and
453 .B RENAME_EXCHANGE
454 were specified in
455 .IR flags .
456 .TP
457 .B EINVAL
458 The filesystem does not support one of the flags in
459 .IR flags .
460 .TP
461 .B ENOENT
462 .I flags
463 contains
464 .B RENAME_EXCHANGE
465 and
466 .IR newpath
467 does not exist.
468 .TP
469 .B EPERM
470 .B RENAME_WHITEOUT
471 was specified in
472 .IR flags ,
473 but the caller does not have the
474 .B CAP_MKNOD
475 capability.
476 .SH VERSIONS
477 .BR renameat ()
478 was added to Linux in kernel 2.6.16;
479 library support was added to glibc in version 2.4.
480 .PP
481 .BR renameat2 ()
482 was added to Linux in kernel 3.15; library support was added in glibc 2.28.
483 .SH CONFORMING TO
484 .BR rename ():
485 4.3BSD, C89, C99, POSIX.1-2001, POSIX.1-2008.
486 .PP
487 .BR renameat ():
488 POSIX.1-2008.
489 .PP
490 .BR renameat2 ()
491 is Linux-specific.
492 .SH NOTES
493 Glibc does not provide a wrapper for the
494 .BR renameat2 ()
495 system call; call it using
496 .BR syscall (2).
497 .\"
498 .SS Glibc notes
499 On older kernels where
500 .BR renameat ()
501 is unavailable, the glibc wrapper function falls back to the use of
502 .BR rename ().
503 When
504 .I oldpath
505 and
506 .I newpath
507 are relative pathnames,
508 glibc constructs pathnames based on the symbolic links in
509 .IR /proc/self/fd
510 that correspond to the
511 .I olddirfd
512 and
513 .IR newdirfd
514 arguments.
515 .SH BUGS
516 On NFS filesystems, you can not assume that if the operation
517 failed, the file was not renamed.
518 If the server does the rename operation
519 and then crashes, the retransmitted RPC which will be processed when the
520 server is up again causes a failure.
521 The application is expected to
522 deal with this.
523 See
524 .BR link (2)
525 for a similar problem.
526 .SH SEE ALSO
527 .BR mv (1),
528 .BR chmod (2),
529 .BR link (2),
530 .BR symlink (2),
531 .BR unlink (2),
532 .BR path_resolution (7),
533 .BR symlink (7)