use crate::shell::{CommandOutput, Shell};
const CP_HELP_TEXT: &str = "\
Usage: cp [OPTION]... SOURCE DEST
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
-a, --archive same as -r -p (recursive, preserve)
-r, -R, --recursive copy directories recursively
-f, --force if an existing destination file cannot be opened, remove it
-n, --no-clobber do not overwrite an existing file
-v, --verbose explain what is being done
-h, --help display this help and exit
";
impl Shell {
pub fn cmd_cp(&self, args: &[&str]) -> CommandOutput {
if args.contains(&"-h") || args.contains(&"--help") {
return CommandOutput::success(CP_HELP_TEXT.to_string());
}
let mut recursive = false;
let mut force = false;
let mut verbose = false;
let mut no_clobber = false;
let mut operands = Vec::new();
for arg in args {
if arg.starts_with("--") {
match *arg {
"--recursive" => recursive = true,
"--archive" => recursive = true,
"--force" => force = true,
"--verbose" => verbose = true,
"--no-clobber" => no_clobber = true,
"--preserve" | "--preserve=mode,timestamps" | "--parents" | "--dereference"
| "--no-dereference" => {}
_ => crate::warn!("cp: warning: unsupported option '{}'", arg),
}
} else if arg.starts_with('-') && arg.len() > 1 {
for ch in arg.chars().skip(1) {
match ch {
'r' | 'R' => recursive = true,
'a' => recursive = true,
'f' => force = true,
'v' => verbose = true,
'n' => no_clobber = true,
'p' | 'i' | 'L' | 'P' | 'd' | 'u' => {} _ => crate::warn!("cp: warning: unsupported option '-{}'", ch),
}
}
} else {
operands.push(arg.to_string());
}
}
if operands.len() < 2 {
return CommandOutput::error("cp: missing file operand\n".to_string(), 1);
}
let dest = operands.pop().unwrap();
let sources = operands;
let mut verbose_out = String::new();
for src in &sources {
let src_path = match self.vfs.resolve(src, &self.cwd) {
Ok(p) => p,
Err(e) => return CommandOutput::error(format!("cp: {}: {}\n", src, e), 1),
};
if src_path.is_dir() && !recursive {
return CommandOutput::error(
format!("cp: {} is a directory (not copied)\n", src),
1,
);
}
let dest_path = if self.vfs.is_dir(&dest, &self.cwd) {
let fname = src_path
.file_name()
.map(|n| n.to_string_lossy().to_string())
.unwrap_or_else(|| src.clone());
format!("{}/{}", dest.trim_end_matches('/'), fname)
} else {
dest.clone()
};
if no_clobber && self.vfs.exists(&dest_path, &self.cwd) {
if verbose {
verbose_out.push_str(&format!("skipped '{}' -> '{}'\n", src, dest_path));
}
continue;
}
if verbose {
verbose_out.push_str(&format!("'{}' -> '{}'\n", src, dest_path));
}
if let Err(e) = self.vfs.copy(src, &dest_path, &self.cwd) {
if !force {
return CommandOutput::error(format!("cp: {}\n", e), 1);
}
}
}
CommandOutput::success(verbose_out)
}
}
#[cfg(test)]
mod tests {
use super::Shell;
use std::fs;
use std::sync::atomic::{AtomicUsize, Ordering};
static TEST_COUNTER: AtomicUsize = AtomicUsize::new(0);
fn mk_shell() -> Shell {
let n = TEST_COUNTER.fetch_add(1, Ordering::SeqCst);
let dir = std::env::temp_dir().join(format!("fastshell_cp_test_{}_{}", std::process::id(), n));
let _ = fs::remove_dir_all(&dir);
let vfs = crate::vfs::Vfs::new(dir).unwrap();
Shell::new(vfs)
}
#[test]
fn test_cp_help() {
let mut s = mk_shell();
let out = s.execute("cp", &["-h"], None);
assert_eq!(out.exit_code, 0);
assert!(!out.stdout.is_empty());
}
#[test]
fn test_cp_help_long() {
let mut s = mk_shell();
let out = s.execute("cp", &["--help"], None);
assert_eq!(out.exit_code, 0);
assert!(!out.stdout.is_empty());
}
}