rust/pv: Replace file-macros with functions

A function does the job as well. This improves code readability and
useability. While at it remove the implicit Buffer wrapper. Users are
currently not use the benefits of a buffered write. Also, streamline the
write_out helper function.

Reviewed-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Steffen Eiden
2024-01-19 14:55:43 +01:00
committed by Jan Höppner
parent b5f7ac95d8
commit cafa99774c
5 changed files with 48 additions and 55 deletions

View File

@@ -3,7 +3,6 @@
// Copyright IBM Corp. 2023
use crate::Result;
use crate::{create_buffered_file, open_buffered_file};
use clap::{ArgGroup, Args, ValueHint};
use std::io::{Read, Write};
@@ -120,7 +119,7 @@ pub fn get_writer_from_cli_file_arg(path: &str) -> Result<Box<dyn Write>> {
if path == STDOUT {
Ok(Box::new(std::io::stdout()))
} else {
Ok(Box::new(create_buffered_file!(path)))
Ok(Box::new(crate::misc::create_file(path)?))
}
}
@@ -134,7 +133,7 @@ pub fn get_reader_from_cli_file_arg(path: &str) -> Result<Box<dyn Read>> {
if path == STDIN {
Ok(Box::new(std::io::stdin()))
} else {
Ok(Box::new(open_buffered_file!(path)))
Ok(Box::new(crate::misc::open_file(path)?))
}
}

View File

@@ -85,8 +85,9 @@ pub mod misc {
};
pub use crate::log::PvLogger;
pub use crate::utils::{
memeq, parse_hex, pv_guest_bit_set, read, read_exact_file, read_file, to_u16, to_u32,
try_parse_u128, try_parse_u64, write, write_file, Flags, Lsb0Flags64, Msb0Flags64,
create_file, memeq, open_file, parse_hex, pv_guest_bit_set, read, read_exact_file,
read_file, to_u16, to_u32, try_parse_u128, try_parse_u64, write, write_file, Flags,
Lsb0Flags64, Msb0Flags64,
};
#[cfg(feature = "request")]
pub use crate::utils::{read_certs, read_crls};
@@ -168,39 +169,3 @@ pub mod request {
pub const fn crate_info() -> &'static str {
concat!(env!("CARGO_PKG_NAME"), "-crate ", env!("CARGO_PKG_VERSION"))
}
#[doc(hidden)]
#[macro_export]
macro_rules! file_acc_error {
($ty: tt, $path:expr, $src: expr) => {
$crate::Error::FileAccess {
ty: $crate::FileAccessErrorType::$ty,
path: $path.to_string(),
source: $src,
}
};
}
#[macro_export]
/// Create a file wrapped in a [BufWriter]
///
/// [BufWriter]: std::io#BufWriter
macro_rules! create_buffered_file {
($path: expr) => {
std::io::BufWriter::new(
std::fs::File::create($path).map_err(|e| $crate::file_acc_error!(Create, $path, e))?,
)
};
}
#[macro_export]
/// Open a file wrapped in a [BufReader]
///
/// [BufReader]: std::io#BufReader
macro_rules! open_buffered_file {
($path: expr) => {
std::io::BufReader::new(
std::fs::File::open($path).map_err(|e| $crate::file_acc_error!(Open, $path, e))?,
)
};
}

View File

@@ -3,7 +3,7 @@
// Copyright IBM Corp. 2023
use crate::{
error::{bail_spec, file_error, path_to_str},
error::{bail_spec, file_error, path_to_str, FileAccessErrorType},
Error, FileIoErrorType, Result,
};
@@ -11,8 +11,11 @@ use crate::{
use openssl::x509::X509Crl;
#[cfg(feature = "request")]
use openssl::x509::X509;
use std::io::{Read, Write};
use std::path::Path;
use std::{
fs::File,
io::{Read, Write},
path::Path,
};
use zerocopy::{AsBytes, BigEndian, FromBytes, U64};
/// Asserts a constant expression evaluates to `true`.
@@ -215,6 +218,32 @@ pub fn try_parse_u64(hex_str: &str, ctx: &str) -> Result<u64> {
Ok(u64::from_str_radix(hex_str, 16)?)
}
/// Open a file.
///
/// Wraps [`File::open`]
///
/// * `path` - Path to file
pub fn open_file<P: AsRef<Path>>(path: P) -> Result<File> {
File::open(&path).map_err(|e| Error::FileAccess {
ty: FileAccessErrorType::Open,
path: path_to_str!(path).to_string(),
source: e,
})
}
/// Create a file.
///
/// Wraps [`File::create`]
///
/// * `path` - Path to file
pub fn create_file<P: AsRef<Path>>(path: P) -> Result<File> {
File::create(&path).map_err(|e| Error::FileAccess {
ty: FileAccessErrorType::Create,
path: path_to_str!(path).to_string(),
source: e,
})
}
/// Read exactly COUNT bytes into the buffer.
///
/// * `path` - Path to file

View File

@@ -3,7 +3,7 @@
// Copyright IBM Corp. 2023
#![allow(non_camel_case_types)]
use crate::file_acc_error;
use crate::FileAccessErrorType;
use crate::{Error, Result};
use libc::c_ulong;
use log::debug;
@@ -178,7 +178,11 @@ impl UvDevice {
.read(true)
.write(true)
.open(UvDevice::PATH)
.map_err(|e| file_acc_error!(Open, UvDevice::PATH, e))?,
.map_err(|e| Error::FileAccess {
ty: FileAccessErrorType::Open,
path: (UvDevice::PATH).to_string(),
source: e,
})?,
))
}

View File

@@ -7,10 +7,9 @@ use anyhow::{anyhow, bail, Context, Result};
use log::{debug, info, trace, warn};
use pv::{
misc::{
get_writer_from_cli_file_arg, parse_hex, pv_guest_bit_set, read_certs, read_exact_file,
read_file, try_parse_u128, try_parse_u64,
get_writer_from_cli_file_arg, open_file, parse_hex, pv_guest_bit_set, read_certs,
read_exact_file, read_file, try_parse_u128, try_parse_u64, write,
},
open_buffered_file,
request::{
openssl::pkey::{PKey, Public},
uvsecret::{AddSecretFlags, AddSecretRequest, AddSecretVersion, ExtSecret, GuestSecret},
@@ -21,11 +20,8 @@ use pv::{
use serde_yaml::Value;
fn write_out<D: AsRef<[u8]>>(path: &str, data: D, ctx: &str) -> pv::Result<()> {
let mut wr = match get_writer_from_cli_file_arg(path) {
Ok(it) => it,
Err(err) => return Err(err),
};
pv::misc::write(&mut wr, data, path, ctx)?;
let mut wr = get_writer_from_cli_file_arg(path)?;
write(&mut wr, data, path, ctx)?;
Ok(())
}
@@ -89,7 +85,7 @@ fn build_asrcb(opt: &CreateSecretOpt) -> Result<AddSecretRequest> {
});
debug!("FLAGS: {flags:x?}");
let mut se_hdr = open_buffered_file!(&opt.hdr);
let mut se_hdr = open_file(&opt.hdr)?;
let mut asrcb = AddSecretRequest::new(
AddSecretVersion::One,
secret,