Skip to content

Commit d1b6e6e

Browse files
kusmagitster
authored andcommitted
win32: use our own dirent.h
The mingw-runtime implemenation of opendir, readdir and closedir sets errno to 0 on success, something that POSIX explicitly forbids. 3ba7a06 ("A loose object is not corrupt if it cannot be read due to EMFILE") introduce a dependency on this behaviour, leading to a broken "git clone" on Windows. compat/mingw.c contains an implementation of readdir, and compat/msvc.c contains implementations of opendir and closedir. Move these to compat/win32/dirent.[ch], and change to our own DIR structure at the same time. This provides a generic Win32-implementation of opendir, readdir and closedir which works on both MinGW and MSVC and does not reset errno, and as a result git clone is working again on Windows. Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e777260 commit d1b6e6e

7 files changed

Lines changed: 137 additions & 271 deletions

File tree

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ LIB_H += compat/mingw.h
499499
LIB_H += compat/win32/pthread.h
500500
LIB_H += compat/win32/syslog.h
501501
LIB_H += compat/win32/sys/poll.h
502+
LIB_H += compat/win32/dirent.h
502503
LIB_H += csum-file.h
503504
LIB_H += decorate.h
504505
LIB_H += delta.h
@@ -1084,7 +1085,9 @@ ifeq ($(uname_S),Windows)
10841085
AR = compat/vcbuild/scripts/lib.pl
10851086
CFLAGS =
10861087
BASIC_CFLAGS = -nologo -I. -I../zlib -Icompat/vcbuild -Icompat/vcbuild/include -DWIN32 -D_CONSOLE -DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE
1087-
COMPAT_OBJS = compat/msvc.o compat/fnmatch/fnmatch.o compat/winansi.o compat/win32/pthread.o compat/win32/syslog.o compat/win32/sys/poll.o
1088+
COMPAT_OBJS = compat/msvc.o compat/fnmatch/fnmatch.o compat/winansi.o \
1089+
compat/win32/pthread.o compat/win32/syslog.o \
1090+
compat/win32/sys/poll.o compat/win32/dirent.o
10881091
COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DNOGDI -DHAVE_STRING_H -DHAVE_ALLOCA_H -Icompat -Icompat/fnmatch -Icompat/regex -Icompat/fnmatch -Icompat/win32 -DSTRIP_EXTENSION=\".exe\"
10891092
BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO -SUBSYSTEM:CONSOLE -NODEFAULTLIB:MSVCRT.lib
10901093
EXTLIBS = advapi32.lib shell32.lib wininet.lib ws2_32.lib
@@ -1137,7 +1140,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
11371140
COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\"
11381141
COMPAT_OBJS += compat/mingw.o compat/fnmatch/fnmatch.o compat/winansi.o \
11391142
compat/win32/pthread.o compat/win32/syslog.o \
1140-
compat/win32/sys/poll.o
1143+
compat/win32/sys/poll.o compat/win32/dirent.o
11411144
EXTLIBS += -lws2_32
11421145
PTHREAD_LIBS =
11431146
X = .exe

compat/mingw.c

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,63 +1564,3 @@ pid_t waitpid(pid_t pid, int *status, unsigned options)
15641564
errno = EINVAL;
15651565
return -1;
15661566
}
1567-
1568-
#ifndef NO_MINGW_REPLACE_READDIR
1569-
/* MinGW readdir implementation to avoid extra lstats for Git */
1570-
struct mingw_DIR
1571-
{
1572-
struct _finddata_t dd_dta; /* disk transfer area for this dir */
1573-
struct mingw_dirent dd_dir; /* Our own implementation, including d_type */
1574-
long dd_handle; /* _findnext handle */
1575-
int dd_stat; /* 0 = next entry to read is first entry, -1 = off the end, positive = 0 based index of next entry */
1576-
char dd_name[1]; /* given path for dir with search pattern (struct is extended) */
1577-
};
1578-
1579-
struct dirent *mingw_readdir(DIR *dir)
1580-
{
1581-
WIN32_FIND_DATAA buf;
1582-
HANDLE handle;
1583-
struct mingw_DIR *mdir = (struct mingw_DIR*)dir;
1584-
1585-
if (!dir || !dir->dd_handle) {
1586-
errno = EBADF; /* No set_errno for mingw */
1587-
return NULL;
1588-
}
1589-
1590-
if (dir->dd_handle == (long)INVALID_HANDLE_VALUE && dir->dd_stat == 0)
1591-
{
1592-
DWORD lasterr;
1593-
handle = FindFirstFileA(dir->dd_name, &buf);
1594-
lasterr = GetLastError();
1595-
dir->dd_handle = (long)handle;
1596-
if (handle == INVALID_HANDLE_VALUE && (lasterr != ERROR_NO_MORE_FILES)) {
1597-
errno = err_win_to_posix(lasterr);
1598-
return NULL;
1599-
}
1600-
} else if (dir->dd_handle == (long)INVALID_HANDLE_VALUE) {
1601-
return NULL;
1602-
} else if (!FindNextFileA((HANDLE)dir->dd_handle, &buf)) {
1603-
DWORD lasterr = GetLastError();
1604-
FindClose((HANDLE)dir->dd_handle);
1605-
dir->dd_handle = (long)INVALID_HANDLE_VALUE;
1606-
/* POSIX says you shouldn't set errno when readdir can't
1607-
find any more files; so, if another error we leave it set. */
1608-
if (lasterr != ERROR_NO_MORE_FILES)
1609-
errno = err_win_to_posix(lasterr);
1610-
return NULL;
1611-
}
1612-
1613-
/* We get here if `buf' contains valid data. */
1614-
strcpy(dir->dd_dir.d_name, buf.cFileName);
1615-
++dir->dd_stat;
1616-
1617-
/* Set file type, based on WIN32_FIND_DATA */
1618-
mdir->dd_dir.d_type = 0;
1619-
if (buf.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1620-
mdir->dd_dir.d_type |= DT_DIR;
1621-
else
1622-
mdir->dd_dir.d_type |= DT_REG;
1623-
1624-
return (struct dirent*)&dir->dd_dir;
1625-
}
1626-
#endif // !NO_MINGW_REPLACE_READDIR

compat/mingw.h

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -319,35 +319,6 @@ int main(int argc, const char **argv) \
319319
} \
320320
static int mingw_main(c,v)
321321

322-
#ifndef NO_MINGW_REPLACE_READDIR
323-
/*
324-
* A replacement of readdir, to ensure that it reads the file type at
325-
* the same time. This avoid extra unneeded lstats in git on MinGW
326-
*/
327-
#undef DT_UNKNOWN
328-
#undef DT_DIR
329-
#undef DT_REG
330-
#undef DT_LNK
331-
#define DT_UNKNOWN 0
332-
#define DT_DIR 1
333-
#define DT_REG 2
334-
#define DT_LNK 3
335-
336-
struct mingw_dirent
337-
{
338-
long d_ino; /* Always zero. */
339-
union {
340-
unsigned short d_reclen; /* Always zero. */
341-
unsigned char d_type; /* Reimplementation adds this */
342-
};
343-
unsigned short d_namlen; /* Length of name in d_name. */
344-
char d_name[FILENAME_MAX]; /* File name. */
345-
};
346-
#define dirent mingw_dirent
347-
#define readdir(x) mingw_readdir(x)
348-
struct dirent *mingw_readdir(DIR *dir);
349-
#endif // !NO_MINGW_REPLACE_READDIR
350-
351322
/*
352323
* Used by Pthread API implementation for Windows
353324
*/

compat/msvc.c

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,4 @@
33
#include <conio.h>
44
#include "../strbuf.h"
55

6-
DIR *opendir(const char *name)
7-
{
8-
DWORD attrs = GetFileAttributes(name);
9-
int len;
10-
DIR *p;
11-
12-
/* check for valid path */
13-
if (attrs == INVALID_FILE_ATTRIBUTES) {
14-
errno = ENOENT;
15-
return NULL;
16-
}
17-
18-
/* check if it's a directory */
19-
if (!(attrs & FILE_ATTRIBUTE_DIRECTORY)) {
20-
errno = ENOTDIR;
21-
return NULL;
22-
}
23-
24-
/* check that the pattern won't be too long for FindFirstFileA */
25-
len = strlen(name);
26-
if (is_dir_sep(name[len - 1]))
27-
len--;
28-
if (len + 2 >= MAX_PATH) {
29-
errno = ENAMETOOLONG;
30-
return NULL;
31-
}
32-
33-
p = malloc(sizeof(DIR) + len + 2);
34-
if (!p)
35-
return NULL;
36-
37-
memset(p, 0, sizeof(DIR) + len + 2);
38-
strcpy(p->dd_name, name);
39-
p->dd_name[len] = '/';
40-
p->dd_name[len+1] = '*';
41-
42-
p->dd_handle = (long)INVALID_HANDLE_VALUE;
43-
return p;
44-
}
45-
int closedir(DIR *dir)
46-
{
47-
if (!dir) {
48-
errno = EBADF;
49-
return -1;
50-
}
51-
52-
if (dir->dd_handle != (long)INVALID_HANDLE_VALUE)
53-
FindClose((HANDLE)dir->dd_handle);
54-
free(dir);
55-
return 0;
56-
}
57-
586
#include "mingw.c"

compat/vcbuild/include/dirent.h

Lines changed: 0 additions & 128 deletions
This file was deleted.

0 commit comments

Comments
 (0)