Merge pull request #571 from jbcrail/fix-cp

Fix cp.
This commit is contained in:
Heather 2015-05-06 07:43:16 +03:00
commit 7c732bcefe
2 changed files with 54 additions and 77 deletions

View file

@ -1,5 +1,5 @@
#![crate_name = "cp"] #![crate_name = "cp"]
#![feature(collections, core, old_io, os, old_path, rustc_private)] #![feature(rustc_private, path_ext)]
/* /*
* This file is part of the uutils coreutils package. * This file is part of the uutils coreutils package.
@ -13,31 +13,28 @@
extern crate getopts; extern crate getopts;
#[macro_use] extern crate log; #[macro_use] extern crate log;
use std::os; use getopts::{getopts, optflag, usage};
use std::old_io as io; use std::fs;
use std::old_io::fs; use std::fs::{PathExt};
use std::io::{ErrorKind, Result};
use std::path::Path;
use getopts::{ #[derive(Clone, Eq, PartialEq)]
getopts,
optflag,
usage,
};
#[derive(Eq, PartialEq)]
pub enum Mode { pub enum Mode {
Copy, Copy,
Help, Help,
Version, Version,
} }
impl Copy for Mode {} static NAME: &'static str = "cp";
static VERSION: &'static str = "1.0.0";
pub fn uumain(args: Vec<String>) -> i32 { pub fn uumain(args: Vec<String>) -> i32 {
let opts = [ let opts = [
optflag("h", "help", "display this help and exit"), optflag("h", "help", "display this help and exit"),
optflag("", "version", "output version information and exit"), optflag("", "version", "output version information and exit"),
]; ];
let matches = match getopts(args.tail(), &opts) { let matches = match getopts(&args[1..], &opts) {
Ok(m) => m, Ok(m) => m,
Err(e) => { Err(e) => {
error!("error: {}", e); error!("error: {}", e);
@ -57,7 +54,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
match mode { match mode {
Mode::Copy => copy(matches), Mode::Copy => copy(matches),
Mode::Help => help(progname.as_slice(), usage.as_slice()), Mode::Help => help(&progname, &usage),
Mode::Version => version(), Mode::Version => version(),
} }
@ -65,10 +62,10 @@ pub fn uumain(args: Vec<String>) -> i32 {
} }
fn version() { fn version() {
println!("cp 1.0.0"); println!("{} {}", NAME, VERSION);
} }
fn help(progname: &str, usage: &str) { fn help(progname: &String, usage: &String) {
let msg = format!("Usage: {0} SOURCE DEST\n \ let msg = format!("Usage: {0} SOURCE DEST\n \
or: {0} SOURCE... DIRECTORY\n \ or: {0} SOURCE... DIRECTORY\n \
or: {0} -t DIRECTORY SOURCE\n\ or: {0} -t DIRECTORY SOURCE\n\
@ -78,101 +75,80 @@ fn help(progname: &str, usage: &str) {
} }
fn copy(matches: getopts::Matches) { fn copy(matches: getopts::Matches) {
let sources : Vec<Path> = if matches.free.len() < 1 { let sources: Vec<String> = if matches.free.is_empty() {
error!("error: Missing SOURCE argument. Try --help."); error!("error: Missing SOURCE argument. Try --help.");
panic!() panic!()
} else { } else {
// All but the last argument: // All but the last argument:
matches.free[..matches.free.len() - 1].iter() matches.free[..matches.free.len() - 1].iter().map(|arg| arg.clone()).collect()
.map(|arg| Path::new(arg.clone())).collect()
}; };
let dest = if matches.free.len() < 2 { let dest = if matches.free.len() < 2 {
error!("error: Missing DEST argument. Try --help."); error!("error: Missing DEST argument. Try --help.");
panic!() panic!()
} else { } else {
// Only the last argument: // Only the last argument:
Path::new(matches.free[matches.free.len() - 1].as_slice()) Path::new(&matches.free[matches.free.len() - 1])
}; };
assert!(sources.len() >= 1); assert!(sources.len() >= 1);
if sources.len() == 1 { if sources.len() == 1 {
let source = &sources[0]; let source = Path::new(&sources[0]);
let same_file = match paths_refer_to_same_file(source, &dest) { let same_file = paths_refer_to_same_file(source, dest).unwrap_or_else(|err| {
Ok(b) => b, match err.kind() {
Err(e) => if e.kind == io::FileNotFound { ErrorKind::NotFound => false,
false _ => {
} else { error!("error: {}", err);
error!("error: {}", e.to_string()); panic!()
panic!() }
} }
}; });
if same_file { if same_file {
error!("error: \"{}\" and \"{}\" are the same file", error!("error: \"{}\" and \"{}\" are the same file",
source.display().to_string(), source.display(),
dest.display().to_string()); dest.display());
panic!(); panic!();
} }
let io_result = fs::copy(source, &dest); if let Err(err) = fs::copy(source, dest) {
error!("error: {}", err);
if let Err(err) = io_result {
error!("error: {}", err.to_string());
panic!(); panic!();
} }
} else { } else {
if fs::stat(&dest).unwrap().kind != io::FileType::Directory { if !fs::metadata(dest).unwrap().is_dir() {
error!("error: TARGET must be a directory"); error!("error: TARGET must be a directory");
panic!(); panic!();
} }
for source in sources.iter() { for src in sources.iter() {
if fs::stat(source).unwrap().kind != io::FileType::RegularFile { let source = Path::new(&src);
error!("error: \"{}\" is not a file", source.display().to_string());
if !fs::metadata(source).unwrap().is_file() {
error!("error: \"{}\" is not a file", source.display());
continue; continue;
} }
let mut full_dest = dest.clone(); let mut full_dest = dest.to_path_buf();
full_dest.push(source.filename_str().unwrap()); full_dest.push(source.to_str().unwrap());
println!("{}", full_dest.display().to_string()); println!("{}", full_dest.display());
let io_result = fs::copy(source, &full_dest); let io_result = fs::copy(source, full_dest);
if let Err(err) = io_result { if let Err(err) = io_result {
error!("error: {}", err.to_string()); error!("error: {}", err);
panic!() panic!()
} }
} }
} }
} }
pub fn paths_refer_to_same_file(p1: &Path, p2: &Path) -> io::IoResult<bool> { pub fn paths_refer_to_same_file(p1: &Path, p2: &Path) -> Result<bool> {
let mut raw_p1 = p1.clone();
let mut raw_p2 = p2.clone();
let p1_lstat = match fs::lstat(&raw_p1) {
Ok(stat) => stat,
Err(e) => return Err(e),
};
let p2_lstat = match fs::lstat(&raw_p2) {
Ok(stat) => stat,
Err(e) => return Err(e),
};
// We have to take symlinks and relative paths into account. // We have to take symlinks and relative paths into account.
if p1_lstat.kind == io::FileType::Symlink { let pathbuf1 = try!(p1.canonicalize());
raw_p1 = fs::readlink(&raw_p1).unwrap(); let pathbuf2 = try!(p2.canonicalize());
}
raw_p1 = os::make_absolute(&raw_p1).unwrap();
if p2_lstat.kind == io::FileType::Symlink { Ok(pathbuf1 == pathbuf2)
raw_p2 = fs::readlink(&raw_p2).unwrap();
}
raw_p2 = os::make_absolute(&raw_p2).unwrap();
Ok(raw_p1 == raw_p2)
} }

View file

@ -1,8 +1,9 @@
#![allow(unstable)] #![feature(path_ext)]
use std::old_io::process::Command; use std::fs::{File, PathExt, remove_file};
use std::old_io::File; use std::io::Read;
use std::old_io::fs::{unlink, PathExtensions}; use std::path::{Path};
use std::process::Command;
static EXE: &'static str = "./cp"; static EXE: &'static str = "./cp";
static TEST_HELLO_WORLD_SOURCE: &'static str = "hello_world.txt"; static TEST_HELLO_WORLD_SOURCE: &'static str = "hello_world.txt";
@ -11,7 +12,7 @@ static TEST_HELLO_WORLD_DEST: &'static str = "copy_of_hello_world.txt";
fn cleanup(filename: &'static str) { fn cleanup(filename: &'static str) {
let path = Path::new(filename); let path = Path::new(filename);
if path.exists() { if path.exists() {
unlink(&path).unwrap(); remove_file(&path).unwrap();
} }
} }
@ -29,10 +30,10 @@ fn test_cp_cp() {
assert_eq!(exit_success, true); assert_eq!(exit_success, true);
// Check the content of the destination file that was copied. // Check the content of the destination file that was copied.
let contents = File::open(&Path::new(TEST_HELLO_WORLD_DEST)) let mut contents = String::new();
.read_to_string() let mut f = File::open(Path::new(TEST_HELLO_WORLD_DEST)).unwrap();
.unwrap(); let _ = f.read_to_string(&mut contents);
assert_eq!(contents.as_slice(), "Hello, World!\n"); assert_eq!(contents, "Hello, World!\n");
cleanup(TEST_HELLO_WORLD_SOURCE); cleanup(TEST_HELLO_WORLD_SOURCE);
cleanup(TEST_HELLO_WORLD_DEST); cleanup(TEST_HELLO_WORLD_DEST);