Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit bb4b8b1

Browse files
kbleeskasal
authored andcommitted
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 <[email protected]>
1 parent 3e60e64 commit bb4b8b1

File tree

2 files changed

+11
-21
lines changed

2 files changed

+11
-21
lines changed

compat/win32/dirent.c

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ struct DIR {
66
int dd_stat; /* 0-based index */
77
};
88

9-
static inline void finddata2dirent(struct dirent *ent, WIN32_FIND_DATAA *fdata)
9+
static inline void finddata2dirent(struct dirent *ent, WIN32_FIND_DATAW *fdata)
1010
{
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));
1313

1414
/* Set file type, based on WIN32_FIND_DATA */
1515
if (fdata->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
@@ -20,25 +20,15 @@ static inline void finddata2dirent(struct dirent *ent, WIN32_FIND_DATAA *fdata)
2020

2121
DIR *opendir(const char *name)
2222
{
23-
char pattern[MAX_PATH];
24-
WIN32_FIND_DATAA fdata;
23+
wchar_t pattern[MAX_PATH + 2]; /* + 2 for '/' '*' */
24+
WIN32_FIND_DATAW fdata;
2525
HANDLE h;
2626
int len;
2727
DIR *dir;
2828

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)
3231
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);
4232

4333
/* append optional '/' and wildcard '*' */
4434
if (len && !is_dir_sep(pattern[len - 1]))
@@ -47,7 +37,7 @@ DIR *opendir(const char *name)
4737
pattern[len] = 0;
4838

4939
/* open find handle */
50-
h = FindFirstFileA(pattern, &fdata);
40+
h = FindFirstFileW(pattern, &fdata);
5141
if (h == INVALID_HANDLE_VALUE) {
5242
DWORD err = GetLastError();
5343
errno = (err == ERROR_DIRECTORY) ? ENOTDIR : err_win_to_posix(err);
@@ -72,8 +62,8 @@ struct dirent *readdir(DIR *dir)
7262
/* if first entry, dirent has already been set up by opendir */
7363
if (dir->dd_stat) {
7464
/* 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)) {
7767
finddata2dirent(&dir->dd_dir, &fdata);
7868
} else {
7969
DWORD lasterr = GetLastError();

compat/win32/dirent.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ typedef struct DIR DIR;
1010

1111
struct dirent {
1212
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) */
1414
};
1515

1616
DIR *opendir(const char *dirname);

0 commit comments

Comments
 (0)