@@ -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,9 +1214,8 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> {
1212
1214
}
1213
1215
}
1214
1216
1215
- pub fn unlink ( p : & Path ) -> io:: Result < ( ) > {
1216
- let p_u16s = maybe_verbatim ( p) ?;
1217
- if unsafe { c:: DeleteFileW ( p_u16s. as_ptr ( ) ) } == 0 {
1217
+ pub fn unlink ( path : & WCStr ) -> io:: Result < ( ) > {
1218
+ if unsafe { c:: DeleteFileW ( path. as_ptr ( ) ) } == 0 {
1218
1219
let err = api:: get_last_error ( ) ;
1219
1220
// if `DeleteFileW` fails with ERROR_ACCESS_DENIED then try to remove
1220
1221
// the file while ignoring the readonly attribute.
@@ -1223,7 +1224,7 @@ pub fn unlink(p: &Path) -> io::Result<()> {
1223
1224
let mut opts = OpenOptions :: new ( ) ;
1224
1225
opts. access_mode ( c:: DELETE ) ;
1225
1226
opts. custom_flags ( c:: FILE_FLAG_OPEN_REPARSE_POINT ) ;
1226
- if let Ok ( f) = File :: open_native ( & p_u16s , & opts) {
1227
+ if let Ok ( f) = File :: open_native ( & path , & opts) {
1227
1228
if f. posix_delete ( ) . is_ok ( ) {
1228
1229
return Ok ( ( ) ) ;
1229
1230
}
@@ -1236,10 +1237,7 @@ pub fn unlink(p: &Path) -> io::Result<()> {
1236
1237
}
1237
1238
}
1238
1239
1239
- pub fn rename ( old : & Path , new : & Path ) -> io:: Result < ( ) > {
1240
- let old = maybe_verbatim ( old) ?;
1241
- let new = maybe_verbatim ( new) ?;
1242
-
1240
+ pub fn rename ( old : & WCStr , new : & WCStr ) -> io:: Result < ( ) > {
1243
1241
if unsafe { c:: MoveFileExW ( old. as_ptr ( ) , new. as_ptr ( ) , c:: MOVEFILE_REPLACE_EXISTING ) } == 0 {
1244
1242
let err = api:: get_last_error ( ) ;
1245
1243
// if `MoveFileExW` fails with ERROR_ACCESS_DENIED then try to move
@@ -1253,7 +1251,8 @@ pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
1253
1251
1254
1252
// Calculate the layout of the `FILE_RENAME_INFO` we pass to `SetFileInformation`
1255
1253
// This is a dynamically sized struct so we need to get the position of the last field to calculate the actual size.
1256
- 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 ( )
1257
1256
else {
1258
1257
return Err ( err) . io_result ( ) ;
1259
1258
} ;
@@ -1282,7 +1281,7 @@ pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
1282
1281
1283
1282
new. as_ptr ( ) . copy_to_nonoverlapping (
1284
1283
( & raw mut ( * file_rename_info) . FileName ) . cast :: < u16 > ( ) ,
1285
- new. len ( ) ,
1284
+ new. count_bytes ( ) ,
1286
1285
) ;
1287
1286
}
1288
1287
@@ -1309,20 +1308,19 @@ pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
1309
1308
Ok ( ( ) )
1310
1309
}
1311
1310
1312
- pub fn rmdir ( p : & Path ) -> io:: Result < ( ) > {
1313
- let p = maybe_verbatim ( p) ?;
1311
+ pub fn rmdir ( p : & WCStr ) -> io:: Result < ( ) > {
1314
1312
cvt ( unsafe { c:: RemoveDirectoryW ( p. as_ptr ( ) ) } ) ?;
1315
1313
Ok ( ( ) )
1316
1314
}
1317
1315
1318
- pub fn remove_dir_all ( path : & Path ) -> io:: Result < ( ) > {
1316
+ pub fn remove_dir_all ( path : & WCStr ) -> io:: Result < ( ) > {
1319
1317
// Open a file or directory without following symlinks.
1320
1318
let mut opts = OpenOptions :: new ( ) ;
1321
1319
opts. access_mode ( c:: FILE_LIST_DIRECTORY ) ;
1322
1320
// `FILE_FLAG_BACKUP_SEMANTICS` allows opening directories.
1323
1321
// `FILE_FLAG_OPEN_REPARSE_POINT` opens a link instead of its target.
1324
1322
opts. custom_flags ( c:: FILE_FLAG_BACKUP_SEMANTICS | c:: FILE_FLAG_OPEN_REPARSE_POINT ) ;
1325
- let file = File :: open ( path, & opts) ?;
1323
+ let file = File :: open_native ( path, & opts) ?;
1326
1324
1327
1325
// Test if the file is not a directory or a symlink to a directory.
1328
1326
if ( file. basic_info ( ) ?. FileAttributes & c:: FILE_ATTRIBUTE_DIRECTORY ) == 0 {
@@ -1333,14 +1331,14 @@ pub fn remove_dir_all(path: &Path) -> io::Result<()> {
1333
1331
remove_dir_all_iterative ( file) . io_result ( )
1334
1332
}
1335
1333
1336
- pub fn readlink ( path : & Path ) -> io:: Result < PathBuf > {
1334
+ pub fn readlink ( path : & WCStr ) -> io:: Result < PathBuf > {
1337
1335
// Open the link with no access mode, instead of generic read.
1338
1336
// By default FILE_LIST_DIRECTORY is denied for the junction "C:\Documents and Settings", so
1339
1337
// this is needed for a common case.
1340
1338
let mut opts = OpenOptions :: new ( ) ;
1341
1339
opts. access_mode ( 0 ) ;
1342
1340
opts. custom_flags ( c:: FILE_FLAG_OPEN_REPARSE_POINT | c:: FILE_FLAG_BACKUP_SEMANTICS ) ;
1343
- let file = File :: open ( path, & opts) ?;
1341
+ let file = File :: open_native ( & path, & opts) ?;
1344
1342
file. readlink ( )
1345
1343
}
1346
1344
@@ -1378,19 +1376,17 @@ pub fn symlink_inner(original: &Path, link: &Path, dir: bool) -> io::Result<()>
1378
1376
}
1379
1377
1380
1378
#[ cfg( not( target_vendor = "uwp" ) ) ]
1381
- pub fn link ( original : & Path , link : & Path ) -> io:: Result < ( ) > {
1382
- let original = maybe_verbatim ( original) ?;
1383
- let link = maybe_verbatim ( link) ?;
1379
+ pub fn link ( original : & WCStr , link : & WCStr ) -> io:: Result < ( ) > {
1384
1380
cvt ( unsafe { c:: CreateHardLinkW ( link. as_ptr ( ) , original. as_ptr ( ) , ptr:: null_mut ( ) ) } ) ?;
1385
1381
Ok ( ( ) )
1386
1382
}
1387
1383
1388
1384
#[ cfg( target_vendor = "uwp" ) ]
1389
- pub fn link ( _original : & Path , _link : & Path ) -> io:: Result < ( ) > {
1385
+ pub fn link ( _original : & WCStr , _link : & WCStr ) -> io:: Result < ( ) > {
1390
1386
return Err ( io:: const_error!( io:: ErrorKind :: Unsupported , "hard link are not supported on UWP" ) ) ;
1391
1387
}
1392
1388
1393
- pub fn stat ( path : & Path ) -> io:: Result < FileAttr > {
1389
+ pub fn stat ( path : & WCStr ) -> io:: Result < FileAttr > {
1394
1390
match metadata ( path, ReparsePoint :: Follow ) {
1395
1391
Err ( err) if err. raw_os_error ( ) == Some ( c:: ERROR_CANT_ACCESS_FILE as i32 ) => {
1396
1392
if let Ok ( attrs) = lstat ( path) {
@@ -1404,7 +1400,7 @@ pub fn stat(path: &Path) -> io::Result<FileAttr> {
1404
1400
}
1405
1401
}
1406
1402
1407
- pub fn lstat ( path : & Path ) -> io:: Result < FileAttr > {
1403
+ pub fn lstat ( path : & WCStr ) -> io:: Result < FileAttr > {
1408
1404
metadata ( path, ReparsePoint :: Open )
1409
1405
}
1410
1406
@@ -1420,7 +1416,7 @@ impl ReparsePoint {
1420
1416
}
1421
1417
}
1422
1418
1423
- fn metadata ( path : & Path , reparse : ReparsePoint ) -> io:: Result < FileAttr > {
1419
+ fn metadata ( path : & WCStr , reparse : ReparsePoint ) -> io:: Result < FileAttr > {
1424
1420
let mut opts = OpenOptions :: new ( ) ;
1425
1421
// No read or write permissions are necessary
1426
1422
opts. access_mode ( 0 ) ;
@@ -1429,7 +1425,7 @@ fn metadata(path: &Path, reparse: ReparsePoint) -> io::Result<FileAttr> {
1429
1425
// Attempt to open the file normally.
1430
1426
// If that fails with `ERROR_SHARING_VIOLATION` then retry using `FindFirstFileExW`.
1431
1427
// If the fallback fails for any reason we return the original error.
1432
- match File :: open ( path, & opts) {
1428
+ match File :: open_native ( & path, & opts) {
1433
1429
Ok ( file) => file. file_attr ( ) ,
1434
1430
Err ( e)
1435
1431
if [ Some ( c:: ERROR_SHARING_VIOLATION as _ ) , Some ( c:: ERROR_ACCESS_DENIED as _ ) ]
@@ -1442,8 +1438,6 @@ fn metadata(path: &Path, reparse: ReparsePoint) -> io::Result<FileAttr> {
1442
1438
// However, there are special system files, such as
1443
1439
// `C:\hiberfil.sys`, that are locked in a way that denies even that.
1444
1440
unsafe {
1445
- let path = maybe_verbatim ( path) ?;
1446
-
1447
1441
// `FindFirstFileExW` accepts wildcard file names.
1448
1442
// Fortunately wildcards are not valid file names and
1449
1443
// `ERROR_SHARING_VIOLATION` means the file exists (but is locked)
@@ -1482,8 +1476,7 @@ fn metadata(path: &Path, reparse: ReparsePoint) -> io::Result<FileAttr> {
1482
1476
}
1483
1477
}
1484
1478
1485
- pub fn set_perm ( p : & Path , perm : FilePermissions ) -> io:: Result < ( ) > {
1486
- let p = maybe_verbatim ( p) ?;
1479
+ pub fn set_perm ( p : & WCStr , perm : FilePermissions ) -> io:: Result < ( ) > {
1487
1480
unsafe {
1488
1481
cvt ( c:: SetFileAttributesW ( p. as_ptr ( ) , perm. attrs ) ) ?;
1489
1482
Ok ( ( ) )
@@ -1499,17 +1492,17 @@ fn get_path(f: &File) -> io::Result<PathBuf> {
1499
1492
)
1500
1493
}
1501
1494
1502
- pub fn canonicalize ( p : & Path ) -> io:: Result < PathBuf > {
1495
+ pub fn canonicalize ( p : & WCStr ) -> io:: Result < PathBuf > {
1503
1496
let mut opts = OpenOptions :: new ( ) ;
1504
1497
// No read or write permissions are necessary
1505
1498
opts. access_mode ( 0 ) ;
1506
1499
// This flag is so we can open directories too
1507
1500
opts. custom_flags ( c:: FILE_FLAG_BACKUP_SEMANTICS ) ;
1508
- let f = File :: open ( p, & opts) ?;
1501
+ let f = File :: open_native ( p, & opts) ?;
1509
1502
get_path ( & f)
1510
1503
}
1511
1504
1512
- pub fn copy ( from : & Path , to : & Path ) -> io:: Result < u64 > {
1505
+ pub fn copy ( from : & WCStr , to : & WCStr ) -> io:: Result < u64 > {
1513
1506
unsafe extern "system" fn callback (
1514
1507
_TotalFileSize : i64 ,
1515
1508
_TotalBytesTransferred : i64 ,
@@ -1528,13 +1521,11 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
1528
1521
c:: PROGRESS_CONTINUE
1529
1522
}
1530
1523
}
1531
- let pfrom = maybe_verbatim ( from) ?;
1532
- let pto = maybe_verbatim ( to) ?;
1533
1524
let mut size = 0i64 ;
1534
1525
cvt ( unsafe {
1535
1526
c:: CopyFileExW (
1536
- pfrom . as_ptr ( ) ,
1537
- pto . as_ptr ( ) ,
1527
+ from . as_ptr ( ) ,
1528
+ to . as_ptr ( ) ,
1538
1529
Some ( callback) ,
1539
1530
( & raw mut size) as * mut _ ,
1540
1531
ptr:: null_mut ( ) ,
@@ -1624,14 +1615,14 @@ pub fn junction_point(original: &Path, link: &Path) -> io::Result<()> {
1624
1615
}
1625
1616
1626
1617
// Try to see if a file exists but, unlike `exists`, report I/O errors.
1627
- pub fn exists ( path : & Path ) -> io:: Result < bool > {
1618
+ pub fn exists ( path : & WCStr ) -> io:: Result < bool > {
1628
1619
// Open the file to ensure any symlinks are followed to their target.
1629
1620
let mut opts = OpenOptions :: new ( ) ;
1630
1621
// No read, write, etc access rights are needed.
1631
1622
opts. access_mode ( 0 ) ;
1632
1623
// Backup semantics enables opening directories as well as files.
1633
1624
opts. custom_flags ( c:: FILE_FLAG_BACKUP_SEMANTICS ) ;
1634
- match File :: open ( path, & opts) {
1625
+ match File :: open_native ( path, & opts) {
1635
1626
Err ( e) => match e. kind ( ) {
1636
1627
// The file definitely does not exist
1637
1628
io:: ErrorKind :: NotFound => Ok ( false ) ,
0 commit comments