Skip to content

Commit 6b9b2ee

Browse files
committed
Make fs::canonicalize work on directories on Windows
Signed-off-by: Peter Atashian <[email protected]>
1 parent edeb4f1 commit 6b9b2ee

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/libstd/fs.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,6 +2083,15 @@ mod tests {
20832083
check!(fs::create_dir_all(&path.join("a/")));
20842084
}
20852085

2086+
#[test]
2087+
fn canonicalize_works_simple() {
2088+
let tmpdir = tmpdir();
2089+
let tmpdir = fs::canonicalize(tmpdir.path()).unwrap();
2090+
let file = tmpdir.join("test");
2091+
File::create(&file).unwrap();
2092+
assert_eq!(fs::canonicalize(&file).unwrap(), file);
2093+
}
2094+
20862095
#[test]
20872096
#[cfg(not(windows))]
20882097
fn realpath_works() {

src/libstd/sys/windows/fs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,8 @@ fn get_path(f: &File) -> io::Result<PathBuf> {
583583
pub fn canonicalize(p: &Path) -> io::Result<PathBuf> {
584584
let mut opts = OpenOptions::new();
585585
opts.read(true);
586+
// This flag is so we can open directories too
587+
opts.flags_and_attributes(c::FILE_FLAG_BACKUP_SEMANTICS);
586588
let f = try!(File::open(p, &opts));
587589
get_path(&f)
588590
}

0 commit comments

Comments
 (0)