2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 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.
|
|
|
|
|
2012-09-19 18:52:32 -05:00
|
|
|
/*!
|
|
|
|
|
|
|
|
Cross-platform file path handling
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
#[allow(missing_doc)];
|
|
|
|
|
2013-07-02 14:47:32 -05:00
|
|
|
use clone::Clone;
|
2013-05-12 19:34:15 -05:00
|
|
|
use container::Container;
|
2012-08-27 18:26:35 -05:00
|
|
|
use cmp::Eq;
|
2013-06-09 08:10:50 -05:00
|
|
|
use iterator::IteratorUtil;
|
2012-12-23 16:41:37 -06:00
|
|
|
use libc;
|
2013-01-08 21:37:25 -06:00
|
|
|
use option::{None, Option, Some};
|
2013-06-14 21:40:11 -05:00
|
|
|
use str::{OwnedStr, Str, StrSlice, StrVector};
|
2013-06-11 21:13:42 -05:00
|
|
|
use str;
|
2013-01-08 21:37:25 -06:00
|
|
|
use to_str::ToStr;
|
2013-04-23 11:20:55 -05:00
|
|
|
use ascii::{AsciiCast, AsciiStr};
|
2013-06-21 07:29:53 -05:00
|
|
|
use vec::{OwnedVector, ImmutableVector};
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2013-04-26 20:52:15 -05:00
|
|
|
#[cfg(windows)]
|
|
|
|
pub use Path = self::WindowsPath;
|
|
|
|
#[cfg(unix)]
|
|
|
|
pub use Path = self::PosixPath;
|
|
|
|
|
2013-03-22 14:33:53 -05:00
|
|
|
#[deriving(Clone, Eq)]
|
2012-09-27 17:44:09 -05:00
|
|
|
pub struct WindowsPath {
|
2012-09-07 16:50:47 -05:00
|
|
|
host: Option<~str>,
|
|
|
|
device: Option<~str>,
|
|
|
|
is_absolute: bool,
|
|
|
|
components: ~[~str],
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn WindowsPath(s: &str) -> WindowsPath {
|
2012-12-13 17:55:05 -06:00
|
|
|
GenericPath::from_str(s)
|
2012-11-17 22:23:44 -06:00
|
|
|
}
|
|
|
|
|
2013-03-22 14:33:53 -05:00
|
|
|
#[deriving(Clone, Eq)]
|
2012-09-27 17:44:09 -05:00
|
|
|
pub struct PosixPath {
|
2012-09-07 16:50:47 -05:00
|
|
|
is_absolute: bool,
|
|
|
|
components: ~[~str],
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn PosixPath(s: &str) -> PosixPath {
|
2012-12-13 17:55:05 -06:00
|
|
|
GenericPath::from_str(s)
|
2012-11-17 22:23:44 -06:00
|
|
|
}
|
|
|
|
|
2012-09-27 17:44:09 -05:00
|
|
|
pub trait GenericPath {
|
2013-04-26 19:50:20 -05:00
|
|
|
/// Converts a string to a Path
|
2013-03-21 23:20:48 -05:00
|
|
|
fn from_str(&str) -> Self;
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-04-26 19:50:20 -05:00
|
|
|
/// Returns the directory component of `self`, as a string
|
2013-03-21 23:20:48 -05:00
|
|
|
fn dirname(&self) -> ~str;
|
2013-04-26 19:50:20 -05:00
|
|
|
/// Returns the file component of `self`, as a string option.
|
|
|
|
/// Returns None if `self` names a directory.
|
2013-03-21 23:20:48 -05:00
|
|
|
fn filename(&self) -> Option<~str>;
|
2013-04-26 19:50:20 -05:00
|
|
|
/// Returns the stem of the file component of `self`, as a string option.
|
|
|
|
/// The stem is the slice of a filename starting at 0 and ending just before
|
|
|
|
/// the last '.' in the name.
|
|
|
|
/// Returns None if `self` names a directory.
|
2013-03-21 23:20:48 -05:00
|
|
|
fn filestem(&self) -> Option<~str>;
|
2013-04-26 19:50:20 -05:00
|
|
|
/// Returns the type of the file component of `self`, as a string option.
|
|
|
|
/// The file type is the slice of a filename starting just after the last
|
|
|
|
/// '.' in the name and ending at the last index in the filename.
|
|
|
|
/// Returns None if `self` names a directory.
|
2013-03-21 23:20:48 -05:00
|
|
|
fn filetype(&self) -> Option<~str>;
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-04-26 19:50:20 -05:00
|
|
|
/// Returns a new path consisting of `self` with the parent directory component replaced
|
|
|
|
/// with the given string.
|
2013-03-21 23:20:48 -05:00
|
|
|
fn with_dirname(&self, (&str)) -> Self;
|
2013-04-26 19:50:20 -05:00
|
|
|
/// Returns a new path consisting of `self` with the file component replaced
|
|
|
|
/// with the given string.
|
2013-03-21 23:20:48 -05:00
|
|
|
fn with_filename(&self, (&str)) -> Self;
|
2013-04-26 19:50:20 -05:00
|
|
|
/// Returns a new path consisting of `self` with the file stem replaced
|
|
|
|
/// with the given string.
|
2013-03-21 23:20:48 -05:00
|
|
|
fn with_filestem(&self, (&str)) -> Self;
|
2013-04-26 19:50:20 -05:00
|
|
|
/// Returns a new path consisting of `self` with the file type replaced
|
|
|
|
/// with the given string.
|
2013-03-21 23:20:48 -05:00
|
|
|
fn with_filetype(&self, (&str)) -> Self;
|
2012-08-29 15:25:31 -05:00
|
|
|
|
2013-04-26 19:50:20 -05:00
|
|
|
/// Returns the directory component of `self`, as a new path.
|
|
|
|
/// If `self` has no parent, returns `self`.
|
2013-03-21 23:20:48 -05:00
|
|
|
fn dir_path(&self) -> Self;
|
2013-04-26 19:50:20 -05:00
|
|
|
/// Returns the file component of `self`, as a new path.
|
|
|
|
/// If `self` names a directory, returns the empty path.
|
2013-03-21 23:20:48 -05:00
|
|
|
fn file_path(&self) -> Self;
|
2012-11-29 20:37:33 -06:00
|
|
|
|
2013-04-26 19:50:20 -05:00
|
|
|
/// Returns a new Path whose parent directory is `self` and whose
|
|
|
|
/// file component is the given string.
|
2013-03-21 23:20:48 -05:00
|
|
|
fn push(&self, (&str)) -> Self;
|
2013-04-26 19:50:20 -05:00
|
|
|
/// Returns a new Path consisting of the given path, made relative to `self`.
|
2013-03-21 23:20:48 -05:00
|
|
|
fn push_rel(&self, (&Self)) -> Self;
|
2013-04-26 19:50:20 -05:00
|
|
|
/// Returns a new Path consisting of the path given by the given vector
|
|
|
|
/// of strings, relative to `self`.
|
2013-06-12 11:56:09 -05:00
|
|
|
fn push_many<S: Str>(&self, (&[S])) -> Self;
|
2013-04-26 19:50:20 -05:00
|
|
|
/// Identical to `dir_path` except in the case where `self` has only one
|
|
|
|
/// component. In this case, `pop` returns the empty path.
|
2013-03-21 23:20:48 -05:00
|
|
|
fn pop(&self) -> Self;
|
2012-08-29 15:59:02 -05:00
|
|
|
|
2013-04-26 19:50:20 -05:00
|
|
|
/// The same as `push_rel`, except that the directory argument must not
|
|
|
|
/// contain directory separators in any of its components.
|
2013-03-21 23:20:48 -05:00
|
|
|
fn unsafe_join(&self, (&Self)) -> Self;
|
2013-04-26 19:50:20 -05:00
|
|
|
/// On Unix, always returns false. On Windows, returns true iff `self`'s
|
|
|
|
/// file stem is one of: `con` `aux` `com1` `com2` `com3` `com4`
|
|
|
|
/// `lpt1` `lpt2` `lpt3` `prn` `nul`
|
2013-03-21 23:20:48 -05:00
|
|
|
fn is_restricted(&self) -> bool;
|
2013-02-18 16:48:18 -06:00
|
|
|
|
2013-04-26 19:50:20 -05:00
|
|
|
/// Returns a new path that names the same file as `self`, without containing
|
|
|
|
/// any '.', '..', or empty components. On Windows, uppercases the drive letter
|
|
|
|
/// as well.
|
2013-03-21 23:20:48 -05:00
|
|
|
fn normalize(&self) -> Self;
|
2013-04-20 00:22:46 -05:00
|
|
|
|
2013-04-26 19:50:20 -05:00
|
|
|
/// Returns `true` if `self` is an absolute path.
|
2013-04-20 00:22:46 -05:00
|
|
|
fn is_absolute(&self) -> bool;
|
2013-06-26 19:42:24 -05:00
|
|
|
|
|
|
|
/// True if `self` is an ancestor of `other`. See `test_is_ancestor_of` for examples
|
|
|
|
fn is_ancestor_of(&self, (&Self)) -> bool;
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-11-09 19:50:23 -06:00
|
|
|
#[cfg(target_os = "linux")]
|
2012-11-29 18:21:49 -06:00
|
|
|
#[cfg(target_os = "android")]
|
2012-11-09 19:50:23 -06:00
|
|
|
mod stat {
|
|
|
|
#[cfg(target_arch = "x86")]
|
|
|
|
pub mod arch {
|
2012-12-23 16:41:37 -06:00
|
|
|
use libc;
|
|
|
|
|
2012-11-09 19:50:23 -06:00
|
|
|
pub fn default_stat() -> libc::stat {
|
|
|
|
libc::stat {
|
|
|
|
st_dev: 0,
|
|
|
|
__pad1: 0,
|
|
|
|
st_ino: 0,
|
|
|
|
st_mode: 0,
|
|
|
|
st_nlink: 0,
|
|
|
|
st_uid: 0,
|
|
|
|
st_gid: 0,
|
|
|
|
st_rdev: 0,
|
|
|
|
__pad2: 0,
|
|
|
|
st_size: 0,
|
|
|
|
st_blksize: 0,
|
|
|
|
st_blocks: 0,
|
|
|
|
st_atime: 0,
|
|
|
|
st_atime_nsec: 0,
|
|
|
|
st_mtime: 0,
|
|
|
|
st_mtime_nsec: 0,
|
|
|
|
st_ctime: 0,
|
|
|
|
st_ctime_nsec: 0,
|
|
|
|
__unused4: 0,
|
|
|
|
__unused5: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-17 04:53:33 -05:00
|
|
|
#[cfg(target_arch = "arm")]
|
|
|
|
pub mod arch {
|
|
|
|
use libc;
|
|
|
|
|
|
|
|
pub fn default_stat() -> libc::stat {
|
|
|
|
libc::stat {
|
|
|
|
st_dev: 0,
|
|
|
|
__pad0: [0, ..4],
|
|
|
|
__st_ino: 0,
|
|
|
|
st_mode: 0,
|
|
|
|
st_nlink: 0,
|
|
|
|
st_uid: 0,
|
|
|
|
st_gid: 0,
|
|
|
|
st_rdev: 0,
|
|
|
|
__pad3: [0, ..4],
|
|
|
|
st_size: 0,
|
|
|
|
st_blksize: 0,
|
|
|
|
st_blocks: 0,
|
|
|
|
st_atime: 0,
|
|
|
|
st_atime_nsec: 0,
|
|
|
|
st_mtime: 0,
|
|
|
|
st_mtime_nsec: 0,
|
|
|
|
st_ctime: 0,
|
|
|
|
st_ctime_nsec: 0,
|
|
|
|
st_ino: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-07 05:03:32 -05:00
|
|
|
#[cfg(target_arch = "mips")]
|
|
|
|
pub mod arch {
|
|
|
|
use libc;
|
|
|
|
|
|
|
|
pub fn default_stat() -> libc::stat {
|
|
|
|
libc::stat {
|
|
|
|
st_dev: 0,
|
|
|
|
st_pad1: [0, ..3],
|
|
|
|
st_ino: 0,
|
|
|
|
st_mode: 0,
|
|
|
|
st_nlink: 0,
|
|
|
|
st_uid: 0,
|
|
|
|
st_gid: 0,
|
|
|
|
st_rdev: 0,
|
|
|
|
st_pad2: [0, ..2],
|
|
|
|
st_size: 0,
|
|
|
|
st_pad3: 0,
|
|
|
|
st_atime: 0,
|
|
|
|
st_atime_nsec: 0,
|
|
|
|
st_mtime: 0,
|
|
|
|
st_mtime_nsec: 0,
|
|
|
|
st_ctime: 0,
|
|
|
|
st_ctime_nsec: 0,
|
|
|
|
st_blksize: 0,
|
|
|
|
st_blocks: 0,
|
|
|
|
st_pad5: [0, ..14],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-09 19:50:23 -06:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
pub mod arch {
|
2012-12-23 16:41:37 -06:00
|
|
|
use libc;
|
|
|
|
|
2012-11-09 19:50:23 -06:00
|
|
|
pub fn default_stat() -> libc::stat {
|
|
|
|
libc::stat {
|
|
|
|
st_dev: 0,
|
|
|
|
st_ino: 0,
|
|
|
|
st_nlink: 0,
|
|
|
|
st_mode: 0,
|
|
|
|
st_uid: 0,
|
|
|
|
st_gid: 0,
|
|
|
|
__pad0: 0,
|
|
|
|
st_rdev: 0,
|
|
|
|
st_size: 0,
|
|
|
|
st_blksize: 0,
|
|
|
|
st_blocks: 0,
|
|
|
|
st_atime: 0,
|
|
|
|
st_atime_nsec: 0,
|
|
|
|
st_mtime: 0,
|
|
|
|
st_mtime_nsec: 0,
|
|
|
|
st_ctime: 0,
|
|
|
|
st_ctime_nsec: 0,
|
|
|
|
__unused: [0, 0, 0],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "freebsd")]
|
|
|
|
mod stat {
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
pub mod arch {
|
2012-12-23 16:41:37 -06:00
|
|
|
use libc;
|
|
|
|
|
2012-11-09 19:50:23 -06:00
|
|
|
pub fn default_stat() -> libc::stat {
|
|
|
|
libc::stat {
|
|
|
|
st_dev: 0,
|
|
|
|
st_ino: 0,
|
|
|
|
st_mode: 0,
|
|
|
|
st_nlink: 0,
|
|
|
|
st_uid: 0,
|
|
|
|
st_gid: 0,
|
|
|
|
st_rdev: 0,
|
|
|
|
st_atime: 0,
|
|
|
|
st_atime_nsec: 0,
|
|
|
|
st_mtime: 0,
|
|
|
|
st_mtime_nsec: 0,
|
|
|
|
st_ctime: 0,
|
|
|
|
st_ctime_nsec: 0,
|
|
|
|
st_size: 0,
|
|
|
|
st_blocks: 0,
|
|
|
|
st_blksize: 0,
|
|
|
|
st_flags: 0,
|
|
|
|
st_gen: 0,
|
|
|
|
st_lspare: 0,
|
|
|
|
st_birthtime: 0,
|
|
|
|
st_birthtime_nsec: 0,
|
|
|
|
__unused: [0, 0],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
mod stat {
|
|
|
|
pub mod arch {
|
2012-12-23 16:41:37 -06:00
|
|
|
use libc;
|
|
|
|
|
2012-11-09 19:50:23 -06:00
|
|
|
pub fn default_stat() -> libc::stat {
|
|
|
|
libc::stat {
|
|
|
|
st_dev: 0,
|
|
|
|
st_mode: 0,
|
|
|
|
st_nlink: 0,
|
|
|
|
st_ino: 0,
|
|
|
|
st_uid: 0,
|
|
|
|
st_gid: 0,
|
|
|
|
st_rdev: 0,
|
|
|
|
st_atime: 0,
|
|
|
|
st_atime_nsec: 0,
|
|
|
|
st_mtime: 0,
|
|
|
|
st_mtime_nsec: 0,
|
|
|
|
st_ctime: 0,
|
|
|
|
st_ctime_nsec: 0,
|
|
|
|
st_birthtime: 0,
|
|
|
|
st_birthtime_nsec: 0,
|
|
|
|
st_size: 0,
|
|
|
|
st_blocks: 0,
|
|
|
|
st_blksize: 0,
|
|
|
|
st_flags: 0,
|
|
|
|
st_gen: 0,
|
|
|
|
st_lspare: 0,
|
|
|
|
st_qspare: [0, 0],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-21 17:35:47 -06:00
|
|
|
#[cfg(target_os = "win32")]
|
2012-11-09 19:50:23 -06:00
|
|
|
mod stat {
|
|
|
|
pub mod arch {
|
2013-01-04 17:38:56 -06:00
|
|
|
use libc;
|
2012-11-09 19:50:23 -06:00
|
|
|
pub fn default_stat() -> libc::stat {
|
|
|
|
libc::stat {
|
|
|
|
st_dev: 0,
|
|
|
|
st_ino: 0,
|
|
|
|
st_mode: 0,
|
|
|
|
st_nlink: 0,
|
|
|
|
st_uid: 0,
|
|
|
|
st_gid: 0,
|
|
|
|
st_rdev: 0,
|
|
|
|
st_size: 0,
|
|
|
|
st_atime: 0,
|
|
|
|
st_mtime: 0,
|
|
|
|
st_ctime: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 20:21:47 -05:00
|
|
|
#[cfg(target_os = "win32")]
|
|
|
|
impl WindowsPath {
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn stat(&self) -> Option<libc::stat> {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
do str::as_c_str(self.to_str()) |buf| {
|
|
|
|
let mut st = stat::arch::default_stat();
|
2013-05-18 01:27:54 -05:00
|
|
|
match libc::stat(buf, &mut st) {
|
|
|
|
0 => Some(st),
|
|
|
|
_ => None,
|
|
|
|
}
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-11-09 19:50:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 20:21:47 -05:00
|
|
|
pub fn exists(&self) -> bool {
|
|
|
|
match self.stat() {
|
|
|
|
None => false,
|
|
|
|
Some(_) => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_size(&self) -> Option<i64> {
|
|
|
|
match self.stat() {
|
|
|
|
None => None,
|
|
|
|
Some(ref st) => Some(st.st_size as i64),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_mode(&self) -> Option<uint> {
|
|
|
|
match self.stat() {
|
|
|
|
None => None,
|
|
|
|
Some(ref st) => Some(st.st_mode as uint),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "win32"))]
|
|
|
|
impl PosixPath {
|
|
|
|
pub fn stat(&self) -> Option<libc::stat> {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2013-06-14 20:21:47 -05:00
|
|
|
do str::as_c_str(self.to_str()) |buf| {
|
2013-01-10 23:23:07 -06:00
|
|
|
let mut st = stat::arch::default_stat();
|
2013-06-14 20:21:47 -05:00
|
|
|
match libc::stat(buf, &mut st) {
|
2013-05-18 01:27:54 -05:00
|
|
|
0 => Some(st),
|
|
|
|
_ => None,
|
|
|
|
}
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-11-09 19:50:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn exists(&self) -> bool {
|
2012-11-09 19:50:23 -06:00
|
|
|
match self.stat() {
|
|
|
|
None => false,
|
|
|
|
Some(_) => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn get_size(&self) -> Option<i64> {
|
2012-11-09 19:50:23 -06:00
|
|
|
match self.stat() {
|
|
|
|
None => None,
|
|
|
|
Some(ref st) => Some(st.st_size as i64),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn get_mode(&self) -> Option<uint> {
|
2012-11-09 19:50:23 -06:00
|
|
|
match self.stat() {
|
|
|
|
None => None,
|
|
|
|
Some(ref st) => Some(st.st_mode as uint),
|
|
|
|
}
|
|
|
|
}
|
2013-06-25 21:49:01 -05:00
|
|
|
|
|
|
|
/// Execute a function on p as well as all of its ancestors
|
|
|
|
pub fn each_parent(&self, f: &fn(&Path)) {
|
|
|
|
if !self.components.is_empty() {
|
|
|
|
f(self);
|
|
|
|
self.pop().each_parent(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-09 19:50:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "freebsd")]
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[cfg(target_os = "macos")]
|
2013-06-14 20:21:47 -05:00
|
|
|
impl PosixPath {
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn get_atime(&self) -> Option<(i64, int)> {
|
2012-11-09 19:50:23 -06:00
|
|
|
match self.stat() {
|
|
|
|
None => None,
|
|
|
|
Some(ref st) => {
|
|
|
|
Some((st.st_atime as i64,
|
|
|
|
st.st_atime_nsec as int))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn get_mtime(&self) -> Option<(i64, int)> {
|
2012-11-09 19:50:23 -06:00
|
|
|
match self.stat() {
|
|
|
|
None => None,
|
|
|
|
Some(ref st) => {
|
|
|
|
Some((st.st_mtime as i64,
|
|
|
|
st.st_mtime_nsec as int))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn get_ctime(&self) -> Option<(i64, int)> {
|
2012-11-09 19:50:23 -06:00
|
|
|
match self.stat() {
|
|
|
|
None => None,
|
|
|
|
Some(ref st) => {
|
|
|
|
Some((st.st_ctime as i64,
|
|
|
|
st.st_ctime_nsec as int))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 20:21:47 -05:00
|
|
|
#[cfg(unix)]
|
|
|
|
impl PosixPath {
|
|
|
|
pub fn lstat(&self) -> Option<libc::stat> {
|
|
|
|
unsafe {
|
|
|
|
do str::as_c_str(self.to_str()) |buf| {
|
|
|
|
let mut st = stat::arch::default_stat();
|
|
|
|
match libc::lstat(buf, &mut st) {
|
|
|
|
0 => Some(st),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-09 19:50:23 -06:00
|
|
|
#[cfg(target_os = "freebsd")]
|
|
|
|
#[cfg(target_os = "macos")]
|
2013-06-14 20:21:47 -05:00
|
|
|
impl PosixPath {
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn get_birthtime(&self) -> Option<(i64, int)> {
|
2012-11-09 19:50:23 -06:00
|
|
|
match self.stat() {
|
|
|
|
None => None,
|
|
|
|
Some(ref st) => {
|
|
|
|
Some((st.st_birthtime as i64,
|
|
|
|
st.st_birthtime_nsec as int))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "win32")]
|
2013-06-14 20:21:47 -05:00
|
|
|
impl WindowsPath {
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn get_atime(&self) -> Option<(i64, int)> {
|
2012-11-09 19:50:23 -06:00
|
|
|
match self.stat() {
|
|
|
|
None => None,
|
|
|
|
Some(ref st) => {
|
|
|
|
Some((st.st_atime as i64, 0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn get_mtime(&self) -> Option<(i64, int)> {
|
2012-11-09 19:50:23 -06:00
|
|
|
match self.stat() {
|
|
|
|
None => None,
|
|
|
|
Some(ref st) => {
|
|
|
|
Some((st.st_mtime as i64, 0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn get_ctime(&self) -> Option<(i64, int)> {
|
2012-11-09 19:50:23 -06:00
|
|
|
match self.stat() {
|
|
|
|
None => None,
|
|
|
|
Some(ref st) => {
|
|
|
|
Some((st.st_ctime as i64, 0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-28 07:51:31 -05:00
|
|
|
|
|
|
|
/// Execute a function on p as well as all of its ancestors
|
|
|
|
pub fn each_parent(&self, f: &fn(&Path)) {
|
|
|
|
if !self.components.is_empty() {
|
|
|
|
f(self);
|
|
|
|
self.pop().each_parent(f);
|
|
|
|
}
|
|
|
|
}
|
2012-11-09 19:50:23 -06:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToStr for PosixPath {
|
2013-03-21 23:20:48 -05:00
|
|
|
fn to_str(&self) -> ~str {
|
2012-08-29 15:25:31 -05:00
|
|
|
let mut s = ~"";
|
|
|
|
if self.is_absolute {
|
2013-06-11 21:13:42 -05:00
|
|
|
s.push_str("/");
|
2012-06-24 22:18:18 -05:00
|
|
|
}
|
2013-06-10 08:25:25 -05:00
|
|
|
s + self.components.connect("/")
|
2012-06-24 22:18:18 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
// FIXME (#3227): when default methods in traits are working, de-duplicate
|
|
|
|
// PosixPath and WindowsPath, most of their methods are common.
|
2013-02-14 13:47:00 -06:00
|
|
|
impl GenericPath for PosixPath {
|
2013-03-21 23:20:48 -05:00
|
|
|
fn from_str(s: &str) -> PosixPath {
|
2013-06-09 08:10:50 -05:00
|
|
|
let components = s.split_iter('/')
|
|
|
|
.filter_map(|s| if s.is_empty() {None} else {Some(s.to_owned())})
|
|
|
|
.collect();
|
2012-08-29 15:25:31 -05:00
|
|
|
let is_absolute = (s.len() != 0 && s[0] == '/' as u8);
|
2013-05-18 01:52:11 -05:00
|
|
|
PosixPath {
|
|
|
|
is_absolute: is_absolute,
|
|
|
|
components: components,
|
|
|
|
}
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn dirname(&self) -> ~str {
|
2013-04-09 00:31:23 -05:00
|
|
|
let s = self.dir_path().to_str();
|
2013-05-18 01:27:54 -05:00
|
|
|
match s.len() {
|
|
|
|
0 => ~".",
|
|
|
|
_ => s,
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn filename(&self) -> Option<~str> {
|
2012-08-29 15:25:31 -05:00
|
|
|
match self.components.len() {
|
2013-05-18 01:52:11 -05:00
|
|
|
0 => None,
|
2013-07-02 14:47:32 -05:00
|
|
|
n => Some(self.components[n - 1].clone()),
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn filestem(&self) -> Option<~str> {
|
2012-08-29 15:25:31 -05:00
|
|
|
match self.filename() {
|
2013-05-18 01:52:11 -05:00
|
|
|
None => None,
|
|
|
|
Some(ref f) => {
|
2013-06-09 22:09:51 -05:00
|
|
|
match f.rfind('.') {
|
|
|
|
Some(p) => Some(f.slice_to(p).to_owned()),
|
2013-07-02 14:47:32 -05:00
|
|
|
None => Some((*f).clone()),
|
2013-05-18 01:52:11 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn filetype(&self) -> Option<~str> {
|
2012-08-29 15:25:31 -05:00
|
|
|
match self.filename() {
|
2013-05-18 01:52:11 -05:00
|
|
|
None => None,
|
|
|
|
Some(ref f) => {
|
2013-06-09 22:09:51 -05:00
|
|
|
match f.rfind('.') {
|
|
|
|
Some(p) if p < f.len() => Some(f.slice_from(p).to_owned()),
|
2013-05-18 01:52:11 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn with_dirname(&self, d: &str) -> PosixPath {
|
2012-11-17 22:23:44 -06:00
|
|
|
let dpath = PosixPath(d);
|
2012-08-29 15:25:31 -05:00
|
|
|
match self.filename() {
|
2013-05-18 01:52:11 -05:00
|
|
|
Some(ref f) => dpath.push(*f),
|
|
|
|
None => dpath,
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn with_filename(&self, f: &str) -> PosixPath {
|
2013-06-09 09:34:23 -05:00
|
|
|
assert!(! f.iter().all(windows::is_sep));
|
2013-04-09 00:31:23 -05:00
|
|
|
self.dir_path().push(f)
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn with_filestem(&self, s: &str) -> PosixPath {
|
2012-08-29 15:25:31 -05:00
|
|
|
match self.filetype() {
|
2013-05-18 01:52:11 -05:00
|
|
|
None => self.with_filename(s),
|
2013-06-13 22:43:05 -05:00
|
|
|
Some(ref t) => self.with_filename(s.to_owned() + *t),
|
2012-05-08 19:33:48 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn with_filetype(&self, t: &str) -> PosixPath {
|
2013-05-18 01:27:54 -05:00
|
|
|
match (t.len(), self.filestem()) {
|
2013-07-02 14:47:32 -05:00
|
|
|
(0, None) => (*self).clone(),
|
2013-05-18 01:27:54 -05:00
|
|
|
(0, Some(ref s)) => self.with_filename(*s),
|
|
|
|
(_, None) => self.with_filename(fmt!(".%s", t)),
|
|
|
|
(_, Some(ref s)) => self.with_filename(fmt!("%s.%s", *s, t)),
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn dir_path(&self) -> PosixPath {
|
2013-05-18 01:27:54 -05:00
|
|
|
match self.components.len() {
|
2013-07-02 14:47:32 -05:00
|
|
|
0 => (*self).clone(),
|
2013-05-18 01:27:54 -05:00
|
|
|
_ => self.pop(),
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn file_path(&self) -> PosixPath {
|
2012-08-29 15:25:31 -05:00
|
|
|
let cs = match self.filename() {
|
|
|
|
None => ~[],
|
2013-07-02 14:47:32 -05:00
|
|
|
Some(ref f) => ~[(*f).clone()]
|
2012-08-29 15:25:31 -05:00
|
|
|
};
|
2013-05-18 01:52:11 -05:00
|
|
|
PosixPath {
|
|
|
|
is_absolute: false,
|
|
|
|
components: cs,
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn push_rel(&self, other: &PosixPath) -> PosixPath {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(!other.is_absolute);
|
2012-08-29 15:25:31 -05:00
|
|
|
self.push_many(other.components)
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn unsafe_join(&self, other: &PosixPath) -> PosixPath {
|
2013-02-18 16:48:18 -06:00
|
|
|
if other.is_absolute {
|
2013-05-18 01:52:11 -05:00
|
|
|
PosixPath {
|
|
|
|
is_absolute: true,
|
2013-07-02 14:47:32 -05:00
|
|
|
components: other.components.clone(),
|
2013-05-18 01:52:11 -05:00
|
|
|
}
|
2013-02-18 16:48:18 -06:00
|
|
|
} else {
|
|
|
|
self.push_rel(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn is_restricted(&self) -> bool {
|
2013-02-18 19:54:05 -06:00
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2013-06-12 11:56:09 -05:00
|
|
|
fn push_many<S: Str>(&self, cs: &[S]) -> PosixPath {
|
2013-07-02 14:47:32 -05:00
|
|
|
let mut v = self.components.clone();
|
2013-06-21 07:29:53 -05:00
|
|
|
for cs.iter().advance |e| {
|
2013-06-12 11:56:09 -05:00
|
|
|
for e.as_slice().split_iter(windows::is_sep).advance |s| {
|
2013-06-09 08:10:50 -05:00
|
|
|
if !s.is_empty() {
|
|
|
|
v.push(s.to_owned())
|
|
|
|
}
|
2013-03-24 01:51:18 -05:00
|
|
|
}
|
2012-09-13 21:34:18 -05:00
|
|
|
}
|
2013-05-18 01:52:11 -05:00
|
|
|
PosixPath {
|
|
|
|
is_absolute: self.is_absolute,
|
|
|
|
components: v,
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn push(&self, s: &str) -> PosixPath {
|
2013-07-02 14:47:32 -05:00
|
|
|
let mut v = self.components.clone();
|
2013-06-09 08:10:50 -05:00
|
|
|
for s.split_iter(windows::is_sep).advance |s| {
|
|
|
|
if !s.is_empty() {
|
|
|
|
v.push(s.to_owned())
|
|
|
|
}
|
2013-03-24 01:51:18 -05:00
|
|
|
}
|
2013-07-02 14:47:32 -05:00
|
|
|
PosixPath {
|
|
|
|
components: v,
|
|
|
|
..(*self).clone()
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn pop(&self) -> PosixPath {
|
2013-07-02 14:47:32 -05:00
|
|
|
let mut cs = self.components.clone();
|
2012-08-29 15:25:31 -05:00
|
|
|
if cs.len() != 0 {
|
2013-04-09 00:31:23 -05:00
|
|
|
cs.pop();
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2013-05-18 01:52:11 -05:00
|
|
|
PosixPath {
|
2012-09-19 00:35:28 -05:00
|
|
|
is_absolute: self.is_absolute,
|
2013-05-18 01:52:11 -05:00
|
|
|
components: cs,
|
|
|
|
} //..self }
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
2012-08-29 15:59:02 -05:00
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn normalize(&self) -> PosixPath {
|
2013-05-18 01:52:11 -05:00
|
|
|
PosixPath {
|
2012-09-19 00:35:28 -05:00
|
|
|
is_absolute: self.is_absolute,
|
2013-05-18 01:52:11 -05:00
|
|
|
components: normalize(self.components),
|
|
|
|
} // ..self }
|
2012-08-29 15:59:02 -05:00
|
|
|
}
|
2013-04-20 00:22:46 -05:00
|
|
|
|
|
|
|
fn is_absolute(&self) -> bool {
|
|
|
|
self.is_absolute
|
|
|
|
}
|
2013-06-26 19:42:24 -05:00
|
|
|
|
|
|
|
fn is_ancestor_of(&self, other: &PosixPath) -> bool {
|
|
|
|
debug!("%s / %s %? %?", self.to_str(), other.to_str(), self.is_absolute,
|
|
|
|
self.components.len());
|
|
|
|
self == other ||
|
|
|
|
(!other.components.is_empty() && !(self.components.is_empty() && !self.is_absolute) &&
|
|
|
|
self.is_ancestor_of(&other.pop()))
|
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToStr for WindowsPath {
|
2013-03-21 23:20:48 -05:00
|
|
|
fn to_str(&self) -> ~str {
|
2012-08-29 15:25:31 -05:00
|
|
|
let mut s = ~"";
|
|
|
|
match self.host {
|
2013-06-11 21:13:42 -05:00
|
|
|
Some(ref h) => {
|
|
|
|
s.push_str("\\\\");
|
|
|
|
s.push_str(*h);
|
|
|
|
}
|
2012-08-29 15:25:31 -05:00
|
|
|
None => { }
|
|
|
|
}
|
|
|
|
match self.device {
|
2013-06-11 21:13:42 -05:00
|
|
|
Some(ref d) => {
|
|
|
|
s.push_str(*d);
|
|
|
|
s.push_str(":");
|
|
|
|
}
|
2012-08-29 15:25:31 -05:00
|
|
|
None => { }
|
|
|
|
}
|
|
|
|
if self.is_absolute {
|
2013-06-11 21:13:42 -05:00
|
|
|
s.push_str("\\");
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2013-06-10 08:25:25 -05:00
|
|
|
s + self.components.connect("\\")
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl GenericPath for WindowsPath {
|
2013-03-21 23:20:48 -05:00
|
|
|
fn from_str(s: &str) -> WindowsPath {
|
2012-08-29 15:25:31 -05:00
|
|
|
let host;
|
|
|
|
let device;
|
|
|
|
let rest;
|
|
|
|
|
2013-05-18 01:27:54 -05:00
|
|
|
match (
|
|
|
|
windows::extract_drive_prefix(s),
|
|
|
|
windows::extract_unc_prefix(s),
|
|
|
|
) {
|
|
|
|
(Some((ref d, ref r)), _) => {
|
|
|
|
host = None;
|
2013-07-02 14:47:32 -05:00
|
|
|
device = Some((*d).clone());
|
|
|
|
rest = (*r).clone();
|
2013-05-18 01:27:54 -05:00
|
|
|
}
|
|
|
|
(None, Some((ref h, ref r))) => {
|
2013-07-02 14:47:32 -05:00
|
|
|
host = Some((*h).clone());
|
2012-08-29 15:25:31 -05:00
|
|
|
device = None;
|
2013-07-02 14:47:32 -05:00
|
|
|
rest = (*r).clone();
|
2013-05-18 01:27:54 -05:00
|
|
|
}
|
|
|
|
(None, None) => {
|
2012-08-29 15:25:31 -05:00
|
|
|
host = None;
|
|
|
|
device = None;
|
2013-06-13 22:43:05 -05:00
|
|
|
rest = s.to_owned();
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-09 08:10:50 -05:00
|
|
|
let components = rest.split_iter(windows::is_sep)
|
|
|
|
.filter_map(|s| if s.is_empty() {None} else {Some(s.to_owned())})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let is_absolute = (rest.len() != 0 && windows::is_sep(rest[0] as char));
|
2013-05-18 01:52:11 -05:00
|
|
|
WindowsPath {
|
|
|
|
host: host,
|
|
|
|
device: device,
|
|
|
|
is_absolute: is_absolute,
|
|
|
|
components: components,
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn dirname(&self) -> ~str {
|
2013-04-09 00:31:23 -05:00
|
|
|
let s = self.dir_path().to_str();
|
2013-05-18 01:27:54 -05:00
|
|
|
match s.len() {
|
|
|
|
0 => ~".",
|
|
|
|
_ => s,
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn filename(&self) -> Option<~str> {
|
2012-08-29 15:25:31 -05:00
|
|
|
match self.components.len() {
|
2013-05-18 01:52:11 -05:00
|
|
|
0 => None,
|
2013-07-02 14:47:32 -05:00
|
|
|
n => Some(self.components[n - 1].clone()),
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn filestem(&self) -> Option<~str> {
|
2012-08-29 15:25:31 -05:00
|
|
|
match self.filename() {
|
2013-05-18 01:52:11 -05:00
|
|
|
None => None,
|
|
|
|
Some(ref f) => {
|
2013-06-09 22:09:51 -05:00
|
|
|
match f.rfind('.') {
|
|
|
|
Some(p) => Some(f.slice_to(p).to_owned()),
|
2013-07-02 14:47:32 -05:00
|
|
|
None => Some((*f).clone()),
|
2013-05-18 01:52:11 -05:00
|
|
|
}
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn filetype(&self) -> Option<~str> {
|
2012-08-29 15:25:31 -05:00
|
|
|
match self.filename() {
|
|
|
|
None => None,
|
|
|
|
Some(ref f) => {
|
2013-06-09 22:09:51 -05:00
|
|
|
match f.rfind('.') {
|
|
|
|
Some(p) if p < f.len() => Some(f.slice_from(p).to_owned()),
|
2013-05-18 01:52:11 -05:00
|
|
|
_ => None,
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn with_dirname(&self, d: &str) -> WindowsPath {
|
2012-11-17 22:23:44 -06:00
|
|
|
let dpath = WindowsPath(d);
|
2012-08-29 15:25:31 -05:00
|
|
|
match self.filename() {
|
2013-05-18 01:52:11 -05:00
|
|
|
Some(ref f) => dpath.push(*f),
|
|
|
|
None => dpath,
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn with_filename(&self, f: &str) -> WindowsPath {
|
2013-06-09 09:34:23 -05:00
|
|
|
assert!(! f.iter().all(windows::is_sep));
|
2012-08-29 15:25:31 -05:00
|
|
|
self.dir_path().push(f)
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn with_filestem(&self, s: &str) -> WindowsPath {
|
2012-08-29 15:25:31 -05:00
|
|
|
match self.filetype() {
|
2013-05-18 01:52:11 -05:00
|
|
|
None => self.with_filename(s),
|
2013-06-13 22:43:05 -05:00
|
|
|
Some(ref t) => self.with_filename(s.to_owned() + *t),
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn with_filetype(&self, t: &str) -> WindowsPath {
|
2013-05-18 01:27:54 -05:00
|
|
|
match (t.len(), self.filestem()) {
|
2013-07-02 14:47:32 -05:00
|
|
|
(0, None) => (*self).clone(),
|
2013-05-18 01:27:54 -05:00
|
|
|
(0, Some(ref s)) => self.with_filename(*s),
|
|
|
|
(_, None) => self.with_filename(fmt!(".%s", t)),
|
|
|
|
(_, Some(ref s)) => self.with_filename(fmt!("%s.%s", *s, t)),
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn dir_path(&self) -> WindowsPath {
|
2013-05-18 01:27:54 -05:00
|
|
|
match self.components.len() {
|
2013-07-02 14:47:32 -05:00
|
|
|
0 => (*self).clone(),
|
2013-05-18 01:27:54 -05:00
|
|
|
_ => self.pop(),
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn file_path(&self) -> WindowsPath {
|
2013-05-18 01:52:11 -05:00
|
|
|
WindowsPath {
|
|
|
|
host: None,
|
|
|
|
device: None,
|
|
|
|
is_absolute: false,
|
|
|
|
components: match self.filename() {
|
|
|
|
None => ~[],
|
2013-07-02 14:47:32 -05:00
|
|
|
Some(ref f) => ~[(*f).clone()],
|
2013-05-18 01:52:11 -05:00
|
|
|
}
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn push_rel(&self, other: &WindowsPath) -> WindowsPath {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(!other.is_absolute);
|
2012-08-29 15:25:31 -05:00
|
|
|
self.push_many(other.components)
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn unsafe_join(&self, other: &WindowsPath) -> WindowsPath {
|
2013-02-18 19:34:48 -06:00
|
|
|
/* rhs not absolute is simple push */
|
2013-02-18 16:48:18 -06:00
|
|
|
if !other.is_absolute {
|
2013-02-18 19:34:48 -06:00
|
|
|
return self.push_many(other.components);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if rhs has a host set, then the whole thing wins */
|
|
|
|
match other.host {
|
2013-05-29 18:59:33 -05:00
|
|
|
Some(ref host) => {
|
2013-02-18 19:34:48 -06:00
|
|
|
return WindowsPath {
|
2013-07-02 14:47:32 -05:00
|
|
|
host: Some((*host).clone()),
|
|
|
|
device: other.device.clone(),
|
2013-02-18 19:34:48 -06:00
|
|
|
is_absolute: true,
|
2013-07-02 14:47:32 -05:00
|
|
|
components: other.components.clone(),
|
2013-02-18 19:34:48 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if rhs has a device set, then a part wins */
|
|
|
|
match other.device {
|
2013-05-29 18:59:33 -05:00
|
|
|
Some(ref device) => {
|
2013-02-18 19:34:48 -06:00
|
|
|
return WindowsPath {
|
|
|
|
host: None,
|
2013-07-02 14:47:32 -05:00
|
|
|
device: Some((*device).clone()),
|
2013-02-18 19:34:48 -06:00
|
|
|
is_absolute: true,
|
2013-07-02 14:47:32 -05:00
|
|
|
components: other.components.clone(),
|
2013-02-18 19:34:48 -06:00
|
|
|
};
|
2013-02-18 16:48:18 -06:00
|
|
|
}
|
2013-02-18 19:34:48 -06:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fallback: host and device of lhs win, but the
|
|
|
|
whole path of the right */
|
|
|
|
WindowsPath {
|
2013-07-02 14:47:32 -05:00
|
|
|
host: self.host.clone(),
|
|
|
|
device: self.device.clone(),
|
2013-02-18 19:34:48 -06:00
|
|
|
is_absolute: self.is_absolute || other.is_absolute,
|
2013-07-02 14:47:32 -05:00
|
|
|
components: other.components.clone(),
|
2013-02-18 16:48:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn is_restricted(&self) -> bool {
|
2013-02-18 19:54:05 -06:00
|
|
|
match self.filestem() {
|
|
|
|
Some(stem) => {
|
2013-04-23 11:20:55 -05:00
|
|
|
// FIXME: #4318 Instead of to_ascii and to_str_ascii, could use
|
|
|
|
// to_ascii_consume and to_str_consume to not do a unnecessary copy.
|
2013-04-23 04:08:13 -05:00
|
|
|
match stem.to_ascii().to_lower().to_str_ascii() {
|
2013-02-18 19:54:05 -06:00
|
|
|
~"con" | ~"aux" | ~"com1" | ~"com2" | ~"com3" | ~"com4" |
|
|
|
|
~"lpt1" | ~"lpt2" | ~"lpt3" | ~"prn" | ~"nul" => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-12 11:56:09 -05:00
|
|
|
fn push_many<S: Str>(&self, cs: &[S]) -> WindowsPath {
|
2013-07-02 14:47:32 -05:00
|
|
|
let mut v = self.components.clone();
|
2013-06-21 07:29:53 -05:00
|
|
|
for cs.iter().advance |e| {
|
2013-06-12 11:56:09 -05:00
|
|
|
for e.as_slice().split_iter(windows::is_sep).advance |s| {
|
2013-06-09 08:10:50 -05:00
|
|
|
if !s.is_empty() {
|
|
|
|
v.push(s.to_owned())
|
|
|
|
}
|
2013-03-24 01:51:18 -05:00
|
|
|
}
|
2012-09-13 21:34:18 -05:00
|
|
|
}
|
2012-09-19 00:35:28 -05:00
|
|
|
// tedious, but as-is, we can't use ..self
|
2013-05-18 01:52:11 -05:00
|
|
|
WindowsPath {
|
2013-07-02 14:47:32 -05:00
|
|
|
host: self.host.clone(),
|
|
|
|
device: self.device.clone(),
|
2012-09-19 00:35:28 -05:00
|
|
|
is_absolute: self.is_absolute,
|
2013-02-15 02:51:28 -06:00
|
|
|
components: v
|
2012-09-19 00:35:28 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn push(&self, s: &str) -> WindowsPath {
|
2013-07-02 14:47:32 -05:00
|
|
|
let mut v = self.components.clone();
|
2013-06-09 08:10:50 -05:00
|
|
|
for s.split_iter(windows::is_sep).advance |s| {
|
|
|
|
if !s.is_empty() {
|
|
|
|
v.push(s.to_owned())
|
|
|
|
}
|
2013-03-24 01:51:18 -05:00
|
|
|
}
|
2013-07-02 14:47:32 -05:00
|
|
|
WindowsPath { components: v, ..(*self).clone() }
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn pop(&self) -> WindowsPath {
|
2013-07-02 14:47:32 -05:00
|
|
|
let mut cs = self.components.clone();
|
2012-08-29 15:25:31 -05:00
|
|
|
if cs.len() != 0 {
|
2013-04-09 00:31:23 -05:00
|
|
|
cs.pop();
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2013-05-18 01:52:11 -05:00
|
|
|
WindowsPath {
|
2013-07-02 14:47:32 -05:00
|
|
|
host: self.host.clone(),
|
|
|
|
device: self.device.clone(),
|
2012-09-19 00:35:28 -05:00
|
|
|
is_absolute: self.is_absolute,
|
2013-05-18 01:52:11 -05:00
|
|
|
components: cs,
|
2012-09-19 00:35:28 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
2012-08-29 15:59:02 -05:00
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn normalize(&self) -> WindowsPath {
|
2013-05-18 01:52:11 -05:00
|
|
|
WindowsPath {
|
2013-07-02 14:47:32 -05:00
|
|
|
host: self.host.clone(),
|
2013-02-18 19:34:48 -06:00
|
|
|
device: match self.device {
|
|
|
|
None => None,
|
2013-04-23 04:08:13 -05:00
|
|
|
|
2013-04-23 11:20:55 -05:00
|
|
|
// FIXME: #4318 Instead of to_ascii and to_str_ascii, could use
|
|
|
|
// to_ascii_consume and to_str_consume to not do a unnecessary copy.
|
2013-04-23 04:08:13 -05:00
|
|
|
Some(ref device) => Some(device.to_ascii().to_upper().to_str_ascii())
|
2013-02-18 19:34:48 -06:00
|
|
|
},
|
2012-09-19 00:35:28 -05:00
|
|
|
is_absolute: self.is_absolute,
|
|
|
|
components: normalize(self.components)
|
2012-08-29 15:59:02 -05:00
|
|
|
}
|
|
|
|
}
|
2013-04-20 00:22:46 -05:00
|
|
|
|
|
|
|
fn is_absolute(&self) -> bool {
|
|
|
|
self.is_absolute
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-06-26 19:42:24 -05:00
|
|
|
fn is_ancestor_of(&self, other: &WindowsPath) -> bool {
|
|
|
|
self == other ||
|
|
|
|
(!other.components.is_empty() && !(self.components.is_empty() && !self.is_absolute) &&
|
|
|
|
self.is_ancestor_of(&other.pop()))
|
|
|
|
}
|
|
|
|
}
|
2012-08-29 15:25:31 -05:00
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn normalize(components: &[~str]) -> ~[~str] {
|
2012-08-29 15:25:31 -05:00
|
|
|
let mut cs = ~[];
|
2013-06-21 07:29:53 -05:00
|
|
|
for components.iter().advance |c| {
|
2013-04-09 00:31:23 -05:00
|
|
|
if *c == ~"." && components.len() > 1 { loop; }
|
|
|
|
if *c == ~"" { loop; }
|
|
|
|
if *c == ~".." && cs.len() != 0 {
|
|
|
|
cs.pop();
|
|
|
|
loop;
|
|
|
|
}
|
2013-07-02 14:47:32 -05:00
|
|
|
cs.push((*c).clone());
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
2013-02-15 02:51:28 -06:00
|
|
|
cs
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
|
|
|
|
2012-11-17 22:09:51 -06:00
|
|
|
// Various windows helpers, and tests for the impl.
|
2012-12-27 19:53:04 -06:00
|
|
|
pub mod windows {
|
2012-12-23 16:41:37 -06:00
|
|
|
use libc;
|
2013-01-08 21:37:25 -06:00
|
|
|
use option::{None, Option, Some};
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-06-09 08:10:50 -05:00
|
|
|
pub fn is_sep(u: char) -> bool {
|
|
|
|
u == '/' || u == '\\'
|
2012-11-17 22:09:51 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn extract_unc_prefix(s: &str) -> Option<(~str,~str)> {
|
2012-11-17 22:09:51 -06:00
|
|
|
if (s.len() > 1 &&
|
2013-02-18 19:34:48 -06:00
|
|
|
(s[0] == '\\' as u8 || s[0] == '/' as u8) &&
|
|
|
|
s[0] == s[1]) {
|
2012-11-17 22:09:51 -06:00
|
|
|
let mut i = 2;
|
|
|
|
while i < s.len() {
|
2013-06-09 08:10:50 -05:00
|
|
|
if is_sep(s[i] as char) {
|
2013-03-21 06:36:21 -05:00
|
|
|
let pre = s.slice(2, i).to_owned();
|
2013-04-12 00:10:01 -05:00
|
|
|
let rest = s.slice(i, s.len()).to_owned();
|
2013-02-15 02:51:28 -06:00
|
|
|
return Some((pre, rest));
|
2012-11-17 22:09:51 -06:00
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2012-08-29 15:59:02 -05:00
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn extract_drive_prefix(s: &str) -> Option<(~str,~str)> {
|
2012-11-17 22:09:51 -06:00
|
|
|
unsafe {
|
|
|
|
if (s.len() > 1 &&
|
|
|
|
libc::isalpha(s[0] as libc::c_int) != 0 &&
|
|
|
|
s[1] == ':' as u8) {
|
|
|
|
let rest = if s.len() == 2 {
|
|
|
|
~""
|
|
|
|
} else {
|
2013-03-21 06:36:21 -05:00
|
|
|
s.slice(2, s.len()).to_owned()
|
2012-11-17 22:09:51 -06:00
|
|
|
};
|
2013-03-21 06:36:21 -05:00
|
|
|
return Some((s.slice(0,1).to_owned(), rest));
|
2012-11-17 22:09:51 -06:00
|
|
|
}
|
|
|
|
None
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
2012-08-29 15:59:02 -05:00
|
|
|
}
|
2012-11-17 22:09:51 -06:00
|
|
|
}
|
|
|
|
|
2012-11-09 19:50:23 -06:00
|
|
|
#[cfg(test)]
|
2012-11-17 22:09:51 -06:00
|
|
|
mod tests {
|
2013-01-08 21:37:25 -06:00
|
|
|
use option::{None, Some};
|
|
|
|
use path::{PosixPath, WindowsPath, windows};
|
2012-12-27 19:53:04 -06:00
|
|
|
|
2012-11-17 22:09:51 -06:00
|
|
|
#[test]
|
|
|
|
fn test_double_slash_collapsing() {
|
|
|
|
let path = PosixPath("tmp/");
|
|
|
|
let path = path.push("/hmm");
|
|
|
|
let path = path.normalize();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(~"tmp/hmm", path.to_str());
|
2012-11-17 22:09:51 -06:00
|
|
|
|
|
|
|
let path = WindowsPath("tmp/");
|
|
|
|
let path = path.push("/hmm");
|
|
|
|
let path = path.normalize();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(~"tmp\\hmm", path.to_str());
|
2012-11-17 22:09:51 -06:00
|
|
|
}
|
2012-08-29 15:25:31 -05:00
|
|
|
|
2012-09-18 13:19:52 -05:00
|
|
|
#[test]
|
|
|
|
fn test_filetype_foo_bar() {
|
2012-11-17 22:09:51 -06:00
|
|
|
let wp = PosixPath("foo.bar");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(wp.filetype(), Some(~".bar"));
|
2012-11-17 22:09:51 -06:00
|
|
|
|
|
|
|
let wp = WindowsPath("foo.bar");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(wp.filetype(), Some(~".bar"));
|
2012-09-18 13:19:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_filetype_foo() {
|
2012-11-17 22:09:51 -06:00
|
|
|
let wp = PosixPath("foo");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(wp.filetype(), None);
|
2012-11-17 22:09:51 -06:00
|
|
|
|
|
|
|
let wp = WindowsPath("foo");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(wp.filetype(), None);
|
2012-09-18 13:19:52 -05:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:59:02 -05:00
|
|
|
#[test]
|
|
|
|
fn test_posix_paths() {
|
2012-11-17 22:09:51 -06:00
|
|
|
fn t(wp: &PosixPath, s: &str) {
|
|
|
|
let ss = wp.to_str();
|
2013-06-13 22:43:05 -05:00
|
|
|
let sss = s.to_owned();
|
2012-11-17 22:09:51 -06:00
|
|
|
if (ss != sss) {
|
|
|
|
debug!("got %s", ss);
|
|
|
|
debug!("expected %s", sss);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(ss, sss);
|
2012-11-17 22:09:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t(&(PosixPath("hi")), "hi");
|
|
|
|
t(&(PosixPath("/lib")), "/lib");
|
|
|
|
t(&(PosixPath("hi/there")), "hi/there");
|
|
|
|
t(&(PosixPath("hi/there.txt")), "hi/there.txt");
|
2012-08-29 15:25:31 -05:00
|
|
|
|
2012-11-17 22:09:51 -06:00
|
|
|
t(&(PosixPath("hi/there.txt")), "hi/there.txt");
|
|
|
|
t(&(PosixPath("hi/there.txt")
|
2012-08-29 15:25:31 -05:00
|
|
|
.with_filetype("")), "hi/there");
|
|
|
|
|
2012-11-17 22:09:51 -06:00
|
|
|
t(&(PosixPath("/a/b/c/there.txt")
|
2012-08-29 15:25:31 -05:00
|
|
|
.with_dirname("hi")), "hi/there.txt");
|
|
|
|
|
2012-11-17 22:09:51 -06:00
|
|
|
t(&(PosixPath("hi/there.txt")
|
2012-08-29 15:59:02 -05:00
|
|
|
.with_dirname(".")), "./there.txt");
|
2012-08-29 15:25:31 -05:00
|
|
|
|
2012-11-17 22:09:51 -06:00
|
|
|
t(&(PosixPath("a/b/c")
|
2012-08-29 15:59:02 -05:00
|
|
|
.push("..")), "a/b/c/..");
|
2012-08-29 15:25:31 -05:00
|
|
|
|
2012-11-17 22:09:51 -06:00
|
|
|
t(&(PosixPath("there.txt")
|
2012-08-29 15:25:31 -05:00
|
|
|
.with_filetype("o")), "there.o");
|
|
|
|
|
2012-11-17 22:09:51 -06:00
|
|
|
t(&(PosixPath("hi/there.txt")
|
2012-08-29 15:25:31 -05:00
|
|
|
.with_filetype("o")), "hi/there.o");
|
|
|
|
|
2012-11-17 22:09:51 -06:00
|
|
|
t(&(PosixPath("hi/there.txt")
|
2012-08-29 15:25:31 -05:00
|
|
|
.with_filetype("o")
|
|
|
|
.with_dirname("/usr/lib")),
|
|
|
|
"/usr/lib/there.o");
|
|
|
|
|
2012-11-17 22:09:51 -06:00
|
|
|
t(&(PosixPath("hi/there.txt")
|
2012-08-29 15:25:31 -05:00
|
|
|
.with_filetype("o")
|
|
|
|
.with_dirname("/usr/lib/")),
|
|
|
|
"/usr/lib/there.o");
|
|
|
|
|
2012-11-17 22:09:51 -06:00
|
|
|
t(&(PosixPath("hi/there.txt")
|
2012-08-29 15:25:31 -05:00
|
|
|
.with_filetype("o")
|
|
|
|
.with_dirname("/usr//lib//")),
|
|
|
|
"/usr/lib/there.o");
|
|
|
|
|
2012-11-17 22:09:51 -06:00
|
|
|
t(&(PosixPath("/usr/bin/rust")
|
2012-08-29 15:25:31 -05:00
|
|
|
.push_many([~"lib", ~"thingy.so"])
|
|
|
|
.with_filestem("librustc")),
|
|
|
|
"/usr/bin/rust/lib/librustc.so");
|
|
|
|
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-29 15:59:02 -05:00
|
|
|
#[test]
|
|
|
|
fn test_normalize() {
|
2012-11-17 22:09:51 -06:00
|
|
|
fn t(wp: &PosixPath, s: &str) {
|
|
|
|
let ss = wp.to_str();
|
2013-06-13 22:43:05 -05:00
|
|
|
let sss = s.to_owned();
|
2012-11-17 22:09:51 -06:00
|
|
|
if (ss != sss) {
|
|
|
|
debug!("got %s", ss);
|
|
|
|
debug!("expected %s", sss);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(ss, sss);
|
2012-11-17 22:09:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t(&(PosixPath("hi/there.txt")
|
2012-08-29 15:59:02 -05:00
|
|
|
.with_dirname(".").normalize()), "there.txt");
|
|
|
|
|
2012-11-17 22:09:51 -06:00
|
|
|
t(&(PosixPath("a/b/../c/././/../foo.txt/").normalize()),
|
2012-08-29 15:59:02 -05:00
|
|
|
"a/foo.txt");
|
|
|
|
|
2012-11-17 22:09:51 -06:00
|
|
|
t(&(PosixPath("a/b/c")
|
2012-08-29 15:59:02 -05:00
|
|
|
.push("..").normalize()), "a/b");
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
|
|
|
#[test]
|
2012-08-29 15:25:31 -05:00
|
|
|
fn test_extract_unc_prefixes() {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(windows::extract_unc_prefix("\\\\").is_none());
|
|
|
|
assert!(windows::extract_unc_prefix("//").is_none());
|
|
|
|
assert!(windows::extract_unc_prefix("\\\\hi").is_none());
|
|
|
|
assert!(windows::extract_unc_prefix("//hi").is_none());
|
|
|
|
assert!(windows::extract_unc_prefix("\\\\hi\\") ==
|
2013-03-06 15:58:02 -06:00
|
|
|
Some((~"hi", ~"\\")));
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(windows::extract_unc_prefix("//hi\\") ==
|
2013-03-06 15:58:02 -06:00
|
|
|
Some((~"hi", ~"\\")));
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(windows::extract_unc_prefix("\\\\hi\\there") ==
|
2013-03-06 15:58:02 -06:00
|
|
|
Some((~"hi", ~"\\there")));
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(windows::extract_unc_prefix("//hi/there") ==
|
2013-03-06 15:58:02 -06:00
|
|
|
Some((~"hi", ~"/there")));
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(windows::extract_unc_prefix(
|
2013-03-06 15:58:02 -06:00
|
|
|
"\\\\hi\\there\\friends.txt") ==
|
|
|
|
Some((~"hi", ~"\\there\\friends.txt")));
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(windows::extract_unc_prefix(
|
2013-03-06 15:58:02 -06:00
|
|
|
"//hi\\there\\friends.txt") ==
|
|
|
|
Some((~"hi", ~"\\there\\friends.txt")));
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-08-29 15:25:31 -05:00
|
|
|
fn test_extract_drive_prefixes() {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(windows::extract_drive_prefix("c").is_none());
|
|
|
|
assert!(windows::extract_drive_prefix("c:") ==
|
2013-03-06 15:58:02 -06:00
|
|
|
Some((~"c", ~"")));
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(windows::extract_drive_prefix("d:") ==
|
2013-03-06 15:58:02 -06:00
|
|
|
Some((~"d", ~"")));
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(windows::extract_drive_prefix("z:") ==
|
2013-03-06 15:58:02 -06:00
|
|
|
Some((~"z", ~"")));
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(windows::extract_drive_prefix("c:\\hi") ==
|
2013-03-06 15:58:02 -06:00
|
|
|
Some((~"c", ~"\\hi")));
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(windows::extract_drive_prefix("d:hi") ==
|
2013-03-06 15:58:02 -06:00
|
|
|
Some((~"d", ~"hi")));
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(windows::extract_drive_prefix("c:hi\\there.txt") ==
|
2013-03-06 15:58:02 -06:00
|
|
|
Some((~"c", ~"hi\\there.txt")));
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(windows::extract_drive_prefix("c:\\hi\\there.txt") ==
|
2013-03-06 15:58:02 -06:00
|
|
|
Some((~"c", ~"\\hi\\there.txt")));
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-08-29 15:25:31 -05:00
|
|
|
fn test_windows_paths() {
|
|
|
|
fn t(wp: &WindowsPath, s: &str) {
|
|
|
|
let ss = wp.to_str();
|
2013-06-13 22:43:05 -05:00
|
|
|
let sss = s.to_owned();
|
2012-08-29 15:25:31 -05:00
|
|
|
if (ss != sss) {
|
|
|
|
debug!("got %s", ss);
|
|
|
|
debug!("expected %s", sss);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(ss, sss);
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-17 22:09:51 -06:00
|
|
|
t(&(WindowsPath("hi")), "hi");
|
|
|
|
t(&(WindowsPath("hi/there")), "hi\\there");
|
|
|
|
t(&(WindowsPath("hi/there.txt")), "hi\\there.txt");
|
2012-08-29 15:25:31 -05:00
|
|
|
|
2012-11-17 22:09:51 -06:00
|
|
|
t(&(WindowsPath("there.txt")
|
2012-08-29 15:25:31 -05:00
|
|
|
.with_filetype("o")), "there.o");
|
|
|
|
|
2012-11-17 22:09:51 -06:00
|
|
|
t(&(WindowsPath("hi/there.txt")
|
2012-08-29 15:25:31 -05:00
|
|
|
.with_filetype("o")), "hi\\there.o");
|
|
|
|
|
2012-11-17 22:09:51 -06:00
|
|
|
t(&(WindowsPath("hi/there.txt")
|
2012-08-29 15:25:31 -05:00
|
|
|
.with_filetype("o")
|
|
|
|
.with_dirname("c:\\program files A")),
|
|
|
|
"c:\\program files A\\there.o");
|
|
|
|
|
2012-11-17 22:09:51 -06:00
|
|
|
t(&(WindowsPath("hi/there.txt")
|
2012-08-29 15:25:31 -05:00
|
|
|
.with_filetype("o")
|
|
|
|
.with_dirname("c:\\program files B\\")),
|
|
|
|
"c:\\program files B\\there.o");
|
|
|
|
|
2012-11-17 22:09:51 -06:00
|
|
|
t(&(WindowsPath("hi/there.txt")
|
2012-08-29 15:25:31 -05:00
|
|
|
.with_filetype("o")
|
|
|
|
.with_dirname("c:\\program files C\\/")),
|
|
|
|
"c:\\program files C\\there.o");
|
|
|
|
|
2012-11-17 22:09:51 -06:00
|
|
|
t(&(WindowsPath("c:\\program files (x86)\\rust")
|
2012-08-29 15:25:31 -05:00
|
|
|
.push_many([~"lib", ~"thingy.dll"])
|
|
|
|
.with_filename("librustc.dll")),
|
|
|
|
"c:\\program files (x86)\\rust\\lib\\librustc.dll");
|
2013-02-18 19:34:48 -06:00
|
|
|
|
|
|
|
t(&(WindowsPath("\\\\computer\\share")
|
|
|
|
.unsafe_join(&WindowsPath("\\a"))),
|
|
|
|
"\\\\computer\\a");
|
|
|
|
|
|
|
|
t(&(WindowsPath("//computer/share")
|
|
|
|
.unsafe_join(&WindowsPath("\\a"))),
|
|
|
|
"\\\\computer\\a");
|
|
|
|
|
|
|
|
t(&(WindowsPath("//computer/share")
|
|
|
|
.unsafe_join(&WindowsPath("\\\\computer\\share"))),
|
|
|
|
"\\\\computer\\share");
|
|
|
|
|
|
|
|
t(&(WindowsPath("C:/whatever")
|
|
|
|
.unsafe_join(&WindowsPath("//computer/share/a/b"))),
|
|
|
|
"\\\\computer\\share\\a\\b");
|
|
|
|
|
|
|
|
t(&(WindowsPath("C:")
|
|
|
|
.unsafe_join(&WindowsPath("D:/foo"))),
|
|
|
|
"D:\\foo");
|
|
|
|
|
|
|
|
t(&(WindowsPath("C:")
|
|
|
|
.unsafe_join(&WindowsPath("B"))),
|
|
|
|
"C:B");
|
|
|
|
|
|
|
|
t(&(WindowsPath("C:")
|
|
|
|
.unsafe_join(&WindowsPath("/foo"))),
|
|
|
|
"C:\\foo");
|
|
|
|
|
|
|
|
t(&(WindowsPath("C:\\")
|
|
|
|
.unsafe_join(&WindowsPath("\\bar"))),
|
|
|
|
"C:\\bar");
|
|
|
|
|
|
|
|
t(&(WindowsPath("")
|
|
|
|
.unsafe_join(&WindowsPath(""))),
|
|
|
|
"");
|
|
|
|
|
|
|
|
t(&(WindowsPath("")
|
|
|
|
.unsafe_join(&WindowsPath("a"))),
|
|
|
|
"a");
|
|
|
|
|
|
|
|
t(&(WindowsPath("")
|
|
|
|
.unsafe_join(&WindowsPath("C:\\a"))),
|
|
|
|
"C:\\a");
|
|
|
|
|
|
|
|
t(&(WindowsPath("c:\\foo")
|
|
|
|
.normalize()),
|
|
|
|
"C:\\foo");
|
2012-09-18 13:19:52 -05:00
|
|
|
}
|
2013-02-18 19:54:05 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_windows_path_restrictions() {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(WindowsPath("hi").is_restricted(), false);
|
|
|
|
assert_eq!(WindowsPath("C:\\NUL").is_restricted(), true);
|
|
|
|
assert_eq!(WindowsPath("C:\\COM1.TXT").is_restricted(), true);
|
|
|
|
assert_eq!(WindowsPath("c:\\prn.exe").is_restricted(), true);
|
2013-02-18 19:54:05 -06:00
|
|
|
}
|
2013-06-26 19:42:24 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_ancestor_of() {
|
|
|
|
assert!(&PosixPath("/a/b").is_ancestor_of(&PosixPath("/a/b/c/d")));
|
|
|
|
assert!(!&PosixPath("/a/b/c/d").is_ancestor_of(&PosixPath("/a/b")));
|
|
|
|
assert!(!&PosixPath("/a/b").is_ancestor_of(&PosixPath("/c/d")));
|
|
|
|
assert!(&PosixPath("/a/b").is_ancestor_of(&PosixPath("/a/b/c/d")));
|
|
|
|
assert!(&PosixPath("/").is_ancestor_of(&PosixPath("/a/b/c")));
|
|
|
|
assert!(!&PosixPath("/").is_ancestor_of(&PosixPath("")));
|
|
|
|
assert!(!&PosixPath("/a/b/c").is_ancestor_of(&PosixPath("")));
|
|
|
|
assert!(!&PosixPath("").is_ancestor_of(&PosixPath("/a/b/c")));
|
|
|
|
|
|
|
|
assert!(&WindowsPath("C:\\a\\b").is_ancestor_of(&WindowsPath("C:\\a\\b\\c\\d")));
|
|
|
|
assert!(!&WindowsPath("C:\\a\\b\\c\\d").is_ancestor_of(&WindowsPath("C:\\a\\b")));
|
|
|
|
assert!(!&WindowsPath("C:\\a\\b").is_ancestor_of(&WindowsPath("C:\\c\\d")));
|
|
|
|
assert!(&WindowsPath("C:\\a\\b").is_ancestor_of(&WindowsPath("C:\\a\\b\\c\\d")));
|
|
|
|
assert!(&WindowsPath("C:\\").is_ancestor_of(&WindowsPath("C:\\a\\b\\c")));
|
|
|
|
assert!(!&WindowsPath("C:\\").is_ancestor_of(&WindowsPath("")));
|
|
|
|
assert!(!&WindowsPath("C:\\a\\b\\c").is_ancestor_of(&WindowsPath("")));
|
|
|
|
assert!(!&WindowsPath("").is_ancestor_of(&WindowsPath("C:\\a\\b\\c")));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-08-29 15:25:31 -05:00
|
|
|
}
|