From f03b22a65cfbe1e257ac01166b8cefec03f2ec97 Mon Sep 17 00:00:00 2001 From: Joseph Crail Date: Fri, 31 Jul 2015 13:59:05 -0400 Subject: [PATCH 1/5] Stabilize several PathExt methods. --- src/chmod/chmod.rs | 15 ++++-- src/chroot/chroot.rs | 7 +-- src/common/filesystem.rs | 42 +++++++++++++++++ src/cp/cp.rs | 11 +++-- src/cut/cut.rs | 11 +++-- src/ln/ln.rs | 19 +++++--- src/mkdir/mkdir.rs | 12 +++-- src/mv/mv.rs | 23 ++++++---- src/realpath/realpath.rs | 4 +- src/rm/rm.rs | 12 +++-- src/stdbuf/stdbuf.rs | 7 ++- src/touch/touch.rs | 11 +++-- src/wc/wc.rs | 10 ++-- test/mv.rs | 98 ++++++++++++++++++++-------------------- test/rm.rs | 30 ++++++------ test/unlink.rs | 6 +-- 16 files changed, 200 insertions(+), 118 deletions(-) create mode 100644 src/common/filesystem.rs diff --git a/src/chmod/chmod.rs b/src/chmod/chmod.rs index f41c5bba1..921ab8b47 100644 --- a/src/chmod/chmod.rs +++ b/src/chmod/chmod.rs @@ -1,5 +1,5 @@ #![crate_name = "chmod"] -#![feature(fs_walk, path_ext)] +#![feature(fs_walk)] /* * This file is part of the uutils coreutils package. @@ -22,7 +22,7 @@ extern crate regex_syntax; use getopts::Options; use regex::Regex; use std::ffi::CString; -use std::fs::{self, PathExt}; +use std::fs; use std::io::{Error, Write}; use std::mem; use std::path::Path; @@ -31,6 +31,11 @@ use std::path::Path; #[macro_use] mod util; +#[path = "../common/filesystem.rs"] +mod filesystem; + +use filesystem::UUPathExt; + const NAME: &'static str = "chmod"; const VERSION: &'static str = "1.0.0"; @@ -149,8 +154,8 @@ fn chmod(files: Vec, changes: bool, quiet: bool, verbose: bool, preserve for filename in files.iter() { let filename = &filename[..]; let file = Path::new(filename); - if file.exists() { - if file.is_dir() { + if file.uu_exists() { + if file.uu_is_dir() { if !preserve_root || filename != "/" { if recursive { let walk_dir = match fs::walk_dir(&file) { @@ -273,7 +278,7 @@ fn chmod_file(file: &Path, name: &str, changes: bool, quiet: bool, verbose: bool 'w' => rwx |= 0o002, 'x' => rwx |= 0o001, 'X' => { - if file.is_dir() || (fperm & 0o0111) != 0 { + if file.uu_is_dir() || (fperm & 0o0111) != 0 { rwx |= 0o001; } } diff --git a/src/chroot/chroot.rs b/src/chroot/chroot.rs index ce6c72400..6ea2699df 100644 --- a/src/chroot/chroot.rs +++ b/src/chroot/chroot.rs @@ -1,5 +1,4 @@ #![crate_name = "chroot"] -#![feature(path_ext)] /* * This file is part of the uutils coreutils package. @@ -17,7 +16,6 @@ use c_types::{get_pw_from_args, get_group}; use getopts::Options; use libc::funcs::posix88::unistd::{setgid, setuid}; use std::ffi::CString; -use std::fs::PathExt; use std::io::{Error, Write}; use std::iter::FromIterator; use std::path::Path; @@ -25,6 +23,9 @@ use std::process::Command; #[path = "../common/util.rs"] #[macro_use] mod util; #[path = "../common/c_types.rs"] mod c_types; +#[path = "../common/filesystem.rs"] mod filesystem; + +use filesystem::UUPathExt; extern { fn chroot(path: *const libc::c_char) -> libc::c_int; @@ -78,7 +79,7 @@ pub fn uumain(args: Vec) -> i32 { let user_shell = std::env::var("SHELL"); let newroot = Path::new(&matches.free[0][..]); - if !newroot.is_dir() { + if !newroot.uu_is_dir() { crash!(1, "cannot change root directory to `{}`: no such directory", newroot.display()); } diff --git a/src/common/filesystem.rs b/src/common/filesystem.rs new file mode 100644 index 000000000..44f192222 --- /dev/null +++ b/src/common/filesystem.rs @@ -0,0 +1,42 @@ +/* + * This file is part of the uutils coreutils package. + * + * (c) Joseph Crail + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +// Based on the pattern using by Cargo, I created a shim over the +// standard PathExt trait, so that the unstable path methods could +// be backported to stable (<= 1.1). This will likely be dropped +// when the path trait stabilizes. + +use std::fs; +use std::io; +use std::path::Path; + +pub trait UUPathExt { + fn uu_exists(&self) -> bool; + fn uu_is_file(&self) -> bool; + fn uu_is_dir(&self) -> bool; + fn uu_metadata(&self) -> io::Result; +} + +impl UUPathExt for Path { + fn uu_exists(&self) -> bool { + fs::metadata(self).is_ok() + } + + fn uu_is_file(&self) -> bool { + fs::metadata(self).map(|m| m.is_file()).unwrap_or(false) + } + + fn uu_is_dir(&self) -> bool { + fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false) + } + + fn uu_metadata(&self) -> io::Result { + fs::metadata(self) + } +} diff --git a/src/cp/cp.rs b/src/cp/cp.rs index 7ae2cf35b..b3d7a5184 100644 --- a/src/cp/cp.rs +++ b/src/cp/cp.rs @@ -13,7 +13,7 @@ extern crate getopts; use getopts::Options; -use std::fs::{self, PathExt}; +use std::fs; use std::io::{ErrorKind, Result, Write}; use std::path::Path; @@ -21,6 +21,11 @@ use std::path::Path; #[macro_use] mod util; +#[path = "../common/filesystem.rs"] +mod filesystem; + +use filesystem::UUPathExt; + #[derive(Clone, Eq, PartialEq)] pub enum Mode { Copy, @@ -119,7 +124,7 @@ fn copy(matches: getopts::Matches) { panic!(); } } else { - if !fs::metadata(dest).unwrap().is_dir() { + if !dest.uu_is_dir() { show_error!("TARGET must be a directory"); panic!(); } @@ -127,7 +132,7 @@ fn copy(matches: getopts::Matches) { for src in sources.iter() { let source = Path::new(&src); - if !fs::metadata(source).unwrap().is_file() { + if !source.uu_is_file() { show_error!("\"{}\" is not a file", source.display()); continue; } diff --git a/src/cut/cut.rs b/src/cut/cut.rs index 0c7914265..1381e115b 100644 --- a/src/cut/cut.rs +++ b/src/cut/cut.rs @@ -1,5 +1,4 @@ #![crate_name = "cut"] -#![feature(path_ext)] /* * This file is part of the uutils coreutils package. @@ -13,7 +12,7 @@ extern crate getopts; extern crate libc; -use std::fs::{File, PathExt}; +use std::fs::File; use std::io::{stdout, stdin, BufRead, BufReader, Read, Stdout, Write}; use std::path::Path; @@ -23,6 +22,12 @@ use searcher::Searcher; #[path = "../common/util.rs"] #[macro_use] mod util; + +#[path = "../common/filesystem.rs"] +mod filesystem; + +use filesystem::UUPathExt; + mod buffer; mod ranges; mod searcher; @@ -379,7 +384,7 @@ fn cut_files(mut filenames: Vec, mode: Mode) -> i32 { } else { let path = Path::new(&filename[..]); - if ! path.exists() { + if !path.uu_exists() { show_error!("{}: No such file or directory", filename); continue } diff --git a/src/ln/ln.rs b/src/ln/ln.rs index 317d0090c..ac9698def 100644 --- a/src/ln/ln.rs +++ b/src/ln/ln.rs @@ -1,5 +1,5 @@ #![crate_name = "ln"] -#![feature(path_ext, slice_patterns, str_char)] +#![feature(slice_patterns, str_char)] /* * This file is part of the uutils coreutils package. @@ -12,7 +12,7 @@ extern crate getopts; -use std::fs::{self, PathExt}; +use std::fs; use std::io::{BufRead, BufReader, Result, stdin, Write}; #[cfg(unix)] use std::os::unix::fs::symlink as symlink_file; #[cfg(windows)] use std::os::windows::fs::symlink_file; @@ -22,6 +22,11 @@ use std::path::{Path, PathBuf}; #[macro_use] mod util; +#[path="../common/filesystem.rs"] +mod filesystem; + +use filesystem::UUPathExt; + static NAME: &'static str = "ln"; static VERSION: &'static str = "1.0.0"; @@ -201,7 +206,7 @@ fn exec(files: &[PathBuf], settings: &Settings) -> i32 { } fn link_files_in_dir(files: &[PathBuf], target_dir: &PathBuf, settings: &Settings) -> i32 { - if !target_dir.is_dir() { + if !target_dir.uu_is_dir() { show_error!("target '{}' is not a directory", target_dir.display()); return 1; } @@ -233,13 +238,13 @@ fn link_files_in_dir(files: &[PathBuf], target_dir: &PathBuf, settings: &Setting fn link(src: &PathBuf, dst: &PathBuf, settings: &Settings) -> Result<()> { let mut backup_path = None; - if dst.is_dir() { + if dst.uu_is_dir() { if settings.no_target_dir { try!(fs::remove_dir(dst)); } } - if is_symlink(dst) || dst.exists() { + if is_symlink(dst) || dst.uu_exists() { match settings.overwrite { OverwriteMode::NoClobber => {}, OverwriteMode::Interactive => { @@ -302,7 +307,7 @@ fn numbered_backup_path(path: &PathBuf) -> PathBuf { let mut i: u64 = 1; loop { let new_path = simple_backup_path(path, &format!(".~{}~", i)); - if !new_path.exists() { + if !new_path.uu_exists() { return new_path; } i += 1; @@ -311,7 +316,7 @@ fn numbered_backup_path(path: &PathBuf) -> PathBuf { fn existing_backup_path(path: &PathBuf, suffix: &String) -> PathBuf { let test_path = simple_backup_path(path, &".~1~".to_string()); - if test_path.exists() { + if test_path.uu_exists() { return numbered_backup_path(path); } simple_backup_path(path, suffix) diff --git a/src/mkdir/mkdir.rs b/src/mkdir/mkdir.rs index 3cffc1f0d..b68fe0d09 100644 --- a/src/mkdir/mkdir.rs +++ b/src/mkdir/mkdir.rs @@ -1,5 +1,4 @@ #![crate_name = "mkdir"] -#![feature(path_ext)] /* * This file is part of the uutils coreutils package. @@ -14,7 +13,7 @@ extern crate getopts; extern crate libc; use std::ffi::CString; -use std::fs::{self, PathExt}; +use std::fs; use std::io::{Error, Write}; use std::path::{Path, PathBuf}; @@ -22,6 +21,11 @@ use std::path::{Path, PathBuf}; #[macro_use] mod util; +#[path = "../common/filesystem.rs"] +mod filesystem; + +use filesystem::UUPathExt; + static NAME: &'static str = "mkdir"; static VERSION: &'static str = "1.0.0"; @@ -102,7 +106,7 @@ fn exec(dirs: Vec, recursive: bool, mode: u16, verbose: bool) -> i32 { } else { match path.parent() { Some(parent) => { - if parent != empty && !parent.exists() { + if parent != empty && !parent.uu_exists() { show_info!("cannot create directory '{}': No such file or directory", path.display()); status = 1; } else { @@ -122,7 +126,7 @@ fn exec(dirs: Vec, recursive: bool, mode: u16, verbose: bool) -> i32 { * Wrapper to catch errors, return 1 if failed */ fn mkdir(path: &Path, mode: u16, verbose: bool) -> i32 { - if path.exists() { + if path.uu_exists() { show_info!("cannot create directory '{}': File exists", path.display()); return 1; } diff --git a/src/mv/mv.rs b/src/mv/mv.rs index 480db0b46..e4c67e0be 100644 --- a/src/mv/mv.rs +++ b/src/mv/mv.rs @@ -1,5 +1,5 @@ #![crate_name = "mv"] -#![feature(path_ext, slice_extras, slice_patterns, str_char)] +#![feature(slice_extras, slice_patterns, str_char)] #![allow(deprecated)] /* @@ -15,7 +15,7 @@ extern crate getopts; extern crate libc; -use std::fs::{self, PathExt}; +use std::fs; use std::io::{BufRead, BufReader, Result, stdin, Write}; use std::os::unix::fs::MetadataExt; use std::path::{Path, PathBuf}; @@ -24,6 +24,11 @@ use std::path::{Path, PathBuf}; #[macro_use] mod util; +#[path = "../common/filesystem.rs"] +mod filesystem; + +use filesystem::UUPathExt; + static NAME: &'static str = "mv"; static VERSION: &'static str = "0.0.1"; @@ -195,14 +200,14 @@ fn exec(files: &[PathBuf], b: Behaviour) -> i32 { return 1; }, [ref source, ref target] => { - if !source.exists() { + if !source.uu_exists() { show_error!("cannot stat ‘{}’: No such file or directory", source.display()); return 1; } - if target.is_dir() { + if target.uu_is_dir() { if b.no_target_dir { - if !source.is_dir() { + if !source.uu_is_dir() { show_error!("cannot overwrite directory ‘{}’ with non-directory", target.display()); return 1; @@ -242,7 +247,7 @@ fn exec(files: &[PathBuf], b: Behaviour) -> i32 { } fn move_files_into_dir(files: &[PathBuf], target_dir: &PathBuf, b: &Behaviour) -> i32 { - if !target_dir.is_dir() { + if !target_dir.uu_is_dir() { show_error!("target ‘{}’ is not a directory", target_dir.display()); return 1; } @@ -275,7 +280,7 @@ fn move_files_into_dir(files: &[PathBuf], target_dir: &PathBuf, b: &Behaviour) - fn rename(from: &PathBuf, to: &PathBuf, b: &Behaviour) -> Result<()> { let mut backup_path = None; - if to.exists() { + if to.uu_exists() { match b.overwrite { OverwriteMode::NoClobber => return Ok(()), OverwriteMode::Interactive => { @@ -337,7 +342,7 @@ fn numbered_backup_path(path: &PathBuf) -> PathBuf { let mut i: u64 = 1; loop { let new_path = simple_backup_path(path, &format!(".~{}~", i)); - if !new_path.exists() { + if !new_path.uu_exists() { return new_path; } i = i + 1; @@ -346,7 +351,7 @@ fn numbered_backup_path(path: &PathBuf) -> PathBuf { fn existing_backup_path(path: &PathBuf, suffix: &String) -> PathBuf { let test_path = simple_backup_path(path, &".~1~".to_string()); - if test_path.exists() { + if test_path.uu_exists() { return numbered_backup_path(path); } simple_backup_path(path, suffix) diff --git a/src/realpath/realpath.rs b/src/realpath/realpath.rs index 3d454edd2..c0db15f09 100644 --- a/src/realpath/realpath.rs +++ b/src/realpath/realpath.rs @@ -84,12 +84,12 @@ fn resolve_path(path: &str, strip: bool, zero: bool, quiet: bool) -> bool { if !quiet { show_error!("Too many symbolic links: {}", path) }; return false } - match result.as_path().metadata() { + match fs::metadata(result.as_path()) { Err(_) => break, Ok(ref m) if !m.file_type().is_symlink() => break, Ok(_) => { links_left -= 1; - match result.as_path().read_link() { + match fs::read_link(result.as_path()) { Ok(x) => { result.pop(); result.push(x.as_path()); diff --git a/src/rm/rm.rs b/src/rm/rm.rs index 5a6b16b78..f8a9c8d44 100644 --- a/src/rm/rm.rs +++ b/src/rm/rm.rs @@ -1,5 +1,4 @@ #![crate_name = "rm"] -#![feature(path_ext)] /* * This file is part of the uutils coreutils package. @@ -14,7 +13,7 @@ extern crate getopts; extern crate libc; use std::collections::VecDeque; -use std::fs::{self, PathExt}; +use std::fs; use std::io::{stdin, stderr, BufRead, Write}; use std::ops::BitOr; use std::path::{Path, PathBuf}; @@ -23,6 +22,11 @@ use std::path::{Path, PathBuf}; #[macro_use] mod util; +#[path = "../common/filesystem.rs"] +mod filesystem; + +use filesystem::UUPathExt; + #[derive(Eq, PartialEq, Clone, Copy)] enum InteractiveMode { InteractiveNone, @@ -131,8 +135,8 @@ fn remove(files: Vec, force: bool, interactive: InteractiveMode, one_fs: for filename in files.iter() { let filename = &filename[..]; let file = Path::new(filename); - if file.exists() { - if file.is_dir() { + if file.uu_exists() { + if file.uu_is_dir() { if recursive && (filename != "/" || !preserve_root) { if interactive != InteractiveMode::InteractiveAlways { match fs::remove_dir_all(file) { diff --git a/src/stdbuf/stdbuf.rs b/src/stdbuf/stdbuf.rs index c2a960dd1..8db5685b5 100644 --- a/src/stdbuf/stdbuf.rs +++ b/src/stdbuf/stdbuf.rs @@ -25,6 +25,11 @@ use std::process::Command; #[macro_use] mod util; +#[path = "../common/filesystem.rs"] +mod filesystem; + +use filesystem::UUPathExt; + static NAME: &'static str = "stdbuf"; static VERSION: &'static str = "1.0.0"; static LIBSTDBUF: &'static str = "libstdbuf"; @@ -204,7 +209,7 @@ fn get_preload_env() -> (String, String) { // First search for library in directory of executable. let mut path = exe_path().unwrap_or_else(|_| crash!(1, "Impossible to fetch the path of this executable.")); path.push(libstdbuf.clone()); - if path.exists() { + if path.uu_exists() { match path.as_os_str().to_str() { Some(s) => { return (preload.to_string(), s.to_string()); }, None => crash!(1, "Error while converting path.") diff --git a/src/touch/touch.rs b/src/touch/touch.rs index 6d8806165..05d2157c2 100644 --- a/src/touch/touch.rs +++ b/src/touch/touch.rs @@ -1,5 +1,5 @@ #![crate_name = "touch"] -#![feature(fs_time, path_ext)] +#![feature(fs_time)] /* * This file is part of the uutils coreutils package. @@ -18,7 +18,7 @@ use libc::types::os::arch::c95::c_char; use libc::types::os::arch::posix01::stat as stat_t; use libc::funcs::posix88::stat_::stat as c_stat; use libc::funcs::posix01::stat_::lstat as c_lstat; -use std::fs::{set_file_times, File, PathExt}; +use std::fs::{set_file_times, File}; use std::io::{Error, Write}; use std::mem::uninitialized; use std::path::Path; @@ -27,6 +27,11 @@ use std::path::Path; #[macro_use] mod util; +#[path = "../common/filesystem.rs"] +mod filesystem; + +use filesystem::UUPathExt; + static NAME: &'static str = "touch"; static VERSION: &'static str = "1.0.0"; @@ -95,7 +100,7 @@ pub fn uumain(args: Vec) -> i32 { for filename in matches.free.iter() { let path = &filename[..]; - if ! Path::new(path).exists() { + if !Path::new(path).uu_exists() { // no-dereference included here for compatibility if matches.opts_present(&["no-create".to_string(), "no-dereference".to_string()]) { continue; diff --git a/src/wc/wc.rs b/src/wc/wc.rs index 11e23403e..a4215b88c 100644 --- a/src/wc/wc.rs +++ b/src/wc/wc.rs @@ -1,5 +1,4 @@ #![crate_name = "wc"] -#![feature(path_ext)] /* * This file is part of the uutils coreutils package. @@ -15,7 +14,7 @@ extern crate libc; use getopts::{Matches, Options}; use std::ascii::AsciiExt; -use std::fs::{File, PathExt}; +use std::fs::File; use std::io::{stdin, BufRead, BufReader, Read, Write}; use std::path::Path; use std::result::Result as StdResult; @@ -25,6 +24,11 @@ use std::str::from_utf8; #[macro_use] mod util; +#[path = "../common/filesystem.rs"] +mod filesystem; + +use filesystem::UUPathExt; + struct Settings { show_bytes: bool, show_chars: bool, @@ -264,7 +268,7 @@ fn open(path: &str) -> StdResult>, i32> { } let fpath = Path::new(path); - if fpath.is_dir() { + if fpath.uu_is_dir() { show_info!("{}: is a directory", path); } match File::open(&fpath) { diff --git a/test/mv.rs b/test/mv.rs index d99ff19ae..74f2943b9 100644 --- a/test/mv.rs +++ b/test/mv.rs @@ -1,9 +1,9 @@ -#![feature(fs_time, path_ext)] +#![feature(fs_time)] extern crate libc; extern crate time; -use std::fs::{self, PathExt}; +use std::fs; use std::path::Path; use std::process::Command; use util::*; @@ -25,7 +25,7 @@ fn test_mv_rename_dir() { assert_empty_stderr!(result); assert!(result.success); - assert!(Path::new(dir2).is_dir()); + assert!(dir_exists(dir2)); } #[test] @@ -39,7 +39,7 @@ fn test_mv_rename_file() { assert_empty_stderr!(result); assert!(result.success); - assert!(Path::new(file2).is_file()); + assert!(file_exists(file2)); } #[test] @@ -54,7 +54,7 @@ fn test_mv_move_file_into_dir() { assert_empty_stderr!(result); assert!(result.success); - assert!(Path::new(&format!("{}/{}", dir, file)).is_file()); + assert!(file_exists(&format!("{}/{}", dir, file))); } #[test] @@ -70,13 +70,13 @@ fn test_mv_strip_slashes() { let result = run(Command::new(PROGNAME).arg(&source).arg(dir)); assert!(!result.success); - assert!(!Path::new(&format!("{}/{}", dir, file)).is_file()); + assert!(!file_exists(&format!("{}/{}", dir, file))); let result = run(Command::new(PROGNAME).arg("--strip-trailing-slashes").arg(source).arg(dir)); assert_empty_stderr!(result); assert!(result.success); - assert!(Path::new(&format!("{}/{}", dir, file)).is_file()); + assert!(file_exists(&format!("{}/{}", dir, file))); } #[test] @@ -93,8 +93,8 @@ fn test_mv_multiple_files() { assert_empty_stderr!(result); assert!(result.success); - assert!(Path::new(&format!("{}/{}", target_dir, file_a)).is_file()); - assert!(Path::new(&format!("{}/{}", target_dir, file_b)).is_file()); + assert!(file_exists(&format!("{}/{}", target_dir, file_a))); + assert!(file_exists(&format!("{}/{}", target_dir, file_b))); } #[test] @@ -111,8 +111,8 @@ fn test_mv_multiple_folders() { assert_empty_stderr!(result); assert!(result.success); - assert!(Path::new(&format!("{}/{}", target_dir, dir_a)).is_dir()); - assert!(Path::new(&format!("{}/{}", target_dir, dir_b)).is_dir()); + assert!(dir_exists(&format!("{}/{}", target_dir, dir_a))); + assert!(dir_exists(&format!("{}/{}", target_dir, dir_b))); } #[test] @@ -129,8 +129,8 @@ fn test_mv_interactive() { assert_empty_stderr!(result1); assert!(result1.success); - assert!(Path::new(file_a).is_file()); - assert!(Path::new(file_b).is_file()); + assert!(file_exists(file_a)); + assert!(file_exists(file_b)); let result2 = run_piped_stdin(Command::new(PROGNAME).arg("-i").arg(file_a).arg(file_b), b"Yesh"); @@ -138,8 +138,8 @@ fn test_mv_interactive() { assert_empty_stderr!(result2); assert!(result2.success); - assert!(!Path::new(file_a).is_file()); - assert!(Path::new(file_b).is_file()); + assert!(!file_exists(file_a)); + assert!(file_exists(file_b)); } #[test] @@ -154,8 +154,8 @@ fn test_mv_no_clobber() { assert_empty_stderr!(result); assert!(result.success); - assert!(Path::new(file_a).is_file()); - assert!(Path::new(file_b).is_file()); + assert!(file_exists(file_a)); + assert!(file_exists(file_b)); } #[test] @@ -170,8 +170,8 @@ fn test_mv_replace_file() { assert_empty_stderr!(result); assert!(result.success); - assert!(!Path::new(file_a).is_file()); - assert!(Path::new(file_b).is_file()); + assert!(!file_exists(file_a)); + assert!(file_exists(file_b)); } #[test] @@ -186,8 +186,8 @@ fn test_mv_force_replace_file() { assert_empty_stderr!(result); assert!(result.success); - assert!(!Path::new(file_a).is_file()); - assert!(Path::new(file_b).is_file()); + assert!(!file_exists(file_a)); + assert!(file_exists(file_b)); } #[test] @@ -202,9 +202,9 @@ fn test_mv_simple_backup() { assert_empty_stderr!(result); assert!(result.success); - assert!(!Path::new(file_a).is_file()); - assert!(Path::new(file_b).is_file()); - assert!(Path::new(&format!("{}~", file_b)).is_file()); + assert!(!file_exists(file_a)); + assert!(file_exists(file_b)); + assert!(file_exists(&format!("{}~", file_b))); } #[test] @@ -222,9 +222,9 @@ fn test_mv_custom_backup_suffix() { assert_empty_stderr!(result); assert!(result.success); - assert!(!Path::new(file_a).is_file()); - assert!(Path::new(file_b).is_file()); - assert!(Path::new(&format!("{}{}", file_b, suffix)).is_file()); + assert!(!file_exists(file_a)); + assert!(file_exists(file_b)); + assert!(file_exists(&format!("{}{}", file_b, suffix))); } #[test] @@ -239,9 +239,9 @@ fn test_mv_backup_numbering() { assert_empty_stderr!(result); assert!(result.success); - assert!(!Path::new(file_a).is_file()); - assert!(Path::new(file_b).is_file()); - assert!(Path::new(&format!("{}.~1~", file_b)).is_file()); + assert!(!file_exists(file_a)); + assert!(file_exists(file_b)); + assert!(file_exists(&format!("{}.~1~", file_b))); } #[test] @@ -259,10 +259,10 @@ fn test_mv_existing_backup() { assert_empty_stderr!(result); assert!(result.success); - assert!(!Path::new(file_a).is_file()); - assert!(Path::new(file_b).is_file()); - assert!(Path::new(file_b_backup).is_file()); - assert!(Path::new(resulting_backup).is_file()); + assert!(!file_exists(file_a)); + assert!(file_exists(file_b)); + assert!(file_exists(file_b_backup)); + assert!(file_exists(resulting_backup)); } #[test] @@ -281,16 +281,16 @@ fn test_mv_update_option() { assert_empty_stderr!(result1); assert!(result1.success); - assert!(Path::new(file_a).is_file()); - assert!(Path::new(file_b).is_file()); + assert!(file_exists(file_a)); + assert!(file_exists(file_b)); let result2 = run(Command::new(PROGNAME).arg("--update").arg(file_b).arg(file_a)); assert_empty_stderr!(result2); assert!(result2.success); - assert!(Path::new(file_a).is_file()); - assert!(!Path::new(file_b).is_file()); + assert!(file_exists(file_a)); + assert!(!file_exists(file_b)); } #[test] @@ -307,10 +307,10 @@ fn test_mv_target_dir() { assert_empty_stderr!(result); assert!(result.success); - assert!(!Path::new(file_a).is_file()); - assert!(!Path::new(file_b).is_file()); - assert!(Path::new(&format!("{}/{}", dir, file_a)).is_file()); - assert!(Path::new(&format!("{}/{}", dir, file_b)).is_file()); + assert!(!file_exists(file_a)); + assert!(!file_exists(file_b)); + assert!(file_exists(&format!("{}/{}", dir, file_a))); + assert!(file_exists(&format!("{}/{}", dir, file_b))); } #[test] @@ -325,8 +325,8 @@ fn test_mv_overwrite_dir() { assert_empty_stderr!(result); assert!(result.success); - assert!(!Path::new(dir_a).is_dir()); - assert!(Path::new(dir_b).is_dir()); + assert!(!dir_exists(dir_a)); + assert!(dir_exists(dir_b)); } #[test] @@ -350,8 +350,8 @@ fn test_mv_overwrite_nonempty_dir() { assert!(result.stdout.len() == 0); assert!(!result.success); - assert!(Path::new(dir_a).is_dir()); - assert!(Path::new(dir_b).is_dir()); + assert!(dir_exists(dir_a)); + assert!(dir_exists(dir_b)); } #[test] @@ -368,9 +368,9 @@ fn test_mv_backup_dir() { format!("‘{}’ -> ‘{}’ (backup: ‘{}~’)\n", dir_a, dir_b, dir_b)); assert!(result.success); - assert!(!Path::new(dir_a).is_dir()); - assert!(Path::new(dir_b).is_dir()); - assert!(Path::new(&format!("{}~", dir_b)).is_dir()); + assert!(!dir_exists(dir_a)); + assert!(dir_exists(dir_b)); + assert!(dir_exists(&format!("{}~", dir_b))); } #[test] diff --git a/test/rm.rs b/test/rm.rs index 0a75aec2c..5eede78b9 100644 --- a/test/rm.rs +++ b/test/rm.rs @@ -1,9 +1,5 @@ -#![feature(path_ext)] - extern crate libc; -use std::fs::PathExt; -use std::path::Path; use std::process::Command; use util::*; @@ -23,7 +19,7 @@ fn test_rm_one_file() { assert_empty_stderr!(result); assert!(result.success); - assert!(!Path::new(file).exists()); + assert!(!file_exists(file)); } #[test] @@ -38,8 +34,8 @@ fn test_rm_multiple_files() { assert_empty_stderr!(result); assert!(result.success); - assert!(!Path::new(file_a).exists()); - assert!(!Path::new(file_b).exists()); + assert!(!file_exists(file_a)); + assert!(!file_exists(file_b)); } #[test] @@ -54,15 +50,15 @@ fn test_rm_interactive() { assert!(result1.success); - assert!(Path::new(file_a).exists()); - assert!(Path::new(file_b).exists()); + assert!(file_exists(file_a)); + assert!(file_exists(file_b)); let result2 = run_piped_stdin(Command::new(PROGNAME).arg("-i").arg(file_a).arg(file_b), b"Yesh"); assert!(result2.success); - assert!(!Path::new(file_a).exists()); - assert!(Path::new(file_b).exists()); + assert!(!file_exists(file_a)); + assert!(file_exists(file_b)); } #[test] @@ -74,8 +70,8 @@ fn test_rm_force() { assert_empty_stderr!(result); assert!(result.success); - assert!(!Path::new(file_a).exists()); - assert!(!Path::new(file_b).exists()); + assert!(!file_exists(file_a)); + assert!(!file_exists(file_b)); } #[test] @@ -88,7 +84,7 @@ fn test_rm_empty_directory() { assert_empty_stderr!(result); assert!(result.success); - assert!(!Path::new(dir).exists()); + assert!(!dir_exists(dir)); } #[test] @@ -105,9 +101,9 @@ fn test_rm_recursive() { assert_empty_stderr!(result); assert!(result.success); - assert!(!Path::new(dir).exists()); - assert!(!Path::new(file_a).exists()); - assert!(!Path::new(file_b).exists()); + assert!(!dir_exists(dir)); + assert!(!file_exists(file_a)); + assert!(!file_exists(file_b)); } #[test] diff --git a/test/unlink.rs b/test/unlink.rs index 4a3fcae67..1d18d9baf 100644 --- a/test/unlink.rs +++ b/test/unlink.rs @@ -1,9 +1,5 @@ -#![feature(path_ext)] - extern crate libc; -use std::fs::PathExt; -use std::path::Path; use std::process::Command; use util::*; @@ -23,7 +19,7 @@ fn test_unlink_file() { assert_empty_stderr!(result); assert!(result.success); - assert!(!Path::new(file).exists()); + assert!(!file_exists(file)); } #[test] From 6ff576e300148d6211c7a2270abb929241375b92 Mon Sep 17 00:00:00 2001 From: Joseph Crail Date: Fri, 31 Jul 2015 13:59:37 -0400 Subject: [PATCH 2/5] Use non-PathExt canonicalize(). --- src/cp/cp.rs | 6 +++--- src/realpath/realpath.rs | 6 +++--- src/relpath/relpath.rs | 10 +++++----- src/stdbuf/stdbuf.rs | 6 +++--- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/cp/cp.rs b/src/cp/cp.rs index b3d7a5184..d6d216579 100644 --- a/src/cp/cp.rs +++ b/src/cp/cp.rs @@ -1,5 +1,5 @@ #![crate_name = "cp"] -#![feature(path_ext)] +#![feature(fs_canonicalize)] /* * This file is part of the uutils coreutils package. @@ -155,8 +155,8 @@ fn copy(matches: getopts::Matches) { pub fn paths_refer_to_same_file(p1: &Path, p2: &Path) -> Result { // We have to take symlinks and relative paths into account. - let pathbuf1 = try!(p1.canonicalize()); - let pathbuf2 = try!(p2.canonicalize()); + let pathbuf1 = try!(fs::canonicalize(p1)); + let pathbuf2 = try!(fs::canonicalize(p2)); Ok(pathbuf1 == pathbuf2) } diff --git a/src/realpath/realpath.rs b/src/realpath/realpath.rs index c0db15f09..19e41456c 100644 --- a/src/realpath/realpath.rs +++ b/src/realpath/realpath.rs @@ -1,5 +1,5 @@ #![crate_name= "realpath"] -#![feature(path_ext)] +#![feature(fs_canonicalize)] /* * This file is part of the uutils coreutils package. @@ -13,7 +13,7 @@ extern crate getopts; extern crate libc; -use std::fs::PathExt; +use std::fs; use std::io::Write; use std::path::{Path, PathBuf}; @@ -63,7 +63,7 @@ pub fn uumain(args: Vec) -> i32 { fn resolve_path(path: &str, strip: bool, zero: bool, quiet: bool) -> bool { let p = Path::new(path).to_path_buf(); - let abs = p.canonicalize().unwrap(); + let abs = fs::canonicalize(p).unwrap(); if strip { if zero { diff --git a/src/relpath/relpath.rs b/src/relpath/relpath.rs index 2775799a6..e02f16e56 100644 --- a/src/relpath/relpath.rs +++ b/src/relpath/relpath.rs @@ -1,5 +1,5 @@ #![crate_name = "relpath"] -#![feature(path_ext)] +#![feature(fs_canonicalize)] /* * This file is part of the uutils coreutils package. @@ -14,7 +14,7 @@ extern crate getopts; extern crate libc; use std::env; -use std::fs::PathExt; +use std::fs; use std::io::Write; use std::path::{Path, PathBuf}; @@ -54,12 +54,12 @@ pub fn uumain(args: Vec) -> i32 { } else { env::current_dir().unwrap() }; - let absto = to.canonicalize().unwrap(); - let absfrom = from.canonicalize().unwrap(); + let absto = fs::canonicalize(to).unwrap(); + let absfrom = fs::canonicalize(from).unwrap(); if matches.opt_present("d") { let base = Path::new(&matches.opt_str("d").unwrap()).to_path_buf(); - let absbase = base.canonicalize().unwrap(); + let absbase = fs::canonicalize(base).unwrap(); if !absto.as_path().starts_with(absbase.as_path()) || !absfrom.as_path().starts_with(absbase.as_path()) { println!("{}", absto.display()); return 0 diff --git a/src/stdbuf/stdbuf.rs b/src/stdbuf/stdbuf.rs index 8db5685b5..f8a1dff9d 100644 --- a/src/stdbuf/stdbuf.rs +++ b/src/stdbuf/stdbuf.rs @@ -1,5 +1,5 @@ #![crate_name = "stdbuf"] -#![feature(negate_unsigned, path_ext)] +#![feature(fs_canonicalize, negate_unsigned)] /* * This file is part of the uutils coreutils package. @@ -15,7 +15,7 @@ extern crate libc; use getopts::{Matches, Options}; use std::env; -use std::fs::PathExt; +use std::fs; use std::io::{self, Write}; use std::os::unix::process::ExitStatusExt; use std::path::PathBuf; @@ -195,7 +195,7 @@ fn set_command_env(command: &mut Command, buffer_name: &str, buffer_type: Buffer fn exe_path() -> io::Result { let exe_path = try!(env::current_exe()); - let absolute_path = try!(exe_path.as_path().canonicalize()); + let absolute_path = try!(fs::canonicalize(exe_path.as_path())); Ok(match absolute_path.parent() { Some(p) => p.to_path_buf(), None => absolute_path.clone() From f41d5b3f8fafe5e31aa29f76085900b4bbc31750 Mon Sep 17 00:00:00 2001 From: Joseph Crail Date: Fri, 31 Jul 2015 14:05:38 -0400 Subject: [PATCH 3/5] Replace unstable fs:walk_dir() w/ external crate. --- deps/Cargo.toml | 1 + src/chmod/chmod.rs | 6 +++--- src/chmod/deps.mk | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/deps/Cargo.toml b/deps/Cargo.toml index e220f7ebd..2851d83a3 100644 --- a/deps/Cargo.toml +++ b/deps/Cargo.toml @@ -20,3 +20,4 @@ unicode-width = "0.1.1" winapi = "0.2" advapi32-sys = "0.1" kernel32-sys = "0.1" +walker = "^1.0.0" diff --git a/src/chmod/chmod.rs b/src/chmod/chmod.rs index 921ab8b47..958e56390 100644 --- a/src/chmod/chmod.rs +++ b/src/chmod/chmod.rs @@ -1,5 +1,4 @@ #![crate_name = "chmod"] -#![feature(fs_walk)] /* * This file is part of the uutils coreutils package. @@ -18,14 +17,15 @@ extern crate libc; extern crate memchr; extern crate regex; extern crate regex_syntax; +extern crate walker; use getopts::Options; use regex::Regex; use std::ffi::CString; -use std::fs; use std::io::{Error, Write}; use std::mem; use std::path::Path; +use walker::Walker; #[path = "../common/util.rs"] #[macro_use] @@ -158,7 +158,7 @@ fn chmod(files: Vec, changes: bool, quiet: bool, verbose: bool, preserve if file.uu_is_dir() { if !preserve_root || filename != "/" { if recursive { - let walk_dir = match fs::walk_dir(&file) { + let walk_dir = match Walker::new(&file) { Ok(m) => m, Err(f) => { crash!(1, "{}", f.to_string()); diff --git a/src/chmod/deps.mk b/src/chmod/deps.mk index 78a05f8b6..07a0616b6 100644 --- a/src/chmod/deps.mk +++ b/src/chmod/deps.mk @@ -1 +1 @@ -DEPLIBS += aho-corasick memchr regex regex-syntax +DEPLIBS += aho-corasick memchr regex regex-syntax walker From 047d963a2605e7e7f070fcba65e390a6e6abe82f Mon Sep 17 00:00:00 2001 From: Joseph Crail Date: Fri, 31 Jul 2015 15:02:19 -0400 Subject: [PATCH 4/5] Remove unused feature attribute. --- src/du/du.rs | 1 - src/mv/mv.rs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/du/du.rs b/src/du/du.rs index ca8072ed9..642ed866b 100644 --- a/src/du/du.rs +++ b/src/du/du.rs @@ -11,7 +11,6 @@ */ #![allow(non_snake_case)] -#![allow(deprecated)] extern crate getopts; extern crate libc; diff --git a/src/mv/mv.rs b/src/mv/mv.rs index e4c67e0be..c651dba85 100644 --- a/src/mv/mv.rs +++ b/src/mv/mv.rs @@ -1,5 +1,5 @@ #![crate_name = "mv"] -#![feature(slice_extras, slice_patterns, str_char)] +#![feature(slice_patterns, str_char)] #![allow(deprecated)] /* From 4300cd5a480fda52713830ce8cb2702756d09572 Mon Sep 17 00:00:00 2001 From: Joseph Crail Date: Fri, 31 Jul 2015 15:05:08 -0400 Subject: [PATCH 5/5] Replace unstable init() method. --- src/mv/mv.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/mv/mv.rs b/src/mv/mv.rs index c651dba85..fb7453839 100644 --- a/src/mv/mv.rs +++ b/src/mv/mv.rs @@ -1,6 +1,5 @@ #![crate_name = "mv"] #![feature(slice_patterns, str_char)] -#![allow(deprecated)] /* * This file is part of the uutils coreutils package. @@ -240,7 +239,7 @@ fn exec(files: &[PathBuf], b: Behaviour) -> i32 { return 1; } let target_dir = fs.last().unwrap(); - move_files_into_dir(fs.init(), target_dir, &b); + move_files_into_dir(&fs[0..fs.len()-1], target_dir, &b); } } 0