auto merge of #9655 : kballard/rust/path-rewrite, r=alexcrichton

Rewrite the entire `std::path` module from scratch.

`PosixPath` is now based on `~[u8]`, which fixes #7225.
Unnecessary allocation has been eliminated.

There are a lot of clients of `Path` that still assume utf-8 paths.
This is covered in #9639.
This commit is contained in:
bors 2013-10-16 11:26:35 -07:00
commit 40180cdbea
62 changed files with 6329 additions and 2750 deletions

View file

@ -60,7 +60,7 @@ use num;
use ops::Drop;
use option::{Some, None};
use os;
use path::Path;
use path::{Path,GenericPath};
use ptr;
use result::{Result, Ok, Err};
use str::{StrSlice, OwnedStr};
@ -1069,7 +1069,9 @@ pub fn file_reader(path: &Path) -> Result<@Reader, ~str> {
};
if f as uint == 0u {
Err(~"error opening " + path.to_str())
do path.display().with_str |p| {
Err(~"error opening " + p)
}
} else {
Ok(FILE_reader(f, true))
}
@ -1335,7 +1337,7 @@ pub fn mk_file_writer(path: &Path, flags: &[FileFlag])
}
};
if fd < (0 as c_int) {
Err(format!("error opening {}: {}", path.to_str(), os::last_os_error()))
Err(format!("error opening {}: {}", path.display(), os::last_os_error()))
} else {
Ok(fd_writer(fd, true))
}
@ -1752,7 +1754,7 @@ pub fn read_whole_file_str(file: &Path) -> Result<~str, ~str> {
if str::is_utf8(bytes) {
Ok(str::from_utf8(bytes))
} else {
Err(file.to_str() + " is not UTF-8")
Err(file.display().to_str() + " is not UTF-8")
}
}
}
@ -1892,8 +1894,8 @@ mod tests {
#[test]
fn test_simple() {
let tmpfile = &Path("tmp/lib-io-test-simple.tmp");
debug2!("{:?}", tmpfile);
let tmpfile = &Path::new("tmp/lib-io-test-simple.tmp");
debug2!("{}", tmpfile.display());
let frood: ~str =
~"A hoopy frood who really knows where his towel is.";
debug2!("{}", frood.clone());
@ -1910,7 +1912,7 @@ mod tests {
#[test]
fn test_each_byte_each_char_file() {
// Issue #5056 -- shouldn't include trailing EOF.
let path = Path("tmp/lib-io-test-each-byte-each-char-file.tmp");
let path = Path::new("tmp/lib-io-test-each-byte-each-char-file.tmp");
{
// create empty, enough to reproduce a problem
@ -2010,7 +2012,7 @@ mod tests {
#[test]
fn file_reader_not_exist() {
match io::file_reader(&Path("not a file")) {
match io::file_reader(&Path::new("not a file")) {
Err(e) => {
assert_eq!(e, ~"error opening not a file");
}
@ -2021,7 +2023,7 @@ mod tests {
#[test]
#[should_fail]
fn test_read_buffer_too_small() {
let path = &Path("tmp/lib-io-test-read-buffer-too-small.tmp");
let path = &Path::new("tmp/lib-io-test-read-buffer-too-small.tmp");
// ensure the file exists
io::file_writer(path, [io::Create]).unwrap();
@ -2032,7 +2034,7 @@ mod tests {
#[test]
fn test_read_buffer_big_enough() {
let path = &Path("tmp/lib-io-test-read-buffer-big-enough.tmp");
let path = &Path::new("tmp/lib-io-test-read-buffer-big-enough.tmp");
// ensure the file exists
io::file_writer(path, [io::Create]).unwrap();
@ -2043,14 +2045,14 @@ mod tests {
#[test]
fn test_write_empty() {
let file = io::file_writer(&Path("tmp/lib-io-test-write-empty.tmp"),
let file = io::file_writer(&Path::new("tmp/lib-io-test-write-empty.tmp"),
[io::Create]).unwrap();
file.write([]);
}
#[test]
fn file_writer_bad_name() {
match io::file_writer(&Path("?/?"), []) {
match io::file_writer(&Path::new("?/?"), []) {
Err(e) => {
assert!(e.starts_with("error opening"));
}
@ -2075,7 +2077,7 @@ mod tests {
#[test]
fn test_read_write_le() {
let path = Path("tmp/lib-io-test-read-write-le.tmp");
let path = Path::new("tmp/lib-io-test-read-write-le.tmp");
let uints = [0, 1, 2, 42, 10_123, 100_123_456, u64::max_value];
// write the ints to the file
@ -2097,7 +2099,7 @@ mod tests {
#[test]
fn test_read_write_be() {
let path = Path("tmp/lib-io-test-read-write-be.tmp");
let path = Path::new("tmp/lib-io-test-read-write-be.tmp");
let uints = [0, 1, 2, 42, 10_123, 100_123_456, u64::max_value];
// write the ints to the file
@ -2119,7 +2121,7 @@ mod tests {
#[test]
fn test_read_be_int_n() {
let path = Path("tmp/lib-io-test-read-be-int-n.tmp");
let path = Path::new("tmp/lib-io-test-read-be-int-n.tmp");
let ints = [i32::min_value, -123456, -42, -5, 0, 1, i32::max_value];
// write the ints to the file
@ -2143,7 +2145,7 @@ mod tests {
#[test]
fn test_read_f32() {
let path = Path("tmp/lib-io-test-read-f32.tmp");
let path = Path::new("tmp/lib-io-test-read-f32.tmp");
//big-endian floating-point 8.1250
let buf = ~[0x41, 0x02, 0x00, 0x00];
@ -2161,7 +2163,7 @@ mod tests {
#[test]
fn test_read_write_f32() {
let path = Path("tmp/lib-io-test-read-write-f32.tmp");
let path = Path::new("tmp/lib-io-test-read-write-f32.tmp");
let f:f32 = 8.1250;
{

View file

@ -28,7 +28,7 @@
#[allow(missing_doc)];
use c_str::ToCStr;
use c_str::{CString, ToCStr};
use clone::Clone;
use container::Container;
use io;
@ -78,22 +78,7 @@ pub fn getcwd() -> Path {
fail2!()
}
Path(str::raw::from_c_str(buf as *c_char))
}
}
}
// FIXME: move these to str perhaps? #2620
pub fn fill_charp_buf(f: &fn(*mut c_char, size_t) -> bool) -> Option<~str> {
let mut buf = [0 as c_char, .. TMPBUF_SZ];
do buf.as_mut_buf |b, sz| {
if f(b, sz as size_t) {
unsafe {
Some(str::raw::from_c_str(b as *c_char))
}
} else {
None
Path::new(CString::new(buf as *c_char, false))
}
}
}
@ -451,70 +436,89 @@ pub fn dll_filename(base: &str) -> ~str {
pub fn self_exe_path() -> Option<Path> {
#[cfg(target_os = "freebsd")]
fn load_self() -> Option<~str> {
fn load_self() -> Option<~[u8]> {
#[fixed_stack_segment]; #[inline(never)];
unsafe {
use libc::funcs::bsd44::*;
use libc::consts::os::extra::*;
do fill_charp_buf() |buf, sz| {
let mib = ~[CTL_KERN as c_int,
KERN_PROC as c_int,
KERN_PROC_PATHNAME as c_int, -1 as c_int];
let mut sz = sz;
let mib = ~[CTL_KERN as c_int,
KERN_PROC as c_int,
KERN_PROC_PATHNAME as c_int, -1 as c_int];
let mut sz: size_t = 0;
let err = sysctl(vec::raw::to_ptr(mib), mib.len() as ::libc::c_uint,
ptr::mut_null(), &mut sz, ptr::null(), 0u as size_t);
if err != 0 { return None; }
if sz == 0 { return None; }
let mut v: ~[u8] = vec::with_capacity(sz as uint);
let err = do v.as_mut_buf |buf,_| {
sysctl(vec::raw::to_ptr(mib), mib.len() as ::libc::c_uint,
buf as *mut c_void, &mut sz, ptr::null(),
0u as size_t) == (0 as c_int)
}
buf as *mut c_void, &mut sz, ptr::null(), 0u as size_t)
};
if err != 0 { return None; }
if sz == 0 { return None; }
vec::raw::set_len(&mut v, sz as uint - 1); // chop off trailing NUL
Some(v)
}
}
#[cfg(target_os = "linux")]
#[cfg(target_os = "android")]
fn load_self() -> Option<~str> {
fn load_self() -> Option<~[u8]> {
#[fixed_stack_segment]; #[inline(never)];
unsafe {
use libc::funcs::posix01::unistd::readlink;
let mut path = [0 as c_char, .. TMPBUF_SZ];
let mut path: ~[u8] = vec::with_capacity(TMPBUF_SZ);
do path.as_mut_buf |buf, len| {
let len = do "/proc/self/exe".with_c_str |proc_self_buf| {
readlink(proc_self_buf, buf, len as size_t) as uint
};
if len == -1 {
None
} else {
Some(str::raw::from_buf_len(buf as *u8, len))
let len = do path.as_mut_buf |buf, _| {
do "/proc/self/exe".with_c_str |proc_self_buf| {
readlink(proc_self_buf, buf as *mut c_char, TMPBUF_SZ as size_t) as uint
}
};
if len == -1 {
None
} else {
vec::raw::set_len(&mut path, len as uint);
Some(path)
}
}
}
#[cfg(target_os = "macos")]
fn load_self() -> Option<~str> {
fn load_self() -> Option<~[u8]> {
#[fixed_stack_segment]; #[inline(never)];
unsafe {
do fill_charp_buf() |buf, sz| {
let mut sz = sz as u32;
libc::funcs::extra::_NSGetExecutablePath(
buf, &mut sz) == (0 as c_int)
}
use libc::funcs::extra::_NSGetExecutablePath;
let mut sz: u32 = 0;
_NSGetExecutablePath(ptr::mut_null(), &mut sz);
if sz == 0 { return None; }
let mut v: ~[u8] = vec::with_capacity(sz as uint);
let err = do v.as_mut_buf |buf,_| {
_NSGetExecutablePath(buf as *mut i8, &mut sz)
};
if err != 0 { return None; }
vec::raw::set_len(&mut v, sz as uint - 1); // chop off trailing NUL
Some(v)
}
}
#[cfg(windows)]
fn load_self() -> Option<~str> {
fn load_self() -> Option<~[u8]> {
#[fixed_stack_segment]; #[inline(never)];
unsafe {
use os::win32::fill_utf16_buf_and_decode;
do fill_utf16_buf_and_decode() |buf, sz| {
libc::GetModuleFileNameW(0u as libc::DWORD, buf, sz)
}
}.map(|s| s.into_bytes())
}
}
load_self().map(|path| Path(path).dir_path())
load_self().and_then(|path| Path::new_opt(path).map(|mut p| { p.pop(); p }))
}
/**
* Returns the path to the user's home directory, if known.
}
@ -532,13 +536,10 @@ pub fn self_exe_path() -> Option<Path> {
* Otherwise, homedir returns option::none.
*/
pub fn homedir() -> Option<Path> {
// FIXME (#7188): getenv needs a ~[u8] variant
return match getenv("HOME") {
Some(ref p) => if !p.is_empty() {
Some(Path(*p))
} else {
secondary()
},
None => secondary()
Some(ref p) if !p.is_empty() => Path::new_opt(p.as_slice()),
_ => secondary()
};
#[cfg(unix)]
@ -550,7 +551,7 @@ pub fn homedir() -> Option<Path> {
fn secondary() -> Option<Path> {
do getenv("USERPROFILE").and_then |p| {
if !p.is_empty() {
Some(Path(p))
Path::new_opt(p)
} else {
None
}
@ -579,7 +580,7 @@ pub fn tmpdir() -> Path {
if x.is_empty() {
None
} else {
Some(Path(x))
Path::new_opt(x)
},
_ => None
}
@ -588,9 +589,9 @@ pub fn tmpdir() -> Path {
#[cfg(unix)]
fn lookup() -> Path {
if cfg!(target_os = "android") {
Path("/data/tmp")
Path::new("/data/tmp")
} else {
getenv_nonempty("TMPDIR").unwrap_or(Path("/tmp"))
getenv_nonempty("TMPDIR").unwrap_or(Path::new("/tmp"))
}
}
@ -599,7 +600,7 @@ pub fn tmpdir() -> Path {
getenv_nonempty("TMP").or(
getenv_nonempty("TEMP").or(
getenv_nonempty("USERPROFILE").or(
getenv_nonempty("WINDIR")))).unwrap_or(Path("C:\\Windows"))
getenv_nonempty("WINDIR")))).unwrap_or(Path::new("C:\\Windows"))
}
}
@ -607,7 +608,7 @@ pub fn tmpdir() -> Path {
pub fn walk_dir(p: &Path, f: &fn(&Path) -> bool) -> bool {
let r = list_dir(p);
r.iter().advance(|q| {
let path = &p.push(*q);
let path = &p.join(q);
f(path) && (!path_is_dir(path) || walk_dir(path, |p| f(p)))
})
}
@ -643,10 +644,12 @@ pub fn path_exists(p: &Path) -> bool {
// querying; what it does depends on the process working directory, not just
// the input paths.
pub fn make_absolute(p: &Path) -> Path {
if p.is_absolute {
(*p).clone()
if p.is_absolute() {
p.clone()
} else {
getcwd().push_many(p.components)
let mut ret = getcwd();
ret.push(p);
ret
}
}
@ -661,7 +664,7 @@ pub fn make_dir(p: &Path, mode: c_int) -> bool {
unsafe {
use os::win32::as_utf16_p;
// FIXME: turn mode into something useful? #2623
do as_utf16_p(p.to_str()) |buf| {
do as_utf16_p(p.as_str().unwrap()) |buf| {
libc::CreateDirectoryW(buf, ptr::mut_null())
!= (0 as libc::BOOL)
}
@ -690,38 +693,33 @@ pub fn mkdir_recursive(p: &Path, mode: c_int) -> bool {
if path_is_dir(p) {
return true;
}
else if p.components.is_empty() {
return false;
}
else if p.components.len() == 1 {
// No parent directories to create
path_is_dir(p) || make_dir(p, mode)
}
else {
mkdir_recursive(&p.pop(), mode) && make_dir(p, mode)
if p.filename().is_some() {
let mut p_ = p.clone();
p_.pop();
if !mkdir_recursive(&p_, mode) {
return false;
}
}
return make_dir(p, mode);
}
/// Lists the contents of a directory
pub fn list_dir(p: &Path) -> ~[~str] {
if p.components.is_empty() && !p.is_absolute() {
// Not sure what the right behavior is here, but this
// prevents a bounds check failure later
return ~[];
}
///
/// Each resulting Path is a relative path with no directory component.
pub fn list_dir(p: &Path) -> ~[Path] {
unsafe {
#[cfg(target_os = "linux")]
#[cfg(target_os = "android")]
#[cfg(target_os = "freebsd")]
#[cfg(target_os = "macos")]
unsafe fn get_list(p: &Path) -> ~[~str] {
unsafe fn get_list(p: &Path) -> ~[Path] {
#[fixed_stack_segment]; #[inline(never)];
use libc::{dirent_t};
use libc::{opendir, readdir, closedir};
extern {
fn rust_list_dir_val(ptr: *dirent_t) -> *libc::c_char;
}
let mut strings = ~[];
let mut paths = ~[];
debug2!("os::list_dir -- BEFORE OPENDIR");
let dir_ptr = do p.with_c_str |buf| {
@ -732,8 +730,8 @@ pub fn list_dir(p: &Path) -> ~[~str] {
debug2!("os::list_dir -- opendir() SUCCESS");
let mut entry_ptr = readdir(dir_ptr);
while (entry_ptr as uint != 0) {
strings.push(str::raw::from_c_str(rust_list_dir_val(
entry_ptr)));
let cstr = CString::new(rust_list_dir_val(entry_ptr), false);
paths.push(Path::new(cstr));
entry_ptr = readdir(dir_ptr);
}
closedir(dir_ptr);
@ -741,11 +739,11 @@ pub fn list_dir(p: &Path) -> ~[~str] {
else {
debug2!("os::list_dir -- opendir() FAILURE");
}
debug2!("os::list_dir -- AFTER -- \\#: {}", strings.len());
strings
debug2!("os::list_dir -- AFTER -- \\#: {}", paths.len());
paths
}
#[cfg(windows)]
unsafe fn get_list(p: &Path) -> ~[~str] {
unsafe fn get_list(p: &Path) -> ~[Path] {
#[fixed_stack_segment]; #[inline(never)];
use libc::consts::os::extra::INVALID_HANDLE_VALUE;
use libc::{wcslen, free};
@ -765,9 +763,9 @@ pub fn list_dir(p: &Path) -> ~[~str] {
fn rust_list_dir_wfd_size() -> libc::size_t;
fn rust_list_dir_wfd_fp_buf(wfd: *libc::c_void) -> *u16;
}
fn star(p: &Path) -> Path { p.push("*") }
do as_utf16_p(star(p).to_str()) |path_ptr| {
let mut strings = ~[];
let star = p.join("*");
do as_utf16_p(star.as_str().unwrap()) |path_ptr| {
let mut paths = ~[];
let wfd_ptr = malloc_raw(rust_list_dir_wfd_size() as uint);
let find_handle = FindFirstFileW(path_ptr, wfd_ptr as HANDLE);
if find_handle as libc::c_int != INVALID_HANDLE_VALUE {
@ -781,18 +779,18 @@ pub fn list_dir(p: &Path) -> ~[~str] {
let fp_vec = vec::from_buf(
fp_buf, wcslen(fp_buf) as uint);
let fp_str = str::from_utf16(fp_vec);
strings.push(fp_str);
paths.push(Path::new(fp_str));
}
more_files = FindNextFileW(find_handle, wfd_ptr as HANDLE);
}
FindClose(find_handle);
free(wfd_ptr)
}
strings
paths
}
}
do get_list(p).move_iter().filter |filename| {
"." != *filename && ".." != *filename
do get_list(p).move_iter().filter |path| {
path.as_vec() != bytes!(".") && path.as_vec() != bytes!("..")
}.collect()
}
}
@ -803,7 +801,7 @@ pub fn list_dir(p: &Path) -> ~[~str] {
* This version prepends each entry with the directory.
*/
pub fn list_dir_path(p: &Path) -> ~[Path] {
list_dir(p).map(|f| p.push(*f))
list_dir(p).map(|f| p.join(f))
}
/// Removes a directory at the specified path, after removing
@ -838,7 +836,7 @@ pub fn remove_dir(p: &Path) -> bool {
#[fixed_stack_segment]; #[inline(never)];
unsafe {
use os::win32::as_utf16_p;
return do as_utf16_p(p.to_str()) |buf| {
return do as_utf16_p(p.as_str().unwrap()) |buf| {
libc::RemoveDirectoryW(buf) != (0 as libc::BOOL)
};
}
@ -865,7 +863,7 @@ pub fn change_dir(p: &Path) -> bool {
#[fixed_stack_segment]; #[inline(never)];
unsafe {
use os::win32::as_utf16_p;
return do as_utf16_p(p.to_str()) |buf| {
return do as_utf16_p(p.as_str().unwrap()) |buf| {
libc::SetCurrentDirectoryW(buf) != (0 as libc::BOOL)
};
}
@ -891,8 +889,8 @@ pub fn copy_file(from: &Path, to: &Path) -> bool {
#[fixed_stack_segment]; #[inline(never)];
unsafe {
use os::win32::as_utf16_p;
return do as_utf16_p(from.to_str()) |fromp| {
do as_utf16_p(to.to_str()) |top| {
return do as_utf16_p(from.as_str().unwrap()) |fromp| {
do as_utf16_p(to.as_str().unwrap()) |top| {
libc::CopyFileW(fromp, top, (0 as libc::BOOL)) !=
(0 as libc::BOOL)
}
@ -968,7 +966,7 @@ pub fn remove_file(p: &Path) -> bool {
#[fixed_stack_segment]; #[inline(never)];
unsafe {
use os::win32::as_utf16_p;
return do as_utf16_p(p.to_str()) |buf| {
return do as_utf16_p(p.as_str().unwrap()) |buf| {
libc::DeleteFileW(buf) != (0 as libc::BOOL)
};
}
@ -1657,35 +1655,45 @@ pub mod consts {
pub static SYSNAME: &'static str = "macos";
pub static DLL_PREFIX: &'static str = "lib";
pub static DLL_SUFFIX: &'static str = ".dylib";
pub static DLL_EXTENSION: &'static str = "dylib";
pub static EXE_SUFFIX: &'static str = "";
pub static EXE_EXTENSION: &'static str = "";
}
pub mod freebsd {
pub static SYSNAME: &'static str = "freebsd";
pub static DLL_PREFIX: &'static str = "lib";
pub static DLL_SUFFIX: &'static str = ".so";
pub static DLL_EXTENSION: &'static str = "so";
pub static EXE_SUFFIX: &'static str = "";
pub static EXE_EXTENSION: &'static str = "";
}
pub mod linux {
pub static SYSNAME: &'static str = "linux";
pub static DLL_PREFIX: &'static str = "lib";
pub static DLL_SUFFIX: &'static str = ".so";
pub static DLL_EXTENSION: &'static str = "so";
pub static EXE_SUFFIX: &'static str = "";
pub static EXE_EXTENSION: &'static str = "";
}
pub mod android {
pub static SYSNAME: &'static str = "android";
pub static DLL_PREFIX: &'static str = "lib";
pub static DLL_SUFFIX: &'static str = ".so";
pub static DLL_EXTENSION: &'static str = "so";
pub static EXE_SUFFIX: &'static str = "";
pub static EXE_EXTENSION: &'static str = "";
}
pub mod win32 {
pub static SYSNAME: &'static str = "win32";
pub static DLL_PREFIX: &'static str = "";
pub static DLL_SUFFIX: &'static str = ".dll";
pub static DLL_EXTENSION: &'static str = "dll";
pub static EXE_SUFFIX: &'static str = ".exe";
pub static EXE_EXTENSION: &'static str = "exe";
}
@ -1790,7 +1798,7 @@ mod tests {
debug2!("{:?}", path.clone());
// Hard to test this function
assert!(path.is_absolute);
assert!(path.is_absolute());
}
#[test]
@ -1823,12 +1831,13 @@ mod tests {
#[test]
fn test() {
assert!((!Path("test-path").is_absolute));
assert!((!Path::new("test-path").is_absolute()));
debug2!("Current working directory: {}", getcwd().to_str());
let cwd = getcwd();
debug2!("Current working directory: {}", cwd.display());
debug2!("{:?}", make_absolute(&Path("test-path")));
debug2!("{:?}", make_absolute(&Path("/usr/bin")));
debug2!("{:?}", make_absolute(&Path::new("test-path")));
debug2!("{:?}", make_absolute(&Path::new("/usr/bin")));
}
#[test]
@ -1837,7 +1846,7 @@ mod tests {
let oldhome = getenv("HOME");
setenv("HOME", "/home/MountainView");
assert_eq!(os::homedir(), Some(Path("/home/MountainView")));
assert_eq!(os::homedir(), Some(Path::new("/home/MountainView")));
setenv("HOME", "");
assert!(os::homedir().is_none());
@ -1858,16 +1867,16 @@ mod tests {
assert!(os::homedir().is_none());
setenv("HOME", "/home/MountainView");
assert_eq!(os::homedir(), Some(Path("/home/MountainView")));
assert_eq!(os::homedir(), Some(Path::new("/home/MountainView")));
setenv("HOME", "");
setenv("USERPROFILE", "/home/MountainView");
assert_eq!(os::homedir(), Some(Path("/home/MountainView")));
assert_eq!(os::homedir(), Some(Path::new("/home/MountainView")));
setenv("HOME", "/home/MountainView");
setenv("USERPROFILE", "/home/PaloAlto");
assert_eq!(os::homedir(), Some(Path("/home/MountainView")));
assert_eq!(os::homedir(), Some(Path::new("/home/MountainView")));
for s in oldhome.iter() { setenv("HOME", *s) }
for s in olduserprofile.iter() { setenv("USERPROFILE", *s) }
@ -1875,18 +1884,20 @@ mod tests {
#[test]
fn tmpdir() {
assert!(!os::tmpdir().to_str().is_empty());
let p = os::tmpdir();
let s = p.as_str();
assert!(s.is_some() && s.unwrap() != ".");
}
// Issue #712
#[test]
fn test_list_dir_no_invalid_memory_access() {
os::list_dir(&Path("."));
os::list_dir(&Path::new("."));
}
#[test]
fn list_dir() {
let dirs = os::list_dir(&Path("."));
let dirs = os::list_dir(&Path::new("."));
// Just assuming that we've got some contents in the current directory
assert!(dirs.len() > 0u);
@ -1895,44 +1906,38 @@ mod tests {
}
}
#[test]
fn list_dir_empty_path() {
let dirs = os::list_dir(&Path(""));
assert!(dirs.is_empty());
}
#[test]
#[cfg(not(windows))]
fn list_dir_root() {
let dirs = os::list_dir(&Path("/"));
let dirs = os::list_dir(&Path::new("/"));
assert!(dirs.len() > 1);
}
#[test]
#[cfg(windows)]
fn list_dir_root() {
let dirs = os::list_dir(&Path("C:\\"));
let dirs = os::list_dir(&Path::new("C:\\"));
assert!(dirs.len() > 1);
}
#[test]
fn path_is_dir() {
assert!((os::path_is_dir(&Path("."))));
assert!((!os::path_is_dir(&Path("test/stdtest/fs.rs"))));
assert!((os::path_is_dir(&Path::new("."))));
assert!((!os::path_is_dir(&Path::new("test/stdtest/fs.rs"))));
}
#[test]
fn path_exists() {
assert!((os::path_exists(&Path("."))));
assert!((!os::path_exists(&Path(
assert!((os::path_exists(&Path::new("."))));
assert!((!os::path_exists(&Path::new(
"test/nonexistent-bogus-path"))));
}
#[test]
fn copy_file_does_not_exist() {
assert!(!os::copy_file(&Path("test/nonexistent-bogus-path"),
&Path("test/other-bogus-path")));
assert!(!os::path_exists(&Path("test/other-bogus-path")));
assert!(!os::copy_file(&Path::new("test/nonexistent-bogus-path"),
&Path::new("test/other-bogus-path")));
assert!(!os::path_exists(&Path::new("test/other-bogus-path")));
}
#[test]
@ -1942,9 +1947,8 @@ mod tests {
unsafe {
let tempdir = getcwd(); // would like to use $TMPDIR,
// doesn't seem to work on Linux
assert!((tempdir.to_str().len() > 0u));
let input = tempdir.push("in.txt");
let out = tempdir.push("out.txt");
let input = tempdir.join("in.txt");
let out = tempdir.join("out.txt");
/* Write the temp input file */
let ostream = do input.with_c_str |fromp| {
@ -1965,10 +1969,12 @@ mod tests {
let in_mode = input.get_mode();
let rs = os::copy_file(&input, &out);
if (!os::path_exists(&input)) {
fail2!("{} doesn't exist", input.to_str());
fail2!("{} doesn't exist", input.display());
}
assert!((rs));
let rslt = run::process_status("diff", [input.to_str(), out.to_str()]);
// FIXME (#9639): This needs to handle non-utf8 paths
let rslt = run::process_status("diff", [input.as_str().unwrap().to_owned(),
out.as_str().unwrap().to_owned()]);
assert_eq!(rslt, 0);
assert_eq!(out.get_mode(), in_mode);
assert!((remove_file(&input)));
@ -1978,16 +1984,10 @@ mod tests {
#[test]
fn recursive_mkdir_slash() {
let path = Path("/");
let path = Path::new("/");
assert!(os::mkdir_recursive(&path, (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
}
#[test]
fn recursive_mkdir_empty() {
let path = Path("");
assert!(!os::mkdir_recursive(&path, (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
}
#[test]
fn memory_map_rw() {
use result::{Ok, Err};
@ -2032,7 +2032,8 @@ mod tests {
}
}
let path = tmpdir().push("mmap_file.tmp");
let mut path = tmpdir();
path.push("mmap_file.tmp");
let size = MemoryMap::granularity() * 2;
remove_file(&path);

File diff suppressed because it is too large Load diff

928
src/libstd/path/mod.rs Normal file
View file

@ -0,0 +1,928 @@
// Copyright 2013 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.
/*!
Cross-platform path support
This module implements support for two flavors of paths. `PosixPath` represents
a path on any unix-like system, whereas `WindowsPath` represents a path on
Windows. This module also exposes a typedef `Path` which is equal to the
appropriate platform-specific path variant.
Both `PosixPath` and `WindowsPath` implement a trait `GenericPath`, which
contains the set of methods that behave the same for both paths. They each also
implement some methods that could not be expressed in `GenericPath`, yet behave
identically for both path flavors, such as `.component_iter()`.
The three main design goals of this module are 1) to avoid unnecessary
allocation, 2) to behave the same regardless of which flavor of path is being
used, and 3) to support paths that cannot be represented in UTF-8 (as Linux has
no restriction on paths beyond disallowing NUL).
## Usage
Usage of this module is fairly straightforward. Unless writing platform-specific
code, `Path` should be used to refer to the platform-native path.
Creation of a path is typically done with either `Path::new(some_str)` or
`Path::new(some_vec)`. This path can be modified with `.push()` and
`.pop()` (and other setters). The resulting Path can either be passed to another
API that expects a path, or can be turned into a &[u8] with `.as_vec()` or a
Option<&str> with `.as_str()`. Similarly, attributes of the path can be queried
with methods such as `.filename()`. There are also methods that return a new
path instead of modifying the receiver, such as `.join()` or `.dir_path()`.
Paths are always kept in normalized form. This means that creating the path
`Path::new("a/b/../c")` will return the path `a/c`. Similarly any attempt
to mutate the path will always leave it in normalized form.
When rendering a path to some form of output, there is a method `.display()`
which is compatible with the `format!()` parameter `{}`. This will render the
path as a string, replacing all non-utf8 sequences with the Replacement
Character (U+FFFD). As such it is not suitable for passing to any API that
actually operates on the path; it is only intended for display.
## Example
```rust
let mut path = Path::new("/tmp/path");
debug2!("path: {}", path.display());
path.set_filename("foo");
path.push("bar");
debug2!("new path: {}", path.display());
let b = std::os::path_exists(&path);
debug2!("path exists: {}", b);
```
*/
use container::Container;
use c_str::CString;
use clone::Clone;
use fmt;
use iter::Iterator;
use option::{Option, None, Some};
use str;
use str::{OwnedStr, Str, StrSlice};
use to_str::ToStr;
use vec;
use vec::{CopyableVector, OwnedCopyableVector, OwnedVector, Vector};
use vec::{ImmutableEqVector, ImmutableVector};
/// Typedef for POSIX file paths.
/// See `posix::Path` for more info.
pub use PosixPath = self::posix::Path;
/// Typedef for Windows file paths.
/// See `windows::Path` for more info.
pub use WindowsPath = self::windows::Path;
/// Typedef for the platform-native path type
#[cfg(unix)]
pub use Path = self::posix::Path;
/// Typedef for the platform-native path type
#[cfg(windows)]
pub use Path = self::windows::Path;
/// Typedef for the platform-native component iterator
#[cfg(unix)]
pub use ComponentIter = self::posix::ComponentIter;
/// Typedef for the platform-native reverse component iterator
#[cfg(unix)]
pub use RevComponentIter = self::posix::RevComponentIter;
/// Typedef for the platform-native component iterator
#[cfg(windows)]
pub use ComponentIter = self::windows::ComponentIter;
/// Typedef for the platform-native reverse component iterator
#[cfg(windows)]
pub use RevComponentIter = self::windows::RevComponentIter;
/// Typedef for the platform-native str component iterator
#[cfg(unix)]
pub use StrComponentIter = self::posix::StrComponentIter;
/// Typedef for the platform-native reverse str component iterator
#[cfg(unix)]
pub use RevStrComponentIter = self::posix::RevStrComponentIter;
/// Typedef for the platform-native str component iterator
#[cfg(windows)]
pub use StrComponentIter = self::windows::StrComponentIter;
/// Typedef for the platform-native reverse str component iterator
#[cfg(windows)]
pub use RevStrComponentIter = self::windows::RevStrComponentIter;
/// Typedef for the platform-native separator char func
#[cfg(unix)]
pub use is_sep = self::posix::is_sep;
/// Typedef for the platform-native separator char func
#[cfg(windows)]
pub use is_sep = self::windows::is_sep;
/// Typedef for the platform-native separator byte func
#[cfg(unix)]
pub use is_sep_byte = self::posix::is_sep_byte;
/// Typedef for the platform-native separator byte func
#[cfg(windows)]
pub use is_sep_byte = self::windows::is_sep_byte;
pub mod posix;
pub mod windows;
// Condition that is raised when a NUL is found in a byte vector given to a Path function
condition! {
// this should be a &[u8] but there's a lifetime issue
null_byte: ~[u8] -> ~[u8];
}
/// A trait that represents the generic operations available on paths
pub trait GenericPath: Clone + GenericPathUnsafe {
/// Creates a new Path from a byte vector or string.
/// The resulting Path will always be normalized.
///
/// # Failure
///
/// Raises the `null_byte` condition if the path contains a NUL.
///
/// See individual Path impls for additional restrictions.
#[inline]
fn new<T: BytesContainer>(path: T) -> Self {
if contains_nul(path.container_as_bytes()) {
let path = self::null_byte::cond.raise(path.container_into_owned_bytes());
assert!(!contains_nul(path));
unsafe { GenericPathUnsafe::new_unchecked(path) }
} else {
unsafe { GenericPathUnsafe::new_unchecked(path) }
}
}
/// Creates a new Path from a byte vector or string, if possible.
/// The resulting Path will always be normalized.
#[inline]
fn new_opt<T: BytesContainer>(path: T) -> Option<Self> {
if contains_nul(path.container_as_bytes()) {
None
} else {
Some(unsafe { GenericPathUnsafe::new_unchecked(path) })
}
}
/// Returns the path as a string, if possible.
/// If the path is not representable in utf-8, this returns None.
#[inline]
fn as_str<'a>(&'a self) -> Option<&'a str> {
str::from_utf8_slice_opt(self.as_vec())
}
/// Returns the path as a byte vector
fn as_vec<'a>(&'a self) -> &'a [u8];
/// Converts the Path into an owned byte vector
fn into_vec(self) -> ~[u8];
/// Returns an object that implements `fmt::Default` for printing paths
///
/// This will print the equivalent of `to_display_str()` when used with a {} format parameter.
fn display<'a>(&'a self) -> Display<'a, Self> {
Display{ path: self, filename: false }
}
/// Returns an object that implements `fmt::Default` for printing filenames
///
/// This will print the equivalent of `to_filename_display_str()` when used with a {}
/// format parameter. If there is no filename, nothing will be printed.
fn filename_display<'a>(&'a self) -> Display<'a, Self> {
Display{ path: self, filename: true }
}
/// Returns the directory component of `self`, as a byte vector (with no trailing separator).
/// If `self` has no directory component, returns ['.'].
fn dirname<'a>(&'a self) -> &'a [u8];
/// Returns the directory component of `self`, as a string, if possible.
/// See `dirname` for details.
#[inline]
fn dirname_str<'a>(&'a self) -> Option<&'a str> {
str::from_utf8_slice_opt(self.dirname())
}
/// Returns the file component of `self`, as a byte vector.
/// If `self` represents the root of the file hierarchy, returns None.
/// If `self` is "." or "..", returns None.
fn filename<'a>(&'a self) -> Option<&'a [u8]>;
/// Returns the file component of `self`, as a string, if possible.
/// See `filename` for details.
#[inline]
fn filename_str<'a>(&'a self) -> Option<&'a str> {
self.filename().and_then(str::from_utf8_slice_opt)
}
/// Returns the stem of the filename of `self`, as a byte vector.
/// The stem is the portion of the filename just before the last '.'.
/// If there is no '.', the entire filename is returned.
fn filestem<'a>(&'a self) -> Option<&'a [u8]> {
match self.filename() {
None => None,
Some(name) => Some({
let dot = '.' as u8;
match name.rposition_elem(&dot) {
None | Some(0) => name,
Some(1) if name == bytes!("..") => name,
Some(pos) => name.slice_to(pos)
}
})
}
}
/// Returns the stem of the filename of `self`, as a string, if possible.
/// See `filestem` for details.
#[inline]
fn filestem_str<'a>(&'a self) -> Option<&'a str> {
self.filestem().and_then(str::from_utf8_slice_opt)
}
/// Returns the extension of the filename of `self`, as an optional byte vector.
/// The extension is the portion of the filename just after the last '.'.
/// If there is no extension, None is returned.
/// If the filename ends in '.', the empty vector is returned.
fn extension<'a>(&'a self) -> Option<&'a [u8]> {
match self.filename() {
None => None,
Some(name) => {
let dot = '.' as u8;
match name.rposition_elem(&dot) {
None | Some(0) => None,
Some(1) if name == bytes!("..") => None,
Some(pos) => Some(name.slice_from(pos+1))
}
}
}
}
/// Returns the extension of the filename of `self`, as a string, if possible.
/// See `extension` for details.
#[inline]
fn extension_str<'a>(&'a self) -> Option<&'a str> {
self.extension().and_then(str::from_utf8_slice_opt)
}
/// Replaces the filename portion of the path with the given byte vector or string.
/// If the replacement name is [], this is equivalent to popping the path.
///
/// # Failure
///
/// Raises the `null_byte` condition if the filename contains a NUL.
#[inline]
fn set_filename<T: BytesContainer>(&mut self, filename: T) {
if contains_nul(filename.container_as_bytes()) {
let filename = self::null_byte::cond.raise(filename.container_into_owned_bytes());
assert!(!contains_nul(filename));
unsafe { self.set_filename_unchecked(filename) }
} else {
unsafe { self.set_filename_unchecked(filename) }
}
}
/// Replaces the extension with the given byte vector or string.
/// If there is no extension in `self`, this adds one.
/// If the argument is [] or "", this removes the extension.
/// If `self` has no filename, this is a no-op.
///
/// # Failure
///
/// Raises the `null_byte` condition if the extension contains a NUL.
fn set_extension<T: BytesContainer>(&mut self, extension: T) {
// borrowck causes problems here too
let val = {
match self.filename() {
None => None,
Some(name) => {
let dot = '.' as u8;
match name.rposition_elem(&dot) {
None | Some(0) => {
if extension.container_as_bytes().is_empty() {
None
} else {
let mut v;
if contains_nul(extension.container_as_bytes()) {
let ext = extension.container_into_owned_bytes();
let extension = self::null_byte::cond.raise(ext);
assert!(!contains_nul(extension));
v = vec::with_capacity(name.len() + extension.len() + 1);
v.push_all(name);
v.push(dot);
v.push_all(extension);
} else {
let extension = extension.container_as_bytes();
v = vec::with_capacity(name.len() + extension.len() + 1);
v.push_all(name);
v.push(dot);
v.push_all(extension);
}
Some(v)
}
}
Some(idx) => {
if extension.container_as_bytes().is_empty() {
Some(name.slice_to(idx).to_owned())
} else {
let mut v;
if contains_nul(extension.container_as_bytes()) {
let ext = extension.container_into_owned_bytes();
let extension = self::null_byte::cond.raise(ext);
assert!(!contains_nul(extension));
v = vec::with_capacity(idx + extension.len() + 1);
v.push_all(name.slice_to(idx+1));
v.push_all(extension);
} else {
let extension = extension.container_as_bytes();
v = vec::with_capacity(idx + extension.len() + 1);
v.push_all(name.slice_to(idx+1));
v.push_all(extension);
}
Some(v)
}
}
}
}
}
};
match val {
None => (),
Some(v) => unsafe { self.set_filename_unchecked(v) }
}
}
/// Returns a new Path constructed by replacing the filename with the given
/// byte vector or string.
/// See `set_filename` for details.
///
/// # Failure
///
/// Raises the `null_byte` condition if the filename contains a NUL.
#[inline]
fn with_filename<T: BytesContainer>(&self, filename: T) -> Self {
let mut p = self.clone();
p.set_filename(filename);
p
}
/// Returns a new Path constructed by setting the extension to the given
/// byte vector or string.
/// See `set_extension` for details.
///
/// # Failure
///
/// Raises the `null_byte` condition if the extension contains a NUL.
#[inline]
fn with_extension<T: BytesContainer>(&self, extension: T) -> Self {
let mut p = self.clone();
p.set_extension(extension);
p
}
/// Returns the directory component of `self`, as a Path.
/// If `self` represents the root of the filesystem hierarchy, returns `self`.
fn dir_path(&self) -> Self {
// self.dirname() returns a NUL-free vector
unsafe { GenericPathUnsafe::new_unchecked(self.dirname()) }
}
/// Returns a Path that represents the filesystem root that `self` is rooted in.
///
/// If `self` is not absolute, or vol-relative in the case of Windows, this returns None.
fn root_path(&self) -> Option<Self>;
/// Pushes a path (as a byte vector or string) onto `self`.
/// If the argument represents an absolute path, it replaces `self`.
///
/// # Failure
///
/// Raises the `null_byte` condition if the path contains a NUL.
#[inline]
fn push<T: BytesContainer>(&mut self, path: T) {
if contains_nul(path.container_as_bytes()) {
let path = self::null_byte::cond.raise(path.container_into_owned_bytes());
assert!(!contains_nul(path));
unsafe { self.push_unchecked(path) }
} else {
unsafe { self.push_unchecked(path) }
}
}
/// Pushes multiple paths (as byte vectors or strings) onto `self`.
/// See `push` for details.
#[inline]
fn push_many<T: BytesContainer>(&mut self, paths: &[T]) {
let t: Option<T> = None;
if BytesContainer::is_str(t) {
for p in paths.iter() {
self.push(p.container_as_str())
}
} else {
for p in paths.iter() {
self.push(p.container_as_bytes())
}
}
}
/// Removes the last path component from the receiver.
/// Returns `true` if the receiver was modified, or `false` if it already
/// represented the root of the file hierarchy.
fn pop(&mut self) -> bool;
/// Returns a new Path constructed by joining `self` with the given path
/// (as a byte vector or string).
/// If the given path is absolute, the new Path will represent just that.
///
/// # Failure
///
/// Raises the `null_byte` condition if the path contains a NUL.
#[inline]
fn join<T: BytesContainer>(&self, path: T) -> Self {
let mut p = self.clone();
p.push(path);
p
}
/// Returns a new Path constructed by joining `self` with the given paths
/// (as byte vectors or strings).
/// See `join` for details.
#[inline]
fn join_many<T: BytesContainer>(&self, paths: &[T]) -> Self {
let mut p = self.clone();
p.push_many(paths);
p
}
/// Returns whether `self` represents an absolute path.
/// An absolute path is defined as one that, when joined to another path, will
/// yield back the same absolute path.
fn is_absolute(&self) -> bool;
/// Returns whether `self` represents a relative path.
/// Typically this is the inverse of `is_absolute`.
/// But for Windows paths, it also means the path is not volume-relative or
/// relative to the current working directory.
fn is_relative(&self) -> bool {
!self.is_absolute()
}
/// Returns whether `self` is equal to, or is an ancestor of, the given path.
/// If both paths are relative, they are compared as though they are relative
/// to the same parent path.
fn is_ancestor_of(&self, other: &Self) -> bool;
/// Returns the Path that, were it joined to `base`, would yield `self`.
/// If no such path exists, None is returned.
/// If `self` is absolute and `base` is relative, or on Windows if both
/// paths refer to separate drives, an absolute path is returned.
fn path_relative_from(&self, base: &Self) -> Option<Self>;
/// Returns whether the relative path `child` is a suffix of `self`.
fn ends_with_path(&self, child: &Self) -> bool;
}
/// A trait that represents something bytes-like (e.g. a &[u8] or a &str)
pub trait BytesContainer {
/// Returns a &[u8] representing the receiver
fn container_as_bytes<'a>(&'a self) -> &'a [u8];
/// Consumes the receiver and converts it into ~[u8]
#[inline]
fn container_into_owned_bytes(self) -> ~[u8] {
self.container_as_bytes().to_owned()
}
/// Returns the receiver interpreted as a utf-8 string
///
/// # Failure
///
/// Raises `str::null_byte` if not utf-8
#[inline]
fn container_as_str<'a>(&'a self) -> &'a str {
str::from_utf8_slice(self.container_as_bytes())
}
/// Returns the receiver interpreted as a utf-8 string, if possible
#[inline]
fn container_as_str_opt<'a>(&'a self) -> Option<&'a str> {
str::from_utf8_slice_opt(self.container_as_bytes())
}
/// Returns whether .container_as_str() is guaranteed to not fail
// FIXME (#8888): Remove unused arg once ::<for T> works
#[inline]
fn is_str(_: Option<Self>) -> bool { false }
}
/// A trait that represents the unsafe operations on GenericPaths
pub trait GenericPathUnsafe {
/// Creates a new Path without checking for null bytes.
/// The resulting Path will always be normalized.
unsafe fn new_unchecked<T: BytesContainer>(path: T) -> Self;
/// Replaces the filename portion of the path without checking for null
/// bytes.
/// See `set_filename` for details.
unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T);
/// Pushes a path onto `self` without checking for null bytes.
/// See `push` for details.
unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T);
}
/// Helper struct for printing paths with format!()
pub struct Display<'self, P> {
priv path: &'self P,
priv filename: bool
}
impl<'self, P: GenericPath> fmt::Default for Display<'self, P> {
fn fmt(d: &Display<P>, f: &mut fmt::Formatter) {
do d.with_str |s| {
f.pad(s)
}
}
}
impl<'self, P: GenericPath> ToStr for Display<'self, P> {
/// Returns the path as a string
///
/// If the path is not UTF-8, invalid sequences with be replaced with the
/// unicode replacement char. This involves allocation.
fn to_str(&self) -> ~str {
if self.filename {
match self.path.filename() {
None => ~"",
Some(v) => from_utf8_with_replacement(v)
}
} else {
from_utf8_with_replacement(self.path.as_vec())
}
}
}
impl<'self, P: GenericPath> Display<'self, P> {
/// Provides the path as a string to a closure
///
/// If the path is not UTF-8, invalid sequences will be replaced with the
/// unicode replacement char. This involves allocation.
#[inline]
pub fn with_str<T>(&self, f: &fn(&str) -> T) -> T {
let opt = if self.filename { self.path.filename_str() }
else { self.path.as_str() };
match opt {
Some(s) => f(s),
None => {
let s = self.to_str();
f(s.as_slice())
}
}
}
}
impl<'self> BytesContainer for &'self str {
#[inline]
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
self.as_bytes()
}
#[inline]
fn container_as_str<'a>(&'a self) -> &'a str {
*self
}
#[inline]
fn container_as_str_opt<'a>(&'a self) -> Option<&'a str> {
Some(*self)
}
#[inline]
fn is_str(_: Option<&'self str>) -> bool { true }
}
impl BytesContainer for ~str {
#[inline]
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
self.as_bytes()
}
#[inline]
fn container_into_owned_bytes(self) -> ~[u8] {
self.into_bytes()
}
#[inline]
fn container_as_str<'a>(&'a self) -> &'a str {
self.as_slice()
}
#[inline]
fn container_as_str_opt<'a>(&'a self) -> Option<&'a str> {
Some(self.as_slice())
}
#[inline]
fn is_str(_: Option<~str>) -> bool { true }
}
impl BytesContainer for @str {
#[inline]
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
self.as_bytes()
}
#[inline]
fn container_as_str<'a>(&'a self) -> &'a str {
self.as_slice()
}
#[inline]
fn container_as_str_opt<'a>(&'a self) -> Option<&'a str> {
Some(self.as_slice())
}
#[inline]
fn is_str(_: Option<@str>) -> bool { true }
}
impl<'self> BytesContainer for &'self [u8] {
#[inline]
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
*self
}
}
impl BytesContainer for ~[u8] {
#[inline]
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
self.as_slice()
}
#[inline]
fn container_into_owned_bytes(self) -> ~[u8] {
self
}
}
impl BytesContainer for @[u8] {
#[inline]
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
self.as_slice()
}
}
impl BytesContainer for CString {
#[inline]
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
let s = self.as_bytes();
s.slice_to(s.len()-1)
}
}
#[inline(always)]
fn contains_nul(v: &[u8]) -> bool {
v.iter().any(|&x| x == 0)
}
#[inline(always)]
fn from_utf8_with_replacement(mut v: &[u8]) -> ~str {
// FIXME (#9516): Don't decode utf-8 manually here once we have a good way to do it in str
// This is a truly horrifically bad implementation, done as a functionality stopgap until
// we have a proper utf-8 decoder. I don't really want to write one here.
static REPLACEMENT_CHAR: char = '\uFFFD';
let mut s = str::with_capacity(v.len());
while !v.is_empty() {
let w = str::utf8_char_width(v[0]);
if w == 0u {
s.push_char(REPLACEMENT_CHAR);
v = v.slice_from(1);
} else if v.len() < w || !str::is_utf8(v.slice_to(w)) {
s.push_char(REPLACEMENT_CHAR);
v = v.slice_from(1);
} else {
s.push_str(unsafe { ::cast::transmute(v.slice_to(w)) });
v = v.slice_from(w);
}
}
s
}
// FIXME (#9537): libc::stat should derive Default
#[cfg(target_os = "linux")]
#[cfg(target_os = "android")]
mod stat {
#[allow(missing_doc)];
#[cfg(target_arch = "x86")]
pub mod arch {
use libc;
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,
}
}
}
#[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
}
}
}
#[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],
}
}
}
#[cfg(target_arch = "x86_64")]
pub mod arch {
use libc;
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 {
#[allow(missing_doc)];
#[cfg(target_arch = "x86_64")]
pub mod arch {
use libc;
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 {
#[allow(missing_doc)];
pub mod arch {
use libc;
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],
}
}
}
}
#[cfg(target_os = "win32")]
mod stat {
#[allow(missing_doc)];
pub mod arch {
use libc;
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,
}
}
}
}
#[cfg(test)]
mod tests {
use super::{GenericPath, PosixPath, WindowsPath};
use c_str::ToCStr;
#[test]
fn test_cstring() {
let input = "/foo/bar/baz";
let path: PosixPath = PosixPath::new(input.to_c_str());
assert_eq!(path.as_vec(), input.as_bytes());
let input = "\\foo\\bar\\baz";
let path: WindowsPath = WindowsPath::new(input.to_c_str());
assert_eq!(path.as_str().unwrap(), input.as_slice());
}
}

1422
src/libstd/path/posix.rs Normal file

File diff suppressed because it is too large Load diff

2433
src/libstd/path/windows.rs Normal file

File diff suppressed because it is too large Load diff

View file

@ -60,10 +60,7 @@ pub use num::{Algebraic, Trigonometric, Exponential, Hyperbolic};
pub use num::{Integer, Fractional, Real, RealExt};
pub use num::{Bitwise, BitCount, Bounded};
pub use num::{Primitive, Int, Float, ToStrRadix, ToPrimitive, FromPrimitive};
pub use path::GenericPath;
pub use path::Path;
pub use path::PosixPath;
pub use path::WindowsPath;
pub use path::{GenericPath, Path, PosixPath, WindowsPath};
pub use ptr::RawPtr;
pub use ascii::{Ascii, AsciiCast, OwnedAsciiCast, AsciiStr, ToBytesConsume};
pub use send_str::{SendStr, SendStrOwned, SendStrStatic, IntoSendStr};

View file

@ -627,12 +627,13 @@ pub trait DirectoryInfo : FileSystemInfo {
fn mkdir(&self) {
match ignore_io_error(|| self.stat()) {
Some(_) => {
let path = self.get_path();
io_error::cond.raise(IoError {
kind: PathAlreadyExists,
desc: "Path already exists",
detail:
Some(format!("{} already exists; can't mkdir it",
self.get_path().to_str()))
path.display()))
})
},
None => mkdir(self.get_path())
@ -655,24 +656,27 @@ pub trait DirectoryInfo : FileSystemInfo {
match s.is_dir {
true => rmdir(self.get_path()),
false => {
let path = self.get_path();
let ioerr = IoError {
kind: MismatchedFileTypeForOperation,
desc: "Cannot do rmdir() on a non-directory",
detail: Some(format!(
"{} is a non-directory; can't rmdir it",
self.get_path().to_str()))
path.display()))
};
io_error::cond.raise(ioerr);
}
}
},
None =>
None => {
let path = self.get_path();
io_error::cond.raise(IoError {
kind: PathDoesntExist,
desc: "Path doesn't exist",
detail: Some(format!("{} doesn't exist; can't rmdir it",
self.get_path().to_str()))
path.display()))
})
}
}
}
@ -699,7 +703,7 @@ mod test {
fn file_test_io_smoke_test() {
do run_in_mt_newsched_task {
let message = "it's alright. have a good time";
let filename = &Path("./tmp/file_rt_io_file_test.txt");
let filename = &Path::new("./tmp/file_rt_io_file_test.txt");
{
let mut write_stream = open(filename, Create, ReadWrite).unwrap();
write_stream.write(message.as_bytes());
@ -721,7 +725,7 @@ mod test {
#[test]
fn file_test_io_invalid_path_opened_without_create_should_raise_condition() {
do run_in_mt_newsched_task {
let filename = &Path("./tmp/file_that_does_not_exist.txt");
let filename = &Path::new("./tmp/file_that_does_not_exist.txt");
let mut called = false;
do io_error::cond.trap(|_| {
called = true;
@ -736,7 +740,7 @@ mod test {
#[test]
fn file_test_iounlinking_invalid_path_should_raise_condition() {
do run_in_mt_newsched_task {
let filename = &Path("./tmp/file_another_file_that_does_not_exist.txt");
let filename = &Path::new("./tmp/file_another_file_that_does_not_exist.txt");
let mut called = false;
do io_error::cond.trap(|_| {
called = true;
@ -753,7 +757,7 @@ mod test {
use str;
let message = "ten-four";
let mut read_mem = [0, .. 8];
let filename = &Path("./tmp/file_rt_io_file_test_positional.txt");
let filename = &Path::new("./tmp/file_rt_io_file_test_positional.txt");
{
let mut rw_stream = open(filename, Create, ReadWrite).unwrap();
rw_stream.write(message.as_bytes());
@ -784,7 +788,7 @@ mod test {
let set_cursor = 4 as u64;
let mut tell_pos_pre_read;
let mut tell_pos_post_read;
let filename = &Path("./tmp/file_rt_io_file_test_seeking.txt");
let filename = &Path::new("./tmp/file_rt_io_file_test_seeking.txt");
{
let mut rw_stream = open(filename, Create, ReadWrite).unwrap();
rw_stream.write(message.as_bytes());
@ -813,7 +817,7 @@ mod test {
let final_msg = "foo-the-bar!!";
let seek_idx = 3;
let mut read_mem = [0, .. 13];
let filename = &Path("./tmp/file_rt_io_file_test_seek_and_write.txt");
let filename = &Path::new("./tmp/file_rt_io_file_test_seek_and_write.txt");
{
let mut rw_stream = open(filename, Create, ReadWrite).unwrap();
rw_stream.write(initial_msg.as_bytes());
@ -839,7 +843,7 @@ mod test {
let chunk_two = "asdf";
let chunk_three = "zxcv";
let mut read_mem = [0, .. 4];
let filename = &Path("./tmp/file_rt_io_file_test_seek_shakedown.txt");
let filename = &Path::new("./tmp/file_rt_io_file_test_seek_shakedown.txt");
{
let mut rw_stream = open(filename, Create, ReadWrite).unwrap();
rw_stream.write(initial_msg.as_bytes());
@ -869,7 +873,7 @@ mod test {
#[test]
fn file_test_stat_is_correct_on_is_file() {
do run_in_mt_newsched_task {
let filename = &Path("./tmp/file_stat_correct_on_is_file.txt");
let filename = &Path::new("./tmp/file_stat_correct_on_is_file.txt");
{
let mut fs = open(filename, Create, ReadWrite).unwrap();
let msg = "hw";
@ -887,7 +891,7 @@ mod test {
#[test]
fn file_test_stat_is_correct_on_is_dir() {
do run_in_mt_newsched_task {
let filename = &Path("./tmp/file_stat_correct_on_is_dir");
let filename = &Path::new("./tmp/file_stat_correct_on_is_dir");
mkdir(filename);
let stat_res = match stat(filename) {
Some(s) => s,
@ -901,7 +905,7 @@ mod test {
#[test]
fn file_test_fileinfo_false_when_checking_is_file_on_a_directory() {
do run_in_mt_newsched_task {
let dir = &Path("./tmp/fileinfo_false_on_dir");
let dir = &Path::new("./tmp/fileinfo_false_on_dir");
mkdir(dir);
assert!(dir.is_file() == false);
rmdir(dir);
@ -911,7 +915,7 @@ mod test {
#[test]
fn file_test_fileinfo_check_exists_before_and_after_file_creation() {
do run_in_mt_newsched_task {
let file = &Path("./tmp/fileinfo_check_exists_b_and_a.txt");
let file = &Path::new("./tmp/fileinfo_check_exists_b_and_a.txt");
{
let msg = "foo".as_bytes();
let mut w = file.open_writer(Create);
@ -926,7 +930,7 @@ mod test {
#[test]
fn file_test_directoryinfo_check_exists_before_and_after_mkdir() {
do run_in_mt_newsched_task {
let dir = &Path("./tmp/before_and_after_dir");
let dir = &Path::new("./tmp/before_and_after_dir");
assert!(!dir.exists());
dir.mkdir();
assert!(dir.exists());
@ -940,11 +944,11 @@ mod test {
fn file_test_directoryinfo_readdir() {
use str;
do run_in_mt_newsched_task {
let dir = &Path("./tmp/di_readdir");
let dir = &Path::new("./tmp/di_readdir");
dir.mkdir();
let prefix = "foo";
for n in range(0,3) {
let f = dir.push(format!("{}.txt", n));
let f = dir.join(format!("{}.txt", n));
let mut w = f.open_writer(Create);
let msg_str = (prefix + n.to_str().to_owned()).to_owned();
let msg = msg_str.as_bytes();
@ -955,13 +959,13 @@ mod test {
let mut mem = [0u8, .. 4];
for f in files.iter() {
{
let n = f.filestem();
let n = f.filestem_str();
let mut r = f.open_reader(Open);
r.read(mem);
let read_str = str::from_utf8(mem);
let expected = match n {
Some(n) => prefix+n,
None => fail2!("really shouldn't happen..")
None|Some("") => fail2!("really shouldn't happen.."),
Some(n) => prefix+n
};
assert!(expected == read_str);
}

View file

@ -22,7 +22,7 @@ impl<'self> PathLike for &'self str {
impl PathLike for Path {
fn path_as_str<T>(&self, f: &fn(&str) -> T) -> T {
let s = self.to_str();
let s = self.as_str().unwrap();
f(s)
}
}
@ -35,7 +35,7 @@ mod test {
#[test]
fn path_like_smoke_test() {
let expected = if cfg!(unix) { "/home" } else { "C:\\" };
let path = Path(expected);
let path = Path::new(expected);
path.path_as_str(|p| assert!(p == expected));
path.path_as_str(|p| assert!(p == expected));
}

View file

@ -16,6 +16,7 @@ use container::Container;
use iter::{Iterator, range};
use super::io::net::ip::{SocketAddr, Ipv4Addr, Ipv6Addr};
use vec::{OwnedVector, MutableVector, ImmutableVector};
use path::GenericPath;
use rt::sched::Scheduler;
use unstable::{run_in_bare_thread};
use rt::thread::Thread;
@ -346,7 +347,6 @@ it is running in and assigns a port range based on it.
fn base_port() -> uint {
use os;
use str::StrSlice;
use to_str::ToStr;
use vec::ImmutableVector;
let base = 9600u;
@ -363,12 +363,14 @@ fn base_port() -> uint {
("dist", base + range * 8)
];
let path = os::getcwd().to_str();
// FIXME (#9639): This needs to handle non-utf8 paths
let path = os::getcwd();
let path_s = path.as_str().unwrap();
let mut final_base = base;
for &(dir, base) in bases.iter() {
if path.contains(dir) {
if path_s.contains(dir) {
final_base = base;
break;
}

View file

@ -391,7 +391,7 @@ mod test {
let read_mem = vec::from_elem(read_buf_len, 0u8);
let read_buf = slice_to_uv_buf(read_mem);
let read_buf_ptr: *Buf = &read_buf;
let p = Path(path_str);
let p = Path::new(path_str);
let open_req = FsRequest::new();
do open_req.open(&loop_, &p, create_flags as int, mode as int)
|req, uverr| {
@ -405,7 +405,7 @@ mod test {
assert!(uverr.is_none());
let loop_ = req.get_loop();
let open_req = FsRequest::new();
do open_req.open(&loop_, &Path(path_str), read_flags as int,0)
do open_req.open(&loop_, &Path::new(path_str), read_flags as int,0)
|req, uverr| {
assert!(uverr.is_none());
let loop_ = req.get_loop();
@ -431,7 +431,7 @@ mod test {
assert!(uverr.is_none());
let loop_ = &req.get_loop();
let unlink_req = FsRequest::new();
do unlink_req.unlink(loop_, &Path(path_str))
do unlink_req.unlink(loop_, &Path::new(path_str))
|_,uverr| {
assert!(uverr.is_none());
};
@ -465,7 +465,7 @@ mod test {
let write_buf = slice_to_uv_buf(write_val);
// open/create
let open_req = FsRequest::new();
let result = open_req.open_sync(&loop_, &Path(path_str),
let result = open_req.open_sync(&loop_, &Path::new(path_str),
create_flags as int, mode as int);
assert!(result.is_ok());
let fd = result.unwrap();
@ -479,7 +479,7 @@ mod test {
assert!(result.is_ok());
// re-open
let open_req = FsRequest::new();
let result = open_req.open_sync(&loop_, &Path(path_str),
let result = open_req.open_sync(&loop_, &Path::new(path_str),
read_flags as int,0);
assert!(result.is_ok());
let len = 1028;
@ -503,7 +503,7 @@ mod test {
assert!(result.is_ok());
// unlink
let unlink_req = FsRequest::new();
let result = unlink_req.unlink_sync(&loop_, &Path(path_str));
let result = unlink_req.unlink_sync(&loop_, &Path::new(path_str));
assert!(result.is_ok());
} else { fail2!("nread was 0.. wudn't expectin' that."); }
loop_.close();

View file

@ -18,6 +18,7 @@ use ops::Drop;
use option::*;
use ptr;
use str;
use str::Str;
use result::*;
use rt::io::IoError;
use rt::io::net::ip::{SocketAddr, IpAddr};
@ -34,7 +35,7 @@ use rt::uv::idle::IdleWatcher;
use rt::uv::net::{UvIpv4SocketAddr, UvIpv6SocketAddr, accum_sockaddrs};
use rt::uv::addrinfo::GetAddrInfoRequest;
use unstable::sync::Exclusive;
use path::Path;
use path::{GenericPath, Path};
use super::super::io::support::PathLike;
use libc::{lseek, off_t, O_CREAT, O_APPEND, O_TRUNC, O_RDWR, O_RDONLY, O_WRONLY,
S_IRUSR, S_IWUSR, S_IRWXU};
@ -631,7 +632,7 @@ impl IoFactory for UvIoFactory {
None => {
let stat = req.get_stat();
Ok(FileStat {
path: Path(path_str),
path: Path::new(path_str.as_slice()),
is_file: stat.is_file(),
is_dir: stat.is_dir(),
size: stat.st_size,
@ -720,7 +721,9 @@ impl IoFactory for UvIoFactory {
let rel_paths = req.get_paths();
let mut paths = ~[];
for r in rel_paths.iter() {
paths.push(Path(path_str+"/"+*r));
let mut p = Path::new(path_str.as_slice());
p.push(r.as_slice());
paths.push(p);
}
Ok(paths)
},
@ -2177,20 +2180,20 @@ fn file_test_uvio_full_simple_impl() {
{
let create_fm = Create;
let create_fa = ReadWrite;
let mut fd = (*io).fs_open(&Path(path), create_fm, create_fa).unwrap();
let mut fd = (*io).fs_open(&Path::new(path), create_fm, create_fa).unwrap();
let write_buf = write_val.as_bytes();
fd.write(write_buf);
}
{
let ro_fm = Open;
let ro_fa = Read;
let mut fd = (*io).fs_open(&Path(path), ro_fm, ro_fa).unwrap();
let mut fd = (*io).fs_open(&Path::new(path), ro_fm, ro_fa).unwrap();
let mut read_vec = [0, .. 1028];
let nread = fd.read(read_vec).unwrap();
let read_val = str::from_utf8(read_vec.slice(0, nread as uint));
assert!(read_val == write_val.to_owned());
}
(*io).fs_unlink(&Path(path));
(*io).fs_unlink(&Path::new(path));
}
}

View file

@ -578,8 +578,8 @@ mod tests {
let mut prog = run_pwd(None);
let output = str::from_utf8(prog.finish_with_output().output);
let parent_dir = os::getcwd().normalize();
let child_dir = Path(output.trim()).normalize();
let parent_dir = os::getcwd();
let child_dir = Path::new(output.trim());
let parent_stat = parent_dir.stat().unwrap();
let child_stat = child_dir.stat().unwrap();
@ -592,11 +592,11 @@ mod tests {
fn test_change_working_directory() {
// test changing to the parent of os::getcwd() because we know
// the path exists (and os::getcwd() is not expected to be root)
let parent_dir = os::getcwd().dir_path().normalize();
let parent_dir = os::getcwd().dir_path();
let mut prog = run_pwd(Some(&parent_dir));
let output = str::from_utf8(prog.finish_with_output().output);
let child_dir = Path(output.trim()).normalize();
let child_dir = Path::new(output.trim());
let parent_stat = parent_dir.stat().unwrap();
let child_stat = child_dir.stat().unwrap();

View file

@ -121,7 +121,7 @@ mod test {
fn test_errors_do_not_crash() {
// Open /dev/null as a library to get an error, and make sure
// that only causes an error, and not a crash.
let path = GenericPath::from_str("/dev/null");
let path = GenericPath::new("/dev/null");
match DynamicLibrary::open(Some(&path)) {
Err(_) => {}
Ok(_) => fail2!("Successfully opened the empty library.")
@ -225,7 +225,7 @@ pub mod dl {
pub unsafe fn open_external(filename: &path::Path) -> *libc::c_void {
#[fixed_stack_segment]; #[inline(never)];
do os::win32::as_utf16_p(filename.to_str()) |raw_name| {
do os::win32::as_utf16_p(filename.as_str().unwrap()) |raw_name| {
LoadLibraryW(raw_name)
}
}