2015-12-08 02:42:08 +00:00
|
|
|
#![crate_name = "uu_cp"]
|
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-11-24 01:00:51 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate uucore;
|
|
|
|
|
2017-06-02 11:19:33 +00:00
|
|
|
extern crate walkdir;
|
|
|
|
|
|
|
|
use walkdir::WalkDir;
|
|
|
|
|
2015-05-22 01:31:21 +00:00
|
|
|
use getopts::Options;
|
2015-07-31 17:59:05 +00:00
|
|
|
use std::fs;
|
2015-05-22 01:31:21 +00:00
|
|
|
use std::io::{ErrorKind, Result, Write};
|
2015-05-05 23:42:38 +00:00
|
|
|
use std::path::Path;
|
2015-12-10 19:20:01 +00:00
|
|
|
use uucore::fs::{canonicalize, CanonicalizeMode};
|
2015-07-31 17:59:05 +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";
|
2015-11-25 09:52:10 +00:00
|
|
|
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
2014-12-11 06:36:58 +00:00
|
|
|
|
2015-02-06 13:48:07 +00:00
|
|
|
pub fn uumain(args: Vec<String>) -> i32 {
|
2015-05-22 01:31:21 +00:00
|
|
|
let mut opts = Options::new();
|
|
|
|
|
|
|
|
opts.optflag("h", "help", "display this help and exit");
|
2017-06-02 11:19:33 +00:00
|
|
|
opts.optflag("r", "recursive", "copy directories recursively");
|
2016-07-15 18:41:50 +00:00
|
|
|
opts.optflag("", "version", "output version information and exit");
|
2017-06-02 11:19:33 +00:00
|
|
|
opts.optopt("t",
|
|
|
|
"target-directory",
|
|
|
|
"copy all SOURCE arguments into DIRECTORY",
|
|
|
|
"DEST");
|
|
|
|
opts.optflag("T",
|
|
|
|
"no-target-directory",
|
|
|
|
"Treat DEST as a regular file and not a directory");
|
2016-07-15 18:41:50 +00:00
|
|
|
opts.optflag("v", "verbose", "explicitly state what is being done");
|
2015-05-22 01:31:21 +00:00
|
|
|
|
|
|
|
let matches = match opts.parse(&args[1..]) {
|
2014-03-19 18:38:11 +00:00
|
|
|
Ok(m) => m,
|
|
|
|
Err(e) => {
|
2015-05-22 01:31:21 +00:00
|
|
|
show_error!("{}", e);
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!()
|
2017-06-02 11:19:33 +00:00
|
|
|
}
|
2014-03-19 18:38:11 +00:00
|
|
|
};
|
2015-05-22 01:31:21 +00:00
|
|
|
let usage = opts.usage("Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.");
|
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 {
|
2017-06-02 11:19:33 +00:00
|
|
|
Mode::Copy => copy(matches),
|
|
|
|
Mode::Help => help(&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
|
|
|
}
|
|
|
|
|
2016-01-05 19:42:52 +00:00
|
|
|
fn help(usage: &str) {
|
2015-05-22 01:31:21 +00:00
|
|
|
let msg = format!("{0} {1}\n\n\
|
|
|
|
Usage: {0} SOURCE DEST\n \
|
2014-03-19 18:38:11 +00:00
|
|
|
or: {0} SOURCE... DIRECTORY\n \
|
2016-07-15 15:34:49 +00:00
|
|
|
or: {0} -t DIRECTORY SOURCE...\n\
|
2014-03-19 18:38:11 +00:00
|
|
|
\n\
|
2017-06-02 11:19:33 +00:00
|
|
|
{2}",
|
|
|
|
NAME,
|
|
|
|
VERSION,
|
|
|
|
usage);
|
2014-03-19 18:38:11 +00:00
|
|
|
println!("{}", msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn copy(matches: getopts::Matches) {
|
2016-07-15 18:41:50 +00:00
|
|
|
let verbose = matches.opt_present("verbose");
|
2015-05-05 23:42:38 +00:00
|
|
|
let sources: Vec<String> = if matches.free.is_empty() {
|
2016-07-15 15:34:49 +00:00
|
|
|
show_error!("Missing SOURCE or DEST argument. Try --help.");
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!()
|
2016-07-15 15:34:49 +00:00
|
|
|
} else if !matches.opt_present("target-directory") {
|
2017-06-02 11:19:33 +00:00
|
|
|
matches.free[..matches.free.len() - 1]
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.collect()
|
2016-07-15 15:34:49 +00:00
|
|
|
} else {
|
|
|
|
matches.free.iter().cloned().collect()
|
|
|
|
};
|
2017-06-02 11:19:33 +00:00
|
|
|
let recursive: bool = matches.opt_present("recursive");
|
|
|
|
|
2016-07-15 15:34:49 +00:00
|
|
|
let dest_str = if matches.opt_present("target-directory") {
|
2017-06-02 11:19:33 +00:00
|
|
|
matches
|
|
|
|
.opt_str("target-directory")
|
|
|
|
.expect("Option -t/--target-directory requires an argument")
|
2016-07-15 15:34:49 +00:00
|
|
|
} else {
|
|
|
|
matches.free[matches.free.len() - 1].clone()
|
2014-03-19 18:38:11 +00:00
|
|
|
};
|
2016-07-15 15:34:49 +00:00
|
|
|
let dest = if matches.free.len() < 2 && !matches.opt_present("target-directory") {
|
2015-05-22 01:31:21 +00:00
|
|
|
show_error!("Missing DEST argument. Try --help.");
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!()
|
2014-03-19 18:38:11 +00:00
|
|
|
} else {
|
2016-07-15 15:34:49 +00:00
|
|
|
//the argument to the -t/--target-directory= options
|
|
|
|
let path = Path::new(&dest_str);
|
|
|
|
if !path.is_dir() && matches.opt_present("target-directory") {
|
2017-06-02 11:19:33 +00:00
|
|
|
show_error!("Target {} is not a directory",
|
|
|
|
matches.opt_str("target-directory").unwrap());
|
2016-07-15 15:34:49 +00:00
|
|
|
panic!()
|
|
|
|
} else {
|
|
|
|
path
|
|
|
|
}
|
|
|
|
|
2014-03-19 18:38:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
assert!(sources.len() >= 1);
|
2016-07-15 18:08:04 +00:00
|
|
|
if matches.opt_present("no-target-directory") && dest.is_dir() {
|
2017-06-02 11:19:33 +00:00
|
|
|
show_error!("Can't overwrite directory {} with non-directory",
|
|
|
|
dest.display());
|
2016-07-15 18:08:04 +00:00
|
|
|
panic!()
|
|
|
|
}
|
2014-03-19 18:38:11 +00:00
|
|
|
|
|
|
|
if sources.len() == 1 {
|
2015-05-05 23:42:38 +00:00
|
|
|
let source = Path::new(&sources[0]);
|
2017-06-02 11:19:33 +00:00
|
|
|
let same_file =
|
|
|
|
paths_refer_to_same_file(source, dest).unwrap_or_else(|err| match err.kind() {
|
|
|
|
ErrorKind::NotFound => false,
|
|
|
|
_ => {
|
|
|
|
show_error!("{}", err);
|
|
|
|
panic!()
|
2014-03-19 18:38:11 +00:00
|
|
|
}
|
2017-06-02 11:19:33 +00:00
|
|
|
});
|
2014-03-19 18:38:11 +00:00
|
|
|
|
|
|
|
if same_file {
|
2015-05-22 01:31:21 +00:00
|
|
|
show_error!("\"{}\" and \"{}\" are the same file",
|
2017-06-02 11:19:33 +00:00
|
|
|
source.display(),
|
|
|
|
dest.display());
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!();
|
2014-03-19 18:38:11 +00:00
|
|
|
}
|
2016-07-15 15:34:49 +00:00
|
|
|
let mut full_dest = dest.to_path_buf();
|
2017-06-02 11:19:33 +00:00
|
|
|
if recursive {
|
|
|
|
for entry in WalkDir::new(source) {
|
|
|
|
let entry = entry.unwrap();
|
|
|
|
if entry.path().is_dir() {
|
|
|
|
let mut dst_path = full_dest.clone();
|
|
|
|
dst_path.push(entry.path());
|
|
|
|
if let Err(err) = fs::create_dir(dst_path) {
|
|
|
|
show_error!("{}", err);
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let mut dst_path = full_dest.clone();
|
|
|
|
dst_path.push(entry.path());
|
|
|
|
if let Err(err) = fs::copy(entry.path(), dst_path) {
|
|
|
|
show_error!("{}", err);
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if dest.is_dir() {
|
|
|
|
full_dest.push(source.file_name().unwrap()); //the destination path is the destination
|
|
|
|
} // directory + the file name we're copying
|
|
|
|
if verbose {
|
|
|
|
println!("{} -> {}", source.display(), full_dest.display());
|
|
|
|
}
|
|
|
|
if let Err(err) = fs::copy(source, full_dest) {
|
|
|
|
show_error!("{}", err);
|
|
|
|
panic!();
|
|
|
|
}
|
2014-03-19 18:38:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-12-10 19:20:01 +00:00
|
|
|
if !dest.is_dir() {
|
2015-05-22 01:31:21 +00:00
|
|
|
show_error!("TARGET must be a directory");
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!();
|
2014-03-19 18:38:11 +00:00
|
|
|
}
|
2016-01-05 19:42:52 +00:00
|
|
|
for src in &sources {
|
2015-05-05 23:42:38 +00:00
|
|
|
let source = Path::new(&src);
|
|
|
|
|
2017-06-02 11:19:33 +00:00
|
|
|
if !recursive {
|
|
|
|
if !source.is_file() {
|
|
|
|
show_error!("\"{}\" is not a file", source.display());
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-19 18:38:11 +00:00
|
|
|
|
2017-06-02 11:19:33 +00:00
|
|
|
let mut full_dest = dest.to_path_buf();
|
2014-03-19 18:38:11 +00:00
|
|
|
|
2017-06-02 11:19:33 +00:00
|
|
|
full_dest.push(source.file_name().unwrap());
|
2014-03-19 18:38:11 +00:00
|
|
|
|
2017-06-02 11:19:33 +00:00
|
|
|
if verbose {
|
|
|
|
println!("{} -> {}", source.display(), full_dest.display());
|
|
|
|
}
|
2014-03-19 18:38:11 +00:00
|
|
|
|
2017-06-02 11:19:33 +00:00
|
|
|
let io_result = fs::copy(source, full_dest);
|
2014-03-19 18:38:11 +00:00
|
|
|
|
2017-06-02 11:19:33 +00:00
|
|
|
if let Err(err) = io_result {
|
|
|
|
show_error!("{}", err);
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for entry in WalkDir::new(source) {
|
|
|
|
let entry = entry.unwrap();
|
|
|
|
let full_dest = dest.to_path_buf();
|
|
|
|
|
|
|
|
if entry.path().is_dir() {
|
|
|
|
let mut dst_path = full_dest.clone();
|
|
|
|
dst_path.push(entry.path());
|
|
|
|
if let Err(err) = fs::create_dir(dst_path) {
|
|
|
|
show_error!("{}", err);
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let mut dst_path = full_dest.clone();
|
|
|
|
dst_path.push(entry.path());
|
|
|
|
if let Err(err) = fs::copy(entry.path(), dst_path) {
|
|
|
|
show_error!("{}", err);
|
|
|
|
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-07-31 22:37:50 +00:00
|
|
|
let pathbuf1 = try!(canonicalize(p1, CanonicalizeMode::Normal));
|
|
|
|
let pathbuf2 = try!(canonicalize(p2, CanonicalizeMode::Normal));
|
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
|
|
|
}
|