Skip to content

Commit 4fa8174

Browse files
committed
fix: std::sys::fs use_with_native_path for read_dir for windows
1 parent 11d2046 commit 4fa8174

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ pub use imp::{
6666
};
6767

6868
pub fn read_dir(path: &Path) -> io::Result<ReadDir> {
69-
// FIXME: use with_native_path on all platforms
70-
imp::readdir(path)
69+
with_native_path(path, &imp::readdir)
7170
}
7271

7372
pub fn remove_file(path: &Path) -> io::Result<()> {

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,11 +1182,23 @@ impl DirBuilder {
11821182
}
11831183
}
11841184

1185-
pub fn readdir(p: &Path) -> io::Result<ReadDir> {
1185+
pub fn readdir(p: &WCStr) -> io::Result<ReadDir> {
1186+
let mut p = unsafe { p.to_wchars_with_null_unchecked() }.to_vec();
1187+
1188+
// `p` already contains NUL, because before reading directory function,
1189+
// it already passes `maybe_verbatim` that appending zero at the end, it
1190+
// should be refactored.
1191+
if let Some(pos) = p.iter().position(|x| *x == 0) {
1192+
p.remove(pos);
1193+
}
1194+
1195+
let p_os_string = OsString::from_wide(&p);
1196+
let p = Path::new(&p_os_string);
1197+
11861198
// We push a `*` to the end of the path which cause the empty path to be
11871199
// treated as the current directory. So, for consistency with other platforms,
11881200
// we explicitly error on the empty path.
1189-
if p.as_os_str().is_empty() {
1201+
if p_os_string.is_empty() {
11901202
// Return an error code consistent with other ways of opening files.
11911203
// E.g. fs::metadata or File::open.
11921204
return Err(io::Error::from_raw_os_error(c::ERROR_PATH_NOT_FOUND as i32));

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::alloc_crate::slice;
12
use crate::ffi::{OsStr, OsString};
23
use crate::path::{Path, PathBuf};
34
use crate::sys::api::utf16;
@@ -29,6 +30,10 @@ impl WCStr {
2930
unsafe { &*(s as *const [u16] as *const Self) }
3031
}
3132

33+
pub unsafe fn to_wchars_with_null_unchecked(&self) -> &[u16] {
34+
unsafe { slice::from_raw_parts(self.as_ptr(), self.0.len()) }
35+
}
36+
3237
pub fn as_ptr(&self) -> *const u16 {
3338
self.0.as_ptr()
3439
}

0 commit comments

Comments
 (0)