| 1 | #include "../../git-compat-util.h" |
| 2 | |
| 3 | struct DIR { |
| 4 | struct dirent dd_dir; /* includes d_type */ |
| 5 | HANDLE dd_handle; /* FindFirstFile handle */ |
| 6 | int dd_stat; /* 0-based index */ |
| 7 | }; |
| 8 | |
| 9 | static inline void finddata2dirent(struct dirent *ent, WIN32_FIND_DATAW *fdata) |
| 10 | { |
| 11 | /* convert UTF-16 name to UTF-8 */ |
| 12 | xwcstoutf(ent->d_name, fdata->cFileName, sizeof(ent->d_name)); |
| 13 | |
| 14 | /* Set file type, based on WIN32_FIND_DATA */ |
| 15 | if ((fdata->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 16 | && fdata->dwReserved0 == IO_REPARSE_TAG_SYMLINK) |
| 17 | ent->d_type = DT_LNK; |
| 18 | else if (fdata->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
| 19 | ent->d_type = DT_DIR; |
| 20 | else |
| 21 | ent->d_type = DT_REG; |
| 22 | } |
| 23 | |
| 24 | DIR *opendir(const char *name) |
| 25 | { |
| 26 | wchar_t pattern[MAX_PATH + 2]; /* + 2 for '/' '*' */ |
| 27 | WIN32_FIND_DATAW fdata; |
| 28 | HANDLE h; |
| 29 | int len; |
| 30 | DIR *dir; |
| 31 | |
| 32 | /* convert name to UTF-16 and check length < MAX_PATH */ |
| 33 | if ((len = xutftowcs_path(pattern, name)) < 0) |
| 34 | return NULL; |
| 35 | |
| 36 | /* append optional '/' and wildcard '*' */ |
| 37 | if (len && !is_dir_sep(pattern[len - 1])) |
| 38 | pattern[len++] = '/'; |
| 39 | pattern[len++] = '*'; |
| 40 | pattern[len] = 0; |
| 41 | |
| 42 | /* open find handle */ |
| 43 | h = FindFirstFileW(pattern, &fdata); |
| 44 | if (h == INVALID_HANDLE_VALUE) { |
| 45 | DWORD err = GetLastError(); |
| 46 | errno = (err == ERROR_DIRECTORY) ? ENOTDIR : err_win_to_posix(err); |
| 47 | return NULL; |
| 48 | } |
| 49 | |
| 50 | /* initialize DIR structure and copy first dir entry */ |
| 51 | dir = xmalloc(sizeof(DIR)); |
| 52 | dir->dd_handle = h; |
| 53 | dir->dd_stat = 0; |
| 54 | finddata2dirent(&dir->dd_dir, &fdata); |
| 55 | return dir; |
| 56 | } |
| 57 | |
| 58 | struct dirent *readdir(DIR *dir) |
| 59 | { |
| 60 | if (!dir) { |
| 61 | errno = EBADF; /* No set_errno for mingw */ |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | /* if first entry, dirent has already been set up by opendir */ |
| 66 | if (dir->dd_stat) { |
| 67 | /* get next entry and convert from WIN32_FIND_DATA to dirent */ |
| 68 | WIN32_FIND_DATAW fdata; |
| 69 | if (FindNextFileW(dir->dd_handle, &fdata)) { |
| 70 | finddata2dirent(&dir->dd_dir, &fdata); |
| 71 | } else { |
| 72 | DWORD lasterr = GetLastError(); |
| 73 | /* POSIX says you shouldn't set errno when readdir can't |
| 74 | find any more files; so, if another error we leave it set. */ |
| 75 | if (lasterr != ERROR_NO_MORE_FILES) |
| 76 | errno = err_win_to_posix(lasterr); |
| 77 | return NULL; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | ++dir->dd_stat; |
| 82 | return &dir->dd_dir; |
| 83 | } |
| 84 | |
| 85 | int closedir(DIR *dir) |
| 86 | { |
| 87 | if (!dir) { |
| 88 | errno = EBADF; |
| 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | FindClose(dir->dd_handle); |
| 93 | free(dir); |
| 94 | return 0; |
| 95 | } |