[libcxx-commits] [libcxx] [libc++] Fix filesystem::remove_all() on FreeBSD (PR #79540)

Mark Johnston via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jan 25 18:34:26 PST 2024


https://github.com/markjdb created https://github.com/llvm/llvm-project/pull/79540

See the commit message for an explanation.

I'm not sure if this bug is the reason for the `XFAIL: LIBCXX-FREEBSD-FIXME` annotation in test/std/input.output/filesystems/fs.op.funcs/fs.op.remove_all/remove_all.pass.cpp. @emaste do you have any idea?

>From e66ce8f0af00c848c91453e73532e6c545607d82 Mon Sep 17 00:00:00 2001
From: Mark Johnston <markj at FreeBSD.org>
Date: Thu, 25 Jan 2024 21:29:01 -0500
Subject: [PATCH] [libc++] Fix filesystem::remove_all() on FreeBSD

remove_all_impl() opens the target path with O_NOFOLLOW, which fails if
the target is a symbolic link.  On FreeBSD, rather than returning ELOOP,
openat() returns EMLINK.  This is unlikely to change for compatibility
reasons, see https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=214633 .

Thus, check for EMLINK as well.
---
 libcxx/src/filesystem/operations.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/libcxx/src/filesystem/operations.cpp b/libcxx/src/filesystem/operations.cpp
index 6253d1551b062ea..c39a3ee53db4394 100644
--- a/libcxx/src/filesystem/operations.cpp
+++ b/libcxx/src/filesystem/operations.cpp
@@ -815,8 +815,10 @@ uintmax_t remove_all_impl(int parent_directory, const path& p, error_code& ec) {
 
   // If opening `p` failed because it wasn't a directory, remove it as
   // a normal file instead. Note that `openat()` can return either ENOTDIR
-  // or ELOOP depending on the exact reason of the failure.
-  if (ec == errc::not_a_directory || ec == errc::too_many_symbolic_link_levels) {
+  // or ELOOP depending on the exact reason of the failure. On FreeBSD it
+  // may return EMLINK instead of ELOOP, contradicting POSIX.
+  if (ec == errc::not_a_directory || ec == errc::too_many_symbolic_link_levels ||
+      ec == errc::too_many_links) {
     ec.clear();
     if (::unlinkat(parent_directory, p.c_str(), /* flags = */ 0) == -1) {
       ec = detail::capture_errno();



More information about the libcxx-commits mailing list