Skip to content

Commit 1e86274

Browse files
committed
Merge branch 'ef/win32-dirent'
* ef/win32-dirent: win32: use our own dirent.h msvc: opendir: handle paths ending with a slash win32: dirent: handle errors msvc: opendir: do not start the search msvc: opendir: allocate enough memory msvc: opendir: fix malloc-failure Conflicts: Makefile
2 parents cd425a1 + d1b6e6e commit 1e86274

7 files changed

Lines changed: 137 additions & 248 deletions

File tree

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ LIB_H += compat/mingw.h
504504
LIB_H += compat/win32/pthread.h
505505
LIB_H += compat/win32/syslog.h
506506
LIB_H += compat/win32/sys/poll.h
507+
LIB_H += compat/win32/dirent.h
507508
LIB_H += csum-file.h
508509
LIB_H += decorate.h
509510
LIB_H += delta.h
@@ -1096,7 +1097,9 @@ ifeq ($(uname_S),Windows)
10961097
AR = compat/vcbuild/scripts/lib.pl
10971098
CFLAGS =
10981099
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
1099-
COMPAT_OBJS = compat/msvc.o compat/winansi.o compat/win32/pthread.o compat/win32/syslog.o compat/win32/sys/poll.o
1100+
COMPAT_OBJS = compat/msvc.o compat/winansi.o \
1101+
compat/win32/pthread.o compat/win32/syslog.o \
1102+
compat/win32/sys/poll.o compat/win32/dirent.o
11001103
COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DNOGDI -DHAVE_STRING_H -DHAVE_ALLOCA_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\"
11011104
BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO -SUBSYSTEM:CONSOLE -NODEFAULTLIB:MSVCRT.lib
11021105
EXTLIBS = advapi32.lib shell32.lib wininet.lib ws2_32.lib
@@ -1169,7 +1172,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
11691172
COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\"
11701173
COMPAT_OBJS += compat/mingw.o compat/winansi.o \
11711174
compat/win32/pthread.o compat/win32/syslog.o \
1172-
compat/win32/sys/poll.o
1175+
compat/win32/sys/poll.o compat/win32/dirent.o
11731176
EXTLIBS += -lws2_32
11741177
PTHREAD_LIBS =
11751178
X = .exe

compat/mingw.c

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

compat/mingw.h

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

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

compat/msvc.c

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

6-
DIR *opendir(const char *name)
7-
{
8-
int len;
9-
DIR *p;
10-
p = (DIR*)malloc(sizeof(DIR));
11-
memset(p, 0, sizeof(DIR));
12-
strncpy(p->dd_name, name, PATH_MAX);
13-
len = strlen(p->dd_name);
14-
p->dd_name[len] = '/';
15-
p->dd_name[len+1] = '*';
16-
17-
if (p == NULL)
18-
return NULL;
19-
20-
p->dd_handle = _findfirst(p->dd_name, &p->dd_dta);
21-
22-
if (p->dd_handle == -1) {
23-
free(p);
24-
return NULL;
25-
}
26-
return p;
27-
}
28-
int closedir(DIR *dir)
29-
{
30-
_findclose(dir->dd_handle);
31-
free(dir);
32-
return 0;
33-
}
34-
356
#include "mingw.c"

compat/vcbuild/include/dirent.h

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

compat/win32/dirent.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include "../git-compat-util.h"
2+
#include "dirent.h"
3+
4+
struct DIR {
5+
struct dirent dd_dir; /* includes d_type */
6+
HANDLE dd_handle; /* FindFirstFile handle */
7+
int dd_stat; /* 0-based index */
8+
char dd_name[1]; /* extend struct */
9+
};
10+
11+
DIR *opendir(const char *name)
12+
{
13+
DWORD attrs = GetFileAttributesA(name);
14+
int len;
15+
DIR *p;
16+
17+
/* check for valid path */
18+
if (attrs == INVALID_FILE_ATTRIBUTES) {
19+
errno = ENOENT;
20+
return NULL;
21+
}
22+
23+
/* check if it's a directory */
24+
if (!(attrs & FILE_ATTRIBUTE_DIRECTORY)) {
25+
errno = ENOTDIR;
26+
return NULL;
27+
}
28+
29+
/* check that the pattern won't be too long for FindFirstFileA */
30+
len = strlen(name);
31+
if (is_dir_sep(name[len - 1]))
32+
len--;
33+
if (len + 2 >= MAX_PATH) {
34+
errno = ENAMETOOLONG;
35+
return NULL;
36+
}
37+
38+
p = malloc(sizeof(DIR) + len + 2);
39+
if (!p)
40+
return NULL;
41+
42+
memset(p, 0, sizeof(DIR) + len + 2);
43+
strcpy(p->dd_name, name);
44+
p->dd_name[len] = '/';
45+
p->dd_name[len+1] = '*';
46+
47+
p->dd_handle = INVALID_HANDLE_VALUE;
48+
return p;
49+
}
50+
51+
struct dirent *readdir(DIR *dir)
52+
{
53+
WIN32_FIND_DATAA buf;
54+
HANDLE handle;
55+
56+
if (!dir || !dir->dd_handle) {
57+
errno = EBADF; /* No set_errno for mingw */
58+
return NULL;
59+
}
60+
61+
if (dir->dd_handle == INVALID_HANDLE_VALUE && dir->dd_stat == 0) {
62+
DWORD lasterr;
63+
handle = FindFirstFileA(dir->dd_name, &buf);
64+
lasterr = GetLastError();
65+
dir->dd_handle = handle;
66+
if (handle == INVALID_HANDLE_VALUE && (lasterr != ERROR_NO_MORE_FILES)) {
67+
errno = err_win_to_posix(lasterr);
68+
return NULL;
69+
}
70+
} else if (dir->dd_handle == INVALID_HANDLE_VALUE) {
71+
return NULL;
72+
} else if (!FindNextFileA(dir->dd_handle, &buf)) {
73+
DWORD lasterr = GetLastError();
74+
FindClose(dir->dd_handle);
75+
dir->dd_handle = INVALID_HANDLE_VALUE;
76+
/* POSIX says you shouldn't set errno when readdir can't
77+
find any more files; so, if another error we leave it set. */
78+
if (lasterr != ERROR_NO_MORE_FILES)
79+
errno = err_win_to_posix(lasterr);
80+
return NULL;
81+
}
82+
83+
/* We get here if `buf' contains valid data. */
84+
strcpy(dir->dd_dir.d_name, buf.cFileName);
85+
++dir->dd_stat;
86+
87+
/* Set file type, based on WIN32_FIND_DATA */
88+
dir->dd_dir.d_type = 0;
89+
if (buf.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
90+
dir->dd_dir.d_type |= DT_DIR;
91+
else
92+
dir->dd_dir.d_type |= DT_REG;
93+
94+
return &dir->dd_dir;
95+
}
96+
97+
int closedir(DIR *dir)
98+
{
99+
if (!dir) {
100+
errno = EBADF;
101+
return -1;
102+
}
103+
104+
if (dir->dd_handle != INVALID_HANDLE_VALUE)
105+
FindClose(dir->dd_handle);
106+
free(dir);
107+
return 0;
108+
}

0 commit comments

Comments
 (0)