Skip to content

Commit f1f6989

Browse files
committed
Use with_native_path for Windows
1 parent 2205455 commit f1f6989

File tree

3 files changed

+51
-43
lines changed

3 files changed

+51
-43
lines changed

library/std/src/sys/fs/mod.rs

+22-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ cfg_if::cfg_if! {
2020
mod windows;
2121
use windows as imp;
2222
pub use windows::{symlink_inner, junction_point};
23+
use crate::sys::path::with_native_path;
2324
} else if #[cfg(target_os = "hermit")] {
2425
mod hermit;
2526
use hermit as imp;
@@ -39,7 +40,7 @@ cfg_if::cfg_if! {
3940
}
4041

4142
// FIXME: Replace this with platform-specific path conversion functions.
42-
#[cfg(not(target_family = "unix"))]
43+
#[cfg(not(any(target_family = "unix", target_os = "windows")))]
4344
#[inline]
4445
pub fn with_native_path<T>(path: &Path, f: &dyn Fn(&Path) -> io::Result<T>) -> io::Result<T> {
4546
f(path)
@@ -51,7 +52,7 @@ pub use imp::{
5152
};
5253

5354
pub fn read_dir(path: &Path) -> io::Result<ReadDir> {
54-
// FIXME: use with_native_path
55+
// FIXME: use with_native_path on all platforms
5556
imp::readdir(path)
5657
}
5758

@@ -68,15 +69,22 @@ pub fn remove_dir(path: &Path) -> io::Result<()> {
6869
}
6970

7071
pub fn remove_dir_all(path: &Path) -> io::Result<()> {
71-
// FIXME: use with_native_path
72-
imp::remove_dir_all(path)
72+
// FIXME: use with_native_path on all platforms
73+
#[cfg(not(windows))]
74+
return imp::remove_dir_all(path);
75+
#[cfg(windows)]
76+
with_native_path(path, &imp::remove_dir_all)
7377
}
7478

7579
pub fn read_link(path: &Path) -> io::Result<PathBuf> {
7680
with_native_path(path, &imp::readlink)
7781
}
7882

7983
pub fn symlink(original: &Path, link: &Path) -> io::Result<()> {
84+
// FIXME: use with_native_path on all platforms
85+
#[cfg(windows)]
86+
return imp::symlink(original, link);
87+
#[cfg(not(windows))]
8088
with_native_path(original, &|original| {
8189
with_native_path(link, &|link| imp::symlink(original, link))
8290
})
@@ -105,11 +113,17 @@ pub fn canonicalize(path: &Path) -> io::Result<PathBuf> {
105113
}
106114

107115
pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
108-
// FIXME: use with_native_path
109-
imp::copy(from, to)
116+
// FIXME: use with_native_path on all platforms
117+
#[cfg(not(windows))]
118+
return imp::copy(from, to);
119+
#[cfg(windows)]
120+
with_native_path(from, &|from| with_native_path(to, &|to| imp::copy(from, to)))
110121
}
111122

112123
pub fn exists(path: &Path) -> io::Result<bool> {
113-
// FIXME: use with_native_path
114-
imp::exists(path)
124+
// FIXME: use with_native_path on all platforms
125+
#[cfg(not(windows))]
126+
return imp::exists(path);
127+
#[cfg(windows)]
128+
with_native_path(path, &imp::exists)
115129
}

library/std/src/sys/fs/windows.rs

+23-35
Original file line numberDiff line numberDiff line change
@@ -1212,9 +1212,8 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> {
12121212
}
12131213
}
12141214

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 {
1215+
pub fn unlink(path: &[u16]) -> io::Result<()> {
1216+
if unsafe { c::DeleteFileW(path.as_ptr()) } == 0 {
12181217
let err = api::get_last_error();
12191218
// if `DeleteFileW` fails with ERROR_ACCESS_DENIED then try to remove
12201219
// the file while ignoring the readonly attribute.
@@ -1223,7 +1222,7 @@ pub fn unlink(p: &Path) -> io::Result<()> {
12231222
let mut opts = OpenOptions::new();
12241223
opts.access_mode(c::DELETE);
12251224
opts.custom_flags(c::FILE_FLAG_OPEN_REPARSE_POINT);
1226-
if let Ok(f) = File::open_native(&p_u16s, &opts) {
1225+
if let Ok(f) = File::open_native(&path, &opts) {
12271226
if f.posix_delete().is_ok() {
12281227
return Ok(());
12291228
}
@@ -1236,10 +1235,7 @@ pub fn unlink(p: &Path) -> io::Result<()> {
12361235
}
12371236
}
12381237

1239-
pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
1240-
let old = maybe_verbatim(old)?;
1241-
let new = maybe_verbatim(new)?;
1242-
1238+
pub fn rename(old: &[u16], new: &[u16]) -> io::Result<()> {
12431239
if unsafe { c::MoveFileExW(old.as_ptr(), new.as_ptr(), c::MOVEFILE_REPLACE_EXISTING) } == 0 {
12441240
let err = api::get_last_error();
12451241
// if `MoveFileExW` fails with ERROR_ACCESS_DENIED then try to move
@@ -1309,20 +1305,19 @@ pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
13091305
Ok(())
13101306
}
13111307

1312-
pub fn rmdir(p: &Path) -> io::Result<()> {
1313-
let p = maybe_verbatim(p)?;
1308+
pub fn rmdir(p: &[u16]) -> io::Result<()> {
13141309
cvt(unsafe { c::RemoveDirectoryW(p.as_ptr()) })?;
13151310
Ok(())
13161311
}
13171312

1318-
pub fn remove_dir_all(path: &Path) -> io::Result<()> {
1313+
pub fn remove_dir_all(path: &[u16]) -> io::Result<()> {
13191314
// Open a file or directory without following symlinks.
13201315
let mut opts = OpenOptions::new();
13211316
opts.access_mode(c::FILE_LIST_DIRECTORY);
13221317
// `FILE_FLAG_BACKUP_SEMANTICS` allows opening directories.
13231318
// `FILE_FLAG_OPEN_REPARSE_POINT` opens a link instead of its target.
13241319
opts.custom_flags(c::FILE_FLAG_BACKUP_SEMANTICS | c::FILE_FLAG_OPEN_REPARSE_POINT);
1325-
let file = File::open(path, &opts)?;
1320+
let file = File::open_native(path, &opts)?;
13261321

13271322
// Test if the file is not a directory or a symlink to a directory.
13281323
if (file.basic_info()?.FileAttributes & c::FILE_ATTRIBUTE_DIRECTORY) == 0 {
@@ -1333,14 +1328,14 @@ pub fn remove_dir_all(path: &Path) -> io::Result<()> {
13331328
remove_dir_all_iterative(file).io_result()
13341329
}
13351330

1336-
pub fn readlink(path: &Path) -> io::Result<PathBuf> {
1331+
pub fn readlink(path: &[u16]) -> io::Result<PathBuf> {
13371332
// Open the link with no access mode, instead of generic read.
13381333
// By default FILE_LIST_DIRECTORY is denied for the junction "C:\Documents and Settings", so
13391334
// this is needed for a common case.
13401335
let mut opts = OpenOptions::new();
13411336
opts.access_mode(0);
13421337
opts.custom_flags(c::FILE_FLAG_OPEN_REPARSE_POINT | c::FILE_FLAG_BACKUP_SEMANTICS);
1343-
let file = File::open(path, &opts)?;
1338+
let file = File::open_native(&path, &opts)?;
13441339
file.readlink()
13451340
}
13461341

@@ -1378,19 +1373,17 @@ pub fn symlink_inner(original: &Path, link: &Path, dir: bool) -> io::Result<()>
13781373
}
13791374

13801375
#[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)?;
1376+
pub fn link(original: &[u16], link: &[u16]) -> io::Result<()> {
13841377
cvt(unsafe { c::CreateHardLinkW(link.as_ptr(), original.as_ptr(), ptr::null_mut()) })?;
13851378
Ok(())
13861379
}
13871380

13881381
#[cfg(target_vendor = "uwp")]
1389-
pub fn link(_original: &Path, _link: &Path) -> io::Result<()> {
1382+
pub fn link(_original: &[u16], _link: &[u16]) -> io::Result<()> {
13901383
return Err(io::const_error!(io::ErrorKind::Unsupported, "hard link are not supported on UWP"));
13911384
}
13921385

1393-
pub fn stat(path: &Path) -> io::Result<FileAttr> {
1386+
pub fn stat(path: &[u16]) -> io::Result<FileAttr> {
13941387
match metadata(path, ReparsePoint::Follow) {
13951388
Err(err) if err.raw_os_error() == Some(c::ERROR_CANT_ACCESS_FILE as i32) => {
13961389
if let Ok(attrs) = lstat(path) {
@@ -1404,7 +1397,7 @@ pub fn stat(path: &Path) -> io::Result<FileAttr> {
14041397
}
14051398
}
14061399

1407-
pub fn lstat(path: &Path) -> io::Result<FileAttr> {
1400+
pub fn lstat(path: &[u16]) -> io::Result<FileAttr> {
14081401
metadata(path, ReparsePoint::Open)
14091402
}
14101403

@@ -1420,7 +1413,7 @@ impl ReparsePoint {
14201413
}
14211414
}
14221415

1423-
fn metadata(path: &Path, reparse: ReparsePoint) -> io::Result<FileAttr> {
1416+
fn metadata(path: &[u16], reparse: ReparsePoint) -> io::Result<FileAttr> {
14241417
let mut opts = OpenOptions::new();
14251418
// No read or write permissions are necessary
14261419
opts.access_mode(0);
@@ -1429,7 +1422,7 @@ fn metadata(path: &Path, reparse: ReparsePoint) -> io::Result<FileAttr> {
14291422
// Attempt to open the file normally.
14301423
// If that fails with `ERROR_SHARING_VIOLATION` then retry using `FindFirstFileExW`.
14311424
// If the fallback fails for any reason we return the original error.
1432-
match File::open(path, &opts) {
1425+
match File::open_native(&path, &opts) {
14331426
Ok(file) => file.file_attr(),
14341427
Err(e)
14351428
if [Some(c::ERROR_SHARING_VIOLATION as _), Some(c::ERROR_ACCESS_DENIED as _)]
@@ -1442,8 +1435,6 @@ fn metadata(path: &Path, reparse: ReparsePoint) -> io::Result<FileAttr> {
14421435
// However, there are special system files, such as
14431436
// `C:\hiberfil.sys`, that are locked in a way that denies even that.
14441437
unsafe {
1445-
let path = maybe_verbatim(path)?;
1446-
14471438
// `FindFirstFileExW` accepts wildcard file names.
14481439
// Fortunately wildcards are not valid file names and
14491440
// `ERROR_SHARING_VIOLATION` means the file exists (but is locked)
@@ -1482,8 +1473,7 @@ fn metadata(path: &Path, reparse: ReparsePoint) -> io::Result<FileAttr> {
14821473
}
14831474
}
14841475

1485-
pub fn set_perm(p: &Path, perm: FilePermissions) -> io::Result<()> {
1486-
let p = maybe_verbatim(p)?;
1476+
pub fn set_perm(p: &[u16], perm: FilePermissions) -> io::Result<()> {
14871477
unsafe {
14881478
cvt(c::SetFileAttributesW(p.as_ptr(), perm.attrs))?;
14891479
Ok(())
@@ -1499,17 +1489,17 @@ fn get_path(f: &File) -> io::Result<PathBuf> {
14991489
)
15001490
}
15011491

1502-
pub fn canonicalize(p: &Path) -> io::Result<PathBuf> {
1492+
pub fn canonicalize(p: &[u16]) -> io::Result<PathBuf> {
15031493
let mut opts = OpenOptions::new();
15041494
// No read or write permissions are necessary
15051495
opts.access_mode(0);
15061496
// This flag is so we can open directories too
15071497
opts.custom_flags(c::FILE_FLAG_BACKUP_SEMANTICS);
1508-
let f = File::open(p, &opts)?;
1498+
let f = File::open_native(p, &opts)?;
15091499
get_path(&f)
15101500
}
15111501

1512-
pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
1502+
pub fn copy(from: &[u16], to: &[u16]) -> io::Result<u64> {
15131503
unsafe extern "system" fn callback(
15141504
_TotalFileSize: i64,
15151505
_TotalBytesTransferred: i64,
@@ -1528,13 +1518,11 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
15281518
c::PROGRESS_CONTINUE
15291519
}
15301520
}
1531-
let pfrom = maybe_verbatim(from)?;
1532-
let pto = maybe_verbatim(to)?;
15331521
let mut size = 0i64;
15341522
cvt(unsafe {
15351523
c::CopyFileExW(
1536-
pfrom.as_ptr(),
1537-
pto.as_ptr(),
1524+
from.as_ptr(),
1525+
to.as_ptr(),
15381526
Some(callback),
15391527
(&raw mut size) as *mut _,
15401528
ptr::null_mut(),
@@ -1624,14 +1612,14 @@ pub fn junction_point(original: &Path, link: &Path) -> io::Result<()> {
16241612
}
16251613

16261614
// Try to see if a file exists but, unlike `exists`, report I/O errors.
1627-
pub fn exists(path: &Path) -> io::Result<bool> {
1615+
pub fn exists(path: &[u16]) -> io::Result<bool> {
16281616
// Open the file to ensure any symlinks are followed to their target.
16291617
let mut opts = OpenOptions::new();
16301618
// No read, write, etc access rights are needed.
16311619
opts.access_mode(0);
16321620
// Backup semantics enables opening directories as well as files.
16331621
opts.custom_flags(c::FILE_FLAG_BACKUP_SEMANTICS);
1634-
match File::open(path, &opts) {
1622+
match File::open_native(path, &opts) {
16351623
Err(e) => match e.kind() {
16361624
// The file definitely does not exist
16371625
io::ErrorKind::NotFound => Ok(false),

library/std/src/sys/path/windows.rs

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ mod tests;
1010
pub const MAIN_SEP_STR: &str = "\\";
1111
pub const MAIN_SEP: char = '\\';
1212

13+
#[inline]
14+
pub fn with_native_path<T>(path: &Path, f: &dyn Fn(&[u16]) -> io::Result<T>) -> io::Result<T> {
15+
let path = maybe_verbatim(path)?;
16+
f(&path)
17+
}
18+
1319
#[inline]
1420
pub fn is_sep_byte(b: u8) -> bool {
1521
b == b'/' || b == b'\\'

0 commit comments

Comments
 (0)