Skip to content

Commit f261cce

Browse files
committed
fix windows tests
1 parent 772df42 commit f261cce

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

library/std/src/fs/tests.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2089,7 +2089,9 @@ fn test_rename_junction() {
20892089
#[test]
20902090
fn test_dir_smoke_test() {
20912091
let tmpdir = tmpdir();
2092-
check!(Dir::new(tmpdir.path()));
2092+
let dir = Dir::new(tmpdir.path());
2093+
println!("{dir:?}");
2094+
check!(dir);
20932095
}
20942096

20952097
#[test]

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

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,8 @@ fn run_path_with_utf16<T, P: AsRef<Path>>(
967967

968968
impl Dir {
969969
pub fn new<P: AsRef<Path>>(path: P) -> io::Result<Self> {
970-
let opts = OpenOptions::new();
970+
let mut opts = OpenOptions::new();
971+
opts.read(true);
971972
run_path_with_wcstr(path.as_ref(), &|path| Self::new_with_native(path, &opts))
972973
}
973974

@@ -981,31 +982,36 @@ impl Dir {
981982

982983
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> {
983984
let mut opts = OpenOptions::new();
984-
let path = path.as_ref().as_os_str().encode_wide().collect::<Vec<_>>();
985985
opts.read(true);
986-
self.open_native(&path, &opts).map(|handle| File { handle })
986+
let path = path.as_ref();
987+
if path.is_absolute() {
988+
return File::open(path, &opts);
989+
}
990+
let path = path.as_os_str().encode_wide().collect::<Vec<_>>();
991+
self.open_native(&path, &opts, false).map(|handle| File { handle })
987992
}
988993

989994
pub fn open_with<P: AsRef<Path>>(&self, path: P, opts: &OpenOptions) -> io::Result<File> {
990995
let path = path.as_ref().as_os_str().encode_wide().collect::<Vec<_>>();
991-
self.open_native(&path, &opts).map(|handle| File { handle })
996+
self.open_native(&path, &opts, false).map(|handle| File { handle })
992997
}
993998

994999
pub fn open_dir<P: AsRef<Path>>(&self, path: P) -> io::Result<Self> {
9951000
let mut opts = OpenOptions::new();
9961001
let path = path.as_ref().as_os_str().encode_wide().collect::<Vec<_>>();
9971002
opts.read(true);
998-
self.open_native(&path, &opts).map(|handle| Self { handle })
1003+
self.open_native(&path, &opts, true).map(|handle| Self { handle })
9991004
}
10001005

10011006
pub fn open_dir_with<P: AsRef<Path>>(&self, path: P, opts: &OpenOptions) -> io::Result<Self> {
10021007
let path = path.as_ref().as_os_str().encode_wide().collect::<Vec<_>>();
1003-
self.open_native(&path, &opts).map(|handle| Self { handle })
1008+
self.open_native(&path, &opts, true).map(|handle| Self { handle })
10041009
}
10051010

10061011
pub fn create_dir<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
10071012
let mut opts = OpenOptions::new();
10081013
opts.write(true);
1014+
opts.create_new(true);
10091015
run_path_with_utf16(path, &|path| self.create_dir_native(path, &opts).map(|_| ()))
10101016
}
10111017

@@ -1023,7 +1029,7 @@ impl Dir {
10231029
to_dir: &Self,
10241030
to: Q,
10251031
) -> io::Result<()> {
1026-
run_path_with_wcstr(to.as_ref(), &|to| self.rename_native(from.as_ref(), to_dir, to))
1032+
run_path_with_wcstr(to.as_ref(), &|to| self.rename_native(from.as_ref(), to_dir, to, false))
10271033
}
10281034

10291035
pub fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(&self, original: P, link: Q) -> io::Result<()> {
@@ -1057,16 +1063,16 @@ impl Dir {
10571063
}
10581064
}
10591065

1060-
fn open_native(&self, path: &[u16], opts: &OpenOptions) -> io::Result<Handle> {
1066+
fn open_native(&self, path: &[u16], opts: &OpenOptions, dir: bool) -> io::Result<Handle> {
10611067
let name = c::UNICODE_STRING {
1062-
Length: path.len() as _,
1063-
MaximumLength: path.len() as _,
1068+
Length: (path.len() * 2) as _,
1069+
MaximumLength: (path.len() * 2) as _,
10641070
Buffer: path.as_ptr() as *mut _,
10651071
};
10661072
let object_attributes = c::OBJECT_ATTRIBUTES {
10671073
Length: size_of::<c::OBJECT_ATTRIBUTES>() as _,
10681074
RootDirectory: self.handle.as_raw_handle(),
1069-
ObjectName: &name,
1075+
ObjectName: &raw const name,
10701076
Attributes: 0,
10711077
SecurityDescriptor: ptr::null(),
10721078
SecurityQualityOfService: ptr::null(),
@@ -1078,16 +1084,16 @@ impl Dir {
10781084
opts.get_disposition()?,
10791085
&object_attributes,
10801086
share,
1081-
false,
1087+
dir,
10821088
)
10831089
}
10841090
.io_result()
10851091
}
10861092

10871093
fn create_dir_native(&self, path: &[u16], opts: &OpenOptions) -> io::Result<Handle> {
10881094
let name = c::UNICODE_STRING {
1089-
Length: path.len() as _,
1090-
MaximumLength: path.len() as _,
1095+
Length: (path.len() * 2) as _,
1096+
MaximumLength: (path.len() * 2) as _,
10911097
Buffer: path.as_ptr() as *mut _,
10921098
};
10931099
let object_attributes = c::OBJECT_ATTRIBUTES {
@@ -1113,9 +1119,8 @@ impl Dir {
11131119

11141120
fn remove_native(&self, path: &[u16], dir: bool) -> io::Result<()> {
11151121
let mut opts = OpenOptions::new();
1116-
opts.access_mode(c::GENERIC_WRITE);
1117-
let handle =
1118-
if dir { self.create_dir_native(path, &opts) } else { self.open_native(path, &opts) }?;
1122+
opts.access_mode(c::DELETE);
1123+
let handle = self.open_native(path, &opts, dir)?;
11191124
let info = c::FILE_DISPOSITION_INFO_EX { Flags: c::FILE_DISPOSITION_FLAG_DELETE };
11201125
let result = unsafe {
11211126
c::SetFileInformationByHandle(
@@ -1128,10 +1133,11 @@ impl Dir {
11281133
if result == 0 { Err(api::get_last_error()).io_result() } else { Ok(()) }
11291134
}
11301135

1131-
fn rename_native(&self, from: &Path, to_dir: &Self, to: &WCStr) -> io::Result<()> {
1136+
fn rename_native(&self, from: &Path, to_dir: &Self, to: &WCStr, dir: bool) -> io::Result<()> {
11321137
let mut opts = OpenOptions::new();
1133-
opts.access_mode(c::GENERIC_WRITE);
1134-
let handle = run_path_with_utf16(from, &|u| self.open_native(u, &opts))?;
1138+
opts.access_mode(c::DELETE);
1139+
opts.custom_flags(c::FILE_FLAG_OPEN_REPARSE_POINT | c::FILE_FLAG_BACKUP_SEMANTICS);
1140+
let handle = run_path_with_utf16(from, &|u| self.open_native(u, &opts, dir))?;
11351141
// Calculate the layout of the `FILE_RENAME_INFO` we pass to `SetFileInformation`
11361142
// This is a dynamically sized struct so we need to get the position of the last field to calculate the actual size.
11371143
const too_long_err: io::Error =
@@ -1143,9 +1149,9 @@ impl Dir {
11431149
.ok_or(too_long_err)?;
11441150
let layout = Layout::from_size_align(struct_size, align_of::<c::FILE_RENAME_INFO>())
11451151
.map_err(|_| too_long_err)?;
1152+
let struct_size = u32::try_from(struct_size).map_err(|_| too_long_err)?;
11461153
let to_byte_len_without_nul =
11471154
u32::try_from((to.count_bytes() - 1) * 2).map_err(|_| too_long_err)?;
1148-
let struct_size = u32::try_from(struct_size).map_err(|_| too_long_err)?;
11491155

11501156
let file_rename_info;
11511157
// SAFETY: We allocate enough memory for a full FILE_RENAME_INFO struct and a filename.
@@ -1165,7 +1171,7 @@ impl Dir {
11651171

11661172
to.as_ptr().copy_to_nonoverlapping(
11671173
(&raw mut (*file_rename_info).FileName).cast::<u16>(),
1168-
run_path_with_wcstr(from, &|s| Ok(s.count_bytes())).unwrap(),
1174+
to.count_bytes(),
11691175
);
11701176
}
11711177

0 commit comments

Comments
 (0)