RENAME(2) System Calls Manual RENAME(2)

rename, renameat, renamex_np, renameatx_npchange the name of a file

#include <stdio.h>

int
rename(const char *old, const char *new);

int
renameat(int fromfd, const char *from, int tofd, const char *to);

int
renamex_np(const char *from, const char *to, unsigned int flags);

int
renameatx_np(int fromfd, const char *from, int tofd, const char *to, unsigned int flags);

The () system call causes the link named old to be renamed as new. If new exists, it is first removed. Both old and new must be of the same type (that is, both must be either directories or non-directories) and must reside on the same file system.

The () system call guarantees that an instance of new will always exist, even if the system should crash in the middle of the operation.

If the final component of old is a symbolic link, the symbolic link is renamed, not the file or directory to which it points.

The () system call is equivalent to rename() except in the case where either from or to specifies a relative path. If from is a relative path, the file to be renamed is located relative to the directory associated with the file descriptor fromfd instead of the current working directory. If the to is a relative path, the same happens only relative to the directory associated with tofd. If the renameat() is passed the special value AT_FDCWD in the fromfd or tofd parameter, the current working directory is used in the determination of the file for the respective path parameter.

The () and () system calls are similar to their counterparts except that they take a flags argument. Values for flags are constructed with below bits set:

On file systems that support it (see getattrlist(2) VOL_CAP_INT_RENAME_SWAP), it will cause the source and target to be atomically swapped. Source and target need not be of the same type, i.e. it is possible to swap a file with a directory. EINVAL is returned in case of bitwise-inclusive OR with RENAME_EXCL.
On file systems that support it (see getattrlist(2) VOL_CAP_INT_RENAME_EXCL), it will cause EEXIST to be returned if the destination already exists. EINVAL is returned in case of bitwise-inclusive OR with RENAME_SWAP.
If any symbolic links are encountered during pathname resolution, an error is returned.

The system can deadlock if a loop is present in the file system graph. 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.

Whether or not hard links to directories are supported is specific to the underlying filesystem implementation.

It is recommended that any hard links to directories in an underlying filesystem should be replaced by symbolic links by the system administrator to avoid the possibility of deadlocks.

Moving or renaming a file or directory into a directory with inheritable ACLs does not result in ACLs being set on the file or directory. Use acl(3) in conjunction with rename() to set ACLs on the file or directory.

A 0 value is returned if the operation succeeds, otherwise rename() returns -1 and the global variable errno indicates the reason for the failure.

The rename() system call will fail and neither of the argument files will be affected if:

[]
A component of either path prefix denies search permission.
[]
The requested operation requires writing in a directory (e.g., new, new/.., or old/..) whose modes disallow this.
[]
old is a directory and it, or some descendent in the namespace, is open and the file system format does does not support renaming a directory with open descendents (see getattrlist(2) VOL_CAP_INT_RENAME_OPENFAIL).
[]
The directory in which the entry for the new name is being placed cannot be extended because the user's quota of disk blocks on the file system containing the directory has been exhausted.
[]
flags has RENAME_EXCL set but new already exists.
[]
points outside the process's allocated address space.
[]
Old is a parent directory of new, or an attempt is made to rename ‘.’ or ‘..’. If RENAME_SWAP is used, then EINVAL will also be returned if new is a parent directory of old. If both RENAME_SWAP and RENAME_EXCL bits are set in flags, then EINVAL will be returned.
[]
flags has an invalid value.
[]
An I/O error occurs while making or updating a directory entry.
[]
new is a directory, but old is not a directory.
[]
Too many symbolic links are encountered in translating either pathname. This is taken to be indicative of a looping symbolic link.
[]
If RENAME_NOFOLLOW_ANY was passed and a symbolic link was encountered in translating either pathname.
[]
A component of a pathname exceeds {NAME_MAX} characters, or an entire path name exceeds {PATH_MAX} characters.
[]
A component of the old path does not exist, or a path prefix of new does not exist.
[]
flags has RENAME_SWAP set but new does not exist.
[]
The directory in which the entry for the new name is being placed cannot be extended because there is no space left on the file system containing the directory.
[]
A component of either path prefix is not a directory.
[]
old is a directory, but new is not a directory.
[]
New is a directory and is not empty.
[]
flags has a value that is not supported by the file system.
[]
The directory containing old is marked sticky, and neither the containing directory nor old are owned by the effective user ID.
[]
The new file exists, the directory containing new is marked sticky, and neither the containing directory nor new are owned by the effective user ID.
[]
The requested link requires writing in a directory on a read-only file system.
[]
The link named by new and the file named by old are on different logical devices (file systems). Note that this error code will not be returned if the implementation permits cross-device links.
[]
A component of either pathname refers to a “dataless” directory that requires materialization and the I/O policy of the current thread or process disallows dataless directory materialization (see getiopolicy_np(3)).
[]
The from pathname refers to a “dataless” file or directory that must be materialized before being moved to its new location and the I/O policy of the current thread or process disallows file or directory materialization (see getiopolicy_np(3)).

The renameat() and renameatx_np() calls may also fail with:

[]
The from argument does not specify an absolute path and the fromfd argument is neither AT_FDCWD nor a valid file descriptor open for searching, or the to argument does not specify an absolute path and the tofd argument is neither AT_FDCWD nor a valid file descriptor open for searching.
[]
The from argument is not an absolute path and fromfd is neither AT_FDCWD nor a file descriptor associated with a directory, or the to argument is not an absolute path and tofd is neither AT_FDCWD nor a file descriptor associated with a directory.

The restriction on renaming a directory whose permissions disallow writing is based on the fact that UFS directories contain a ".." entry. If renaming a directory would move it to another parent directory, this entry needs to be changed.

This restriction has been generalized to disallow renaming of any write-disabled directory, even when this would not require a change to the ".." entry. For consistency, HFS+ directories emulate this behavior.

open(2), symlink(7)

The rename() function conforms to IEEE Std 1003.1-1988 (“POSIX.1”). The renameat() system call is expected to conform to POSIX.1-2008 .

June 3, 2021 BSD 4.2