Subject: Re: bug#58050: [INSTALLED] rm: fix diagnostics on I/O error

From: Paul Eggert <[email-masked]>
Date: Sun, 25 Sep 2022 18:37:49 -0700

On 9/25/22 07:25, Pádraig Brady wrote:
> How about the attached to add a NEWS entry,
> and add DS_EMPTY, DS_NONEMPTY enums to make the code easier to read?

Sure, that looks good; thanks.

Oh, I forgot that via code inspection I found a theoretical portability 
bug in fts while I was looking into Bug#58050. I fixed that by 
installing the attached into Gnulib.

From e00de604fd7012fd912f7580cd658ed9363ed6ad Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Sun, 25 Sep 2022 18:33:49 -0700
Subject: [PATCH] fts: fix errno handling if dirfd fails
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* lib/fts.c (fts_build): Use proper errno if dirfd failed.
Although I don’t know of any platform where dirfd can fail here,
we might as well get it right.
---
 ChangeLog | 7 +++++++
 lib/fts.c | 3 ++-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 24553445f6..6027e5ed94 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2022-09-25  Paul Eggert  <[email-masked]>
+
+	fts: fix errno handling if dirfd fails
+	* lib/fts.c (fts_build): Use proper errno if dirfd failed.
+	Although I don’t know of any platform where dirfd can fail here,
+	we might as well get it right.
+
 2022-09-25  Bruno Haible  <[email-masked]>
 
 	stdbool: Mostly revert last patch.
diff --git a/lib/fts.c b/lib/fts.c
index 954cbb7b40..5811f6ea20 100644
--- a/lib/fts.c
+++ b/lib/fts.c
@@ -1290,11 +1290,12 @@ fts_build (register FTS *sp, int type)
             dir_fd = dirfd (dp);
             if (dir_fd < 0)
               {
+                int dirfd_errno = errno;
                 closedir_and_clear (cur->fts_dirp);
                 if (type == BREAD)
                   {
                     cur->fts_info = FTS_DNR;
-                    cur->fts_errno = errno;
+                    cur->fts_errno = dirfd_errno;
                   }
                 return NULL;
               }
-- 
2.37.3