RENAME(2) | System Calls Manual | RENAME(2) |
rename
, renameat
— change the name of a file
#include
<stdio.h>
int
rename
(const
char *from, const char
*to);
#include <fcntl.h>
#include <stdio.h>
int
renameat
(int
fromfd, const char
*from, int tofd,
const char *to);
The
rename
()
function causes the link named from to be renamed as
to. If to exists, it is first
removed. Both from and to must
be of the same type (that is, both directories or both non-directories), and
must reside on the same file system.
rename
()
guarantees that if to already exists, an instance of
to will always exist, even if the system should crash
in the middle of the operation.
If the final component of from is a symbolic link, the symbolic link is renamed, not the file or directory to which it points.
The
renameat
()
function is equivalent to rename
() except that where
from or to specifies a relative
path, the directory entry names used are resolved relative to the
directories associated with file descriptors fromfd or
tofd (respectively) instead of the current working
directory.
If
renameat
()
is passed the special value AT_FDCWD
(defined in
<fcntl.h>
) in the
fromfd or tofd parameter, the
current working directory is used for resolving the respective
from or to argument.
Upon successful completion, the value 0 is returned; otherwise the value -1 is returned and the global variable errno is set to indicate the error.
rename
() and
renameat
() will fail and neither of the argument
files will be affected if:
ENAMETOOLONG
]NAME_MAX
characters, or an entire pathname (including the terminating NUL) exceeded
PATH_MAX
bytes.ENOENT
]EACCES
]EACCES
]EACCES
]EPERM
]EPERM
]ELOOP
]EMLINK
]ENOTDIR
]ENOTDIR
]EISDIR
]EXDEV
]ENOSPC
]EDQUOT
]EIO
]EROFS
]EFAULT
]EINVAL
].
’ or
‘..
’.ENOTEMPTY
]Additionally, renameat
() will fail if:
EBADF
]AT_FDCWD
nor a valid file descriptor.ENOTDIR
]EACCES
]The rename
() and
renameat
() functions conform to
IEEE Std 1003.1-2008 (“POSIX.1”).
The renameat
() function appeared in
OpenBSD 5.0.
The system can deadlock if a loop in the file system graph is
present. This loop takes the form of an entry in directory
‘a’, say
‘a/foo’, being a hard link to
directory ‘b’, and an entry in
directory ‘b’, say
‘b/bar’, being a hard link to
directory ‘a’. When such a loop exists
and two separate processes attempt to perform
‘rename a/foo b/bar
’ and
‘rename b/bar a/foo
’, respectively,
the system may deadlock attempting to lock both directories for
modification. Hard links to directories should be replaced by symbolic links
by the system administrator.
September 10, 2015 | OpenBSD-6.4 |