RMDIR(3POSIX)

πŸ’– RMDIR(3POSIX) πŸ’–

okay bestie, sit DOWN. we're doing the POSIX Programmer's Manual today, and this one is rmdir β€” the girl whose whole job is deleting directories. she's small, she's picky, she has OPINIONS. let's get into it πŸŽ€


🚨 PROLOG (read this first, no cap) 🚨

This manual page is part of the POSIX Programmer's Manual, bestie. The Linux implementation of this interface may differ β€” so like, consult the corresponding Linux manual page for details of Linux behavior β€” or the interface may not even be implemented on Linux. πŸ™ˆ iykyk.


✨ NAME ✨

rmdir β€” remove a directory πŸ’…


🌸 SYNOPSIS 🌸

#include <unistd.h>

int rmdir(const char *path);

that's it. that's the whole vibe. one function, one path, so much drama.


πŸ¦‹ DESCRIPTION πŸ¦‹

The rmdir() function shall remove a directory whose name is given by path. and listen β€” the directory shall be removed only if it is an empty directory. she does not do clutter. empty or nothing. 🧹

If the directory is the root directory or the current working directory of any process, it is unspecified whether the function succeeds, or whether it shall fail and set errno to [EBUSY]. yeah. unspecified. the standard is being coy about it. 😭

If path names a symbolic link, then rmdir() shall fail and set errno to [ENOTDIR]. no symlink shenanigans allowed, periodt.

If the path argument refers to a path whose final component is either dot or dot-dot, rmdir() shall fail. you cannot delete . or ... absolutely not. not on my watch. πŸ’€

If the directory's link count becomes 0 and no process has the directory open, the space occupied by the directory shall be freed and the directory shall no longer be accessible. If one or more processes have the directory open when the last link is removed, the dot and dot-dot entries, if present, shall be removed before rmdir() returns and no new entries may be created in the directory, but the directory shall not be removed until all references to the directory are closed. so like β€” she's marked, she's sealed, but she's still lingering until everybody lets go. ghosting but make it filesystem. πŸ‘»

If the directory is not an empty directory, rmdir() shall fail and set errno to [EEXIST] or [ENOTEMPTY]. two errors, one situation, iconic.

Upon successful completion, rmdir() shall mark for update the last data modification and last file status change timestamps of the parent directory. yes bestie, mom gets the timestamp update, not the deleted child. that's just how it is. ✨


πŸ”₯ RETURN VALUE πŸ”₯

Upon successful completion, the function rmdir() shall return 0. slay.

Otherwise, -1 shall be returned, and errno set to indicate the error. If -1 is returned, the named directory shall not be changed. no half-deletes, no chaos, the directory stays exactly how it was. 🫢


πŸ’€ ERRORS πŸ’€

The rmdir() function shall fail if:

  • EACCES β€” Search permission is denied on a component of the path prefix, or write permission is denied on the parent directory of the directory to be removed. permission said no 🚫
  • EBUSY β€” The directory to be removed is currently in use by the system or some process and the implementation considers this to be an error. she's busy, bestie, try later.
  • [EEXIST] or [ENOTEMPTY] β€” The path argument names a directory that is not an empty directory, or there are hard links to the directory other than dot or a single entry in dot-dot.
  • EINVAL β€” The path argument contains a last component that is dot. nope. not dot. never dot.
  • EIO β€” A physical I/O error has occurred. the hardware is going through something 😭
  • ELOOP β€” A loop exists in symbolic links encountered during resolution of the path argument. she's going in circles, literally.
  • ENAMETOOLONG β€” The length of a component of a pathname is longer than {NAME_MAX}.
  • ENOENT β€” A component of path does not name an existing file, or the path argument names a nonexistent directory or points to an empty string.
  • ENOTDIR β€” A component of path names an existing file that is neither a directory nor a symbolic link to a directory.
  • [EPERM] or [EACCES] β€” The S_ISVTX flag is set on the directory containing the file referred to by the path argument and the process does not satisfy the criteria specified in the Base Definitions volume of POSIX.1‐2017, Section 4.3, Directory Protection.
  • EROFS β€” The directory entry to be removed resides on a read-only file system. read-only means read-only, bestie, stop pushing πŸ”’

The rmdir() function may fail if:

  • ELOOP β€” More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the path argument.
  • ENAMETOOLONG β€” The length of a pathname exceeds {PATH_MAX}, or pathname resolution of a symbolic link produced an intermediate result with a length that exceeds {PATH_MAX}.

The following sections are informative. (translation: the drama is over, this is just lore now πŸ“)


🌈 EXAMPLES 🌈

Removing a Directory πŸ’«

The following example shows how to remove a directory named /home/cnd/mod1.

#include <unistd.h>

int status;
...
status = rmdir("/home/cnd/mod1");

look at her go. one call. so clean. so powerful. ✨


πŸŽ€ APPLICATION USAGE πŸŽ€

None.

(genuinely nothing. she said no notes. 😌)


πŸ‘‘ RATIONALE πŸ‘‘

okay this is the lore section and it's lowkey juicy.

The rmdir() and rename() functions originated in 4.2 BSD, and they used [ENOTEMPTY] for the condition when the directory to be removed does not exist or new already exists. When the 1984 /usr/group standard was published, it contained [EEXIST] instead. When these functions were adopted into System V, the 1984 /usr/group standard was used as a reference. Therefore, several existing applications and implementations support/use both forms, and no agreement could be reached on either value. All implementations are required to supply both [EEXIST] and [ENOTEMPTY] in <errno.h> with distinct values, so that applications can use both values in C-language case statements.

the way that TWO whole standards beefed over one error code and we all just have to live with both?? πŸ’€ iconic behavior honestly.

The meaning of deleting pathname/dot is unclear, because the name of the file (directory) in the parent directory to be removed is not clear, particularly in the presence of multiple links to a directory.

The POSIX.1‐1990 standard was silent with regard to the behavior of rmdir() when there are multiple hard links to the directory being removed. The requirement to set errno to [EEXIST] or [ENOTEMPTY] clarifies the behavior in this case.

If the current working directory of the process is being removed, that should be an allowed error.

Virtually all existing implementations detect [ENOTEMPTY] or the case of dot-dot. The text in Section 2.3, Error Numbers about returning any one of the possible errors permits that behavior to continue. The [ELOOP] error may be returned if more than {SYMLOOP_MAX} symbolic links are encountered during resolution of the path argument.


🧚 FUTURE DIRECTIONS 🧚

None.

(no glow-up planned. she's perfect as she is. πŸ’…)


πŸ’œ SEE ALSO πŸ’œ

  • Section 2.3, Error Numbers
  • mkdir()
  • remove()
  • rename()
  • unlink()

The Base Definitions volume of POSIX.1‐2017, Section 4.3, Directory Protection, <unistd.h>


Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


and that's the whole page bestie!! brought to you by IEEE/The Open Group, 2017 β€” aka the girls who wrote RMDIR(3POSIX). stan them or whatever πŸ’– now go delete an empty directory, responsibly. πŸŒΈπŸ¦‹βœ¨