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
92
93
94
95
96
97
98
99
100
101
102
103
use std::old_io;
use std::old_io::fs;
use std::env;
pub fn realpath(original: &Path) -> old_io::IoResult<Path> {
static MAX_LINKS_FOLLOWED: uint = 256;
let original = try!(env::current_dir()).join(original);
if cfg!(windows) {
return Ok(original)
}
let result = original.root_path();
let mut result = result.expect("make_absolute has no root_path");
let mut followed = 0;
for part in original.components() {
result.push(part);
loop {
if followed == MAX_LINKS_FOLLOWED {
return Err(old_io::standard_error(old_io::InvalidInput))
}
match fs::lstat(&result) {
Err(..) => break,
Ok(ref stat) if stat.kind != old_io::FileType::Symlink => break,
Ok(..) => {
followed += 1;
let path = try!(fs::readlink(&result));
result.pop();
result.push(path);
}
}
}
}
return Ok(result);
}
#[cfg(all(not(windows), test))]
mod test {
use std::old_io;
use std::old_io::fs::{File, symlink, mkdir, mkdir_recursive};
use super::realpath;
use std::old_io::TempDir;
#[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();
mkdir(&dir, old_io::USER_RWX).unwrap();
symlink(&file, &link).unwrap();
symlink(&dir, &linkdir).unwrap();
assert!(realpath(&tmpdir).unwrap() == tmpdir);
assert!(realpath(&file).unwrap() == file);
assert!(realpath(&link).unwrap() == file);
assert!(realpath(&linkdir).unwrap() == dir);
assert!(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");
mkdir_recursive(&b, old_io::USER_RWX).unwrap();
mkdir_recursive(&d, old_io::USER_RWX).unwrap();
File::create(&f).unwrap();
symlink(&Path::new("../d/e"), &c).unwrap();
symlink(&Path::new("../f"), &e).unwrap();
assert!(realpath(&c).unwrap() == f);
assert!(realpath(&e).unwrap() == f);
}
}