xref: /freebsd/lib/libsys/unlink.2 (revision 7899f917b1c0ea178f1d2be0cfb452086d079d23)
1.\" Copyright (c) 1980, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.Dd February 23, 2021
29.Dt UNLINK 2
30.Os
31.Sh NAME
32.Nm unlink ,
33.Nm unlinkat ,
34.Nm funlinkat
35.Nd remove directory entry
36.Sh LIBRARY
37.Lb libc
38.Sh SYNOPSIS
39.In unistd.h
40.Ft int
41.Fn unlink "const char *path"
42.Ft int
43.Fn unlinkat "int dfd" "const char *path" "int flag"
44.Ft int
45.Fn funlinkat "int dfd" "const char *path" "int fd" "int flag"
46.Sh DESCRIPTION
47The
48.Fn unlink
49system call
50removes the link named by
51.Fa path
52from its directory and decrements the link count of the
53file which was referenced by the link.
54If that decrement reduces the link count of the file
55to zero,
56and no process has the file open, then
57all resources associated with the file are reclaimed.
58If one or more process have the file open when the last link is removed,
59the link is removed, but the removal of the file is delayed until
60all references to it have been closed.
61The
62.Fa path
63argument
64may not be a directory.
65.Pp
66The
67.Fn unlinkat
68system call is equivalent to
69.Fn unlink
70or
71.Fn rmdir
72except in the case where
73.Fa path
74specifies a relative path.
75In this case the directory entry to be removed is determined
76relative to the directory associated with the file descriptor
77.Fa dfd
78instead of the current working directory.
79.Pp
80The values for
81.Fa flag
82are constructed by a bitwise-inclusive OR of flags from the following list,
83defined in
84.In fcntl.h :
85.Bl -tag -width indent
86.It Dv AT_REMOVEDIR
87Remove the directory entry specified by
88.Fa fd
89and
90.Fa path
91as a directory, not a normal file.
92.It Dv AT_RESOLVE_BENEATH
93Only walk paths below the directory specified by the
94.Ar fd
95descriptor.
96See the description of the
97.Dv O_RESOLVE_BENEATH
98flag in the
99.Xr open 2
100manual page.
101.El
102.Pp
103If
104.Fn unlinkat
105is passed the special value
106.Dv AT_FDCWD
107in the
108.Fa fd
109parameter, the current working directory is used and the behavior is
110identical to a call to
111.Fa unlink
112or
113.Fa rmdir
114respectively, depending on whether or not the
115.Dv AT_REMOVEDIR
116bit is set in flag.
117.Pp
118The
119.Fn funlinkat
120system call can be used to unlink an already-opened file, unless that
121file has been replaced since it was opened.
122It is equivalent to
123.Fn unlinkat
124in the case where
125.Fa path
126is already open as the file descriptor
127.Fa fd .
128Otherwise, the path will not be removed and an error will be returned.
129The
130.Fa fd
131can be set the
132.Dv FD_NONE .
133In that case
134.Fn funlinkat
135behaves exactly like
136.Fn unlinkat .
137.Sh RETURN VALUES
138.Rv -std unlink
139.Sh ERRORS
140The
141.Fn unlink
142succeeds unless:
143.Bl -tag -width Er
144.It Bq Er ENOTDIR
145A component of the path prefix is not a directory.
146.It Bq Er EISDIR
147The named file is a directory.
148.It Bq Er ENAMETOOLONG
149A component of a pathname exceeded 255 characters,
150or an entire path name exceeded 1023 characters.
151.It Bq Er ENOENT
152The named file does not exist.
153.It Bq Er EACCES
154Search permission is denied for a component of the path prefix.
155.It Bq Er EACCES
156Write permission is denied on the directory containing the link
157to be removed.
158.It Bq Er ELOOP
159Too many symbolic links were encountered in translating the pathname.
160.It Bq Er EPERM
161The named file is a directory.
162.It Bq Er EPERM
163The named file has its immutable, undeletable or append-only flag set, see the
164.Xr chflags 2
165manual page for more information.
166.It Bq Er EPERM
167The parent directory of the named file has its immutable or append-only flag
168set.
169.It Bq Er EPERM
170The directory containing the file is marked sticky,
171and neither the containing directory nor the file to be removed
172are owned by the effective user ID.
173.It Bq Er EIO
174An I/O error occurred while deleting the directory entry
175or deallocating the inode.
176.It Bq Er EINTEGRITY
177Corrupted data was detected while reading from the file system.
178.It Bq Er EROFS
179The named file resides on a read-only file system.
180.It Bq Er EFAULT
181The
182.Fa path
183argument
184points outside the process's allocated address space.
185.It Bq Er ENOSPC
186On file systems supporting copy-on-write or snapshots, there was not enough
187free space to record metadata for the delete operation of the file.
188.El
189.Pp
190In addition to the errors returned by the
191.Fn unlink ,
192the
193.Fn unlinkat
194may fail if:
195.Bl -tag -width Er
196.It Bq Er EBADF
197The
198.Fa path
199argument does not specify an absolute path and the
200.Fa fd
201argument is neither
202.Dv AT_FDCWD
203nor a valid file descriptor open for searching.
204.It Bq Er ENOTEMPTY
205The
206.Fa flag
207parameter has the
208.Dv AT_REMOVEDIR
209bit set and the
210.Fa path
211argument names a directory that is not an empty directory,
212or there are hard links to the directory other than dot or
213a single entry in dot-dot.
214.It Bq Er ENOTDIR
215The
216.Fa flag
217parameter has the
218.Dv AT_REMOVEDIR
219bit set and
220.Fa path
221does not name a directory.
222.It Bq Er EINVAL
223The value of the
224.Fa flag
225argument is not valid.
226.It Bq Er ENOTDIR
227The
228.Fa path
229argument is not an absolute path and
230.Fa fd
231is neither
232.Dv AT_FDCWD
233nor a file descriptor associated with a directory.
234.It Bq Er ENOTCAPABLE
235.Fa path
236is an absolute path,
237or contained a ".." component leading to a
238directory outside of the directory hierarchy specified by
239.Fa fd ,
240and the process is in capability mode or the
241.Dv AT_RESOLVE_BENEATH
242flag was specified.
243.El
244.Pp
245In addition to the errors returned by
246.Fn unlinkat ,
247.Fn funlinkat
248may fail if:
249.Bl -tag -width Er
250.It Bq Er EDEADLK
251The file descriptor is not associated with the path.
252.El
253.Sh SEE ALSO
254.Xr chflags 2 ,
255.Xr close 2 ,
256.Xr link 2 ,
257.Xr rmdir 2 ,
258.Xr symlink 7
259.Sh STANDARDS
260The
261.Fn unlinkat
262system call follows The Open Group Extended API Set 2 specification.
263.Sh HISTORY
264The
265.Fn unlink
266function appeared in
267.At v1 .
268The
269.Fn unlinkat
270system call appeared in
271.Fx 8.0 .
272The
273.Fn funlinkat
274system call appeared in
275.Fx 13.0 .
276.Pp
277The
278.Fn unlink
279system call traditionally allows the super-user to unlink directories which
280can damage the file system integrity.
281This implementation no longer permits it.
282