Skip to content

Commit beaba52

Browse files
Rollup merge of #163065 - RalfJung:win-is-dir, r=ChrisDenton
windows Dir::rename: remove incorrect is_dir query Hopefully fixes #163022 r? @ChrisDenton try-jobs: test-x86_64-msvc-1
2 parents 6977d52 + 8a2e879 commit beaba52

2 files changed

Lines changed: 25 additions & 11 deletions

File tree

‎library/std/src/fs/tests.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3071,6 +3071,16 @@ fn test_dir_remove_dir() {
30713071
assert!(!matches!(exists(tmpdir.join("foo")), Ok(true)));
30723072
}
30733073

3074+
#[test]
3075+
fn test_dir_rename_dir() {
3076+
let tmpdir = tmpdir();
3077+
check!(fs::create_dir(tmpdir.join("foo")));
3078+
let dir = check!(Dir::open(tmpdir.path()));
3079+
check!(dir.rename("foo", &dir, "baz"));
3080+
let m = check!(tmpdir.join("baz").metadata());
3081+
assert!(m.is_dir());
3082+
}
3083+
30743084
#[test]
30753085
fn test_dir_create_dir() {
30763086
let tmpdir = tmpdir();

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,18 @@ impl Dir {
8282
return File::open(path, opts);
8383
}
8484
let path = to_u16s_without_nul(path)?;
85-
self.open_file_native(&path, opts, false).map(|handle| File { handle })
85+
self.open_file_native(&path, opts, c::FILE_NON_DIRECTORY_FILE).map(|handle| File { handle })
8686
}
8787

8888
pub fn remove_file(&self, path: &Path) -> io::Result<()> {
8989
let path = to_u16s_without_nul(path)?;
90-
self.remove_native(&path, false)
90+
self.remove_native(&path, /* dir */ false)
9191
}
9292

9393
pub fn rename(&self, from: &Path, to_dir: &Self, to: &Path) -> io::Result<()> {
94-
let is_dir = from.is_dir();
9594
let from = to_u16s_without_nul(from)?;
9695
let to = to_u16s_without_nul(to)?;
97-
self.rename_native(&from, to_dir, &to, is_dir)
96+
self.rename_native(&from, to_dir, &to)
9897
}
9998

10099
pub fn create_dir(&self, path: &Path) -> io::Result<()> {
@@ -107,12 +106,12 @@ impl Dir {
107106

108107
pub fn open_dir(&self, path: &Path, opts: &OpenOptions) -> io::Result<Self> {
109108
let path = to_u16s_without_nul(&path)?;
110-
self.open_file_native(&path, &opts, true).map(|handle| Self { handle })
109+
self.open_file_native(&path, &opts, c::FILE_DIRECTORY_FILE).map(|handle| Self { handle })
111110
}
112111

113112
pub fn remove_dir(&self, path: &Path) -> io::Result<()> {
114113
let path = to_u16s_without_nul(&path)?;
115-
self.remove_native(&path, true)
114+
self.remove_native(&path, /* dir */ true)
116115
}
117116

118117
fn open_with_native(path: &WCStr, opts: &OpenOptions) -> io::Result<Self> {
@@ -140,29 +139,34 @@ impl Dir {
140139
}
141140
}
142141

143-
fn open_file_native(&self, path: &[u16], opts: &OpenOptions, dir: bool) -> io::Result<Handle> {
142+
fn open_file_native(
143+
&self,
144+
path: &[u16],
145+
opts: &OpenOptions,
146+
create_opt: u32,
147+
) -> io::Result<Handle> {
144148
let name = UnicodeStrRef::from_slice(path);
145149
let object_attributes = c::OBJECT_ATTRIBUTES {
146150
RootDirectory: self.handle.as_raw_handle(),
147151
ObjectName: name.as_ptr().cast_mut(),
148152
..c::OBJECT_ATTRIBUTES::with_length()
149153
};
150-
let create_opt = if dir { c::FILE_DIRECTORY_FILE } else { c::FILE_NON_DIRECTORY_FILE };
151154
unsafe { nt_create_file(opts, &object_attributes, create_opt) }
152155
}
153156

154157
fn remove_native(&self, path: &[u16], dir: bool) -> io::Result<()> {
155158
let mut opts = OpenOptions::new();
156159
opts.access_mode(c::DELETE);
157-
let handle = self.open_file_native(path, &opts, dir)?;
160+
let create_opt = if dir { c::FILE_DIRECTORY_FILE } else { c::FILE_NON_DIRECTORY_FILE };
161+
let handle = self.open_file_native(path, &opts, create_opt)?;
158162
File::from_inner(handle).delete().io_result()
159163
}
160164

161-
fn rename_native(&self, from: &[u16], to_dir: &Self, to: &[u16], dir: bool) -> io::Result<()> {
165+
fn rename_native(&self, from: &[u16], to_dir: &Self, to: &[u16]) -> io::Result<()> {
162166
let mut opts = OpenOptions::new();
163167
opts.access_mode(c::DELETE);
164168
opts.custom_flags(c::FILE_FLAG_OPEN_REPARSE_POINT | c::FILE_FLAG_BACKUP_SEMANTICS);
165-
let handle = self.open_file_native(from, &opts, dir)?;
169+
let handle = self.open_file_native(from, &opts, /* create_opt */ 0)?;
166170
// Calculate the layout of the `FILE_RENAME_INFORMATION` we pass to `NtSetInformationFile`
167171
// This is a dynamically sized struct so we need to get the position of the last field to calculate the actual size.
168172
const too_long_err: io::Error =

0 commit comments

Comments
 (0)