Git fork

Win32: Unicode file name support (dirent)

Changes opendir/readdir to use Windows Unicode APIs and convert between
UTF-8/UTF-16.

Removes parameter checks that are already covered by xutftowcs_path. This
changes detection of ENAMETOOLONG from MAX_PATH - 2 to MAX_PATH (matching
is_dir_empty in mingw.c). If name + "/*" or the resulting absolute path is
too long, FindFirstFile fails and errno is set through err_win_to_posix.

Increases the size of dirent.d_name to accommodate the full
WIN32_FIND_DATA.cFileName converted to UTF-8 (UTF-16 to UTF-8 conversion
may grow by factor three in the worst case).

Signed-off-by: Karsten Blees <blees@dcon.de>
Signed-off-by: Stepan Kasal <kasal@ucw.cz>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

Karsten Blees and committed by
Junio C Hamano
0217569b 85faec9d

+11 -21
+10 -20
compat/win32/dirent.c
··· 6 6 int dd_stat; /* 0-based index */ 7 7 }; 8 8 9 - static inline void finddata2dirent(struct dirent *ent, WIN32_FIND_DATAA *fdata) 9 + static inline void finddata2dirent(struct dirent *ent, WIN32_FIND_DATAW *fdata) 10 10 { 11 - /* copy file name from WIN32_FIND_DATA to dirent */ 12 - memcpy(ent->d_name, fdata->cFileName, sizeof(ent->d_name)); 11 + /* convert UTF-16 name to UTF-8 */ 12 + xwcstoutf(ent->d_name, fdata->cFileName, sizeof(ent->d_name)); 13 13 14 14 /* Set file type, based on WIN32_FIND_DATA */ 15 15 if (fdata->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ··· 20 20 21 21 DIR *opendir(const char *name) 22 22 { 23 - char pattern[MAX_PATH]; 24 - WIN32_FIND_DATAA fdata; 23 + wchar_t pattern[MAX_PATH + 2]; /* + 2 for '/' '*' */ 24 + WIN32_FIND_DATAW fdata; 25 25 HANDLE h; 26 26 int len; 27 27 DIR *dir; 28 28 29 - /* check that name is not NULL */ 30 - if (!name) { 31 - errno = EINVAL; 32 - return NULL; 33 - } 34 - /* check that the pattern won't be too long for FindFirstFileA */ 35 - len = strlen(name); 36 - if (len + 2 >= MAX_PATH) { 37 - errno = ENAMETOOLONG; 29 + /* convert name to UTF-16 and check length < MAX_PATH */ 30 + if ((len = xutftowcs_path(pattern, name)) < 0) 38 31 return NULL; 39 - } 40 - /* copy name to temp buffer */ 41 - memcpy(pattern, name, len + 1); 42 32 43 33 /* append optional '/' and wildcard '*' */ 44 34 if (len && !is_dir_sep(pattern[len - 1])) ··· 47 37 pattern[len] = 0; 48 38 49 39 /* open find handle */ 50 - h = FindFirstFileA(pattern, &fdata); 40 + h = FindFirstFileW(pattern, &fdata); 51 41 if (h == INVALID_HANDLE_VALUE) { 52 42 DWORD err = GetLastError(); 53 43 errno = (err == ERROR_DIRECTORY) ? ENOTDIR : err_win_to_posix(err); ··· 72 62 /* if first entry, dirent has already been set up by opendir */ 73 63 if (dir->dd_stat) { 74 64 /* get next entry and convert from WIN32_FIND_DATA to dirent */ 75 - WIN32_FIND_DATAA fdata; 76 - if (FindNextFileA(dir->dd_handle, &fdata)) { 65 + WIN32_FIND_DATAW fdata; 66 + if (FindNextFileW(dir->dd_handle, &fdata)) { 77 67 finddata2dirent(&dir->dd_dir, &fdata); 78 68 } else { 79 69 DWORD lasterr = GetLastError();
+1 -1
compat/win32/dirent.h
··· 10 10 11 11 struct dirent { 12 12 unsigned char d_type; /* file type to prevent lstat after readdir */ 13 - char d_name[MAX_PATH]; /* file name */ 13 + char d_name[MAX_PATH * 3]; /* file name (* 3 for UTF-8 conversion) */ 14 14 }; 15 15 16 16 DIR *opendir(const char *dirname);