1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use std::io;
use std::path::{Path, PathBuf};
#[cfg(windows)]
pub fn realpath(original: &Path) -> io::Result<PathBuf> {
Ok(original.to_path_buf())
}
#[cfg(unix)]
pub fn realpath(original: &Path) -> io::Result<PathBuf> {
use libc;
use std::ffi::{OsString, CString};
use std::os::unix::prelude::*;
extern {
fn realpath(pathname: *const libc::c_char, resolved: *mut libc::c_char)
-> *mut libc::c_char;
}
let path = try!(CString::new(original.as_os_str().as_bytes()));
let mut buf = vec![0u8; 16 * 1024];
unsafe {
let r = realpath(path.as_ptr(), buf.as_mut_ptr() as *mut _);
if r.is_null() {
return Err(io::Error::last_os_error())
}
}
let p = buf.iter().position(|i| *i == 0).unwrap();
buf.truncate(p);
Ok(PathBuf::from(OsString::from_vec(buf)))
}
#[cfg(all(not(windows), test))]
mod test {
use tempdir::TempDir;
use std::fs::{self, File};
use super::realpath;
#[test]
fn realpath_works() {
let tmpdir = TempDir::new("rustc-fs").unwrap();
let tmpdir = realpath(tmpdir.path()).unwrap();
let file = tmpdir.join("test");
let dir = tmpdir.join("test2");
let link = dir.join("link");
let linkdir = tmpdir.join("test3");
File::create(&file).unwrap();
fs::create_dir(&dir).unwrap();
fs::soft_link(&file, &link).unwrap();
fs::soft_link(&dir, &linkdir).unwrap();
assert_eq!(realpath(&tmpdir).unwrap(), tmpdir);
assert_eq!(realpath(&file).unwrap(), file);
assert_eq!(realpath(&link).unwrap(), file);
assert_eq!(realpath(&linkdir).unwrap(), dir);
assert_eq!(realpath(&linkdir.join("link")).unwrap(), file);
}
#[test]
fn realpath_works_tricky() {
let tmpdir = TempDir::new("rustc-fs").unwrap();
let tmpdir = realpath(tmpdir.path()).unwrap();
let a = tmpdir.join("a");
let b = a.join("b");
let c = b.join("c");
let d = a.join("d");
let e = d.join("e");
let f = a.join("f");
fs::create_dir_all(&b).unwrap();
fs::create_dir_all(&d).unwrap();
File::create(&f).unwrap();
fs::soft_link("../d/e", &c).unwrap();
fs::soft_link("../f", &e).unwrap();
assert_eq!(realpath(&c).unwrap(), f);
assert_eq!(realpath(&e).unwrap(), f);
}
}