]>
git.proxmox.com Git - rustc.git/blob - src/librustc_back/fs.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use std
::path
::{Path, PathBuf}
;
15 pub fn realpath(original
: &Path
) -> io
::Result
<PathBuf
> {
16 Ok(original
.to_path_buf())
20 pub fn realpath(original
: &Path
) -> io
::Result
<PathBuf
> {
22 use std
::ffi
::{OsString, CString}
;
23 use std
::os
::unix
::prelude
::*;
26 fn realpath(pathname
: *const libc
::c_char
, resolved
: *mut libc
::c_char
)
30 let path
= try
!(CString
::new(original
.as_os_str().as_bytes()));
31 let mut buf
= vec
![0u8; 16 * 1024];
33 let r
= realpath(path
.as_ptr(), buf
.as_mut_ptr() as *mut _
);
35 return Err(io
::Error
::last_os_error())
38 let p
= buf
.iter().position(|i
| *i
== 0).unwrap();
40 Ok(PathBuf
::from(OsString
::from_vec(buf
)))
43 #[cfg(all(not(windows), test))]
46 use std
::fs
::{self, File}
;
51 let tmpdir
= TempDir
::new("rustc-fs").unwrap();
52 let tmpdir
= realpath(tmpdir
.path()).unwrap();
53 let file
= tmpdir
.join("test");
54 let dir
= tmpdir
.join("test2");
55 let link
= dir
.join("link");
56 let linkdir
= tmpdir
.join("test3");
58 File
::create(&file
).unwrap();
59 fs
::create_dir(&dir
).unwrap();
60 fs
::soft_link(&file
, &link
).unwrap();
61 fs
::soft_link(&dir
, &linkdir
).unwrap();
63 assert_eq
!(realpath(&tmpdir
).unwrap(), tmpdir
);
64 assert_eq
!(realpath(&file
).unwrap(), file
);
65 assert_eq
!(realpath(&link
).unwrap(), file
);
66 assert_eq
!(realpath(&linkdir
).unwrap(), dir
);
67 assert_eq
!(realpath(&linkdir
.join("link")).unwrap(), file
);
71 fn realpath_works_tricky() {
72 let tmpdir
= TempDir
::new("rustc-fs").unwrap();
73 let tmpdir
= realpath(tmpdir
.path()).unwrap();
75 let a
= tmpdir
.join("a");
82 fs
::create_dir_all(&b
).unwrap();
83 fs
::create_dir_all(&d
).unwrap();
84 File
::create(&f
).unwrap();
85 fs
::soft_link("../d/e", &c
).unwrap();
86 fs
::soft_link("../f", &e
).unwrap();
88 assert_eq
!(realpath(&c
).unwrap(), f
);
89 assert_eq
!(realpath(&e
).unwrap(), f
);