Subject: bug#78245: rm -d fails to remove non-empty directory

From: Yannick Le Pennec <[email-masked]>
Date: Sun, 04 May 2025 12:35:52 +0200

Dear Maintainer,

On CephFS, creating a snapshot is done suchly:
  $ mkdir .snap/snapshot-name

This creates a directory which contains a view of the current directory
at the time the snapshot was created, and is therefore non-empty if the
current directory was non-empty at that time. Its contents are immutable.

Removing a snapshot is done suchly:
  $ rmdir .snap/snapshot-name

This works fine even if .snap/snapshot-name is non-empty, as rmdir calls
rmdir(2) directly without trying to be smart, and the Ceph filesystem
understands what rmdir on a snapshot directory means: remove the snapshot.

One would naturally expect rm -d to behave *exactly the same* as rmdir,
but this isn't the case, because rm -d first does a getdents on the
directory, observes it is non-empty, and refuses to perform the removal.

>From what I understand, this is non-compliant behavior per XCU rm 2.a. and 4.
and XSH remove():

https://pubs.opengroup.org/onlinepubs/9799919799/utilities/rm.html

> 2. If file is of type directory, the following steps shall be taken:
>   a. If neither the -R option nor the -r option is specified, but -d
>   is specified, rm shall proceed with step 3 for the current file.

> 4. rm shall perform actions equivalent to the remove() function defined
>   in the System Interfaces volume of POSIX.1-2024 called with a
>   pathname of the current file used as the path argument.

https://pubs.opengroup.org/onlinepubs/9799919799/functions/remove.html

>   If path names a directory, remove(path) shall be equivalent to rmdir(path)

In short:
   rm -d .snap/snapshot-name
shall be equivalent to:
   remove(".snap/snapshot-name")
which in turn shall be equivalent to:
   rmdir(".snap/snapshot-name")

Moreover other coreutils rm implementations (such as uutils or voreutils)
do not exhibit this bug and correctly remove non-empty directories.

Regards,

Yannick Le Pennec