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>
Karsten Blees committed
Jan 14, 2012 at 22:01 UTC
0217569bb2db23b8686f67b0f4dda7e517dec6fd
2 files changed
+11
-21
compat/win32/dirent.c
+10
-20
@@ -6,10 +6,10 @@ struct DIR {
6
int dd_stat; /* 0-based index */
7
};
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
{
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
14
/* Set file type, based on WIN32_FIND_DATA */
15
if (fdata->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
@@ -20,25 +20,15 @@ static inline void finddata2dirent(struct dirent *ent, WIN32_FIND_DATAA *fdata)
20
21
DIR *opendir(const char *name)
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
HANDLE h;
26
int len;
27
DIR *dir;
28
29
- /* check that name is not NULL */
30
- if (!name) {
31
- errno = EINVAL;
29
+ /* convert name to UTF-16 and check length < MAX_PATH */
30
+ if ((len = xutftowcs_path(pattern, name)) < 0)
31
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;
38
- return NULL;
39
- }
40
- /* copy name to temp buffer */
41
- memcpy(pattern, name, len + 1);
32
33
/* append optional '/' and wildcard '*' */
34
if (len && !is_dir_sep(pattern[len - 1]))
@@ -47,7 +37,7 @@ DIR *opendir(const char *name)
37
pattern[len] = 0;
38
39
/* open find handle */
50
- h = FindFirstFileA(pattern, &fdata);
40
+ h = FindFirstFileW(pattern, &fdata);
41
if (h == INVALID_HANDLE_VALUE) {
42
DWORD err = GetLastError();
43
errno = (err == ERROR_DIRECTORY) ? ENOTDIR : err_win_to_posix(err);
@@ -72,8 +62,8 @@ struct dirent *readdir(DIR *dir)
62
/* if first entry, dirent has already been set up by opendir */
63
if (dir->dd_stat) {
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)) {
67
finddata2dirent(&dir->dd_dir, &fdata);
68
} else {
69
DWORD lasterr = GetLastError();
compat/win32/dirent.h
+1
-1
@@ -10,7 +10,7 @@ typedef struct DIR DIR;
10
11
struct dirent {
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
};
15
16
DIR *opendir(const char *dirname);