On 17/11/2026 03:30, Paul Eggert wrote:
On 2026-11-16 14:30, Pádraig Brady wrote:
After searching a bit more, I should add that the FreeBSD kernel
handles this transparently.

By "transparently" I assume you mean that on FreeBSD, "rm -r s/" ends by calling rmdir("s/") which removes 
the empty directory that the symlink s points to. In contrast, GNU/Linux "rm -r s/" ends by calling unlinkat(AT_FDCWD, 
"s/", AT_REMOVEDIR), the equivalent of rmdir("s/"), and this fails with ENOTDIR when s is a symlink to an empty (or 
nonempty) directory.

Exactly


As I understand it, the FreeBSD rmdir syscall conforms to a strict reading of 
POSIX, whereas the Linux unlinkat/rmdir syscall does not.

Perhaps something along the lines of:

if (errno == ENOTDIR) {
    char *resolved = canonicalize_filename_mode (ent->fts_path,
CAN_EXISTING);
    if (resolved && SAME_INODE (lstat(resolved), fts_statp)))
      unlinkat (AT_FDCWD, resolved, flag);
}

I'm mildly inclined to stick to the existing behavior, as any such workaround 
will introduce a race. My guess is that the Linux kernel folks intentionally 
deviated from POSIX in this area, and it's not clear that it's worthwhile to 
try to work around this particular deviation.

Right. Though the (very narrow after inode check) race is fairly benign,
as rmdir is fairly benign, only working on empty dirs.
I won't do this for the upcoming release at least,
but will think about it a bit more.

That being said, we should at least document the situation (and then change the 
documentation if we change the code later...). I installed the attached to 
document better what coreutils currently does. This affects the documentation 
for rm and for rmdir, and updates the already-existing documentation for mv in 
this area.

Thanks for doing that.
LGTM.


If we do change rm -r's behavior here, I assume we should make similar changes to rmdir

Yes rmdir should be changed similarly.

and to mv.

I suppose mv is related as you'd want cp;rm to match mv,
so therefore it would be overlapping with rm.
However cp is already different in this area wrt trailing slash,
so this is less of a requirement I think.

BTW I notice FreeBSD (cp userspace only apparently) has special meaning
for a trailing slash to mean the contents rather than the name itself,
so is best avoided for portable scripts.
For my reference for portable scripts that want to copy to
an existing "dest" directory:

  copy dir-link contents:     cp -R  dir-link/. dest/.
  copy to dest/dir-link/...:  cp -RH dir-link   dest/.

thanks,
Padraig




Reply via email to