When I build a testdir for module 'copy-file' on native Windows, I see this
compilation error:
x86_64-w64-mingw32-gcc -std=gnu23 -DHAVE_CONFIG_H -I. -I../../gllib -I..
-DGNULIB_STRICT_CHECKING=1 -Wall -DCONTINUE_AFTER_ASSERT -g -O2 -MT close.o
-MD -MP -MF $depbase.Tpo -c -o close.o ../../gllib/close.c &&\
mv -f $depbase.Tpo $depbase.Po
../../gllib/close.c: In function 'rpl_close':
../../gllib/close.c:71:5: error: implicit declaration of function
'_gl_unregister_fd' [-Wimplicit-function-declaration]
71 | _gl_unregister_fd (fd);
| ^~~~~~~~~~~~~~~~~
make[4]: *** [Makefile:2823: close.o] Error 1
The cause is that the 'close' module sits in gllib/, whereas the 'fchdir'
module with its per-fd information about directories (and the _gl_unregister_fd
function) sits in gltests/. A function in gllib/ cannot invoke a function in
gltests/. But the close() function must be able to access the per-fd
information, since its argument may be a directory fd opened by some
code in gltests/. (And we cannot have two different close() functions
in gllib/ and in gltests/, since that would lead to all sorts of linking
trouble.)
A possible workaround is to use a testdir for
copy-file fchdir
instead of only
fchdir
But this is ugly.
This bug is known for many years already. It has caused problems in GNU poke,
which has two gnulib-tool invocations: one for libpoke, one for poke. The
situation is similar: poke can pass file descriptors to libpoke, but
libpoke cannot call functions in poke.
This patch fixes it, by splitting the 'fchdir' module into three modules:
- dirinfo-abstract: the abstract API for the per-fd information,
- dirinfo-impl: the concrete implementation of that API,
- fchdir: only the fchdir() function.
This separation of modules allows for
- close and dirinfo-abstract to live in gllib/,
- while fchdir and dirinfo-impl live in gltests/.
And similarly for libpoke vs. poke.
The dirinfo-abstract module is conceptually similar to the 'fd-hook' module,
except that
- it is simpler,
- it deals with a different kind of file descriptors (directories, not
sockets).
2026-11-25 Bruno Haible <[email protected]>
fchdir: Fix link error on native Windows in some situations.
Reported by Hannes Domani in
<https://lists.gnu.org/archive/html/poke-devel/2024-02/msg00060.html>.
* lib/dirinfo-abstract.h: New file, based on lib/fchdir.c.
* lib/dirinfo-abstract.c: New file, based on lib/fchdir.c.
* lib/dirinfo-impl.h: New file, based on lib/fchdir.c.
* lib/dirinfo-impl.c: New file, based on lib/fchdir.c.
* lib/fchdir.c: Remove definitions that have been moved to
dirinfo-impl.c. Include dirinfo-abstract.h.
* lib/unistd.in.h (_gl_register_fd, _gl_unregister_fd, _gl_register_dup,
_gl_directory_name): Remove declarations.
* lib/close.c: Include dirinfo-abstract.h.
* lib/dup.c: Likewise.
* lib/dup2.c: Likewise.
* lib/dup3.c: Likewise.
* lib/fclose.c: Likewise.
* lib/fcntl.c: Likewise.
* lib/fdopendir.c: Likewise.
* lib/closedir.c: Include dirinfo-abstract.h instead of <unistd.h>.
* lib/fstat.c: Likewise.
* lib/open.c: Include dirinfo-impl.h instead of <unistd.h>.
* lib/opendir.c: Likewise.
* m4/fchdir.m4 (gl_FUNC_FCHDIR): Define through AC_DEFUN_ONCE.
* modules/dirinfo-abstract: New file.
* modules/dirinfo-impl: New file.
* modules/fchdir (Depends-on): Just depend on dirinfo-abstract.
* modules/close (Depends-on): Add dirinfo-abstract.
* modules/closedir (Depends-on): Likewise.
* modules/dup (Depends-on): Likewise.
* modules/dup2 (Depends-on): Likewise.
* modules/dup3 (Depends-on): Likewise.
* modules/fclose (Depends-on): Likewise.
* modules/fcntl (Depends-on): Likewise.
* modules/fdopendir (Depends-on): Likewise.
* modules/fstat (Depends-on): Likewise.
* modules/open (Depends-on): Add dirinfo-impl.
* modules/opendir (Depends-on): Add dirinfo-impl instead of unistd-h.
>From e18187815d51d391ea3eda4eb944b764e2b52343 Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Thu, 26 Nov 2026 02:16:51 +0100
Subject: [PATCH] fchdir: Fix link error on native Windows in some situations.
Reported by Hannes Domani in
<https://lists.gnu.org/archive/html/poke-devel/2024-02/msg00060.html>.
* lib/dirinfo-abstract.h: New file, based on lib/fchdir.c.
* lib/dirinfo-abstract.c: New file, based on lib/fchdir.c.
* lib/dirinfo-impl.h: New file, based on lib/fchdir.c.
* lib/dirinfo-impl.c: New file, based on lib/fchdir.c.
* lib/fchdir.c: Remove definitions that have been moved to
dirinfo-impl.c. Include dirinfo-abstract.h.
* lib/unistd.in.h (_gl_register_fd, _gl_unregister_fd, _gl_register_dup,
_gl_directory_name): Remove declarations.
* lib/close.c: Include dirinfo-abstract.h.
* lib/dup.c: Likewise.
* lib/dup2.c: Likewise.
* lib/dup3.c: Likewise.
* lib/fclose.c: Likewise.
* lib/fcntl.c: Likewise.
* lib/fdopendir.c: Likewise.
* lib/closedir.c: Include dirinfo-abstract.h instead of <unistd.h>.
* lib/fstat.c: Likewise.
* lib/open.c: Include dirinfo-impl.h instead of <unistd.h>.
* lib/opendir.c: Likewise.
* m4/fchdir.m4 (gl_FUNC_FCHDIR): Define through AC_DEFUN_ONCE.
* modules/dirinfo-abstract: New file.
* modules/dirinfo-impl: New file.
* modules/fchdir (Depends-on): Just depend on dirinfo-abstract.
* modules/close (Depends-on): Add dirinfo-abstract.
* modules/closedir (Depends-on): Likewise.
* modules/dup (Depends-on): Likewise.
* modules/dup2 (Depends-on): Likewise.
* modules/dup3 (Depends-on): Likewise.
* modules/fclose (Depends-on): Likewise.
* modules/fcntl (Depends-on): Likewise.
* modules/fdopendir (Depends-on): Likewise.
* modules/fstat (Depends-on): Likewise.
* modules/open (Depends-on): Add dirinfo-impl.
* modules/opendir (Depends-on): Add dirinfo-impl instead of unistd-h.
---
ChangeLog | 40 ++++++++
lib/close.c | 5 +
lib/closedir.c | 6 +-
lib/dirinfo-abstract.c | 68 ++++++++++++++
lib/dirinfo-abstract.h | 60 ++++++++++++
lib/dirinfo-impl.c | 203 +++++++++++++++++++++++++++++++++++++++++
lib/dirinfo-impl.h | 45 +++++++++
lib/dup.c | 4 +
lib/dup2.c | 4 +
lib/dup3.c | 4 +
lib/fchdir.c | 169 +---------------------------------
lib/fclose.c | 3 +
lib/fcntl.c | 4 +
lib/fdopendir.c | 4 +
lib/fstat.c | 5 +-
lib/open.c | 5 +-
lib/opendir.c | 2 +-
lib/unistd.in.h | 8 --
m4/fchdir.m4 | 4 +-
modules/close | 5 +-
modules/closedir | 3 +-
modules/dup | 3 +-
modules/dup2 | 5 +-
modules/dup3 | 1 +
modules/fchdir | 20 +---
modules/fclose | 13 +--
modules/fcntl | 9 +-
modules/fdopendir | 19 ++--
modules/fstat | 12 +--
modules/open | 1 +
modules/opendir | 2 +-
31 files changed, 501 insertions(+), 235 deletions(-)
create mode 100644 lib/dirinfo-abstract.c
create mode 100644 lib/dirinfo-abstract.h
create mode 100644 lib/dirinfo-impl.c
create mode 100644 lib/dirinfo-impl.h
diff --git a/ChangeLog b/ChangeLog
index 7efa1f1c4a..4d261d5508 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,43 @@
+2026-11-25 Bruno Haible <[email protected]>
+
+ fchdir: Fix link error on native Windows in some situations.
+ Reported by Hannes Domani in
+ <https://lists.gnu.org/archive/html/poke-devel/2024-02/msg00060.html>.
+ * lib/dirinfo-abstract.h: New file, based on lib/fchdir.c.
+ * lib/dirinfo-abstract.c: New file, based on lib/fchdir.c.
+ * lib/dirinfo-impl.h: New file, based on lib/fchdir.c.
+ * lib/dirinfo-impl.c: New file, based on lib/fchdir.c.
+ * lib/fchdir.c: Remove definitions that have been moved to
+ dirinfo-impl.c. Include dirinfo-abstract.h.
+ * lib/unistd.in.h (_gl_register_fd, _gl_unregister_fd, _gl_register_dup,
+ _gl_directory_name): Remove declarations.
+ * lib/close.c: Include dirinfo-abstract.h.
+ * lib/dup.c: Likewise.
+ * lib/dup2.c: Likewise.
+ * lib/dup3.c: Likewise.
+ * lib/fclose.c: Likewise.
+ * lib/fcntl.c: Likewise.
+ * lib/fdopendir.c: Likewise.
+ * lib/closedir.c: Include dirinfo-abstract.h instead of <unistd.h>.
+ * lib/fstat.c: Likewise.
+ * lib/open.c: Include dirinfo-impl.h instead of <unistd.h>.
+ * lib/opendir.c: Likewise.
+ * m4/fchdir.m4 (gl_FUNC_FCHDIR): Define through AC_DEFUN_ONCE.
+ * modules/dirinfo-abstract: New file.
+ * modules/dirinfo-impl: New file.
+ * modules/fchdir (Depends-on): Just depend on dirinfo-abstract.
+ * modules/close (Depends-on): Add dirinfo-abstract.
+ * modules/closedir (Depends-on): Likewise.
+ * modules/dup (Depends-on): Likewise.
+ * modules/dup2 (Depends-on): Likewise.
+ * modules/dup3 (Depends-on): Likewise.
+ * modules/fclose (Depends-on): Likewise.
+ * modules/fcntl (Depends-on): Likewise.
+ * modules/fdopendir (Depends-on): Likewise.
+ * modules/fstat (Depends-on): Likewise.
+ * modules/open (Depends-on): Add dirinfo-impl.
+ * modules/opendir (Depends-on): Add dirinfo-impl instead of unistd-h.
+
2026-11-25 Bruno Haible <[email protected]>
linkat, renameat, renameatu: Fix dependencies.
diff --git a/lib/close.c b/lib/close.c
index 86ccdf9ad0..5e40727a65 100644
--- a/lib/close.c
+++ b/lib/close.c
@@ -22,6 +22,11 @@
#include <errno.h>
#include "fd-hook.h"
+
+#if REPLACE_FCHDIR
+# include "dirinfo-abstract.h"
+#endif
+
#if HAVE_MSVC_INVALID_PARAMETER_HANDLER
# include "msvc-inval.h"
#endif
diff --git a/lib/closedir.c b/lib/closedir.c
index 8de37d3369..62d993783d 100644
--- a/lib/closedir.c
+++ b/lib/closedir.c
@@ -19,12 +19,12 @@
/* Specification. */
#include <dirent.h>
+#include <stdlib.h>
+
#if REPLACE_FCHDIR
-# include <unistd.h>
+# include "dirinfo-abstract.h"
#endif
-#include <stdlib.h>
-
#if HAVE_CLOSEDIR
/* Override closedir(), to keep track of the open file descriptors.
diff --git a/lib/dirinfo-abstract.c b/lib/dirinfo-abstract.c
new file mode 100644
index 0000000000..9f820ebece
--- /dev/null
+++ b/lib/dirinfo-abstract.c
@@ -0,0 +1,68 @@
+/* Abstract interface of directory information per file descriptor.
+ Copyright (C) 2006-2026 Free Software Foundation, Inc.
+
+ This file is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This file is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>. */
+
+/* Written by Bruno Haible. */
+
+#include <config.h>
+
+/* Specification. */
+#include "dirinfo-abstract.h"
+
+#include <errno.h>
+#include <stddef.h>
+#include <unistd.h>
+
+static const struct dirinfo_vtable_t *impl_vtable /* = NULL */;
+
+void
+_gl_set_dirinfo_impl (const struct dirinfo_vtable_t *vtable)
+{
+ impl_vtable = vtable;
+}
+
+void
+_gl_unregister_fd (int fd)
+{
+ if (impl_vtable != NULL)
+ impl_vtable->unregister_fd (fd);
+}
+
+int
+_gl_register_dup (int oldfd, int newfd)
+{
+ if (impl_vtable != NULL)
+ return impl_vtable->register_dup (oldfd, newfd);
+ else
+ return newfd;
+}
+
+const char *
+_gl_directory_name (int fd)
+{
+ if (impl_vtable != NULL)
+ return impl_vtable->directory_name (fd);
+ else
+ {
+ if (0 <= fd)
+ {
+ if (dup2 (fd, fd) == fd)
+ errno = ENOTDIR;
+ }
+ else
+ errno = EBADF;
+ return NULL;
+ }
+}
diff --git a/lib/dirinfo-abstract.h b/lib/dirinfo-abstract.h
new file mode 100644
index 0000000000..8a7f677372
--- /dev/null
+++ b/lib/dirinfo-abstract.h
@@ -0,0 +1,60 @@
+/* Abstract interface of directory information per file descriptor.
+ Copyright (C) 2006-2026 Free Software Foundation, Inc.
+
+ This file is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This file is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>. */
+
+/* Written by Bruno Haible. */
+
+#ifndef _DIRINFO_ABSTRACT_H
+#define _DIRINFO_ABSTRACT_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/* Close FD, cleaning up any fd to name mapping if fd was visiting a
+ directory. */
+extern void _gl_unregister_fd (int fd);
+
+/* Mark NEWFD as a duplicate of OLDFD; useful from dup, dup2, dup3,
+ and fcntl. Both arguments must be valid and distinct file
+ descriptors. Close NEWFD and return -1 if OLDFD is tracking a
+ directory, but there is insufficient memory to track the same
+ directory in NEWFD; otherwise return NEWFD. */
+extern int _gl_register_dup (int oldfd, int newfd);
+
+/* If FD is currently visiting a directory, then return the name of
+ that directory. Otherwise, return NULL and set errno. */
+extern const char *_gl_directory_name (int fd);
+
+
+/* An implementation of these functions. */
+struct dirinfo_vtable_t
+{
+ void (*unregister_fd) (int fd);
+ int (*register_dup) (int oldfd, int newfd);
+ const char * (*directory_name) (int fd);
+};
+
+/* Set the implementation of these functions.
+ Use a null VTABLE to withdraw the previously set implementation. */
+extern void _gl_set_dirinfo_impl (const struct dirinfo_vtable_t *vtable);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _DIRINFO_ABSTRACT_H */
diff --git a/lib/dirinfo-impl.c b/lib/dirinfo-impl.c
new file mode 100644
index 0000000000..c3ac30178d
--- /dev/null
+++ b/lib/dirinfo-impl.c
@@ -0,0 +1,203 @@
+/* Implementation of directory information per file descriptor.
+ Copyright (C) 2006-2026 Free Software Foundation, Inc.
+
+ This file is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This file is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>. */
+
+/* Written by Bruno Haible. */
+
+#include <config.h>
+
+/* Specification. */
+#include "dirinfo-impl.h"
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include "assure.h"
+#include "dirinfo-abstract.h"
+#include "filename.h"
+#include "filenamecat.h"
+
+#ifndef REPLACE_OPEN_DIRECTORY
+# define REPLACE_OPEN_DIRECTORY 0
+#endif
+
+/* This module that a directory is not renamed while opened through
+ a file descriptor.
+
+ FIXME: On mingw, this would be possible to enforce if we were to
+ also open a HANDLE to each directory currently visited by a file
+ descriptor, since mingw refuses to rename any in-use file system
+ object. */
+
+/* Array of file descriptors opened. If REPLACE_OPEN_DIRECTORY or if it points
+ to a directory, it stores info about this directory. */
+typedef struct
+{
+ char *name; /* Absolute name of the directory, or NULL. */
+} dir_info_t;
+static dir_info_t *dirs;
+static size_t dirs_allocated;
+
+/* Try to ensure dirs has enough room for a slot at index fd; free any
+ contents already in that slot. Return false and set errno to
+ ENOMEM on allocation failure. */
+static bool
+ensure_dirs_slot (size_t fd)
+{
+ if (fd < dirs_allocated)
+ free (dirs[fd].name);
+ else
+ {
+ size_t new_allocated = 2 * dirs_allocated + 1;
+ if (new_allocated <= fd)
+ new_allocated = fd + 1;
+ dir_info_t *new_dirs =
+ (dirs != NULL
+ ? (dir_info_t *) realloc (dirs, new_allocated * sizeof *dirs)
+ : (dir_info_t *) malloc (new_allocated * sizeof *dirs));
+ if (new_dirs == NULL)
+ return false;
+ memset (new_dirs + dirs_allocated, 0,
+ (new_allocated - dirs_allocated) * sizeof *dirs);
+ dirs = new_dirs;
+ dirs_allocated = new_allocated;
+ }
+ return true;
+}
+
+/* Return an absolute name of DIR in malloc'd storage.
+ Upon failure, return NULL with errno set. */
+static char *
+get_name (char const *dir)
+{
+ if (IS_ABSOLUTE_FILE_NAME (dir))
+ return strdup (dir);
+
+ /* We often encounter "."; treat it as a special case. */
+ char *cwd = getcwd (NULL, 0);
+ if (!cwd || (dir[0] == '.' && dir[1] == '\0'))
+ return cwd;
+
+ char *result = mfile_name_concat (cwd, dir, NULL);
+ free (cwd);
+ return result;
+}
+
+/* Hook into the gnulib replacements for open() and close() to keep track
+ of the open file descriptors. */
+
+/* Close FD, cleaning up any fd to name mapping if fd was visiting a
+ directory. */
+static void
+unregister_fd (int fd)
+{
+ if (fd >= 0 && fd < dirs_allocated)
+ {
+ free (dirs[fd].name);
+ dirs[fd].name = NULL;
+ }
+}
+
+/* Mark NEWFD as a duplicate of OLDFD; useful from dup, dup2, dup3,
+ and fcntl. Both arguments must be valid and distinct file
+ descriptors. Close NEWFD and return -1 if OLDFD is tracking a
+ directory, but there is insufficient memory to track the same
+ directory in NEWFD; otherwise return NEWFD. */
+static int
+register_dup (int oldfd, int newfd)
+{
+ assure (0 <= oldfd && 0 <= newfd && oldfd != newfd);
+ if (oldfd < dirs_allocated && dirs[oldfd].name)
+ {
+ /* Duplicated a directory; must ensure newfd is allocated. */
+ if (!ensure_dirs_slot (newfd)
+ || (dirs[newfd].name = strdup (dirs[oldfd].name)) == NULL)
+ {
+ int saved_errno = errno;
+ close (newfd);
+ errno = saved_errno;
+ newfd = -1;
+ }
+ }
+ else if (newfd < dirs_allocated)
+ {
+ /* Duplicated a non-directory; ensure newfd is cleared. */
+ free (dirs[newfd].name);
+ dirs[newfd].name = NULL;
+ }
+ return newfd;
+}
+
+/* If FD is currently visiting a directory, then return the name of
+ that directory. Otherwise, return NULL and set errno. */
+static const char *
+directory_name (int fd)
+{
+ if (0 <= fd && fd < dirs_allocated && dirs[fd].name != NULL)
+ return dirs[fd].name;
+ /* At this point, fd is either invalid, or open but not a directory.
+ If dup2 fails, errno is correctly EBADF. */
+ if (0 <= fd)
+ {
+ if (dup2 (fd, fd) == fd)
+ errno = ENOTDIR;
+ }
+ else
+ errno = EBADF;
+ return NULL;
+}
+
+static struct dirinfo_vtable_t this_vtable =
+ {
+ unregister_fd,
+ register_dup,
+ directory_name
+ };
+
+/* Mark FD as visiting FILENAME. FD must be non-negative, and refer
+ to an open file descriptor. If REPLACE_OPEN_DIRECTORY is non-zero,
+ this should only be called if FD is visiting a directory. Close FD
+ and return -1 with errno set if there is insufficient memory to track
+ the directory name; otherwise return FD. */
+int
+_gl_register_fd (int fd, const char *filename)
+{
+ assure (0 <= fd);
+
+ /* At the first invocation of this function, we start to have file descriptors
+ that may correspond to directories. Now is the time to register this
+ implementation's vtable.
+ It does not hurt to execute this at every invocation; it is fast enough. */
+ _gl_set_dirinfo_impl (&this_vtable);
+
+ struct stat statbuf;
+ if (REPLACE_OPEN_DIRECTORY
+ || (fstat (fd, &statbuf) == 0 && S_ISDIR (statbuf.st_mode)))
+ {
+ if (!ensure_dirs_slot (fd)
+ || (dirs[fd].name = get_name (filename)) == NULL)
+ {
+ int saved_errno = errno;
+ close (fd);
+ errno = saved_errno;
+ return -1;
+ }
+ }
+ return fd;
+}
diff --git a/lib/dirinfo-impl.h b/lib/dirinfo-impl.h
new file mode 100644
index 0000000000..55be4968dc
--- /dev/null
+++ b/lib/dirinfo-impl.h
@@ -0,0 +1,45 @@
+/* Implementation of directory information per file descriptor.
+ Copyright (C) 2006-2026 Free Software Foundation, Inc.
+
+ This file is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This file is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>. */
+
+/* Written by Bruno Haible. */
+
+#ifndef _DIRINFO_IMPL_H
+#define _DIRINFO_IMPL_H
+
+/* This file uses _GL_ATTRIBUTE_NONNULL. */
+#if !_GL_CONFIG_H_INCLUDED
+ #error "Please include config.h first."
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/* Mark FD as visiting FILENAME. FD must be non-negative, and refer
+ to an open file descriptor. If REPLACE_OPEN_DIRECTORY is non-zero,
+ this should only be called if FD is visiting a directory. Close FD
+ and return -1 with errno set if there is insufficient memory to track
+ the directory name; otherwise return FD. */
+extern int _gl_register_fd (int fd, const char *filename)
+ _GL_ATTRIBUTE_NONNULL ((2));
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _DIRINFO_IMPL_H */
diff --git a/lib/dup.c b/lib/dup.c
index 23a11c5ffc..5b49ab005a 100644
--- a/lib/dup.c
+++ b/lib/dup.c
@@ -22,6 +22,10 @@
#include <errno.h>
+#if REPLACE_FCHDIR
+# include "dirinfo-abstract.h"
+#endif
+
#if HAVE_MSVC_INVALID_PARAMETER_HANDLER
# include "msvc-inval.h"
#endif
diff --git a/lib/dup2.c b/lib/dup2.c
index b9a55263be..6f6a3a411e 100644
--- a/lib/dup2.c
+++ b/lib/dup2.c
@@ -25,6 +25,10 @@
#include <errno.h>
#include <fcntl.h>
+#if REPLACE_FCHDIR
+# include "dirinfo-abstract.h"
+#endif
+
#undef dup2
#if defined _WIN32 && ! defined __CYGWIN__
diff --git a/lib/dup3.c b/lib/dup3.c
index 7419327b1d..60e28c732f 100644
--- a/lib/dup3.c
+++ b/lib/dup3.c
@@ -25,6 +25,10 @@
#include "binary-io.h"
+#if REPLACE_FCHDIR
+# include "dirinfo-abstract.h"
+#endif
+
int
dup3 (int oldfd, int newfd, int flags)
{
diff --git a/lib/fchdir.c b/lib/fchdir.c
index d8df5f9129..c7d124e25a 100644
--- a/lib/fchdir.c
+++ b/lib/fchdir.c
@@ -19,174 +19,7 @@
/* Specification. */
#include <unistd.h>
-#include <dirent.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-
-#include "assure.h"
-#include "filename.h"
-#include "filenamecat.h"
-
-#ifndef REPLACE_OPEN_DIRECTORY
-# define REPLACE_OPEN_DIRECTORY 0
-#endif
-
-/* This replacement assumes that a directory is not renamed while opened
- through a file descriptor.
-
- FIXME: On mingw, this would be possible to enforce if we were to
- also open a HANDLE to each directory currently visited by a file
- descriptor, since mingw refuses to rename any in-use file system
- object. */
-
-/* Array of file descriptors opened. If REPLACE_OPEN_DIRECTORY or if it points
- to a directory, it stores info about this directory. */
-typedef struct
-{
- char *name; /* Absolute name of the directory, or NULL. */
-} dir_info_t;
-static dir_info_t *dirs;
-static size_t dirs_allocated;
-
-/* Try to ensure dirs has enough room for a slot at index fd; free any
- contents already in that slot. Return false and set errno to
- ENOMEM on allocation failure. */
-static bool
-ensure_dirs_slot (size_t fd)
-{
- if (fd < dirs_allocated)
- free (dirs[fd].name);
- else
- {
- size_t new_allocated = 2 * dirs_allocated + 1;
- if (new_allocated <= fd)
- new_allocated = fd + 1;
- dir_info_t *new_dirs =
- (dirs != NULL
- ? (dir_info_t *) realloc (dirs, new_allocated * sizeof *dirs)
- : (dir_info_t *) malloc (new_allocated * sizeof *dirs));
- if (new_dirs == NULL)
- return false;
- memset (new_dirs + dirs_allocated, 0,
- (new_allocated - dirs_allocated) * sizeof *dirs);
- dirs = new_dirs;
- dirs_allocated = new_allocated;
- }
- return true;
-}
-
-/* Return an absolute name of DIR in malloc'd storage.
- Upon failure, return NULL with errno set. */
-static char *
-get_name (char const *dir)
-{
- if (IS_ABSOLUTE_FILE_NAME (dir))
- return strdup (dir);
-
- /* We often encounter "."; treat it as a special case. */
- char *cwd = getcwd (NULL, 0);
- if (!cwd || (dir[0] == '.' && dir[1] == '\0'))
- return cwd;
-
- char *result = mfile_name_concat (cwd, dir, NULL);
- free (cwd);
- return result;
-}
-
-/* Hook into the gnulib replacements for open() and close() to keep track
- of the open file descriptors. */
-
-/* Close FD, cleaning up any fd to name mapping if fd was visiting a
- directory. */
-void
-_gl_unregister_fd (int fd)
-{
- if (fd >= 0 && fd < dirs_allocated)
- {
- free (dirs[fd].name);
- dirs[fd].name = NULL;
- }
-}
-
-/* Mark FD as visiting FILENAME. FD must be non-negative, and refer
- to an open file descriptor. If REPLACE_OPEN_DIRECTORY is non-zero,
- this should only be called if FD is visiting a directory. Close FD
- and return -1 with errno set if there is insufficient memory to track
- the directory name; otherwise return FD. */
-int
-_gl_register_fd (int fd, const char *filename)
-{
- assure (0 <= fd);
-
- struct stat statbuf;
- if (REPLACE_OPEN_DIRECTORY
- || (fstat (fd, &statbuf) == 0 && S_ISDIR (statbuf.st_mode)))
- {
- if (!ensure_dirs_slot (fd)
- || (dirs[fd].name = get_name (filename)) == NULL)
- {
- int saved_errno = errno;
- close (fd);
- errno = saved_errno;
- return -1;
- }
- }
- return fd;
-}
-
-/* Mark NEWFD as a duplicate of OLDFD; useful from dup, dup2, dup3,
- and fcntl. Both arguments must be valid and distinct file
- descriptors. Close NEWFD and return -1 if OLDFD is tracking a
- directory, but there is insufficient memory to track the same
- directory in NEWFD; otherwise return NEWFD. */
-int
-_gl_register_dup (int oldfd, int newfd)
-{
- assure (0 <= oldfd && 0 <= newfd && oldfd != newfd);
- if (oldfd < dirs_allocated && dirs[oldfd].name)
- {
- /* Duplicated a directory; must ensure newfd is allocated. */
- if (!ensure_dirs_slot (newfd)
- || (dirs[newfd].name = strdup (dirs[oldfd].name)) == NULL)
- {
- int saved_errno = errno;
- close (newfd);
- errno = saved_errno;
- newfd = -1;
- }
- }
- else if (newfd < dirs_allocated)
- {
- /* Duplicated a non-directory; ensure newfd is cleared. */
- free (dirs[newfd].name);
- dirs[newfd].name = NULL;
- }
- return newfd;
-}
-
-/* If FD is currently visiting a directory, then return the name of
- that directory. Otherwise, return NULL and set errno. */
-const char *
-_gl_directory_name (int fd)
-{
- if (0 <= fd && fd < dirs_allocated && dirs[fd].name != NULL)
- return dirs[fd].name;
- /* At this point, fd is either invalid, or open but not a directory.
- If dup2 fails, errno is correctly EBADF. */
- if (0 <= fd)
- {
- if (dup2 (fd, fd) == fd)
- errno = ENOTDIR;
- }
- else
- errno = EBADF;
- return NULL;
-}
-
+#include "dirinfo-abstract.h"
/* Implement fchdir() in terms of chdir(). */
diff --git a/lib/fclose.c b/lib/fclose.c
index ab7453e576..b94b5b5122 100644
--- a/lib/fclose.c
+++ b/lib/fclose.c
@@ -23,6 +23,9 @@
#include <unistd.h>
#include "freading.h"
+#if REPLACE_FCHDIR
+# include "dirinfo-abstract.h"
+#endif
#if HAVE_MSVC_INVALID_PARAMETER_HANDLER
# include "msvc-inval.h"
#endif
diff --git a/lib/fcntl.c b/lib/fcntl.c
index 1c0e52d226..7ad4cfb957 100644
--- a/lib/fcntl.c
+++ b/lib/fcntl.c
@@ -28,6 +28,10 @@
#include <stdlib.h>
#include <unistd.h>
+#if REPLACE_FCHDIR
+# include "dirinfo-abstract.h"
+#endif
+
#ifdef __KLIBC__
# include <emx/io.h>
# include <InnoTekLIBC/backend.h>
diff --git a/lib/fdopendir.c b/lib/fdopendir.c
index 6265c20881..054643333f 100644
--- a/lib/fdopendir.c
+++ b/lib/fdopendir.c
@@ -23,6 +23,10 @@
#include <stdlib.h>
#include <unistd.h>
+#if REPLACE_FCHDIR
+# include "dirinfo-abstract.h"
+#endif
+
#if !HAVE_FDOPENDIR
# if GNULIB_defined_DIR
diff --git a/lib/fstat.c b/lib/fstat.c
index 292ce75fa5..0ff7a538ba 100644
--- a/lib/fstat.c
+++ b/lib/fstat.c
@@ -44,8 +44,11 @@ orig_fstat (int fd, struct stat *buf)
#include "stat-time.h"
+#if REPLACE_FCHDIR
+# include "dirinfo-abstract.h"
+#endif
+
#include <errno.h>
-#include <unistd.h>
#ifdef WINDOWS_NATIVE
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
diff --git a/lib/open.c b/lib/open.c
index c3b5648eca..22c385c568 100644
--- a/lib/open.c
+++ b/lib/open.c
@@ -47,7 +47,10 @@ orig_open (const char *filename, int flags, mode_t mode)
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
-#include <unistd.h>
+
+#if REPLACE_FCHDIR
+# include "dirinfo-impl.h"
+#endif
#ifndef HAVE_WORKING_O_DIRECTORY
# define HAVE_WORKING_O_DIRECTORY false
diff --git a/lib/opendir.c b/lib/opendir.c
index ea3d2ef166..e1dafd0666 100644
--- a/lib/opendir.c
+++ b/lib/opendir.c
@@ -41,7 +41,7 @@
#endif
#if REPLACE_FCHDIR
-# include <unistd.h>
+# include "dirinfo-impl.h"
#endif
#if defined _WIN32 && ! defined __CYGWIN__
diff --git a/lib/unistd.in.h b/lib/unistd.in.h
index 8f40c19e2a..fe68cae3dc 100644
--- a/lib/unistd.in.h
+++ b/lib/unistd.in.h
@@ -974,14 +974,6 @@ _GL_FUNCDECL_SYS (fchdir, int, (int /*fd*/), _GL_ATTRIBUTE_NODISCARD);
_GL_CXXALIAS_SYS (fchdir, int, (int /*fd*/));
# endif
_GL_CXXALIASWARN (fchdir);
-# if @REPLACE_FCHDIR@ || !@HAVE_FCHDIR@
-/* Gnulib internal hooks needed to maintain the fchdir metadata. */
-_GL_EXTERN_C int _gl_register_fd (int fd, const char *filename)
- _GL_ARG_NONNULL ((2));
-_GL_EXTERN_C void _gl_unregister_fd (int fd);
-_GL_EXTERN_C int _gl_register_dup (int oldfd, int newfd);
-_GL_EXTERN_C const char *_gl_directory_name (int fd);
-# endif
#elif defined GNULIB_POSIXCHECK
# if HAVE_RAW_DECL_FCHDIR
_GL_WARN_ON_USE (fchdir, "fchdir is unportable - "
diff --git a/m4/fchdir.m4 b/m4/fchdir.m4
index 83157d98f6..8c2403047c 100644
--- a/m4/fchdir.m4
+++ b/m4/fchdir.m4
@@ -1,12 +1,12 @@
# fchdir.m4
-# serial 32
+# serial 33
dnl Copyright (C) 2006-2026 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.
dnl This file is offered as-is, without any warranty.
-AC_DEFUN([gl_FUNC_FCHDIR],
+AC_DEFUN_ONCE([gl_FUNC_FCHDIR],
[
AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
AC_REQUIRE([gl_DIRENT_H_DEFAULTS])
diff --git a/modules/close b/modules/close
index 45b3a29062..7f20d80dfa 100644
--- a/modules/close
+++ b/modules/close
@@ -7,8 +7,9 @@ m4/close.m4
Depends-on:
unistd-h
-fd-hook [test $REPLACE_CLOSE = 1]
-msvc-inval [test $REPLACE_CLOSE = 1]
+dirinfo-abstract [test $REPLACE_CLOSE = 1]
+fd-hook [test $REPLACE_CLOSE = 1]
+msvc-inval [test $REPLACE_CLOSE = 1]
configure.ac:
gl_FUNC_CLOSE
diff --git a/modules/closedir b/modules/closedir
index c20870db47..f23072f24c 100644
--- a/modules/closedir
+++ b/modules/closedir
@@ -8,7 +8,8 @@ m4/closedir.m4
Depends-on:
dirent-h
-dirfd [test $HAVE_CLOSEDIR = 0 || test $REPLACE_CLOSEDIR = 1]
+dirfd [test $HAVE_CLOSEDIR = 0 || test $REPLACE_CLOSEDIR = 1]
+dirinfo-abstract [test $HAVE_CLOSEDIR = 0 || test $REPLACE_CLOSEDIR = 1]
configure.ac:
gl_FUNC_CLOSEDIR
diff --git a/modules/dup b/modules/dup
index a6bdf37ba7..066da80aa8 100644
--- a/modules/dup
+++ b/modules/dup
@@ -7,7 +7,8 @@ m4/dup.m4
Depends-on:
unistd-h
-msvc-inval [test $REPLACE_DUP = 1]
+dirinfo-abstract [test $REPLACE_DUP = 1]
+msvc-inval [test $REPLACE_DUP = 1]
configure.ac:
gl_FUNC_DUP
diff --git a/modules/dup2 b/modules/dup2
index c297a625ab..eb52295c38 100644
--- a/modules/dup2
+++ b/modules/dup2
@@ -7,8 +7,9 @@ m4/dup2.m4
Depends-on:
unistd-h
-msvc-inval [test $REPLACE_DUP2 = 1]
-msvc-nothrow [test $REPLACE_DUP2 = 1]
+dirinfo-abstract [test $REPLACE_DUP2 = 1]
+msvc-inval [test $REPLACE_DUP2 = 1]
+msvc-nothrow [test $REPLACE_DUP2 = 1]
configure.ac:
gl_FUNC_DUP2
diff --git a/modules/dup3 b/modules/dup3
index 966cbd2725..38dcfc28f9 100644
--- a/modules/dup3
+++ b/modules/dup3
@@ -9,6 +9,7 @@ Depends-on:
unistd-h
fcntl
binary-io
+dirinfo-abstract
getdtablesize
extensions
diff --git a/modules/fchdir b/modules/fchdir
index db639ace7b..b197941945 100644
--- a/modules/fchdir
+++ b/modules/fchdir
@@ -6,27 +6,9 @@ lib/fchdir.c
m4/fchdir.m4
Depends-on:
-dirent-h
unistd-h
-assure [test $HAVE_FCHDIR = 0 || test $REPLACE_FCHDIR = 1]
+dirinfo-abstract [test $HAVE_FCHDIR = 0 || test $REPLACE_FCHDIR = 1]
chdir [test $HAVE_FCHDIR = 0 || test $REPLACE_FCHDIR = 1]
-close [test $HAVE_FCHDIR = 0 || test $REPLACE_FCHDIR = 1]
-dirfd [test $HAVE_FCHDIR = 0 || test $REPLACE_FCHDIR = 1]
-dup2 [test $HAVE_FCHDIR = 0 || test $REPLACE_FCHDIR = 1]
-fcntl [test $HAVE_FCHDIR = 0 || test $REPLACE_FCHDIR = 1]
-fcntl-h [test $HAVE_FCHDIR = 0 || test $REPLACE_FCHDIR = 1]
-filename [test $HAVE_FCHDIR = 0 || test $REPLACE_FCHDIR = 1]
-filenamecat-lgpl [test $HAVE_FCHDIR = 0 || test $REPLACE_FCHDIR = 1]
-free-posix [test $HAVE_FCHDIR = 0 || test $REPLACE_FCHDIR = 1]
-fstat [test $HAVE_FCHDIR = 0 || test $REPLACE_FCHDIR = 1]
-getcwd-lgpl [test $HAVE_FCHDIR = 0 || test $REPLACE_FCHDIR = 1]
-malloc-posix [test $HAVE_FCHDIR = 0 || test $REPLACE_FCHDIR = 1]
-open [test $HAVE_FCHDIR = 0 || test $REPLACE_FCHDIR = 1]
-realloc-posix [test $HAVE_FCHDIR = 0 || test $REPLACE_FCHDIR = 1]
-stat [test $HAVE_FCHDIR = 0 || test $REPLACE_FCHDIR = 1]
-bool [test $HAVE_FCHDIR = 0 || test $REPLACE_FCHDIR = 1]
-strdup-posix [test $HAVE_FCHDIR = 0 || test $REPLACE_FCHDIR = 1]
-sys_stat-h [test $HAVE_FCHDIR = 0 || test $REPLACE_FCHDIR = 1]
configure.ac:
gl_FUNC_FCHDIR
diff --git a/modules/fclose b/modules/fclose
index 6de425d896..57fbede4ee 100644
--- a/modules/fclose
+++ b/modules/fclose
@@ -7,15 +7,16 @@ m4/fclose.m4
Depends-on:
stdio-h
-close [test $REPLACE_FCLOSE = 1]
-fflush [test $REPLACE_FCLOSE = 1]
-freading [test $REPLACE_FCLOSE = 1]
-lseek [test $REPLACE_FCLOSE = 1]
-msvc-inval [test $REPLACE_FCLOSE = 1]
+close [test $REPLACE_FCLOSE = 1]
+dirinfo-abstract [test $REPLACE_FCLOSE = 1]
+fflush [test $REPLACE_FCLOSE = 1]
+freading [test $REPLACE_FCLOSE = 1]
+lseek [test $REPLACE_FCLOSE = 1]
+msvc-inval [test $REPLACE_FCLOSE = 1]
# The code of fclose does not need to call fopen. But when gcc's '-fanalyzer'
# option is in use and stdio.h does '#define fclose rpl_fclose', stdio.h also
# needs to do '#define fopen rpl_fopen', otherwise a warning may result.
-fopen [test $REPLACE_FCLOSE = 1]
+fopen [test $REPLACE_FCLOSE = 1]
configure.ac:
gl_FUNC_FCLOSE
diff --git a/modules/fcntl b/modules/fcntl
index ad5d6e9366..72b854ffb6 100644
--- a/modules/fcntl
+++ b/modules/fcntl
@@ -8,10 +8,11 @@ lib/fcntl.c
Depends-on:
fcntl-h
extensions
-close [test $HAVE_FCNTL = 0 || test $REPLACE_FCNTL = 1]
-dup2 [test $HAVE_FCNTL = 0 || test $REPLACE_FCNTL = 1]
-getdtablesize [test $HAVE_FCNTL = 0 || test $REPLACE_FCNTL = 1]
-msvc-nothrow [test $HAVE_FCNTL = 0 || test $REPLACE_FCNTL = 1]
+close [test $HAVE_FCNTL = 0 || test $REPLACE_FCNTL = 1]
+dirinfo-abstract [test $HAVE_FCNTL = 0 || test $REPLACE_FCNTL = 1]
+dup2 [test $HAVE_FCNTL = 0 || test $REPLACE_FCNTL = 1]
+getdtablesize [test $HAVE_FCNTL = 0 || test $REPLACE_FCNTL = 1]
+msvc-nothrow [test $HAVE_FCNTL = 0 || test $REPLACE_FCNTL = 1]
configure.ac:
gl_FUNC_FCNTL
diff --git a/modules/fdopendir b/modules/fdopendir
index a543a6d865..268ecfc363 100644
--- a/modules/fdopendir
+++ b/modules/fdopendir
@@ -9,15 +9,16 @@ m4/fdopendir.m4
Depends-on:
dirent-h
extensions
-at-internal [test $HAVE_FDOPENDIR = 0]
-dirfd [test $HAVE_FDOPENDIR = 0]
-dup [test $HAVE_FDOPENDIR = 0]
-errno-h [test $HAVE_FDOPENDIR = 0]
-fchdir [test $HAVE_FDOPENDIR = 0]
-fstat [test $HAVE_FDOPENDIR = 0]
-openat-die [test $HAVE_FDOPENDIR = 0]
-opendir [test $HAVE_FDOPENDIR = 0]
-save-cwd [test $HAVE_FDOPENDIR = 0]
+at-internal [test $HAVE_FDOPENDIR = 0]
+dirfd [test $HAVE_FDOPENDIR = 0]
+dirinfo-abstract [test $HAVE_FDOPENDIR = 0]
+dup [test $HAVE_FDOPENDIR = 0]
+errno-h [test $HAVE_FDOPENDIR = 0]
+fchdir [test $HAVE_FDOPENDIR = 0]
+fstat [test $HAVE_FDOPENDIR = 0]
+openat-die [test $HAVE_FDOPENDIR = 0]
+opendir [test $HAVE_FDOPENDIR = 0]
+save-cwd [test $HAVE_FDOPENDIR = 0]
configure.ac:
gl_FUNC_FDOPENDIR
diff --git a/modules/fstat b/modules/fstat
index 432742e979..f9744f84ff 100644
--- a/modules/fstat
+++ b/modules/fstat
@@ -10,12 +10,12 @@ m4/fstat.m4
Depends-on:
sys_stat-h
largefile
-pathmax [test $REPLACE_FSTAT = 1]
-stat [test $REPLACE_FSTAT = 1]
-stat-time [test $REPLACE_FSTAT = 1]
-unistd-h [test $REPLACE_FSTAT = 1]
-assert-h [test $REPLACE_FSTAT = 1]
-msvc-nothrow [test $REPLACE_FSTAT = 1]
+dirinfo-abstract [test $REPLACE_FSTAT = 1]
+pathmax [test $REPLACE_FSTAT = 1]
+stat [test $REPLACE_FSTAT = 1]
+stat-time [test $REPLACE_FSTAT = 1]
+assert-h [test $REPLACE_FSTAT = 1]
+msvc-nothrow [test $REPLACE_FSTAT = 1]
configure.ac:
gl_FUNC_FSTAT
diff --git a/modules/open b/modules/open
index 7ae3da61f2..fadb4c66ce 100644
--- a/modules/open
+++ b/modules/open
@@ -13,6 +13,7 @@ fcntl-h
largefile
bool [test $REPLACE_OPEN = 1]
cloexec [test $REPLACE_OPEN = 1]
+dirinfo-impl [test $REPLACE_OPEN = 1]
fstat [test $REPLACE_OPEN = 1]
lstat [test $REPLACE_OPEN = 1]
stat [test $REPLACE_OPEN = 1]
diff --git a/modules/opendir b/modules/opendir
index 631d138022..ebea2c0453 100644
--- a/modules/opendir
+++ b/modules/opendir
@@ -9,8 +9,8 @@ m4/opendir.m4
Depends-on:
dirent-h
largefile
+dirinfo-impl [test $HAVE_OPENDIR = 0 || test $REPLACE_OPENDIR = 1]
filename [test $HAVE_OPENDIR = 0 || test $REPLACE_OPENDIR = 1]
-unistd-h [test $HAVE_OPENDIR = 0 || test $REPLACE_OPENDIR = 1]
closedir [test $HAVE_OPENDIR = 0 || test $REPLACE_OPENDIR = 1]
dirfd [test $HAVE_OPENDIR = 0 || test $REPLACE_OPENDIR = 1]
strnul [test $HAVE_OPENDIR = 0 || test $REPLACE_OPENDIR = 1]
--
2.53.0