migrate to rust

This commit is contained in:
Mikaël Cluseau
2024-04-29 12:54:25 +02:00
parent 6e1cb57e03
commit 8fcd2d6684
85 changed files with 3451 additions and 2460 deletions

21
src/blockdev.rs Normal file
View File

@ -0,0 +1,21 @@
use tokio::fs;
use tokio::io::{AsyncReadExt, AsyncWriteExt, Result};
/// Checks if a block device is uninitialized.
/// It assumed that a initialized device always has a non-zero byte in the first 8kiB.
pub async fn is_uninitialized(dev_path: &str) -> Result<bool> {
let mut dev = fs::File::open(dev_path).await?;
let mut buf = [0u8; 8 << 10];
dev.read_exact(&mut buf).await?;
Ok(buf.into_iter().all(|b| b == 0))
}
/// zeroes the first 8kiB of the device so it looks uninitialized again.
pub async fn uninitialize(dev_path: &str) -> Result<()> {
let mut dev = fs::File::options().write(true).open(dev_path).await?;
let buf = [0u8; 8 << 10];
dev.write_all(&buf).await
}