1 #include "../../git-compat-util.h"
3 typedef struct dirent_DIR
{
4 struct DIR base_dir
; /* extend base 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
[MAX_PATH
* 3]; /* file name (* 3 for UTF-8 conversion) */
11 DIR *(*opendir
)(const char *dirname
) = dirent_opendir
;
13 static inline void finddata2dirent(struct dirent
*ent
, WIN32_FIND_DATAW
*fdata
)
15 /* convert UTF-16 name to UTF-8 (d_name points to dirent_DIR.dd_name) */
16 xwcstoutf(ent
->d_name
, fdata
->cFileName
, MAX_PATH
* 3);
18 /* Set file type, based on WIN32_FIND_DATA */
19 /* First check for symlinks since a directory symlink has the FILE_ATTRIBUTE_DIRECTORY
20 * attribute as well. Posix doesn't distinguish between directory/file symlinks, but
23 if (fdata
->dwFileAttributes
& FILE_ATTRIBUTE_REPARSE_POINT
24 && (fdata
->dwReserved0
== IO_REPARSE_TAG_SYMLINK
))
26 else if (fdata
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
32 static struct dirent
*dirent_readdir(dirent_DIR
*dir
)
35 errno
= EBADF
; /* No set_errno for mingw */
39 /* if first entry, dirent has already been set up by opendir */
41 /* get next entry and convert from WIN32_FIND_DATA to dirent */
42 WIN32_FIND_DATAW fdata
;
43 if (FindNextFileW(dir
->dd_handle
, &fdata
)) {
44 finddata2dirent(&dir
->dd_dir
, &fdata
);
46 DWORD lasterr
= GetLastError();
47 /* POSIX says you shouldn't set errno when readdir can't
48 find any more files; so, if another error we leave it set. */
49 if (lasterr
!= ERROR_NO_MORE_FILES
)
50 errno
= err_win_to_posix(lasterr
);
59 static int dirent_closedir(dirent_DIR
*dir
)
66 FindClose(dir
->dd_handle
);
71 DIR *dirent_opendir(const char *name
)
73 wchar_t pattern
[MAX_LONG_PATH
+ 2]; /* + 2 for "\*" */
74 WIN32_FIND_DATAW fdata
;
79 /* convert name to UTF-16 and check length */
80 if ((len
= xutftowcs_path_ex(pattern
, name
, MAX_LONG_PATH
, -1,
81 MAX_PATH
- 2, core_long_paths
)) < 0)
85 * append optional '\' and wildcard '*'. Note: we need to use '\' as
86 * Windows doesn't translate '/' to '\' for "\\?\"-prefixed paths.
88 if (len
&& !is_dir_sep(pattern
[len
- 1]))
89 pattern
[len
++] = '\\';
93 /* open find handle */
94 h
= FindFirstFileW(pattern
, &fdata
);
95 if (h
== INVALID_HANDLE_VALUE
) {
96 DWORD err
= GetLastError();
97 errno
= (err
== ERROR_DIRECTORY
) ? ENOTDIR
: err_win_to_posix(err
);
101 /* initialize DIR structure and copy first dir entry */
102 dir
= xmalloc(sizeof(dirent_DIR
));
103 dir
->base_dir
.preaddir
= (struct dirent
*(*)(DIR *dir
)) dirent_readdir
;
104 dir
->base_dir
.pclosedir
= (int (*)(DIR *dir
)) dirent_closedir
;
105 dir
->dd_dir
.d_name
= dir
->dd_name
;
108 finddata2dirent(&dir
->dd_dir
, &fdata
);