2014-04-08 12:06:11 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-01-22 18:31:00 -06:00
|
|
|
use std::old_io;
|
|
|
|
use std::old_io::fs;
|
2014-04-08 12:06:11 -05:00
|
|
|
use std::os;
|
|
|
|
|
|
|
|
/// Returns an absolute path in the filesystem that `path` points to. The
|
|
|
|
/// returned path does not contain any symlinks in its hierarchy.
|
2015-01-22 18:31:00 -06:00
|
|
|
pub fn realpath(original: &Path) -> old_io::IoResult<Path> {
|
2014-04-08 12:06:11 -05:00
|
|
|
static MAX_LINKS_FOLLOWED: uint = 256;
|
2014-11-10 23:38:20 -06:00
|
|
|
let original = os::make_absolute(original).unwrap();
|
2014-04-08 12:06:11 -05:00
|
|
|
|
|
|
|
// Right now lstat on windows doesn't work quite well
|
|
|
|
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 {
|
2015-01-22 18:31:00 -06:00
|
|
|
return Err(old_io::standard_error(old_io::InvalidInput))
|
2014-04-08 12:06:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
match fs::lstat(&result) {
|
|
|
|
Err(..) => break,
|
2015-01-22 18:31:00 -06:00
|
|
|
Ok(ref stat) if stat.kind != old_io::FileType::Symlink => break,
|
2014-04-08 12:06:11 -05:00
|
|
|
Ok(..) => {
|
|
|
|
followed += 1;
|
|
|
|
let path = try!(fs::readlink(&result));
|
|
|
|
result.pop();
|
|
|
|
result.push(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(result);
|
|
|
|
}
|
|
|
|
|
2014-09-29 01:38:54 -05:00
|
|
|
#[cfg(all(not(windows), test))]
|
2014-04-08 12:06:11 -05:00
|
|
|
mod test {
|
2015-01-22 18:31:00 -06:00
|
|
|
use std::old_io;
|
|
|
|
use std::old_io::fs::{File, symlink, mkdir, mkdir_recursive};
|
2014-04-08 12:06:11 -05:00
|
|
|
use super::realpath;
|
2015-01-22 18:31:00 -06:00
|
|
|
use std::old_io::TempDir;
|
2014-04-08 12:06:11 -05:00
|
|
|
|
|
|
|
#[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();
|
2015-01-22 18:31:00 -06:00
|
|
|
mkdir(&dir, old_io::USER_RWX).unwrap();
|
2014-04-08 12:06:11 -05:00
|
|
|
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");
|
|
|
|
|
2015-01-22 18:31:00 -06:00
|
|
|
mkdir_recursive(&b, old_io::USER_RWX).unwrap();
|
|
|
|
mkdir_recursive(&d, old_io::USER_RWX).unwrap();
|
2014-04-08 12:06:11 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|