#include <stdio.h>
int rename(const char *old, const char *new);
The function rename() changes the name of a file. old points to the pathname of the file to be renamed. new points to the new pathname of the file.
If old and new both refer to the same existing file, the rename() function returns successfully and performs no other action.
If old points to the pathname of a file that is not a directory, new must not point to the pathname of a directory. If the link named by new exists, it will be removed and old will be renamed to new. In this case, a link named new must remain visible to other processes throughout the renaming operation and will refer to either the file referred to by new or the file referred to as old before the operation began.
If old points to the pathname of a directory, new must not point to the pathname of a file that is not a directory. If the directory named by new exists, it will be removed and old will be renamed to new. In this case, a link named new will exist throughout the renaming operation and will refer to either the file referred to by new or the file referred to as old before the operation began. Thus, if new names an existing directory, it must be an empty directory.
The new pathname must not contain a path prefix that names old. Write access permission is required for both the directory containing old and the directory containing new. If old points to the pathname of a directory, write access permission is required for the directory named by old, and, if it exists, the directory named by new.
If the directory containing old has the sticky bit set, at least one of the following conditions listed below must be true:
the user must be a privileged user
- the user must own old
- the user must own the directory containing old
- old must be writable by the user
If new exists, and the directory containing new is writable and has the sticky bit set, at least one of the following conditions must be true:
the user must be a privileged user
- the user must own new
- the user must own the directory containing new
- new must be writable by the user
If the link named by new exists, the file’s link count becomes zero when it is removed, and no process has the file open, then the space occupied by the file will be freed and the file will no longer be accessible. If one or more processes have the file open when the last link is removed, the link will be removed before rename() returns, but the removal of the file contents will be postponed until all references to the file have been closed.
Upon successful completion, the rename() function will mark for update the st_ctime and st_mtime fields of the parent directory of each file.
Upon successful completion, the function rename() returns a value of 0; otherwise, it returns a value of -1 and sets errno to indicate an error.
Under the following conditions, the function rename() fails, and sets errno to:
The system can deadlock if there is a loop in the file system graph. Such a loop takes the form of an entry in directory a, say a/name1, being a hard link to directory b, and an entry in directory b, say b/name2, being a hard link to directory a. When such a loop exists and two separate processes attempt to rename a/name1 to b/name2 and rename b/name2 to a/name1, respectively, the system may deadlock attempting to lock both directories for modification. Use symbolic links instead of hard links for directories.