git: bd1c0ff49c48 - main - Revert "fts: refactor to use fd-relative operations internally"

From: Alan Somers <asomers_at_FreeBSD.org>
Date: Wed, 24 Jun 2026 17:01:29 UTC
The branch main has been updated by asomers:

URL: https://cgit.FreeBSD.org/src/commit/?id=bd1c0ff49c48950dbd999c024a17199022f81d36

commit bd1c0ff49c48950dbd999c024a17199022f81d36
Author:     Alan Somers <asomers@FreeBSD.org>
AuthorDate: 2026-06-24 16:56:50 +0000
Commit:     Alan Somers <asomers@FreeBSD.org>
CommitDate: 2026-06-24 17:00:04 +0000

    Revert "fts: refactor to use fd-relative operations internally"
    
    This reverts commit e03ed9daeb49fffa1d16b8d00240c65e92650d01.
    
    The change to the size of struct FTSENT is breaking backwards
    compatibility for some binaries.  Jitendra is working on a new version
    that will move the new field into a private struct.
    
    Reported by:    bdrewery
    Fixes:          e03ed9daeb4 ("fts: refactor to use fd-relative operations")
    Sponsored by:   ConnectWise
---
 include/fts.h      |  1 -
 lib/libc/gen/fts.c | 38 +++++++++++---------------------------
 2 files changed, 11 insertions(+), 28 deletions(-)

diff --git a/include/fts.h b/include/fts.h
index 716103189dca..479905bda463 100644
--- a/include/fts.h
+++ b/include/fts.h
@@ -133,7 +133,6 @@ struct _ftsent {
 	struct stat *fts_statp;		/* stat(2) information */
 	char *fts_name;			/* file name */
 	FTS *fts_fts;			/* back pointer to main FTS */
-	int fts_dirfd;                  /* fd for parent directory */
 };
 
 #include <sys/cdefs.h>
diff --git a/lib/libc/gen/fts.c b/lib/libc/gen/fts.c
index 90d5abc49e60..e8063ecb646e 100644
--- a/lib/libc/gen/fts.c
+++ b/lib/libc/gen/fts.c
@@ -67,7 +67,6 @@ static FTSENT	*fts_build(FTS *, int);
 static void	 fts_lfree(FTSENT *);
 static void	 fts_load(FTS *, FTSENT *);
 static size_t	 fts_maxarglen(char * const *);
-static FTS	*__fts_open(FTS *, char * const *, int);
 static void	 fts_padjust(FTS *, FTSENT *);
 static int	 fts_palloc(FTS *, size_t);
 static FTSENT	*fts_sort(FTS *, FTSENT *, size_t);
@@ -130,7 +129,7 @@ static const char *ufslike_filesystems[] = {
 		default: 0)
 
 static FTS *
-__fts_open(FTS *sp, char * const *argv, int rootfd)
+__fts_open(FTS *sp, char * const *argv)
 {
 	FTSENT *p, *root;
 	FTSENT *parent, *tmp;
@@ -213,9 +212,9 @@ __fts_open(FTS *sp, char * const *argv, int rootfd)
 	 * descriptor we run anyway, just more slowly.
 	 */
 	if (!ISSET(FTS_NOCHDIR) &&
-	    (sp->fts_rfd = _openat(rootfd, ".", O_RDONLY |
-	    O_CLOEXEC, 0)) < 0)
+	    (sp->fts_rfd = _open(".", O_RDONLY | O_CLOEXEC, 0)) < 0)
 		SET(FTS_NOCHDIR);
+
 	return (sp);
 
 mem3:	fts_lfree(root);
@@ -251,7 +250,7 @@ fts_open(char * const *argv, int options,
 	sp->fts_compar = compar;
 	sp->fts_options = options;
 
-	return (__fts_open(sp, argv, AT_FDCWD));
+	return (__fts_open(sp, argv));
 }
 
 #ifdef __BLOCKS__
@@ -305,7 +304,7 @@ fts_open_b(char * const *argv, int options, fts_block compar)
 	sp->fts_compar_b = compar;
 	sp->fts_options = options | FTS_COMPAR_B;
 
-	if ((sp = __fts_open(sp, argv, AT_FDCWD)) == NULL) {
+	if ((sp = __fts_open(sp, argv)) == NULL) {
 #ifdef __BLOCKS__
 		Block_release(compar);
 #else
@@ -357,8 +356,6 @@ fts_close(FTS *sp)
 			p = p->fts_link != NULL ? p->fts_link : p->fts_parent;
 			free(freep);
 		}
-		if (p->fts_dirfd >= 0)
-			(void)_close(p->fts_dirfd);
 		free(p);
 	}
 
@@ -442,9 +439,8 @@ fts_read(FTS *sp)
 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
 		p->fts_info = fts_stat(sp, p, 1, -1);
 		if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
-			if ((p->fts_symfd = p->fts_dirfd >= 0 ?
-                            _dup(p->fts_dirfd) : _openat(AT_FDCWD, ".",
-                            O_RDONLY | O_CLOEXEC, 0)) < 0) {
+			if ((p->fts_symfd = _open(".", O_RDONLY | O_CLOEXEC,
+			    0)) < 0) {
 				p->fts_errno = errno;
 				p->fts_info = FTS_ERR;
 			} else
@@ -488,7 +484,7 @@ fts_read(FTS *sp)
 		 * FTS_STOP or the fts_info field of the node.
 		 */
 		if (sp->fts_child != NULL) {
-			if (fts_safe_changedir(sp, p, p->fts_dirfd, p->fts_name)) {
+			if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) {
 				p->fts_errno = errno;
 				p->fts_flags |= FTS_DONTCHDIR;
 				for (p = sp->fts_child; p != NULL;
@@ -536,9 +532,7 @@ next:	tmp = p;
 			p->fts_info = fts_stat(sp, p, 1, -1);
 			if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
 				if ((p->fts_symfd =
-                                    p->fts_dirfd >= 0 ?
-                                    _dup(p->fts_dirfd) : _openat(AT_FDCWD, ".",
-                                    O_RDONLY | O_CLOEXEC, 0)) < 0) {
+				    _open(".", O_RDONLY | O_CLOEXEC, 0)) < 0) {
 					p->fts_errno = errno;
 					p->fts_info = FTS_ERR;
 				} else
@@ -564,8 +558,6 @@ name:		t = sp->fts_path + NAPPEND(p->fts_parent);
 		 * can distinguish between error and EOF.
 		 */
 		free(tmp);
-		if (p->fts_dirfd >= 0)
-			(void)_close(p->fts_dirfd);
 		free(p);
 		errno = 0;
 		return (sp->fts_cur = NULL);
@@ -679,9 +671,7 @@ fts_children(FTS *sp, int instr)
 	    ISSET(FTS_NOCHDIR))
 		return (sp->fts_child = fts_build(sp, instr));
 
-	if ((fd = sp->fts_cur->fts_dirfd >= 0 ?
-            _dup(sp->fts_cur->fts_dirfd) : _openat(AT_FDCWD, ".",
-            O_RDONLY | O_CLOEXEC, 0)) < 0)
+	if ((fd = _open(".", O_RDONLY | O_CLOEXEC, 0)) < 0)
 		return (NULL);
 	sp->fts_child = fts_build(sp, instr);
 	serrno = (sp->fts_child == NULL) ? errno : 0;
@@ -917,7 +907,6 @@ mem1:				saved_errno = errno;
 		}
 
 		p->fts_level = level;
-		p->fts_dirfd = _dup(_dirfd(dirp));
 		p->fts_parent = sp->fts_cur;
 		p->fts_pathlen = len + dnamlen;
 
@@ -1196,7 +1185,6 @@ fts_alloc(FTS *sp, char *name, size_t namelen)
 		return (NULL);
 
 	p->fts_symfd = -1;
-	p->fts_dirfd = -1;
 	p->fts_path = sp->fts_path;
 	p->fts_name = (char *)(p + 1);
 	p->fts_namelen = namelen;
@@ -1217,8 +1205,6 @@ fts_lfree(FTSENT *head)
 	/* Free a linked list of structures. */
 	while ((p = head)) {
 		head = head->fts_link;
-		if (p->fts_dirfd >= 0)
-			(void)_close(p->fts_dirfd);
 		free(p);
 	}
 }
@@ -1336,9 +1322,7 @@ fts_ufslinks(FTS *sp, const FTSENT *ent)
 	 * avoidance.
 	 */
 	if (priv->ftsp_dev != ent->fts_dev) {
-		if ((ent->fts_dirfd >= 0 ?
-                    _fstatfs(ent->fts_dirfd, &priv->ftsp_statfs) :
-                    statfs(ent->fts_path, &priv->ftsp_statfs)) != -1) {
+		if (statfs(ent->fts_path, &priv->ftsp_statfs) != -1) {
 			priv->ftsp_dev = ent->fts_dev;
 			priv->ftsp_linksreliable = 0;
 			for (cpp = ufslike_filesystems; *cpp; cpp++) {