man-pages/man2/rename.2.html
2021-03-31 01:06:50 +01:00

814 lines
15 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of RENAME</TITLE>
</HEAD><BODY>
<H1>RENAME</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
rename, renameat, renameat2 - change the name or location of a file
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;</B>
<B>int rename(const char *</B><I>oldpath</I><B>, const char *</B><I>newpath</I><B>);</B>
<B>#include &lt;<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>&gt; </B>/* Definition of AT_* constants */
<B>#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;</B>
<B>int renameat(int </B><I>olddirfd</I><B>, const char *</B><I>oldpath</I><B>,</B>
<B> int </B><I>newdirfd</I><B>, const char *</B><I>newpath</I><B>);</B>
<B>int renameat2(int </B><I>olddirfd</I><B>, const char *</B><I>oldpath</I><B>,</B>
<B> int </B><I>newdirfd</I><B>, const char *</B><I>newpath</I><B>, unsigned int </B><I>flags</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>renameat</B>():
<DL COMPACT><DT id="1"><DD>
<DL COMPACT>
<DT id="2">Since glibc 2.10:<DD>
_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;200809L
<DT id="3">Before glibc 2.10:<DD>
_ATFILE_SOURCE
</DL>
</DL>
<P>
<B>renameat2</B>():
<DL COMPACT><DT id="4"><DD>
<DL COMPACT>
<DT id="5">_GNU_SOURCE<DD>
</DL>
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>rename</B>()
renames a file, moving it between directories if required.
Any other hard links to the file (as created using
<B><A HREF="/cgi-bin/man/man2html?2+link">link</A></B>(2))
are unaffected.
Open file descriptors for
<I>oldpath</I>
are also unaffected.
<P>
Various restrictions determine whether or not the rename operation succeeds:
see ERRORS below.
<P>
If
<I>newpath</I>
already exists, it will be atomically replaced, so that there is
no point at which another process attempting to access
<I>newpath</I>
will find it missing.
However, there will probably be a window in which both
<I>oldpath</I>
and
<I>newpath</I>
refer to the file being renamed.
<P>
If
<I>oldpath</I>
and
<I>newpath</I>
are existing hard links referring to the same file, then
<B>rename</B>()
does nothing, and returns a success status.
<P>
If
<I>newpath</I>
exists but the operation fails for some reason,
<B>rename</B>()
guarantees to leave an instance of
<I>newpath</I>
in place.
<P>
<I>oldpath</I>
can specify a directory.
In this case,
<I>newpath</I>
must either not exist, or it must specify an empty directory.
<P>
If
<I>oldpath</I>
refers to a symbolic link, the link is renamed; if
<I>newpath</I>
refers to a symbolic link, the link will be overwritten.
<A NAME="lbAE">&nbsp;</A>
<H3>renameat()</H3>
The
<B>renameat</B>()
system call operates in exactly the same way as
<B>rename</B>(),
except for the differences described here.
<P>
If the pathname given in
<I>oldpath</I>
is relative, then it is interpreted relative to the directory
referred to by the file descriptor
<I>olddirfd</I>
(rather than relative to the current working directory of
the calling process, as is done by
<B>rename</B>()
for a relative pathname).
<P>
If
<I>oldpath</I>
is relative and
<I>olddirfd</I>
is the special value
<B>AT_FDCWD</B>,
then
<I>oldpath</I>
is interpreted relative to the current working
directory of the calling process (like
<B>rename</B>()).
<P>
If
<I>oldpath</I>
is absolute, then
<I>olddirfd</I>
is ignored.
<P>
The interpretation of
<I>newpath</I>
is as for
<I>oldpath</I>,
except that a relative pathname is interpreted relative
to the directory referred to by the file descriptor
<I>newdirfd</I>.
<P>
See
<B><A HREF="/cgi-bin/man/man2html?2+openat">openat</A></B>(2)
for an explanation of the need for
<B>renameat</B>().
<A NAME="lbAF">&nbsp;</A>
<H3>renameat2()</H3>
<B>renameat2</B>()
has an additional
<I>flags</I>
argument.
A
<B>renameat2</B>()
call with a zero
<I>flags</I>
argument is equivalent to
<B>renameat</B>().
<P>
The
<I>flags</I>
argument is a bit mask consisting of zero or more of the following flags:
<DL COMPACT>
<DT id="6"><B>RENAME_EXCHANGE</B>
<DD>
Atomically exchange
<I>oldpath</I>
and
<I>newpath</I>.
Both pathnames must exist
but may be of different types (e.g., one could be a non-empty directory
and the other a symbolic link).
<DT id="7"><B>RENAME_NOREPLACE</B>
<DD>
Don't overwrite
<I>newpath</I>
of the rename.
Return an error if
<I>newpath</I>
already exists.
<DT id="8"><DD>
<B>RENAME_NOREPLACE</B>
can't be employed together with
<B>RENAME_EXCHANGE</B>.
<DT id="9"><DD>
<B>RENAME_NOREPLACE</B>
requires support from the underlying filesystem.
Support for various filesystems was added as follows:
<DL COMPACT><DT id="10"><DD>
<DL COMPACT>
<DT id="11">*<DD>
ext4 (Linux 3.15);
<DT id="12">*<DD>
btrfs, shmem, and cifs (Linux 3.17);
<DT id="13">*<DD>
xfs (Linux 4.0);
<DT id="14">*<DD>
Support for many other filesystems was added in Linux 4.9, including
etx2, minix, reiserfs, jfs, vfat, and bpf.
</DL>
</DL>
<DT id="15"><B>RENAME_WHITEOUT</B> (since Linux 3.18)
<DD>
This operation makes sense only for overlay/union
filesystem implementations.
<DT id="16"><DD>
Specifying
<B>RENAME_WHITEOUT</B>
creates a &quot;whiteout&quot; object at the source of
the rename at the same time as performing the rename.
The whole operation is atomic,
so that if the rename succeeds then the whiteout will also have been created.
<DT id="17"><DD>
A &quot;whiteout&quot; is an object that has special meaning in union/overlay
filesystem constructs.
In these constructs,
multiple layers exist and only the top one is ever modified.
A whiteout on an upper layer will effectively hide a
matching file in the lower layer,
making it appear as if the file didn't exist.
<DT id="18"><DD>
When a file that exists on the lower layer is renamed,
the file is first copied up (if not already on the upper layer)
and then renamed on the upper, read-write layer.
At the same time, the source file needs to be &quot;whiteouted&quot;
(so that the version of the source file in the lower layer
is rendered invisible).
The whole operation needs to be done atomically.
<DT id="19"><DD>
When not part of a union/overlay,
the whiteout appears as a character device with a {0,0} device number.
(Note that other union/overlay implementations may employ different methods
for storing whiteout entries; specifically, BSD union mount employs
a separate inode type,
<B>DT_WHT</B>,
which, while supported by some filesystems available in Linux,
such as CODA and XFS, is ignored by the kernel's whiteout support code,
as of Linux 4.19, at least.)
<DT id="20"><DD>
<B>RENAME_WHITEOUT</B>
requires the same privileges as creating a device node (i.e., the
<B>CAP_MKNOD</B>
capability).
<DT id="21"><DD>
<B>RENAME_WHITEOUT</B>
can't be employed together with
<B>RENAME_EXCHANGE</B>.
<DT id="22"><DD>
<B>RENAME_WHITEOUT</B>
requires support from the underlying filesystem.
Among the filesystems that provide that support are
tmpfs (since Linux 3.18),
ext4 (since Linux 3.18),
XFS (since Linux 4.1),
f2fs (since Linux 4.2).
btrfs (since Linux 4.7),
and ubifs (since Linux 4.9).
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, zero is returned.
On error, -1 is returned, and
<I>errno</I>
is set appropriately.
<A NAME="lbAH">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="23"><B>EACCES</B>
<DD>
Write permission is denied for the directory containing
<I>oldpath</I>
or
<I>newpath</I>,
or, search permission is denied for one of the directories
in the path prefix of
<I>oldpath</I>
or
<I>newpath</I>,
or
<I>oldpath</I>
is a directory and does not allow write permission (needed to update
the
<I>..</I>
entry).
(See also
<B><A HREF="/cgi-bin/man/man2html?7+path_resolution">path_resolution</A></B>(7).)
<DT id="24"><B>EBUSY</B>
<DD>
The rename fails because
<I>oldpath</I> or <I>newpath</I>
is a directory that is in use by some process (perhaps as
current working directory, or as root directory, or because
it was open for reading) or is in use by the system
(for example as mount point), while the system considers
this an error.
(Note that there is no requirement to return
<B>EBUSY</B>
in such
cases---there is nothing wrong with doing the rename anyway---but
it is allowed to return
<B>EBUSY</B>
if the system cannot otherwise
handle such situations.)
<DT id="25"><B>EDQUOT</B>
<DD>
The user's quota of disk blocks on the filesystem has been exhausted.
<DT id="26"><B>EFAULT</B>
<DD>
<I>oldpath</I> or <I>newpath</I> points outside your accessible address space.
<DT id="27"><B>EINVAL</B>
<DD>
The new pathname contained a path prefix of the old, or, more generally,
an attempt was made to make a directory a subdirectory of itself.
<DT id="28"><B>EISDIR</B>
<DD>
<I>newpath</I>
is an existing directory, but
<I>oldpath</I>
is not a directory.
<DT id="29"><B>ELOOP</B>
<DD>
Too many symbolic links were encountered in resolving
<I>oldpath</I> or <I>newpath</I>.
<DT id="30"><B>EMLINK</B>
<DD>
<I>oldpath</I>
already has the maximum number of links to it, or
it was a directory and the directory containing
<I>newpath</I>
has the maximum number of links.
<DT id="31"><B>ENAMETOOLONG</B>
<DD>
<I>oldpath</I> or <I>newpath</I> was too long.
<DT id="32"><B>ENOENT</B>
<DD>
The link named by
<I>oldpath</I>
does not exist;
or, a directory component in
<I>newpath</I>
does not exist;
or,
<I>oldpath</I>
or
<I>newpath</I>
is an empty string.
<DT id="33"><B>ENOMEM</B>
<DD>
Insufficient kernel memory was available.
<DT id="34"><B>ENOSPC</B>
<DD>
The device containing the file has no room for the new directory
entry.
<DT id="35"><B>ENOTDIR</B>
<DD>
A component used as a directory in
<I>oldpath</I> or <I>newpath</I>
is not, in fact, a directory.
Or,
<I>oldpath</I>
is a directory, and
<I>newpath</I>
exists but is not a directory.
<DT id="36"><B>ENOTEMPTY</B> or <B>EEXIST</B>
<DD>
<I>newpath</I>
is a nonempty directory, that is, contains entries other than &quot;.&quot; and &quot;..&quot;.
<DT id="37"><B>EPERM</B> or <B>EACCES</B>
<DD>
The directory containing
<I>oldpath</I>
has the sticky bit
(<B>S_ISVTX</B>)
set and the process's effective user ID is neither
the user ID of the file to be deleted nor that of the directory
containing it, and the process is not privileged
(Linux: does not have the
<B>CAP_FOWNER</B>
capability);
or
<I>newpath</I>
is an existing file and the directory containing it has the sticky bit set
and the process's effective user ID is neither the user ID of the file
to be replaced nor that of the directory containing it,
and the process is not privileged
(Linux: does not have the
<B>CAP_FOWNER</B>
capability);
or the filesystem containing
<I>pathname</I>
does not support renaming of the type requested.
<DT id="38"><B>EROFS</B>
<DD>
The file is on a read-only filesystem.
<DT id="39"><B>EXDEV</B>
<DD>
<I>oldpath</I> and <I>newpath</I>
are not on the same mounted filesystem.
(Linux permits a filesystem to be mounted at multiple points, but
<B>rename</B>()
does not work across different mount points,
even if the same filesystem is mounted on both.)
</DL>
<P>
The following additional errors can occur for
<B>renameat</B>()
and
<B>renameat2</B>():
<DL COMPACT>
<DT id="40"><B>EBADF</B>
<DD>
<I>olddirfd</I>
or
<I>newdirfd</I>
is not a valid file descriptor.
<DT id="41"><B>ENOTDIR</B>
<DD>
<I>oldpath</I>
is relative and
<I>olddirfd</I>
is a file descriptor referring to a file other than a directory;
or similar for
<I>newpath</I>
and
<I>newdirfd</I>
</DL>
<P>
The following additional errors can occur for
<B>renameat2</B>():
<DL COMPACT>
<DT id="42"><B>EEXIST</B>
<DD>
<I>flags</I>
contains
<B>RENAME_NOREPLACE</B>
and
<I>newpath</I>
already exists.
<DT id="43"><B>EINVAL</B>
<DD>
An invalid flag was specified in
<I>flags</I>.
<DT id="44"><B>EINVAL</B>
<DD>
Both
<B>RENAME_NOREPLACE</B>
and
<B>RENAME_EXCHANGE</B>
were specified in
<I>flags</I>.
<DT id="45"><B>EINVAL</B>
<DD>
Both
<B>RENAME_WHITEOUT</B>
and
<B>RENAME_EXCHANGE</B>
were specified in
<I>flags</I>.
<DT id="46"><B>EINVAL</B>
<DD>
The filesystem does not support one of the flags in
<I>flags</I>.
<DT id="47"><B>ENOENT</B>
<DD>
<I>flags</I>
contains
<B>RENAME_EXCHANGE</B>
and
<I>newpath</I>
does not exist.
<DT id="48"><B>EPERM</B>
<DD>
<B>RENAME_WHITEOUT</B>
was specified in
<I>flags</I>,
but the caller does not have the
<B>CAP_MKNOD</B>
capability.
</DL>
<A NAME="lbAI">&nbsp;</A>
<H2>VERSIONS</H2>
<B>renameat</B>()
was added to Linux in kernel 2.6.16;
library support was added to glibc in version 2.4.
<P>
<B>renameat2</B>()
was added to Linux in kernel 3.15; library support was added in glibc 2.28.
<A NAME="lbAJ">&nbsp;</A>
<H2>CONFORMING TO</H2>
<B>rename</B>():
4.3BSD, C89, C99, POSIX.1-2001, POSIX.1-2008.
<P>
<B>renameat</B>():
POSIX.1-2008.
<P>
<B>renameat2</B>()
is Linux-specific.
<A NAME="lbAK">&nbsp;</A>
<H2>NOTES</H2>
<A NAME="lbAL">&nbsp;</A>
<H3>Glibc notes</H3>
On older kernels where
<B>renameat</B>()
is unavailable, the glibc wrapper function falls back to the use of
<B>rename</B>().
When
<I>oldpath</I>
and
<I>newpath</I>
are relative pathnames,
glibc constructs pathnames based on the symbolic links in
<I>/proc/self/fd</I>
that correspond to the
<I>olddirfd</I>
and
<I>newdirfd</I>
arguments.
<A NAME="lbAM">&nbsp;</A>
<H2>BUGS</H2>
On NFS filesystems, you can not assume that if the operation
failed, the file was not renamed.
If the server does the rename operation
and then crashes, the retransmitted RPC which will be processed when the
server is up again causes a failure.
The application is expected to
deal with this.
See
<B><A HREF="/cgi-bin/man/man2html?2+link">link</A></B>(2)
for a similar problem.
<A NAME="lbAN">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+mv">mv</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?2+chmod">chmod</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+link">link</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+symlink">symlink</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+unlink">unlink</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?7+path_resolution">path_resolution</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+symlink">symlink</A></B>(7)
<A NAME="lbAO">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="49"><A HREF="#lbAB">NAME</A><DD>
<DT id="50"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="51"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DL>
<DT id="52"><A HREF="#lbAE">renameat()</A><DD>
<DT id="53"><A HREF="#lbAF">renameat2()</A><DD>
</DL>
<DT id="54"><A HREF="#lbAG">RETURN VALUE</A><DD>
<DT id="55"><A HREF="#lbAH">ERRORS</A><DD>
<DT id="56"><A HREF="#lbAI">VERSIONS</A><DD>
<DT id="57"><A HREF="#lbAJ">CONFORMING TO</A><DD>
<DT id="58"><A HREF="#lbAK">NOTES</A><DD>
<DL>
<DT id="59"><A HREF="#lbAL">Glibc notes</A><DD>
</DL>
<DT id="60"><A HREF="#lbAM">BUGS</A><DD>
<DT id="61"><A HREF="#lbAN">SEE ALSO</A><DD>
<DT id="62"><A HREF="#lbAO">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:34 GMT, March 31, 2021
</BODY>
</HTML>