xargo 0.2.0

Effortless cross compilation to custom bare-metal targets like ARM Cortex-M
use std::{fs, io};
use std::path::Path;

use walkdir::WalkDir;

use errors::*;
use flock::FileLock;

pub fn cp_r(src: &Path, dst: &Path) -> Result<()> {
    fn cp_r_(src: &Path, dst: &Path) -> io::Result<()> {
        let nc = src.components().count();

        for entry in WalkDir::new(src) {
            let entry = try!(entry);
            let src = entry.path();

            let mut components = src.components();
            for _ in 0..nc {
                components.next();
            }
            let dst = dst.join(components.as_path());

            if entry.file_type().is_file() {
                try!(fs::copy(src, dst));
            } else {
                if !dst.exists() {
                    try!(fs::create_dir(dst));
                }
            }
        }

        Ok(())
    }

    cp_r_(src, dst).chain_err(|| {
        format!("failed to recursively copy {} to {}",
                src.display(),
                dst.display())
    })
}

pub fn mkdir(path: &Path) -> Result<()> {
    try!(fs::create_dir(path).chain_err(|| {
        format!("couldn't create a directory at {}", path.display())
    }));
    Ok(())
}

pub fn remove_siblings(lock: &FileLock) -> Result<()> {
    try!(lock.remove_siblings()
        .chain_err(|| {
            format!("couldn't clear the contents of {}",
                    lock.parent().display())
        }));

    Ok(())
}