Merge pull request #285 from dscho/readme
[git/mingw/4msysgit.git] / compat / win32 / dirent.c
blobb3bd8d7af77291457c10f462249a273db7587184
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) */
9 } dirent_DIR;
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)
20 ent->d_type = DT_DIR;
21 else
22 ent->d_type = DT_REG;
25 static struct dirent *dirent_readdir(dirent_DIR *dir)
27 if (!dir) {
28 errno = EBADF; /* No set_errno for mingw */
29 return NULL;
32 /* if first entry, dirent has already been set up by opendir */
33 if (dir->dd_stat) {
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);
38 } else {
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);
44 return NULL;
48 ++dir->dd_stat;
49 return &dir->dd_dir;
52 static int dirent_closedir(dirent_DIR *dir)
54 if (!dir) {
55 errno = EBADF;
56 return -1;
59 FindClose(dir->dd_handle);
60 free(dir);
61 return 0;
64 DIR *dirent_opendir(const char *name)
66 wchar_t pattern[MAX_LONG_PATH + 2]; /* + 2 for "\*" */
67 WIN32_FIND_DATAW fdata;
68 HANDLE h;
69 int len;
70 dirent_DIR *dir;
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)
75 return NULL;
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++] = '\\';
83 pattern[len++] = '*';
84 pattern[len] = 0;
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);
91 return NULL;
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;
99 dir->dd_handle = h;
100 dir->dd_stat = 0;
101 finddata2dirent(&dir->dd_dir, &fdata);
102 return (DIR*) dir;