@@ -408,6 +408,28 @@ dispatch_io_create_f(dispatch_io_type_t type, dispatch_fd_t fd,
408
408
409
409
#if defined(_WIN32 )
410
410
#define _is_separator (ch ) ((ch) == '/' || (ch) == '\\')
411
+
412
+ static WCHAR *
413
+ _dispatch_win32_copy_wide_from_utf8 (const char * str ) {
414
+ int cchLength = MultiByteToWideChar (CP_UTF8 , 0 , str , -1 , NULL , 0 );
415
+ if (cchLength <= 0 ) {
416
+ return NULL ;
417
+ }
418
+
419
+ WCHAR * wszResult = malloc (cchLength * sizeof (WCHAR ));
420
+ if (!wszResult ) {
421
+ return NULL ;
422
+ }
423
+
424
+ cchLength = MultiByteToWideChar (CP_UTF8 , 0 , str , -1 , wszResult , cchLength );
425
+ if (cchLength ) {
426
+ return wszResult ;
427
+ }
428
+
429
+ free (wszResult );
430
+ return NULL ;
431
+ }
432
+
411
433
#else
412
434
#define _is_separator (ch ) ((ch) == '/')
413
435
#endif
@@ -447,10 +469,23 @@ dispatch_io_create_with_path(dispatch_io_type_t type, const char *path,
447
469
_dispatch_retain (channel );
448
470
dispatch_async (channel -> queue , ^{
449
471
int err = 0 ;
472
+ #if defined(_WIN32 )
473
+ struct _stat st ;
474
+ WCHAR * wszPath = _dispatch_win32_copy_wide_from_utf8 (path );
475
+ if (!wszPath ) {
476
+ err = (int )GetLastError ();
477
+ free (path_data );
478
+ _dispatch_io_init (channel , NULL , queue , err , cleanup_handler );
479
+ _dispatch_release (channel );
480
+ _dispatch_release (queue );
481
+ return ;
482
+ }
483
+ #else
450
484
struct stat st ;
485
+ #endif
451
486
_dispatch_io_syscall_switch_noerr (err ,
452
487
#if defined(_WIN32 )
453
- stat ( path_data -> path , & st ),
488
+ _wstat ( wszPath , & st ),
454
489
#else
455
490
(path_data -> oflag & O_NOFOLLOW ) == O_NOFOLLOW
456
491
#if __APPLE__
@@ -476,7 +511,11 @@ dispatch_io_create_with_path(dispatch_io_type_t type, const char *path,
476
511
* c = 0 ;
477
512
int perr ;
478
513
_dispatch_io_syscall_switch_noerr (perr ,
514
+ #if defined(_WIN32 )
515
+ _wstat (wszPath , & st ),
516
+ #else
479
517
stat (path_data -> path , & st ),
518
+ #endif
480
519
case 0 :
481
520
// Since the parent directory exists, open() will
482
521
// create a regular file after the fd_entry has
@@ -493,6 +532,9 @@ dispatch_io_create_with_path(dispatch_io_type_t type, const char *path,
493
532
}
494
533
break ;
495
534
);
535
+ #if defined(_WIN32 )
536
+ free (wszPath );
537
+ #endif
496
538
channel -> err = err ;
497
539
if (err ) {
498
540
free (path_data );
@@ -1333,9 +1375,15 @@ _dispatch_fd_entry_guarded_open(dispatch_fd_entry_t fd_entry, const char *path,
1333
1375
} else if (oflag & _O_TRUNC ) {
1334
1376
dwCreationDisposition = TRUNCATE_EXISTING ;
1335
1377
}
1336
- return (dispatch_fd_t )CreateFile (path , dwDesiredAccess ,
1378
+ WCHAR * wszPath = _dispatch_win32_copy_wide_from_utf8 (path );
1379
+ if (!wszPath ) {
1380
+ return -1 ;
1381
+ }
1382
+ dispatch_fd_t fd = (dispatch_fd_t )CreateFileW (wszPath , dwDesiredAccess ,
1337
1383
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE , NULL ,
1338
1384
dwCreationDisposition , 0 , NULL );
1385
+ free (wszPath );
1386
+ return fd ;
1339
1387
#else
1340
1388
return open (path , oflag , mode );
1341
1389
#endif
0 commit comments