@@ -22,7 +22,8 @@ use sync::Arc;
22
22
use sys:: handle:: Handle ;
23
23
use sys:: { c, cvt} ;
24
24
use sys_common:: FromInner ;
25
- use vec:: Vec ;
25
+
26
+ use super :: to_u16s;
26
27
27
28
pub struct File { handle : Handle }
28
29
@@ -226,7 +227,7 @@ impl File {
226
227
}
227
228
228
229
pub fn open ( path : & Path , opts : & OpenOptions ) -> io:: Result < File > {
229
- let path = to_utf16 ( path) ;
230
+ let path = try! ( to_u16s ( path) ) ;
230
231
let handle = unsafe {
231
232
c:: CreateFileW ( path. as_ptr ( ) ,
232
233
opts. get_desired_access ( ) ,
@@ -377,10 +378,6 @@ impl fmt::Debug for File {
377
378
}
378
379
}
379
380
380
- pub fn to_utf16 ( s : & Path ) -> Vec < u16 > {
381
- s. as_os_str ( ) . encode_wide ( ) . chain ( Some ( 0 ) ) . collect ( )
382
- }
383
-
384
381
impl FileAttr {
385
382
pub fn size ( & self ) -> u64 {
386
383
( ( self . data . nFileSizeHigh as u64 ) << 32 ) | ( self . data . nFileSizeLow as u64 )
@@ -449,7 +446,7 @@ impl DirBuilder {
449
446
pub fn new ( ) -> DirBuilder { DirBuilder }
450
447
451
448
pub fn mkdir ( & self , p : & Path ) -> io:: Result < ( ) > {
452
- let p = to_utf16 ( p ) ;
449
+ let p = try! ( to_u16s ( p ) ) ;
453
450
try!( cvt ( unsafe {
454
451
c:: CreateDirectoryW ( p. as_ptr ( ) , ptr:: null_mut ( ) )
455
452
} ) ) ;
@@ -460,7 +457,7 @@ impl DirBuilder {
460
457
pub fn readdir ( p : & Path ) -> io:: Result < ReadDir > {
461
458
let root = p. to_path_buf ( ) ;
462
459
let star = p. join ( "*" ) ;
463
- let path = to_utf16 ( & star) ;
460
+ let path = try! ( to_u16s ( & star) ) ;
464
461
465
462
unsafe {
466
463
let mut wfd = mem:: zeroed ( ) ;
@@ -478,22 +475,22 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> {
478
475
}
479
476
480
477
pub fn unlink ( p : & Path ) -> io:: Result < ( ) > {
481
- let p_utf16 = to_utf16 ( p ) ;
482
- try!( cvt ( unsafe { c:: DeleteFileW ( p_utf16 . as_ptr ( ) ) } ) ) ;
478
+ let p_u16s = try! ( to_u16s ( p ) ) ;
479
+ try!( cvt ( unsafe { c:: DeleteFileW ( p_u16s . as_ptr ( ) ) } ) ) ;
483
480
Ok ( ( ) )
484
481
}
485
482
486
483
pub fn rename ( old : & Path , new : & Path ) -> io:: Result < ( ) > {
487
- let old = to_utf16 ( old) ;
488
- let new = to_utf16 ( new) ;
484
+ let old = try! ( to_u16s ( old) ) ;
485
+ let new = try! ( to_u16s ( new) ) ;
489
486
try!( cvt ( unsafe {
490
487
c:: MoveFileExW ( old. as_ptr ( ) , new. as_ptr ( ) , c:: MOVEFILE_REPLACE_EXISTING )
491
488
} ) ) ;
492
489
Ok ( ( ) )
493
490
}
494
491
495
492
pub fn rmdir ( p : & Path ) -> io:: Result < ( ) > {
496
- let p = to_utf16 ( p ) ;
493
+ let p = try! ( to_u16s ( p ) ) ;
497
494
try!( cvt ( unsafe { c:: RemoveDirectoryW ( p. as_ptr ( ) ) } ) ) ;
498
495
Ok ( ( ) )
499
496
}
@@ -508,8 +505,8 @@ pub fn symlink(src: &Path, dst: &Path) -> io::Result<()> {
508
505
}
509
506
510
507
pub fn symlink_inner ( src : & Path , dst : & Path , dir : bool ) -> io:: Result < ( ) > {
511
- let src = to_utf16 ( src) ;
512
- let dst = to_utf16 ( dst) ;
508
+ let src = try! ( to_u16s ( src) ) ;
509
+ let dst = try! ( to_u16s ( dst) ) ;
513
510
let flags = if dir { c:: SYMBOLIC_LINK_FLAG_DIRECTORY } else { 0 } ;
514
511
try!( cvt ( unsafe {
515
512
c:: CreateSymbolicLinkW ( dst. as_ptr ( ) , src. as_ptr ( ) , flags) as c:: BOOL
@@ -518,8 +515,8 @@ pub fn symlink_inner(src: &Path, dst: &Path, dir: bool) -> io::Result<()> {
518
515
}
519
516
520
517
pub fn link ( src : & Path , dst : & Path ) -> io:: Result < ( ) > {
521
- let src = to_utf16 ( src) ;
522
- let dst = to_utf16 ( dst) ;
518
+ let src = try! ( to_u16s ( src) ) ;
519
+ let dst = try! ( to_u16s ( dst) ) ;
523
520
try!( cvt ( unsafe {
524
521
c:: CreateHardLinkW ( dst. as_ptr ( ) , src. as_ptr ( ) , ptr:: null_mut ( ) )
525
522
} ) ) ;
@@ -545,10 +542,10 @@ pub fn stat(p: &Path) -> io::Result<FileAttr> {
545
542
}
546
543
547
544
pub fn lstat ( p : & Path ) -> io:: Result < FileAttr > {
548
- let utf16 = to_utf16 ( p ) ;
545
+ let u16s = try! ( to_u16s ( p ) ) ;
549
546
unsafe {
550
547
let mut attr: FileAttr = mem:: zeroed ( ) ;
551
- try!( cvt ( c:: GetFileAttributesExW ( utf16 . as_ptr ( ) ,
548
+ try!( cvt ( c:: GetFileAttributesExW ( u16s . as_ptr ( ) ,
552
549
c:: GetFileExInfoStandard ,
553
550
& mut attr. data as * mut _ as * mut _ ) ) ) ;
554
551
if attr. is_reparse_point ( ) {
@@ -562,7 +559,7 @@ pub fn lstat(p: &Path) -> io::Result<FileAttr> {
562
559
}
563
560
564
561
pub fn set_perm ( p : & Path , perm : FilePermissions ) -> io:: Result < ( ) > {
565
- let p = to_utf16 ( p ) ;
562
+ let p = try! ( to_u16s ( p ) ) ;
566
563
unsafe {
567
564
try!( cvt ( c:: SetFileAttributesW ( p. as_ptr ( ) , perm. attrs ) ) ) ;
568
565
Ok ( ( ) )
@@ -602,8 +599,8 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
602
599
* ( lpData as * mut i64 ) = TotalBytesTransferred ;
603
600
c:: PROGRESS_CONTINUE
604
601
}
605
- let pfrom = to_utf16 ( from) ;
606
- let pto = to_utf16 ( to ) ;
602
+ let pfrom = try! ( to_u16s ( from) ) ;
603
+ let pto = try! ( to_u16s ( to ) ) ;
607
604
let mut size = 0i64 ;
608
605
try!( cvt ( unsafe {
609
606
c:: CopyFileExW ( pfrom. as_ptr ( ) , pto. as_ptr ( ) , Some ( callback) ,
@@ -617,6 +614,7 @@ fn directory_junctions_are_directories() {
617
614
use ffi:: OsStr ;
618
615
use env;
619
616
use rand:: { self , StdRng , Rng } ;
617
+ use vec:: Vec ;
620
618
621
619
macro_rules! t {
622
620
( $e: expr) => ( match $e {
0 commit comments