Skip to content

Commit

Permalink
Use File specializations
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Nov 5, 2023
1 parent d9423f8 commit d82fbd8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
11 changes: 5 additions & 6 deletions src/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ pub(crate) fn main(args: Cli) -> Result<(), Error> {
.suffix(".tmp")
.tempdir_in(output_dir)
.map_err(Error::Tempfile)?;
let mut output_file = tempfile::Builder::new()
let mut temp_file = tempfile::Builder::new()
.tempfile_in(&tempdir)
.map_err(Error::Tempfile)?;
let output_file = temp_file.as_file_mut();

let mut input = File::open(&args.input).map_err(Error::InputOpen)?;
match kind {
Expand All @@ -121,16 +122,14 @@ pub(crate) fn main(args: Cli) -> Result<(), Error> {
let data = gather_signature_data(&keys, &prehashed_message, Some(context))?;
output_file.write_all(&data).map_err(Error::OutputWrite)?;
},
ArchiveKind::Zip => {
copy_and_sign_zip(&mut input, &mut output_file, &keys, Some(context))?;
},
ArchiveKind::Zip => copy_and_sign_zip(&mut input, output_file, &keys, Some(context))?,
ArchiveKind::Tar => {
copy_and_sign_tar(&mut input, &mut output_file, &keys, Some(context))?;
copy_and_sign_tar(&mut input, output_file, &keys, Some(context))?;
},
}
// drop input so it can be overwritten if input=output
drop(input);

rename(output_file.into_temp_path(), output_path).map_err(Error::OutputRename)?;
rename(temp_file.into_temp_path(), output_path).map_err(Error::OutputRename)?;
Ok(())
}
9 changes: 5 additions & 4 deletions src/unsign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,19 @@ pub(crate) fn main(args: Cli) -> Result<(), Error> {
.suffix(".tmp")
.tempdir_in(output_dir)
.map_err(Error::Tempfile)?;
let mut output_file = tempfile::Builder::new()
let mut temp_file = tempfile::Builder::new()
.tempfile_in(&tempdir)
.map_err(Error::Tempfile)?;
let output_file = temp_file.as_file_mut();

let mut input = File::open(&args.input).map_err(Error::InputOpen)?;
match kind {
ArchiveKind::Zip => copy_and_unsign_zip(&mut input, &mut output_file)?,
ArchiveKind::Tar => copy_and_unsign_tar(&mut input, &mut output_file)?,
ArchiveKind::Zip => copy_and_unsign_zip(&mut input, output_file)?,
ArchiveKind::Tar => copy_and_unsign_tar(&mut input, output_file)?,
}
// drop input so it can be overwritten if input=output
drop(input);

rename(output_file.into_temp_path(), output_path).map_err(Error::OutputRename)?;
rename(temp_file.into_temp_path(), output_path).map_err(Error::OutputRename)?;
Ok(())
}

0 comments on commit d82fbd8

Please sign in to comment.