2014-07-06 08:13:36 +00:00
|
|
|
#![crate_name = "cp"]
|
2015-05-05 23:42:38 +00:00
|
|
|
#![feature(rustc_private, path_ext)]
|
2014-03-19 18:38:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the uutils coreutils package.
|
|
|
|
*
|
|
|
|
* (c) Jordy Dickinson <jordy.dickinson@gmail.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE file
|
|
|
|
* that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern crate getopts;
|
2015-01-08 12:54:22 +00:00
|
|
|
#[macro_use] extern crate log;
|
2014-03-19 18:38:11 +00:00
|
|
|
|
2015-05-05 23:42:38 +00:00
|
|
|
use getopts::{getopts, optflag, usage};
|
|
|
|
use std::fs;
|
|
|
|
use std::fs::{PathExt};
|
|
|
|
use std::io::{ErrorKind, Result};
|
|
|
|
use std::path::Path;
|
2014-03-19 18:38:11 +00:00
|
|
|
|
2015-05-05 23:42:38 +00:00
|
|
|
#[derive(Clone, Eq, PartialEq)]
|
2014-03-19 18:38:11 +00:00
|
|
|
pub enum Mode {
|
|
|
|
Copy,
|
|
|
|
Help,
|
|
|
|
Version,
|
|
|
|
}
|
|
|
|
|
2015-05-05 23:42:38 +00:00
|
|
|
static NAME: &'static str = "cp";
|
|
|
|
static VERSION: &'static str = "1.0.0";
|
2014-12-11 06:36:58 +00:00
|
|
|
|
2015-02-06 13:48:07 +00:00
|
|
|
pub fn uumain(args: Vec<String>) -> i32 {
|
2014-05-30 08:35:54 +00:00
|
|
|
let opts = [
|
2014-03-19 18:38:11 +00:00
|
|
|
optflag("h", "help", "display this help and exit"),
|
|
|
|
optflag("", "version", "output version information and exit"),
|
|
|
|
];
|
2015-05-05 23:42:38 +00:00
|
|
|
let matches = match getopts(&args[1..], &opts) {
|
2014-03-19 18:38:11 +00:00
|
|
|
Ok(m) => m,
|
|
|
|
Err(e) => {
|
2014-06-15 10:50:40 +00:00
|
|
|
error!("error: {}", e);
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!()
|
2014-03-19 18:38:11 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2014-07-20 01:13:55 +00:00
|
|
|
let progname = &args[0];
|
2014-11-19 20:55:25 +00:00
|
|
|
let usage = usage("Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.", &opts);
|
2014-03-19 18:38:11 +00:00
|
|
|
let mode = if matches.opt_present("version") {
|
2014-11-19 20:50:37 +00:00
|
|
|
Mode::Version
|
2014-03-19 18:38:11 +00:00
|
|
|
} else if matches.opt_present("help") {
|
2014-11-19 20:50:37 +00:00
|
|
|
Mode::Help
|
2014-03-19 18:38:11 +00:00
|
|
|
} else {
|
2014-11-19 20:50:37 +00:00
|
|
|
Mode::Copy
|
2014-03-19 18:38:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
match mode {
|
2014-11-19 20:50:37 +00:00
|
|
|
Mode::Copy => copy(matches),
|
2015-05-05 23:42:38 +00:00
|
|
|
Mode::Help => help(&progname, &usage),
|
2014-11-19 20:50:37 +00:00
|
|
|
Mode::Version => version(),
|
2014-03-19 18:38:11 +00:00
|
|
|
}
|
2014-06-08 07:56:37 +00:00
|
|
|
|
2014-06-12 04:41:53 +00:00
|
|
|
0
|
2014-03-19 18:38:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn version() {
|
2015-05-05 23:42:38 +00:00
|
|
|
println!("{} {}", NAME, VERSION);
|
2014-03-19 18:38:11 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 23:42:38 +00:00
|
|
|
fn help(progname: &String, usage: &String) {
|
2014-03-19 18:38:11 +00:00
|
|
|
let msg = format!("Usage: {0} SOURCE DEST\n \
|
|
|
|
or: {0} SOURCE... DIRECTORY\n \
|
|
|
|
or: {0} -t DIRECTORY SOURCE\n\
|
|
|
|
\n\
|
|
|
|
{1}", progname, usage);
|
|
|
|
println!("{}", msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn copy(matches: getopts::Matches) {
|
2015-05-05 23:42:38 +00:00
|
|
|
let sources: Vec<String> = if matches.free.is_empty() {
|
2014-03-19 18:38:11 +00:00
|
|
|
error!("error: Missing SOURCE argument. Try --help.");
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!()
|
2014-03-19 18:38:11 +00:00
|
|
|
} else {
|
|
|
|
// All but the last argument:
|
2015-05-05 23:42:38 +00:00
|
|
|
matches.free[..matches.free.len() - 1].iter().map(|arg| arg.clone()).collect()
|
2014-03-19 18:38:11 +00:00
|
|
|
};
|
|
|
|
let dest = if matches.free.len() < 2 {
|
|
|
|
error!("error: Missing DEST argument. Try --help.");
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!()
|
2014-03-19 18:38:11 +00:00
|
|
|
} else {
|
|
|
|
// Only the last argument:
|
2015-05-05 23:42:38 +00:00
|
|
|
Path::new(&matches.free[matches.free.len() - 1])
|
2014-03-19 18:38:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
assert!(sources.len() >= 1);
|
|
|
|
|
|
|
|
if sources.len() == 1 {
|
2015-05-05 23:42:38 +00:00
|
|
|
let source = Path::new(&sources[0]);
|
|
|
|
let same_file = paths_refer_to_same_file(source, dest).unwrap_or_else(|err| {
|
|
|
|
match err.kind() {
|
|
|
|
ErrorKind::NotFound => false,
|
|
|
|
_ => {
|
|
|
|
error!("error: {}", err);
|
|
|
|
panic!()
|
|
|
|
}
|
2014-03-19 18:38:11 +00:00
|
|
|
}
|
2015-05-05 23:42:38 +00:00
|
|
|
});
|
2014-03-19 18:38:11 +00:00
|
|
|
|
|
|
|
if same_file {
|
2014-11-21 09:09:43 +00:00
|
|
|
error!("error: \"{}\" and \"{}\" are the same file",
|
2015-05-05 23:42:38 +00:00
|
|
|
source.display(),
|
|
|
|
dest.display());
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!();
|
2014-03-19 18:38:11 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 23:42:38 +00:00
|
|
|
if let Err(err) = fs::copy(source, dest) {
|
|
|
|
error!("error: {}", err);
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!();
|
2014-03-19 18:38:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-05-05 23:42:38 +00:00
|
|
|
if !fs::metadata(dest).unwrap().is_dir() {
|
2014-03-19 18:38:11 +00:00
|
|
|
error!("error: TARGET must be a directory");
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!();
|
2014-03-19 18:38:11 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 23:42:38 +00:00
|
|
|
for src in sources.iter() {
|
|
|
|
let source = Path::new(&src);
|
|
|
|
|
|
|
|
if !fs::metadata(source).unwrap().is_file() {
|
|
|
|
error!("error: \"{}\" is not a file", source.display());
|
2014-03-19 18:38:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-05-05 23:42:38 +00:00
|
|
|
let mut full_dest = dest.to_path_buf();
|
2014-03-19 18:38:11 +00:00
|
|
|
|
2015-05-05 23:42:38 +00:00
|
|
|
full_dest.push(source.to_str().unwrap());
|
2014-03-19 18:38:11 +00:00
|
|
|
|
2015-05-05 23:42:38 +00:00
|
|
|
println!("{}", full_dest.display());
|
2014-03-19 18:38:11 +00:00
|
|
|
|
2015-05-05 23:42:38 +00:00
|
|
|
let io_result = fs::copy(source, full_dest);
|
2014-03-19 18:38:11 +00:00
|
|
|
|
2015-01-24 02:56:37 +00:00
|
|
|
if let Err(err) = io_result {
|
2015-05-05 23:42:38 +00:00
|
|
|
error!("error: {}", err);
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!()
|
2014-03-19 18:38:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 23:42:38 +00:00
|
|
|
pub fn paths_refer_to_same_file(p1: &Path, p2: &Path) -> Result<bool> {
|
2014-03-19 18:38:11 +00:00
|
|
|
// We have to take symlinks and relative paths into account.
|
2015-05-05 23:42:38 +00:00
|
|
|
let pathbuf1 = try!(p1.canonicalize());
|
|
|
|
let pathbuf2 = try!(p2.canonicalize());
|
2014-03-19 18:38:11 +00:00
|
|
|
|
2015-05-05 23:42:38 +00:00
|
|
|
Ok(pathbuf1 == pathbuf2)
|
2014-03-19 18:38:11 +00:00
|
|
|
}
|