1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use std::fs::File;
use std::io::{Error, Write};
use seahorse::{App, Command, Context};
use crate::bytes_iter::BytesIter;
use crate::constants::*;
use crate::error::*;
pub fn cp_app() -> App {
App::new("cp")
.author("Brandon Elam Barker")
.action(run_cp_seahorse_action)
.command(run_cp_seahorse_cmd())
}
const CP_USAGE: &str = "cp [SOURCE_FILE] [DEST_FILE]";
pub fn run_cp_seahorse_cmd() -> Command {
Command::new("cp")
.description("cp: copy a file to another file")
.usage(CP_USAGE)
.action(run_cp_seahorse_action)
}
pub fn run_cp_seahorse_action(ctxt: &Context) {
let mut args = ctxt.args.iter();
let src = args.next().user_err("cp: missing source");
let dst = args.next().user_err("cp: missing destination");
run_cp(&src, &dst);
}
pub fn run_cp(src: &str, dst: &str) {
cp(src, dst).user_err("Error in cp");
}
pub fn cp(src: &str, dst: &str) -> Result<(), Error> {
let f_in = File::open(&src).sfw_err("Couldn't open source")?;
let mut f_in_iter = BytesIter::new(f_in, DEFAULT_BUF_SIZE);
let mut f_out = File::create(&dst)
.sfw_err(&*format!("Couldn't open destination: {}", &dst))?;
f_in_iter.try_for_each(|b_slice_res| match b_slice_res {
Ok(b_slice) => f_out.write_all(&b_slice),
Err(err) => Err(err),
})
}