Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions library/std/src/sys/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ pub use imp::{
};

pub fn read_dir(path: &Path) -> io::Result<ReadDir> {
// FIXME: use with_native_path on all platforms
imp::readdir(path)
#[cfg(not(windows))]
return imp::readdir(path);
#[cfg(windows)]
with_native_path(path, &imp::readdir)
}

pub fn remove_file(path: &Path) -> io::Result<()> {
Expand Down
16 changes: 14 additions & 2 deletions library/std/src/sys/fs/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1182,11 +1182,23 @@ impl DirBuilder {
}
}

pub fn readdir(p: &Path) -> io::Result<ReadDir> {
pub fn readdir(p: &WCStr) -> io::Result<ReadDir> {
let mut p = unsafe { p.to_wchars_with_null_unchecked() }.to_vec();

// `p` already contains NUL, because before reading directory function,
// it already passes `maybe_verbatim` that appending zero at the end, it
// should be refactored.
if let Some(pos) = p.iter().position(|x| *x == 0) {
p.remove(pos);
}

let p_os_string = OsString::from_wide(&p);
let p = Path::new(&p_os_string);

// We push a `*` to the end of the path which cause the empty path to be
// treated as the current directory. So, for consistency with other platforms,
// we explicitly error on the empty path.
if p.as_os_str().is_empty() {
if p_os_string.is_empty() {
// Return an error code consistent with other ways of opening files.
// E.g. fs::metadata or File::open.
return Err(io::Error::from_raw_os_error(c::ERROR_PATH_NOT_FOUND as i32));
Expand Down
5 changes: 5 additions & 0 deletions library/std/src/sys/path/windows.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::alloc_crate::slice;
use crate::ffi::{OsStr, OsString};
use crate::path::{Path, PathBuf};
use crate::sys::api::utf16;
Expand Down Expand Up @@ -29,6 +30,10 @@ impl WCStr {
unsafe { &*(s as *const [u16] as *const Self) }
}

pub unsafe fn to_wchars_with_null_unchecked(&self) -> &[u16] {
unsafe { slice::from_raw_parts(self.as_ptr(), self.0.len()) }
}

pub fn as_ptr(&self) -> *const u16 {
self.0.as_ptr()
}
Expand Down
Loading