2014-07-06 08:13:36 +00:00
|
|
|
#![crate_name = "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-01-08 12:54:22 +00:00
|
|
|
#[macro_use] extern crate log;
|
2014-03-19 18:38:11 +00:00
|
|
|
|
|
|
|
use std::os;
|
|
|
|
use std::io;
|
|
|
|
use std::io::fs;
|
|
|
|
|
|
|
|
use getopts::{
|
|
|
|
getopts,
|
|
|
|
optflag,
|
|
|
|
usage,
|
|
|
|
};
|
|
|
|
|
2015-01-08 12:56:41 +00:00
|
|
|
#[derive(Eq, PartialEq)]
|
2014-03-19 18:38:11 +00:00
|
|
|
pub enum Mode {
|
|
|
|
Copy,
|
|
|
|
Help,
|
|
|
|
Version,
|
|
|
|
}
|
|
|
|
|
2014-12-11 06:36:58 +00:00
|
|
|
impl Copy for Mode {}
|
|
|
|
|
2015-01-10 13:07:39 +00:00
|
|
|
pub fn uumain(args: Vec<String>) -> isize {
|
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"),
|
|
|
|
];
|
2014-11-19 20:55:25 +00:00
|
|
|
let matches = match getopts(args.tail(), &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),
|
|
|
|
Mode::Help => help(progname.as_slice(), usage.as_slice()),
|
|
|
|
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() {
|
|
|
|
println!("cp 1.0.0");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn help(progname: &str, usage: &str) {
|
|
|
|
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) {
|
2014-05-17 17:01:17 +00:00
|
|
|
let sources : Vec<Path> = if matches.free.len() < 1 {
|
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:
|
2014-06-03 10:49:12 +00:00
|
|
|
matches.free.slice(0, matches.free.len() - 1).iter()
|
2014-05-17 17:01:17 +00:00
|
|
|
.map(|arg| Path::new(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:
|
2014-07-20 01:13:55 +00:00
|
|
|
Path::new(matches.free[matches.free.len() - 1].as_slice())
|
2014-03-19 18:38:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
assert!(sources.len() >= 1);
|
|
|
|
|
|
|
|
if sources.len() == 1 {
|
2014-07-20 01:13:55 +00:00
|
|
|
let source = &sources[0];
|
2014-05-17 17:01:17 +00:00
|
|
|
let same_file = match paths_refer_to_same_file(source, &dest) {
|
2014-03-19 18:38:11 +00:00
|
|
|
Ok(b) => b,
|
|
|
|
Err(e) => if e.kind == io::FileNotFound {
|
|
|
|
false
|
|
|
|
} else {
|
2014-11-21 09:09:43 +00:00
|
|
|
error!("error: {}", e.to_string());
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!()
|
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",
|
2014-07-09 08:29:50 +00:00
|
|
|
source.display().to_string(),
|
|
|
|
dest.display().to_string());
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!();
|
2014-03-19 18:38:11 +00:00
|
|
|
}
|
|
|
|
|
2014-05-17 17:01:17 +00:00
|
|
|
let io_result = fs::copy(source, &dest);
|
2014-03-19 18:38:11 +00:00
|
|
|
|
|
|
|
if io_result.is_err() {
|
|
|
|
let err = io_result.unwrap_err();
|
2014-11-21 09:09:43 +00:00
|
|
|
error!("error: {}", err.to_string());
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!();
|
2014-03-19 18:38:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-12-03 05:29:53 +00:00
|
|
|
if fs::stat(&dest).unwrap().kind != io::FileType::Directory {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
for source in sources.iter() {
|
2014-12-03 05:29:53 +00:00
|
|
|
if fs::stat(source).unwrap().kind != io::FileType::RegularFile {
|
2014-11-21 09:09:43 +00:00
|
|
|
error!("error: \"{}\" is not a file", source.display().to_string());
|
2014-03-19 18:38:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut full_dest = dest.clone();
|
|
|
|
|
|
|
|
full_dest.push(source.filename_str().unwrap());
|
|
|
|
|
2014-11-21 09:09:43 +00:00
|
|
|
println!("{}", full_dest.display().to_string());
|
2014-03-19 18:38:11 +00:00
|
|
|
|
2014-05-17 17:01:17 +00:00
|
|
|
let io_result = fs::copy(source, &full_dest);
|
2014-03-19 18:38:11 +00:00
|
|
|
|
|
|
|
if io_result.is_err() {
|
|
|
|
let err = io_result.unwrap_err();
|
2014-11-21 09:09:43 +00:00
|
|
|
error!("error: {}", err.to_string());
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!()
|
2014-03-19 18:38:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn paths_refer_to_same_file(p1: &Path, p2: &Path) -> io::IoResult<bool> {
|
2014-05-17 17:01:17 +00:00
|
|
|
let mut raw_p1 = p1.clone();
|
|
|
|
let mut raw_p2 = p2.clone();
|
2014-03-19 18:38:11 +00:00
|
|
|
|
2014-05-17 17:01:17 +00:00
|
|
|
let p1_lstat = match fs::lstat(&raw_p1) {
|
2014-03-19 18:38:11 +00:00
|
|
|
Ok(stat) => stat,
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
};
|
|
|
|
|
2014-05-17 17:01:17 +00:00
|
|
|
let p2_lstat = match fs::lstat(&raw_p2) {
|
2014-03-19 18:38:11 +00:00
|
|
|
Ok(stat) => stat,
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
};
|
|
|
|
|
|
|
|
// We have to take symlinks and relative paths into account.
|
2014-12-03 05:29:53 +00:00
|
|
|
if p1_lstat.kind == io::FileType::Symlink {
|
2014-05-17 17:01:17 +00:00
|
|
|
raw_p1 = fs::readlink(&raw_p1).unwrap();
|
2014-03-19 18:38:11 +00:00
|
|
|
}
|
2014-11-19 20:56:52 +00:00
|
|
|
raw_p1 = os::make_absolute(&raw_p1).unwrap();
|
2014-03-19 18:38:11 +00:00
|
|
|
|
2014-12-03 05:29:53 +00:00
|
|
|
if p2_lstat.kind == io::FileType::Symlink {
|
2014-05-17 17:01:17 +00:00
|
|
|
raw_p2 = fs::readlink(&raw_p2).unwrap();
|
2014-03-19 18:38:11 +00:00
|
|
|
}
|
2014-11-19 20:56:52 +00:00
|
|
|
raw_p2 = os::make_absolute(&raw_p2).unwrap();
|
2014-03-19 18:38:11 +00:00
|
|
|
|
|
|
|
Ok(raw_p1 == raw_p2)
|
|
|
|
}
|