From 6b9b2ee8781ddb3dea00ba156bd3b5b0638c6f43 Mon Sep 17 00:00:00 2001 From: Peter Atashian Date: Tue, 29 Sep 2015 14:21:39 -0400 Subject: [PATCH] Make fs::canonicalize work on directories on Windows Signed-off-by: Peter Atashian --- src/libstd/fs.rs | 9 +++++++++ src/libstd/sys/windows/fs.rs | 2 ++ 2 files changed, 11 insertions(+) diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs index a3d509ba0f12d..16bf534563ecb 100644 --- a/src/libstd/fs.rs +++ b/src/libstd/fs.rs @@ -2083,6 +2083,15 @@ mod tests { check!(fs::create_dir_all(&path.join("a/"))); } + #[test] + fn canonicalize_works_simple() { + let tmpdir = tmpdir(); + let tmpdir = fs::canonicalize(tmpdir.path()).unwrap(); + let file = tmpdir.join("test"); + File::create(&file).unwrap(); + assert_eq!(fs::canonicalize(&file).unwrap(), file); + } + #[test] #[cfg(not(windows))] fn realpath_works() { diff --git a/src/libstd/sys/windows/fs.rs b/src/libstd/sys/windows/fs.rs index e9d98b36a43f8..721e259823ad6 100644 --- a/src/libstd/sys/windows/fs.rs +++ b/src/libstd/sys/windows/fs.rs @@ -583,6 +583,8 @@ fn get_path(f: &File) -> io::Result { pub fn canonicalize(p: &Path) -> io::Result { let mut opts = OpenOptions::new(); opts.read(true); + // This flag is so we can open directories too + opts.flags_and_attributes(c::FILE_FLAG_BACKUP_SEMANTICS); let f = try!(File::open(p, &opts)); get_path(&f) }