Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bmap::copy also can truncate file #26

Closed
wants to merge 1 commit into from
Closed

Conversation

Razaloc
Copy link
Contributor

@Razaloc Razaloc commented Sep 30, 2022

ftruncate was executed into the main function and it should be done into the copying process if it's needed

Closes: #24

Signed-off-by: Rafael Garcia Ruiz [email protected]

bmap/src/lib.rs Outdated
@@ -4,6 +4,7 @@ mod discarder;
pub use crate::discarder::*;
use sha2::{Digest, Sha256};
use thiserror::Error;
use nix::unistd::ftruncate;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest to split this into two commits:

  1. to move the ftruncate into the bmap::copy function,
  2. to call ftruncate only on a file :-)

bmap/src/lib.rs Outdated
@@ -41,6 +42,11 @@ where
HashType::Sha256 => Sha256::new(),
};

//truncate if the target is a file and not a block device
match output {
std::fs::OpenOptions => ftruncate(output.as_raw_fd(), map.image_size() as i64).context("Failed to truncate file")?,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix the clippy errors

@Razaloc
Copy link
Contributor Author

Razaloc commented Oct 2, 2022

Right now ftruncate's error is not handled I'm figuring out how to make the two kind of errors compatible. Tried to create a CopyError TruncateError but didn't worked so far

@Razaloc Razaloc force-pushed the wip/rafaelgarrui/cleanmain branch 2 times, most recently from 8c6d0f5 to ecefb62 Compare October 3, 2022 07:37
@Razaloc Razaloc self-assigned this Oct 3, 2022
@@ -104,8 +102,6 @@ fn copy(c: Copy) -> Result<()> {
.create(true)
.open(c.dest)?;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I think it is already being assumed that the output is going to be a file. Should this be absorbed by the copy library function too and only be executed if the output is actually a file ?

ftruncate was executed into the main function and it should be done into
the copying process

Signed-off-by: Rafael Garcia Ruiz <[email protected]>
@@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a too heavy requirement as it will limit where we can use the copy function; E.g. that blocks using it if the output is not something that can be a raw file (e.g. if it's a usb protocol where we write to some on-device flash)

Razaloc added a commit that referenced this pull request Oct 5, 2022
helpers mod contains functions and structs needed to setup common inputs/outputs

Closes #26

Signed-off-by: Rafael Garcia Ruiz <[email protected]>
Razaloc added a commit that referenced this pull request Oct 5, 2022
helpers mod contains functions and structs needed to setup common inputs/outputs

Closes #26

Signed-off-by: Rafael Garcia Ruiz <[email protected]>
@Razaloc Razaloc closed this Oct 5, 2022
@Razaloc Razaloc deleted the wip/rafaelgarrui/cleanmain branch December 1, 2022 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Refactor low-level functions from bmap command line tool into some library functions
3 participants