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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
use anyhow::Result;
use std::fs::OpenOptions;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use thinp::file_utils;
use crate::common::test_dir::TestDir;
//------------------------------------------
pub fn mk_zeroed_md(td: &mut TestDir) -> Result<PathBuf> {
let md = td.mk_path("meta.bin");
eprintln!("path = {:?}", md);
let _file = file_utils::create_sized_file(&md, 1024 * 1024 * 16);
Ok(md)
}
pub fn mk_zeroed_md_sized(td: &mut TestDir, nr_bytes: u64) -> Result<PathBuf> {
let md = td.mk_path("meta.bin");
eprintln!("path = {:?}", md);
let _file = file_utils::create_sized_file(&md, nr_bytes);
Ok(md)
}
pub fn damage_superblock(path: &Path) -> Result<()> {
let mut output = OpenOptions::new().read(false).write(true).open(path)?;
let buf = [0u8; 512];
output.write_all(&buf)?;
Ok(())
}
//------------------------------------------
pub fn sha256sum(md: &Path) -> Result<String> {
let output = duct::cmd!("sha256sum", "-b", &md).stdout_capture().run()?;
let csum = std::str::from_utf8(&output.stdout[0..])?.to_string();
let csum = csum.split_ascii_whitespace().next().unwrap().to_string();
Ok(csum)
}
// This checksums the file before and after the thunk is run to
// ensure it is unchanged.
pub fn ensure_untouched<F>(p: &Path, thunk: F) -> Result<()>
where
F: Fn() -> Result<()>,
{
let csum = sha256sum(p)?;
thunk()?;
assert_eq!(csum, sha256sum(p)?);
Ok(())
}
pub fn superblock_all_zeroes(path: &Path) -> Result<bool> {
let mut input = OpenOptions::new().read(true).write(false).open(path)?;
let mut buf = vec![0; 4096];
input.read_exact(&mut buf[0..])?;
for b in buf {
if b != 0 {
return Ok(false);
}
}
Ok(true)
}
pub fn ensure_superblock_zeroed<F>(p: &Path, thunk: F) -> Result<()>
where
F: Fn() -> Result<()>,
{
thunk()?;
assert!(superblock_all_zeroes(p)?);
Ok(())
}
//------------------------------------------
pub fn write_file(p: &Path, buf: &[u8]) -> Result<()> {
let mut output = std::fs::File::create(p)?;
output.write_all(buf)?;
Ok(())
}
//------------------------------------------
|