Hash :
3f0950f6
Author :
Date :
2023-04-27T01:42:25
fdopendir: Fix fd leak and test failure on native Windows. * lib/dirent-private.h: On mingw, define 'struct gl_directory' as a wrapper around the original DIR. On MSVC, add an 'fd_to_close' field to 'struct gl_directory'. * lib/dirent.in.h (DIR): Define when DIR_HAS_FD_MEMBER is 0, i.e. on both mingw and MSVC. (GNULIB_defined_DIR): New macro. (opendir): Avoid incompatible redeclaration. (readdir): Consider REPLACE_READDIR. (rewinddir): Consider REPLACE_REWINDDIR. * m4/dirent_h.m4 (gl_DIRENT_DIR): New macro. (gl_DIRENT_H): Invoke it. (gl_DIRENT_H_DEFAULTS): Initialize REPLACE_READDIR, REPLACE_REWINDDIR. * modules/dirent (Makefile.am): Substitute DIR_HAS_FD_MEMBER, REPLACE_READDIR, REPLACE_REWINDDIR. -- * lib/dirfd.c (dirfd): If GNULIB_defined_DIR, just use the 'fd_to_close' field. * m4/dirfd.m4 (gl_FUNC_DIRFD): Set HAVE_DIRFD. Don't set REPLACE_DIRFD to 1 if HAVE_DIRFD is 0. If DIR_HAS_FD_MEMBER is 0, ensure dirfd.c gets compiled. * modules/dirfd (Files): Add lib/dirent-private.h. (Depends-on, configure.ac): Simplify conditions. -- * lib/closedir.c: Include <stdlib.h> always, for free(). (closedir): If GNULIB_defined_DIR, arrange to call close(dirfd(dirp)) at the end. On mingw, call free() of dirp. Prefer testing HAVE_DIRENT_H, for consistency with dirent.h. * m4/closedir.m4 (gl_FUNC_CLOSEDIR): Don't set REPLACE_CLOSEDIR to 1 if HAVE_CLOSEDIR is 0. If DIR_HAS_FD_MEMBER is 0, ensure closedir.c gets compiled. -- * lib/opendir.c: Include <stdlib.h> always. Include <string.h>. (opendir): On mingw, allocate the 'struct gl_directory' through malloc. If GNULIB_defined_DIR, set the 'fd_to_close' field to -1. Prefer testing HAVE_DIRENT_H, for consistency with dirent.h. * m4/opendir.m4 (gl_FUNC_OPENDIR): Don't set REPLACE_OPENDIR to 1 if HAVE_OPENDIR is 0. If DIR_HAS_FD_MEMBER is 0, ensure opendir.c gets compiled. -- * lib/fdopendir.c (fdopendir): If GNULIB_defined_DIR, use a simple implementation based on opendir and the fchdir module. If __KLIBC__, don't define unused auxiliary functions. * modules/fdopendir (Files): Add lib/dirent-private.h. -- * lib/readdir.c (readdir): On mingw, redirect to the original readdir function. Prefer testing HAVE_DIRENT_H, for consistency with dirent.h. * m4/readdir.m4 (gl_FUNC_READDIR): If DIR_HAS_FD_MEMBER is 0, ensure readdir.c gets compiled. * modules/readdir (configure.ac): Consider REPLACE_READDIR. -- * lib/rewinddir.c (rewinddir): On mingw, redirect to the original rewinddir function. Prefer testing HAVE_DIRENT_H, for consistency with dirent.h. * m4/rewinddir.m4 (gl_FUNC_REWINDDIR): If DIR_HAS_FD_MEMBER is 0, ensure rewinddir.c gets compiled. * modules/rewinddir (configure.ac): Consider REPLACE_REWINDDIR. -- * lib/fchdir.c (dir_info_t): Remove a FIXME.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
/* Read the next entry of a directory.
Copyright (C) 2011-2023 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 2.1 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/>. */
#include <config.h>
/* Specification. */
#include <dirent.h>
#include <errno.h>
#include <stddef.h>
#if GNULIB_defined_DIR
# include "dirent-private.h"
#endif
/* Don't assume that UNICODE is not defined. */
#undef FindNextFile
#define FindNextFile FindNextFileA
struct dirent *
readdir (DIR *dirp)
#undef readdir
{
#if HAVE_DIRENT_H /* equivalent to HAVE_READDIR */
return readdir (dirp->real_dirp);
#else
char type;
struct dirent *result;
/* There is no need to add code to produce entries for "." and "..".
According to the POSIX:2008 section "4.12 Pathname Resolution"
<https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html>
"." and ".." are syntactic entities.
POSIX also says:
"If entries for dot or dot-dot exist, one entry shall be returned
for dot and one entry shall be returned for dot-dot; otherwise,
they shall not be returned." */
switch (dirp->status)
{
case -2:
/* End of directory already reached. */
return NULL;
case -1:
break;
case 0:
if (!FindNextFile (dirp->current, &dirp->entry))
{
switch (GetLastError ())
{
case ERROR_NO_MORE_FILES:
dirp->status = -2;
return NULL;
default:
errno = EIO;
return NULL;
}
}
break;
default:
errno = dirp->status;
return NULL;
}
dirp->status = 0;
if (dirp->entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
type = DT_DIR;
else if (dirp->entry.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
type = DT_LNK;
else if ((dirp->entry.dwFileAttributes
& ~(FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_ARCHIVE
| FILE_ATTRIBUTE_NORMAL
| FILE_ATTRIBUTE_TEMPORARY
| FILE_ATTRIBUTE_SPARSE_FILE
| FILE_ATTRIBUTE_COMPRESSED
| FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
| FILE_ATTRIBUTE_ENCRYPTED)) == 0)
/* Devices like COM1, LPT1, NUL would also have the attributes 0x20 but
they cannot occur here. */
type = DT_REG;
else
type = DT_UNKNOWN;
/* Reuse the memory of dirp->entry for the result. */
result =
(struct dirent *)
((char *) dirp->entry.cFileName - offsetof (struct dirent, d_name[0]));
result->d_type = type;
return result;
#endif
}