Skip to content

Commit addd018

Browse files
committed
Handle multibyte paths on Windows
1 parent ee39300 commit addd018

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/io.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,17 @@ dispatch_io_create_with_path(dispatch_io_type_t type, const char *path,
447447
_dispatch_retain(channel);
448448
dispatch_async(channel->queue, ^{
449449
int err = 0;
450+
#if defined(_WIN32)
451+
struct _stat st;
452+
int wide_len = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
453+
WCHAR *wide_path = malloc(wide_len * sizeof(WCHAR));
454+
MultiByteToWideChar(CP_UTF8, 0, path, -1, wide_path, wide_len);
455+
#else
450456
struct stat st;
457+
#endif
451458
_dispatch_io_syscall_switch_noerr(err,
452459
#if defined(_WIN32)
453-
stat(path_data->path, &st),
460+
_wstat(wide_path, &st),
454461
#else
455462
(path_data->oflag & O_NOFOLLOW) == O_NOFOLLOW
456463
#if __APPLE__
@@ -476,7 +483,11 @@ dispatch_io_create_with_path(dispatch_io_type_t type, const char *path,
476483
*c = 0;
477484
int perr;
478485
_dispatch_io_syscall_switch_noerr(perr,
486+
#if defined(_WIN32)
487+
_wstat(wide_path, &st),
488+
#else
479489
stat(path_data->path, &st),
490+
#endif
480491
case 0:
481492
// Since the parent directory exists, open() will
482493
// create a regular file after the fd_entry has
@@ -493,6 +504,9 @@ dispatch_io_create_with_path(dispatch_io_type_t type, const char *path,
493504
}
494505
break;
495506
);
507+
#if defined(_WIN32)
508+
free(wide_path);
509+
#endif
496510
channel->err = err;
497511
if (err) {
498512
free(path_data);
@@ -1333,9 +1347,14 @@ _dispatch_fd_entry_guarded_open(dispatch_fd_entry_t fd_entry, const char *path,
13331347
} else if (oflag & _O_TRUNC) {
13341348
dwCreationDisposition = TRUNCATE_EXISTING;
13351349
}
1336-
return (dispatch_fd_t)CreateFile(path, dwDesiredAccess,
1350+
int wide_len = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
1351+
WCHAR *wide_path = malloc(wide_len * sizeof(WCHAR));
1352+
MultiByteToWideChar(CP_UTF8, 0, path, -1, wide_path, wide_len);
1353+
dispatch_fd_t fd = (dispatch_fd_t)CreateFileW(wide_path, dwDesiredAccess,
13371354
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
13381355
dwCreationDisposition, 0, NULL);
1356+
free(wide_path);
1357+
return fd;
13391358
#else
13401359
return open(path, oflag, mode);
13411360
#endif

0 commit comments

Comments
 (0)