@@ -12,7 +12,7 @@ use crate::sync::Arc;
1212use crate :: sys:: handle:: Handle ;
1313use crate :: sys:: pal:: api:: { self , WinError , set_file_information_by_handle} ;
1414use crate :: sys:: pal:: { IoResult , fill_utf16_buf, to_u16s, truncate_utf16_at_nul} ;
15- use crate :: sys:: path:: maybe_verbatim;
15+ use crate :: sys:: path:: { WCStr , maybe_verbatim} ;
1616use crate :: sys:: time:: SystemTime ;
1717use crate :: sys:: { Align8 , c, cvt} ;
1818use crate :: sys_common:: { AsInner , FromInner , IntoInner } ;
@@ -298,10 +298,12 @@ impl OpenOptions {
298298impl File {
299299 pub fn open ( path : & Path , opts : & OpenOptions ) -> io:: Result < File > {
300300 let path = maybe_verbatim ( path) ?;
301+ // SAFETY: maybe_verbatim returns null-terminated strings
302+ let path = unsafe { WCStr :: new_unchecked ( & path) } ;
301303 Self :: open_native ( & path, opts)
302304 }
303305
304- fn open_native ( path : & [ u16 ] , opts : & OpenOptions ) -> io:: Result < File > {
306+ fn open_native ( path : & WCStr , opts : & OpenOptions ) -> io:: Result < File > {
305307 let creation = opts. get_creation_mode ( ) ?;
306308 let handle = unsafe {
307309 c:: CreateFileW (
@@ -1212,7 +1214,7 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> {
12121214 }
12131215}
12141216
1215- pub fn unlink ( path : & [ u16 ] ) -> io:: Result < ( ) > {
1217+ pub fn unlink ( path : & WCStr ) -> io:: Result < ( ) > {
12161218 if unsafe { c:: DeleteFileW ( path. as_ptr ( ) ) } == 0 {
12171219 let err = api:: get_last_error ( ) ;
12181220 // if `DeleteFileW` fails with ERROR_ACCESS_DENIED then try to remove
@@ -1235,7 +1237,7 @@ pub fn unlink(path: &[u16]) -> io::Result<()> {
12351237 }
12361238}
12371239
1238- pub fn rename ( old : & [ u16 ] , new : & [ u16 ] ) -> io:: Result < ( ) > {
1240+ pub fn rename ( old : & WCStr , new : & WCStr ) -> io:: Result < ( ) > {
12391241 if unsafe { c:: MoveFileExW ( old. as_ptr ( ) , new. as_ptr ( ) , c:: MOVEFILE_REPLACE_EXISTING ) } == 0 {
12401242 let err = api:: get_last_error ( ) ;
12411243 // if `MoveFileExW` fails with ERROR_ACCESS_DENIED then try to move
@@ -1305,12 +1307,12 @@ pub fn rename(old: &[u16], new: &[u16]) -> io::Result<()> {
13051307 Ok ( ( ) )
13061308}
13071309
1308- pub fn rmdir ( p : & [ u16 ] ) -> io:: Result < ( ) > {
1310+ pub fn rmdir ( p : & WCStr ) -> io:: Result < ( ) > {
13091311 cvt ( unsafe { c:: RemoveDirectoryW ( p. as_ptr ( ) ) } ) ?;
13101312 Ok ( ( ) )
13111313}
13121314
1313- pub fn remove_dir_all ( path : & [ u16 ] ) -> io:: Result < ( ) > {
1315+ pub fn remove_dir_all ( path : & WCStr ) -> io:: Result < ( ) > {
13141316 // Open a file or directory without following symlinks.
13151317 let mut opts = OpenOptions :: new ( ) ;
13161318 opts. access_mode ( c:: FILE_LIST_DIRECTORY ) ;
@@ -1328,7 +1330,7 @@ pub fn remove_dir_all(path: &[u16]) -> io::Result<()> {
13281330 remove_dir_all_iterative ( file) . io_result ( )
13291331}
13301332
1331- pub fn readlink ( path : & [ u16 ] ) -> io:: Result < PathBuf > {
1333+ pub fn readlink ( path : & WCStr ) -> io:: Result < PathBuf > {
13321334 // Open the link with no access mode, instead of generic read.
13331335 // By default FILE_LIST_DIRECTORY is denied for the junction "C:\Documents and Settings", so
13341336 // this is needed for a common case.
@@ -1373,17 +1375,17 @@ pub fn symlink_inner(original: &Path, link: &Path, dir: bool) -> io::Result<()>
13731375}
13741376
13751377#[ cfg( not( target_vendor = "uwp" ) ) ]
1376- pub fn link ( original : & [ u16 ] , link : & [ u16 ] ) -> io:: Result < ( ) > {
1378+ pub fn link ( original : & WCStr , link : & WCStr ) -> io:: Result < ( ) > {
13771379 cvt ( unsafe { c:: CreateHardLinkW ( link. as_ptr ( ) , original. as_ptr ( ) , ptr:: null_mut ( ) ) } ) ?;
13781380 Ok ( ( ) )
13791381}
13801382
13811383#[ cfg( target_vendor = "uwp" ) ]
1382- pub fn link ( _original : & [ u16 ] , _link : & [ u16 ] ) -> io:: Result < ( ) > {
1384+ pub fn link ( _original : & WCStr , _link : & WCStr ) -> io:: Result < ( ) > {
13831385 return Err ( io:: const_error!( io:: ErrorKind :: Unsupported , "hard link are not supported on UWP" ) ) ;
13841386}
13851387
1386- pub fn stat ( path : & [ u16 ] ) -> io:: Result < FileAttr > {
1388+ pub fn stat ( path : & WCStr ) -> io:: Result < FileAttr > {
13871389 match metadata ( path, ReparsePoint :: Follow ) {
13881390 Err ( err) if err. raw_os_error ( ) == Some ( c:: ERROR_CANT_ACCESS_FILE as i32 ) => {
13891391 if let Ok ( attrs) = lstat ( path) {
@@ -1397,7 +1399,7 @@ pub fn stat(path: &[u16]) -> io::Result<FileAttr> {
13971399 }
13981400}
13991401
1400- pub fn lstat ( path : & [ u16 ] ) -> io:: Result < FileAttr > {
1402+ pub fn lstat ( path : & WCStr ) -> io:: Result < FileAttr > {
14011403 metadata ( path, ReparsePoint :: Open )
14021404}
14031405
@@ -1413,7 +1415,7 @@ impl ReparsePoint {
14131415 }
14141416}
14151417
1416- fn metadata ( path : & [ u16 ] , reparse : ReparsePoint ) -> io:: Result < FileAttr > {
1418+ fn metadata ( path : & WCStr , reparse : ReparsePoint ) -> io:: Result < FileAttr > {
14171419 let mut opts = OpenOptions :: new ( ) ;
14181420 // No read or write permissions are necessary
14191421 opts. access_mode ( 0 ) ;
@@ -1473,7 +1475,7 @@ fn metadata(path: &[u16], reparse: ReparsePoint) -> io::Result<FileAttr> {
14731475 }
14741476}
14751477
1476- pub fn set_perm ( p : & [ u16 ] , perm : FilePermissions ) -> io:: Result < ( ) > {
1478+ pub fn set_perm ( p : & WCStr , perm : FilePermissions ) -> io:: Result < ( ) > {
14771479 unsafe {
14781480 cvt ( c:: SetFileAttributesW ( p. as_ptr ( ) , perm. attrs ) ) ?;
14791481 Ok ( ( ) )
@@ -1489,7 +1491,7 @@ fn get_path(f: &File) -> io::Result<PathBuf> {
14891491 )
14901492}
14911493
1492- pub fn canonicalize ( p : & [ u16 ] ) -> io:: Result < PathBuf > {
1494+ pub fn canonicalize ( p : & WCStr ) -> io:: Result < PathBuf > {
14931495 let mut opts = OpenOptions :: new ( ) ;
14941496 // No read or write permissions are necessary
14951497 opts. access_mode ( 0 ) ;
@@ -1499,7 +1501,7 @@ pub fn canonicalize(p: &[u16]) -> io::Result<PathBuf> {
14991501 get_path ( & f)
15001502}
15011503
1502- pub fn copy ( from : & [ u16 ] , to : & [ u16 ] ) -> io:: Result < u64 > {
1504+ pub fn copy ( from : & WCStr , to : & WCStr ) -> io:: Result < u64 > {
15031505 unsafe extern "system" fn callback (
15041506 _TotalFileSize : i64 ,
15051507 _TotalBytesTransferred : i64 ,
@@ -1612,7 +1614,7 @@ pub fn junction_point(original: &Path, link: &Path) -> io::Result<()> {
16121614}
16131615
16141616// Try to see if a file exists but, unlike `exists`, report I/O errors.
1615- pub fn exists ( path : & [ u16 ] ) -> io:: Result < bool > {
1617+ pub fn exists ( path : & WCStr ) -> io:: Result < bool > {
16161618 // Open the file to ensure any symlinks are followed to their target.
16171619 let mut opts = OpenOptions :: new ( ) ;
16181620 // No read, write, etc access rights are needed.
0 commit comments