xref: /freebsd/lib/libsys/rename.2 (revision 33f58ac0795b2b02593ad0a8bf8a1ea24c1dc5e1)
18269e767SBrooks Davis.\" Copyright (c) 1983, 1991, 1993
28269e767SBrooks Davis.\"	The Regents of the University of California.  All rights reserved.
38269e767SBrooks Davis.\"
48269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without
58269e767SBrooks Davis.\" modification, are permitted provided that the following conditions
68269e767SBrooks Davis.\" are met:
78269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright
88269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer.
98269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright
108269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer in the
118269e767SBrooks Davis.\"    documentation and/or other materials provided with the distribution.
128269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors
138269e767SBrooks Davis.\"    may be used to endorse or promote products derived from this software
148269e767SBrooks Davis.\"    without specific prior written permission.
158269e767SBrooks Davis.\"
168269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
178269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198269e767SBrooks Davis.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
208269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268269e767SBrooks Davis.\" SUCH DAMAGE.
278269e767SBrooks Davis.\"
28*33f58ac0SJose Luis Duran.Dd August 25, 2024
298269e767SBrooks Davis.Dt RENAME 2
308269e767SBrooks Davis.Os
318269e767SBrooks Davis.Sh NAME
328269e767SBrooks Davis.Nm rename
338269e767SBrooks Davis.Nd change the name of a file
348269e767SBrooks Davis.Sh LIBRARY
358269e767SBrooks Davis.Lb libc
368269e767SBrooks Davis.Sh SYNOPSIS
378269e767SBrooks Davis.In stdio.h
388269e767SBrooks Davis.Ft int
398269e767SBrooks Davis.Fn rename "const char *from" "const char *to"
408269e767SBrooks Davis.Ft int
418269e767SBrooks Davis.Fn renameat "int fromfd" "const char *from" "int tofd" "const char *to"
428269e767SBrooks Davis.Sh DESCRIPTION
438269e767SBrooks DavisThe
448269e767SBrooks Davis.Fn rename
458269e767SBrooks Davissystem call
468269e767SBrooks Daviscauses the link named
478269e767SBrooks Davis.Fa from
488269e767SBrooks Davisto be renamed as
498269e767SBrooks Davis.Fa to .
508269e767SBrooks DavisIf
518269e767SBrooks Davis.Fa to
528269e767SBrooks Davisexists, it is first removed.
538269e767SBrooks DavisBoth
548269e767SBrooks Davis.Fa from
558269e767SBrooks Davisand
568269e767SBrooks Davis.Fa to
578269e767SBrooks Davismust be of the same type (that is, both directories or both
588269e767SBrooks Davisnon-directories), and must reside on the same file system.
598269e767SBrooks Davis.Pp
608269e767SBrooks DavisThe
618269e767SBrooks Davis.Fn rename
628269e767SBrooks Davissystem call
638269e767SBrooks Davisguarantees that if
648269e767SBrooks Davis.Fa to
658269e767SBrooks Davisalready exists, an instance of
668269e767SBrooks Davis.Fa to
678269e767SBrooks Daviswill always exist, even if the system should crash in
688269e767SBrooks Davisthe middle of the operation.
698269e767SBrooks Davis.Pp
708269e767SBrooks DavisIf the final component of
718269e767SBrooks Davis.Fa from
728269e767SBrooks Davisis a symbolic link,
738269e767SBrooks Davisthe symbolic link is renamed,
748269e767SBrooks Davisnot the file or directory to which it points.
758269e767SBrooks Davis.Pp
768269e767SBrooks DavisIf
778269e767SBrooks Davis.Fa from
788269e767SBrooks Davisand
798269e767SBrooks Davis.Fa to
808269e767SBrooks Davisresolve to the same directory entry, or to different directory
818269e767SBrooks Davisentries for the same existing file,
828269e767SBrooks Davis.Fn rename
838269e767SBrooks Davisreturns success without taking any further action.
848269e767SBrooks Davis.Pp
858269e767SBrooks DavisThe
868269e767SBrooks Davis.Fn renameat
878269e767SBrooks Davissystem call is equivalent to
888269e767SBrooks Davis.Fn rename
898269e767SBrooks Davisexcept in the case where either
908269e767SBrooks Davis.Fa from
918269e767SBrooks Davisor
928269e767SBrooks Davis.Fa to
938269e767SBrooks Davisspecifies a relative path.
948269e767SBrooks DavisIf
958269e767SBrooks Davis.Fa from
968269e767SBrooks Davisis a relative path, the file to be renamed is located
978269e767SBrooks Davisrelative to the directory associated with the file descriptor
988269e767SBrooks Davis.Fa fromfd
998269e767SBrooks Davisinstead of the current working directory.
1008269e767SBrooks DavisIf the
1018269e767SBrooks Davis.Fa to
1028269e767SBrooks Davisis a relative path, the same happens only relative to the directory associated
1038269e767SBrooks Daviswith
1048269e767SBrooks Davis.Fa tofd .
1058269e767SBrooks DavisIf the
1068269e767SBrooks Davis.Fn renameat
1078269e767SBrooks Davisis passed the special value
1088269e767SBrooks Davis.Dv AT_FDCWD
1098269e767SBrooks Davisin the
1108269e767SBrooks Davis.Fa fromfd
1118269e767SBrooks Davisor
1128269e767SBrooks Davis.Fa tofd
1138269e767SBrooks Davisparameter, the current working directory is used in the determination
1148269e767SBrooks Davisof the file for the respective path parameter.
1158269e767SBrooks Davis.\".Sh CAVEAT
1168269e767SBrooks Davis.\"The system can deadlock if a loop in the file system graph is present.
1178269e767SBrooks Davis.\"This loop takes the form of an entry in directory
1188269e767SBrooks Davis.\".Pa a ,
1198269e767SBrooks Davis.\"say
1208269e767SBrooks Davis.\".Pa a/foo ,
1218269e767SBrooks Davis.\"being a hard link to directory
1228269e767SBrooks Davis.\".Pa b ,
1238269e767SBrooks Davis.\"and an entry in
1248269e767SBrooks Davis.\"directory
1258269e767SBrooks Davis.\".Pa b ,
1268269e767SBrooks Davis.\"say
1278269e767SBrooks Davis.\".Pa b/bar ,
1288269e767SBrooks Davis.\"being a hard link
1298269e767SBrooks Davis.\"to directory
1308269e767SBrooks Davis.\".Pa a .
1318269e767SBrooks Davis.\"When such a loop exists and two separate processes attempt to
1328269e767SBrooks Davis.\"perform
1338269e767SBrooks Davis.\".Ql rename a/foo b/bar
1348269e767SBrooks Davis.\"and
1358269e767SBrooks Davis.\".Ql rename b/bar a/foo ,
1368269e767SBrooks Davis.\"respectively,
1378269e767SBrooks Davis.\"the system may deadlock attempting to lock
1388269e767SBrooks Davis.\"both directories for modification.
1398269e767SBrooks Davis.\"Hard links to directories should be
1408269e767SBrooks Davis.\"replaced by symbolic links by the system administrator.
1418269e767SBrooks Davis.Sh RETURN VALUES
1428269e767SBrooks Davis.Rv -std rename
1438269e767SBrooks Davis.Sh ERRORS
1448269e767SBrooks DavisThe
1458269e767SBrooks Davis.Fn rename
1468269e767SBrooks Davissystem call
1478269e767SBrooks Daviswill fail and neither of the argument files will be
1488269e767SBrooks Davisaffected if:
1498269e767SBrooks Davis.Bl -tag -width Er
1508269e767SBrooks Davis.It Bq Er ENAMETOOLONG
1518269e767SBrooks DavisA component of either pathname exceeded 255 characters,
1528269e767SBrooks Davisor the entire length of either path name exceeded 1023 characters.
1538269e767SBrooks Davis.It Bq Er ENOENT
1548269e767SBrooks DavisA component of the
1558269e767SBrooks Davis.Fa from
1568269e767SBrooks Davispath does not exist,
1578269e767SBrooks Davisor a path prefix of
1588269e767SBrooks Davis.Fa to
1598269e767SBrooks Davisdoes not exist.
1608269e767SBrooks Davis.It Bq Er EACCES
1618269e767SBrooks DavisA component of either path prefix denies search permission.
1628269e767SBrooks Davis.It Bq Er EACCES
1638269e767SBrooks DavisThe requested link requires writing in a directory with a mode
1648269e767SBrooks Davisthat denies write permission.
1658269e767SBrooks Davis.It Bq Er EACCES
1668269e767SBrooks DavisThe directory pointed at by the
1678269e767SBrooks Davis.Fa from
1688269e767SBrooks Davisargument denies write permission, and the operation would move
1698269e767SBrooks Davisit to another parent directory.
1708269e767SBrooks Davis.It Bq Er EPERM
1718269e767SBrooks DavisThe file pointed at by the
1728269e767SBrooks Davis.Fa from
1738269e767SBrooks Davisargument has its immutable, undeletable or append-only flag set, see the
1748269e767SBrooks Davis.Xr chflags 2
1758269e767SBrooks Davismanual page for more information.
1768269e767SBrooks Davis.It Bq Er EPERM
1778269e767SBrooks DavisThe parent directory of the file pointed at by the
1788269e767SBrooks Davis.Fa from
1798269e767SBrooks Davisargument has its immutable or append-only flag set.
1808269e767SBrooks Davis.It Bq Er EPERM
1818269e767SBrooks DavisThe parent directory of the file pointed at by the
1828269e767SBrooks Davis.Fa to
1838269e767SBrooks Davisargument has its immutable flag set.
1848269e767SBrooks Davis.It Bq Er EPERM
1858269e767SBrooks DavisThe directory containing
1868269e767SBrooks Davis.Fa from
1878269e767SBrooks Davisis marked sticky,
1888269e767SBrooks Davisand neither the containing directory nor
1898269e767SBrooks Davis.Fa from
1908269e767SBrooks Davisare owned by the effective user ID.
1918269e767SBrooks Davis.It Bq Er EPERM
1928269e767SBrooks DavisThe file pointed at by the
1938269e767SBrooks Davis.Fa to
1948269e767SBrooks Davisargument
1958269e767SBrooks Davisexists,
1968269e767SBrooks Davisthe directory containing
1978269e767SBrooks Davis.Fa to
1988269e767SBrooks Davisis marked sticky,
1998269e767SBrooks Davisand neither the containing directory nor
2008269e767SBrooks Davis.Fa to
2018269e767SBrooks Davisare owned by the effective user ID.
2028269e767SBrooks Davis.It Bq Er ELOOP
2038269e767SBrooks DavisToo many symbolic links were encountered in translating either pathname.
2048269e767SBrooks Davis.It Bq Er ENOTDIR
2058269e767SBrooks DavisA component of either path prefix is not a directory.
2068269e767SBrooks Davis.It Bq Er ENOTDIR
2078269e767SBrooks DavisThe
2088269e767SBrooks Davis.Fa from
2098269e767SBrooks Davisargument
2108269e767SBrooks Davisis a directory, but
2118269e767SBrooks Davis.Fa to
2128269e767SBrooks Davisis not a directory.
2138269e767SBrooks Davis.It Bq Er EISDIR
2148269e767SBrooks DavisThe
2158269e767SBrooks Davis.Fa to
2168269e767SBrooks Davisargument
2178269e767SBrooks Davisis a directory, but
2188269e767SBrooks Davis.Fa from
2198269e767SBrooks Davisis not a directory.
2208269e767SBrooks Davis.It Bq Er EXDEV
2218269e767SBrooks DavisThe link named by
2228269e767SBrooks Davis.Fa to
2238269e767SBrooks Davisand the file named by
2248269e767SBrooks Davis.Fa from
2258269e767SBrooks Davisare on different logical devices (file systems).
2268269e767SBrooks DavisNote that this error
2278269e767SBrooks Daviscode will not be returned if the implementation permits cross-device
2288269e767SBrooks Davislinks.
2298269e767SBrooks Davis.It Bq Er ENOSPC
2308269e767SBrooks DavisThe directory in which the entry for the new name is being placed
2318269e767SBrooks Daviscannot be extended because there is no space left on the file
2328269e767SBrooks Davissystem containing the directory.
2338269e767SBrooks Davis.It Bq Er EDQUOT
2348269e767SBrooks DavisThe directory in which the entry for the new name
2358269e767SBrooks Davisis being placed cannot be extended because the
2368269e767SBrooks Davisuser's quota of disk blocks on the file system
2378269e767SBrooks Daviscontaining the directory has been exhausted.
2388269e767SBrooks Davis.It Bq Er EIO
2398269e767SBrooks DavisAn I/O error occurred while making or updating a directory entry.
2408269e767SBrooks Davis.It Bq Er EINTEGRITY
2418269e767SBrooks DavisCorrupted data was detected while reading from the file system.
2428269e767SBrooks Davis.It Bq Er EROFS
2438269e767SBrooks DavisThe requested link requires writing in a directory on a read-only file
2448269e767SBrooks Davissystem.
2458269e767SBrooks Davis.It Bq Er EFAULT
2468269e767SBrooks DavisPath
2478269e767SBrooks Davispoints outside the process's allocated address space.
2488269e767SBrooks Davis.It Bq Er EINVAL
2498269e767SBrooks DavisThe
2508269e767SBrooks Davis.Fa from
2518269e767SBrooks Davisargument
2528269e767SBrooks Davisis a parent directory of
2538269e767SBrooks Davis.Fa to ,
2548269e767SBrooks Davisor an attempt is made to rename
2558269e767SBrooks Davis.Ql .\&
2568269e767SBrooks Davisor
2578269e767SBrooks Davis.Ql \&.. .
258*33f58ac0SJose Luis Duran.It Bq Er EINVAL
259*33f58ac0SJose Luis DuranThe last component of the
260*33f58ac0SJose Luis Duran.Fa to
261*33f58ac0SJose Luis Duranpath is invalid on the target file system.
2628269e767SBrooks Davis.It Bq Er ENOTEMPTY
2638269e767SBrooks DavisThe
2648269e767SBrooks Davis.Fa to
2658269e767SBrooks Davisargument
2668269e767SBrooks Davisis a directory and is not empty.
2678269e767SBrooks Davis.It Bq Er ECAPMODE
2688269e767SBrooks Davis.Fn rename
2698269e767SBrooks Daviswas called and the process is in capability mode.
2708269e767SBrooks Davis.El
2718269e767SBrooks Davis.Pp
2728269e767SBrooks DavisIn addition to the errors returned by the
2738269e767SBrooks Davis.Fn rename ,
2748269e767SBrooks Davisthe
2758269e767SBrooks Davis.Fn renameat
2768269e767SBrooks Davismay fail if:
2778269e767SBrooks Davis.Bl -tag -width Er
2788269e767SBrooks Davis.It Bq Er EBADF
2798269e767SBrooks DavisThe
2808269e767SBrooks Davis.Fa from
2818269e767SBrooks Davisargument does not specify an absolute path and the
2828269e767SBrooks Davis.Fa fromfd
2838269e767SBrooks Davisargument is neither
2848269e767SBrooks Davis.Dv AT_FDCWD
2858269e767SBrooks Davisnor a valid file descriptor open for searching, or the
2868269e767SBrooks Davis.Fa to
2878269e767SBrooks Davisargument does not specify an absolute path and the
2888269e767SBrooks Davis.Fa tofd
2898269e767SBrooks Davisargument is neither
2908269e767SBrooks Davis.Dv AT_FDCWD
2918269e767SBrooks Davisnor a valid file descriptor open for searching.
2928269e767SBrooks Davis.It Bq Er ENOTDIR
2938269e767SBrooks DavisThe
2948269e767SBrooks Davis.Fa from
2958269e767SBrooks Davisargument is not an absolute path and
2968269e767SBrooks Davis.Fa fromfd
2978269e767SBrooks Davisis neither
2988269e767SBrooks Davis.Dv AT_FDCWD
2998269e767SBrooks Davisnor a file descriptor associated with a directory, or the
3008269e767SBrooks Davis.Fa to
3018269e767SBrooks Davisargument is not an absolute path and
3028269e767SBrooks Davis.Fa tofd
3038269e767SBrooks Davisis neither
3048269e767SBrooks Davis.Dv AT_FDCWD
3058269e767SBrooks Davisnor a file descriptor associated with a directory.
3068269e767SBrooks Davis.It Bq Er ECAPMODE
3078269e767SBrooks Davis.Dv AT_FDCWD
3088269e767SBrooks Davisis specified and the process is in capability mode.
3098269e767SBrooks Davis.It Bq Er ENOTCAPABLE
3108269e767SBrooks Davis.Fa path
3118269e767SBrooks Davisis an absolute path or contained a ".." component leading to a directory
3128269e767SBrooks Davisoutside of the directory hierarchy specified by
3138269e767SBrooks Davis.Fa fromfd
3148269e767SBrooks Davisor
3158269e767SBrooks Davis.Fa tofd .
3168269e767SBrooks Davis.It Bq Er ENOTCAPABLE
3178269e767SBrooks DavisThe
3188269e767SBrooks Davis.Fa fromfd
3198269e767SBrooks Davisfile descriptor lacks the
3208269e767SBrooks Davis.Dv CAP_RENAMEAT_SOURCE
3218269e767SBrooks Davisright, or the
3228269e767SBrooks Davis.Fa tofd
3238269e767SBrooks Davisfile descriptor lacks the
3248269e767SBrooks Davis.Dv CAP_RENAMEAT_TARGET
3258269e767SBrooks Davisright.
3268269e767SBrooks Davis.El
3278269e767SBrooks Davis.Sh SEE ALSO
3288269e767SBrooks Davis.Xr chflags 2 ,
3298269e767SBrooks Davis.Xr open 2 ,
3308269e767SBrooks Davis.Xr symlink 7
3318269e767SBrooks Davis.Sh STANDARDS
3328269e767SBrooks DavisThe
3338269e767SBrooks Davis.Fn rename
3348269e767SBrooks Davissystem call is expected to conform to
3358269e767SBrooks Davis.St -p1003.1-96 .
3368269e767SBrooks DavisThe
3378269e767SBrooks Davis.Fn renameat
3388269e767SBrooks Davissystem call follows The Open Group Extended API Set 2 specification.
3398269e767SBrooks Davis.Sh HISTORY
3408269e767SBrooks DavisThe
3418269e767SBrooks Davis.Fn renameat
3428269e767SBrooks Davissystem call appeared in
3438269e767SBrooks Davis.Fx 8.0 .
344