coreutils/src/cp/cp.rs

180 lines
4.7 KiB
Rust
Raw Normal View History

2014-07-06 08:13:36 +00:00
#![crate_name = "cp"]
2014-05-17 17:01:17 +00:00
#![feature(macro_rules)]
#![feature(phase)]
/*
* 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;
2014-06-12 04:43:47 +00:00
#[phase(plugin, link)] extern crate log;
use std::os;
use std::io;
use std::io::fs;
use getopts::{
getopts,
optflag,
usage,
};
2014-06-01 00:41:32 +00:00
#[deriving(Eq, PartialEq)]
pub enum Mode {
Copy,
Help,
Version,
}
pub fn uumain(args: Vec<String>) -> int {
2014-05-30 08:35:54 +00:00
let opts = [
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) {
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-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);
let mode = if matches.opt_present("version") {
2014-11-19 20:50:37 +00:00
Mode::Version
} else if matches.opt_present("help") {
2014-11-19 20:50:37 +00:00
Mode::Help
} else {
2014-11-19 20:50:37 +00:00
Mode::Copy
};
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(),
}
0
}
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 {
error!("error: Missing SOURCE argument. Try --help.");
2014-10-30 09:06:47 +00:00
panic!()
} 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()
};
let dest = if matches.free.len() < 2 {
error!("error: Missing DEST argument. Try --help.");
2014-10-30 09:06:47 +00:00
panic!()
} else {
// Only the last argument:
2014-07-20 01:13:55 +00:00
Path::new(matches.free[matches.free.len() - 1].as_slice())
};
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) {
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!()
}
};
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-05-17 17:01:17 +00:00
let io_result = fs::copy(source, &dest);
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!();
}
} else {
2014-12-03 05:29:53 +00:00
if fs::stat(&dest).unwrap().kind != io::FileType::Directory {
error!("error: TARGET must be a directory");
2014-10-30 09:06:47 +00:00
panic!();
}
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());
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-05-17 17:01:17 +00:00
let io_result = fs::copy(source, &full_dest);
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!()
}
}
}
}
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-05-17 17:01:17 +00:00
let p1_lstat = match fs::lstat(&raw_p1) {
Ok(stat) => stat,
Err(e) => return Err(e),
};
2014-05-17 17:01:17 +00:00
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.
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-11-19 20:56:52 +00:00
raw_p1 = os::make_absolute(&raw_p1).unwrap();
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-11-19 20:56:52 +00:00
raw_p2 = os::make_absolute(&raw_p2).unwrap();
Ok(raw_p1 == raw_p2)
}