1 #include "../../git-compat-util.h"
4 struct dirent dd_dir
; /* includes d_type */
5 HANDLE dd_handle
; /* FindFirstFile handle */
6 int dd_stat
; /* 0-based index */
9 static inline void finddata2dirent(struct dirent
*ent
, WIN32_FIND_DATAA
*fdata
)
11 /* copy file name from WIN32_FIND_DATA to dirent */
12 memcpy(ent
->d_name
, fdata
->cFileName
, sizeof(ent
->d_name
));
14 /* Set file type, based on WIN32_FIND_DATA */
15 if (fdata
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
21 DIR *opendir(const char *name
)
23 char pattern
[MAX_PATH
];
24 WIN32_FIND_DATAA fdata
;
29 /* check that name is not NULL */
34 /* check that the pattern won't be too long for FindFirstFileA */
36 if (len
+ 2 >= MAX_PATH
) {
40 /* copy name to temp buffer */
41 memcpy(pattern
, name
, len
+ 1);
43 /* append optional '/' and wildcard '*' */
44 if (len
&& !is_dir_sep(pattern
[len
- 1]))
49 /* open find handle */
50 h
= FindFirstFileA(pattern
, &fdata
);
51 if (h
== INVALID_HANDLE_VALUE
) {
52 DWORD err
= GetLastError();
53 errno
= (err
== ERROR_DIRECTORY
) ? ENOTDIR
: err_win_to_posix(err
);
57 /* initialize DIR structure and copy first dir entry */
58 dir
= xmalloc(sizeof(DIR));
61 finddata2dirent(&dir
->dd_dir
, &fdata
);
65 struct dirent
*readdir(DIR *dir
)
68 errno
= EBADF
; /* No set_errno for mingw */
72 /* if first entry, dirent has already been set up by opendir */
74 /* get next entry and convert from WIN32_FIND_DATA to dirent */
75 WIN32_FIND_DATAA fdata
;
76 if (FindNextFileA(dir
->dd_handle
, &fdata
)) {
77 finddata2dirent(&dir
->dd_dir
, &fdata
);
79 DWORD lasterr
= GetLastError();
80 /* POSIX says you shouldn't set errno when readdir can't
81 find any more files; so, if another error we leave it set. */
82 if (lasterr
!= ERROR_NO_MORE_FILES
)
83 errno
= err_win_to_posix(lasterr
);
92 int closedir(DIR *dir
)
99 FindClose(dir
->dd_handle
);