From 8c6d0f541ac934dcca3c226e125100eae0994de7 Mon Sep 17 00:00:00 2001 From: Rafael Garcia Ruiz Date: Fri, 30 Sep 2022 18:39:50 +0200 Subject: [PATCH] bmap::copy truncates file ftruncate was executed into the main function and it should be done into the copying process Signed-off-by: Rafael Garcia Ruiz --- bmap-rs/Cargo.toml | 1 - bmap-rs/src/main.rs | 4 ---- bmap/Cargo.toml | 1 + bmap/src/lib.rs | 6 +++++- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bmap-rs/Cargo.toml b/bmap-rs/Cargo.toml index 0faea7c..ffb91da 100644 --- a/bmap-rs/Cargo.toml +++ b/bmap-rs/Cargo.toml @@ -10,5 +10,4 @@ edition = "2018" bmap = { path = "../bmap" } anyhow = "1" structopt = "0.3" -nix = "0.25" flate2 = "1" diff --git a/bmap-rs/src/main.rs b/bmap-rs/src/main.rs index 72c1fb7..99b98b8 100644 --- a/bmap-rs/src/main.rs +++ b/bmap-rs/src/main.rs @@ -1,11 +1,9 @@ use anyhow::{anyhow, bail, Context, Result}; use bmap::{Bmap, Discarder, SeekForward}; use flate2::read::GzDecoder; -use nix::unistd::ftruncate; use std::ffi::OsStr; use std::fs::File; use std::io::Read; -use std::os::unix::io::AsRawFd; use std::path::{Path, PathBuf}; use structopt::StructOpt; @@ -104,8 +102,6 @@ fn copy(c: Copy) -> Result<()> { .create(true) .open(c.dest)?; - ftruncate(output.as_raw_fd(), bmap.image_size() as i64).context("Failed to truncate file")?; - let mut input = setup_input(&c.image)?; bmap::copy(&mut input, &mut output, &bmap)?; println!("Done: Syncing..."); diff --git a/bmap/Cargo.toml b/bmap/Cargo.toml index 72a60ff..445f578 100644 --- a/bmap/Cargo.toml +++ b/bmap/Cargo.toml @@ -16,3 +16,4 @@ sha2 = { version = "0.10.6", features = [ "asm" ] } strum = { version = "0.24.1", features = [ "derive"] } digest = "0.10.5" flate2 = "1.0.20" +nix = "0.25" diff --git a/bmap/src/lib.rs b/bmap/src/lib.rs index 643f9cb..0e389f3 100644 --- a/bmap/src/lib.rs +++ b/bmap/src/lib.rs @@ -2,11 +2,13 @@ mod bmap; pub use crate::bmap::*; mod discarder; pub use crate::discarder::*; +use nix::unistd::ftruncate; use sha2::{Digest, Sha256}; use thiserror::Error; use std::io::Result as IOResult; use std::io::{Read, Seek, SeekFrom, Write}; +use std::os::unix::prelude::AsRawFd; /// Trait that can only seek further forwards pub trait SeekForward { @@ -35,12 +37,14 @@ pub enum CopyError { pub fn copy(input: &mut I, output: &mut O, map: &Bmap) -> Result<(), CopyError> where I: Read + SeekForward, - O: Write + SeekForward, + O: Write + SeekForward + AsRawFd, { let mut hasher = match map.checksum_type() { HashType::Sha256 => Sha256::new(), }; + let _ = ftruncate(output.as_raw_fd(), map.image_size() as i64); + let mut v = Vec::new(); // TODO benchmark a reasonable size for this v.resize(8 * 1024 * 1024, 0);