xch 1.1.0

A CLI utility and rust crate to atomically swap the content of two paths.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use std::path;
use std::fs;
use std::io::{Write, Read};
use std::error::Error;


pub fn ensure_file_content<A: AsRef<path::Path>>(path: A, content: &[u8]) -> Result<bool, Box<dyn Error>> {
    let mut f = fs::File::open(path)?;
    let mut buf = Vec::new();
    f.read_to_end(&mut buf)?;

    Ok(content == &buf[..])
}

pub fn create_file_with_content<A: AsRef<path::Path>>(path: A, content: &[u8]) -> Result<(), Box<dyn Error>> {
    let mut f = fs::File::create(path)?;
    f.write_all(content).map_err(Into::into)
}