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 if (fdata
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
25 static struct dirent
*dirent_readdir(dirent_DIR
*dir
)
28 errno
= EBADF
; /* No set_errno for mingw */
32 /* if first entry, dirent has already been set up by opendir */
34 /* get next entry and convert from WIN32_FIND_DATA to dirent */
35 WIN32_FIND_DATAW fdata
;
36 if (FindNextFileW(dir
->dd_handle
, &fdata
)) {
37 finddata2dirent(&dir
->dd_dir
, &fdata
);
39 DWORD lasterr
= GetLastError();
40 /* POSIX says you shouldn't set errno when readdir can't
41 find any more files; so, if another error we leave it set. */
42 if (lasterr
!= ERROR_NO_MORE_FILES
)
43 errno
= err_win_to_posix(lasterr
);
52 static int dirent_closedir(dirent_DIR
*dir
)
59 FindClose(dir
->dd_handle
);
64 DIR *dirent_opendir(const char *name
)
66 wchar_t pattern
[MAX_LONG_PATH
+ 2]; /* + 2 for "\*" */
67 WIN32_FIND_DATAW fdata
;
72 /* convert name to UTF-16 and check length */
73 if ((len
= xutftowcs_path_ex(pattern
, name
, MAX_LONG_PATH
, -1,
74 MAX_PATH
- 2, core_long_paths
)) < 0)
78 * append optional '\' and wildcard '*'. Note: we need to use '\' as
79 * Windows doesn't translate '/' to '\' for "\\?\"-prefixed paths.
81 if (len
&& !is_dir_sep(pattern
[len
- 1]))
82 pattern
[len
++] = '\\';
86 /* open find handle */
87 h
= FindFirstFileW(pattern
, &fdata
);
88 if (h
== INVALID_HANDLE_VALUE
) {
89 DWORD err
= GetLastError();
90 errno
= (err
== ERROR_DIRECTORY
) ? ENOTDIR
: err_win_to_posix(err
);
94 /* initialize DIR structure and copy first dir entry */
95 dir
= xmalloc(sizeof(dirent_DIR
));
96 dir
->base_dir
.preaddir
= (struct dirent
*(*)(DIR *dir
)) dirent_readdir
;
97 dir
->base_dir
.pclosedir
= (int (*)(DIR *dir
)) dirent_closedir
;
98 dir
->dd_dir
.d_name
= dir
->dd_name
;
101 finddata2dirent(&dir
->dd_dir
, &fdata
);