Skip to content

Commit

Permalink
bmap::copy truncates file
Browse files Browse the repository at this point in the history
ftruncate was executed into the main function and it should be done into
the copying process

Signed-off-by: Rafael Garcia Ruiz <[email protected]>
  • Loading branch information
Razaloc committed Oct 2, 2022
1 parent f3c66eb commit 8c6d0f5
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
1 change: 0 additions & 1 deletion bmap-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ edition = "2018"
bmap = { path = "../bmap" }
anyhow = "1"
structopt = "0.3"
nix = "0.25"
flate2 = "1"
4 changes: 0 additions & 4 deletions bmap-rs/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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...");
Expand Down
1 change: 1 addition & 0 deletions bmap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 5 additions & 1 deletion bmap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -35,12 +37,14 @@ pub enum CopyError {
pub fn copy<I, O>(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);
Expand Down

0 comments on commit 8c6d0f5

Please sign in to comment.