@@ -12,7 +12,7 @@ use crate::sync::Arc;
12
12
use crate :: sys:: handle:: Handle ;
13
13
use crate :: sys:: pal:: api:: { self , WinError , set_file_information_by_handle} ;
14
14
use 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} ;
16
16
use crate :: sys:: time:: SystemTime ;
17
17
use crate :: sys:: { Align8 , c, cvt} ;
18
18
use crate :: sys_common:: { AsInner , FromInner , IntoInner } ;
@@ -298,10 +298,12 @@ impl OpenOptions {
298
298
impl File {
299
299
pub fn open ( path : & Path , opts : & OpenOptions ) -> io:: Result < File > {
300
300
let path = maybe_verbatim ( path) ?;
301
+ // SAFETY: maybe_verbatim returns null-terminated strings
302
+ let path = unsafe { WCStr :: from_wchars_with_null_unchecked ( & path) } ;
301
303
Self :: open_native ( & path, opts)
302
304
}
303
305
304
- fn open_native ( path : & [ u16 ] , opts : & OpenOptions ) -> io:: Result < File > {
306
+ fn open_native ( path : & WCStr , opts : & OpenOptions ) -> io:: Result < File > {
305
307
let creation = opts. get_creation_mode ( ) ?;
306
308
let handle = unsafe {
307
309
c:: CreateFileW (
@@ -1212,7 +1214,7 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> {
1212
1214
}
1213
1215
}
1214
1216
1215
- pub fn unlink ( path : & [ u16 ] ) -> io:: Result < ( ) > {
1217
+ pub fn unlink ( path : & WCStr ) -> io:: Result < ( ) > {
1216
1218
if unsafe { c:: DeleteFileW ( path. as_ptr ( ) ) } == 0 {
1217
1219
let err = api:: get_last_error ( ) ;
1218
1220
// if `DeleteFileW` fails with ERROR_ACCESS_DENIED then try to remove
@@ -1235,7 +1237,7 @@ pub fn unlink(path: &[u16]) -> io::Result<()> {
1235
1237
}
1236
1238
}
1237
1239
1238
- pub fn rename ( old : & [ u16 ] , new : & [ u16 ] ) -> io:: Result < ( ) > {
1240
+ pub fn rename ( old : & WCStr , new : & WCStr ) -> io:: Result < ( ) > {
1239
1241
if unsafe { c:: MoveFileExW ( old. as_ptr ( ) , new. as_ptr ( ) , c:: MOVEFILE_REPLACE_EXISTING ) } == 0 {
1240
1242
let err = api:: get_last_error ( ) ;
1241
1243
// if `MoveFileExW` fails with ERROR_ACCESS_DENIED then try to move
@@ -1249,7 +1251,8 @@ pub fn rename(old: &[u16], new: &[u16]) -> io::Result<()> {
1249
1251
1250
1252
// Calculate the layout of the `FILE_RENAME_INFO` we pass to `SetFileInformation`
1251
1253
// This is a dynamically sized struct so we need to get the position of the last field to calculate the actual size.
1252
- let Ok ( new_len_without_nul_in_bytes) : Result < u32 , _ > = ( ( new. len ( ) - 1 ) * 2 ) . try_into ( )
1254
+ let Ok ( new_len_without_nul_in_bytes) : Result < u32 , _ > =
1255
+ ( ( new. count_bytes ( ) - 1 ) * 2 ) . try_into ( )
1253
1256
else {
1254
1257
return Err ( err) . io_result ( ) ;
1255
1258
} ;
@@ -1278,7 +1281,7 @@ pub fn rename(old: &[u16], new: &[u16]) -> io::Result<()> {
1278
1281
1279
1282
new. as_ptr ( ) . copy_to_nonoverlapping (
1280
1283
( & raw mut ( * file_rename_info) . FileName ) . cast :: < u16 > ( ) ,
1281
- new. len ( ) ,
1284
+ new. count_bytes ( ) ,
1282
1285
) ;
1283
1286
}
1284
1287
@@ -1305,12 +1308,12 @@ pub fn rename(old: &[u16], new: &[u16]) -> io::Result<()> {
1305
1308
Ok ( ( ) )
1306
1309
}
1307
1310
1308
- pub fn rmdir ( p : & [ u16 ] ) -> io:: Result < ( ) > {
1311
+ pub fn rmdir ( p : & WCStr ) -> io:: Result < ( ) > {
1309
1312
cvt ( unsafe { c:: RemoveDirectoryW ( p. as_ptr ( ) ) } ) ?;
1310
1313
Ok ( ( ) )
1311
1314
}
1312
1315
1313
- pub fn remove_dir_all ( path : & [ u16 ] ) -> io:: Result < ( ) > {
1316
+ pub fn remove_dir_all ( path : & WCStr ) -> io:: Result < ( ) > {
1314
1317
// Open a file or directory without following symlinks.
1315
1318
let mut opts = OpenOptions :: new ( ) ;
1316
1319
opts. access_mode ( c:: FILE_LIST_DIRECTORY ) ;
@@ -1328,7 +1331,7 @@ pub fn remove_dir_all(path: &[u16]) -> io::Result<()> {
1328
1331
remove_dir_all_iterative ( file) . io_result ( )
1329
1332
}
1330
1333
1331
- pub fn readlink ( path : & [ u16 ] ) -> io:: Result < PathBuf > {
1334
+ pub fn readlink ( path : & WCStr ) -> io:: Result < PathBuf > {
1332
1335
// Open the link with no access mode, instead of generic read.
1333
1336
// By default FILE_LIST_DIRECTORY is denied for the junction "C:\Documents and Settings", so
1334
1337
// this is needed for a common case.
@@ -1373,17 +1376,17 @@ pub fn symlink_inner(original: &Path, link: &Path, dir: bool) -> io::Result<()>
1373
1376
}
1374
1377
1375
1378
#[ cfg( not( target_vendor = "uwp" ) ) ]
1376
- pub fn link ( original : & [ u16 ] , link : & [ u16 ] ) -> io:: Result < ( ) > {
1379
+ pub fn link ( original : & WCStr , link : & WCStr ) -> io:: Result < ( ) > {
1377
1380
cvt ( unsafe { c:: CreateHardLinkW ( link. as_ptr ( ) , original. as_ptr ( ) , ptr:: null_mut ( ) ) } ) ?;
1378
1381
Ok ( ( ) )
1379
1382
}
1380
1383
1381
1384
#[ cfg( target_vendor = "uwp" ) ]
1382
- pub fn link ( _original : & [ u16 ] , _link : & [ u16 ] ) -> io:: Result < ( ) > {
1385
+ pub fn link ( _original : & WCStr , _link : & WCStr ) -> io:: Result < ( ) > {
1383
1386
return Err ( io:: const_error!( io:: ErrorKind :: Unsupported , "hard link are not supported on UWP" ) ) ;
1384
1387
}
1385
1388
1386
- pub fn stat ( path : & [ u16 ] ) -> io:: Result < FileAttr > {
1389
+ pub fn stat ( path : & WCStr ) -> io:: Result < FileAttr > {
1387
1390
match metadata ( path, ReparsePoint :: Follow ) {
1388
1391
Err ( err) if err. raw_os_error ( ) == Some ( c:: ERROR_CANT_ACCESS_FILE as i32 ) => {
1389
1392
if let Ok ( attrs) = lstat ( path) {
@@ -1397,7 +1400,7 @@ pub fn stat(path: &[u16]) -> io::Result<FileAttr> {
1397
1400
}
1398
1401
}
1399
1402
1400
- pub fn lstat ( path : & [ u16 ] ) -> io:: Result < FileAttr > {
1403
+ pub fn lstat ( path : & WCStr ) -> io:: Result < FileAttr > {
1401
1404
metadata ( path, ReparsePoint :: Open )
1402
1405
}
1403
1406
@@ -1413,7 +1416,7 @@ impl ReparsePoint {
1413
1416
}
1414
1417
}
1415
1418
1416
- fn metadata ( path : & [ u16 ] , reparse : ReparsePoint ) -> io:: Result < FileAttr > {
1419
+ fn metadata ( path : & WCStr , reparse : ReparsePoint ) -> io:: Result < FileAttr > {
1417
1420
let mut opts = OpenOptions :: new ( ) ;
1418
1421
// No read or write permissions are necessary
1419
1422
opts. access_mode ( 0 ) ;
@@ -1473,7 +1476,7 @@ fn metadata(path: &[u16], reparse: ReparsePoint) -> io::Result<FileAttr> {
1473
1476
}
1474
1477
}
1475
1478
1476
- pub fn set_perm ( p : & [ u16 ] , perm : FilePermissions ) -> io:: Result < ( ) > {
1479
+ pub fn set_perm ( p : & WCStr , perm : FilePermissions ) -> io:: Result < ( ) > {
1477
1480
unsafe {
1478
1481
cvt ( c:: SetFileAttributesW ( p. as_ptr ( ) , perm. attrs ) ) ?;
1479
1482
Ok ( ( ) )
@@ -1489,7 +1492,7 @@ fn get_path(f: &File) -> io::Result<PathBuf> {
1489
1492
)
1490
1493
}
1491
1494
1492
- pub fn canonicalize ( p : & [ u16 ] ) -> io:: Result < PathBuf > {
1495
+ pub fn canonicalize ( p : & WCStr ) -> io:: Result < PathBuf > {
1493
1496
let mut opts = OpenOptions :: new ( ) ;
1494
1497
// No read or write permissions are necessary
1495
1498
opts. access_mode ( 0 ) ;
@@ -1499,7 +1502,7 @@ pub fn canonicalize(p: &[u16]) -> io::Result<PathBuf> {
1499
1502
get_path ( & f)
1500
1503
}
1501
1504
1502
- pub fn copy ( from : & [ u16 ] , to : & [ u16 ] ) -> io:: Result < u64 > {
1505
+ pub fn copy ( from : & WCStr , to : & WCStr ) -> io:: Result < u64 > {
1503
1506
unsafe extern "system" fn callback (
1504
1507
_TotalFileSize : i64 ,
1505
1508
_TotalBytesTransferred : i64 ,
@@ -1612,7 +1615,7 @@ pub fn junction_point(original: &Path, link: &Path) -> io::Result<()> {
1612
1615
}
1613
1616
1614
1617
// Try to see if a file exists but, unlike `exists`, report I/O errors.
1615
- pub fn exists ( path : & [ u16 ] ) -> io:: Result < bool > {
1618
+ pub fn exists ( path : & WCStr ) -> io:: Result < bool > {
1616
1619
// Open the file to ensure any symlinks are followed to their target.
1617
1620
let mut opts = OpenOptions :: new ( ) ;
1618
1621
// No read, write, etc access rights are needed.
0 commit comments