From cafa99774cff5a9cb15bb03463ffd9aafae72946 Mon Sep 17 00:00:00 2001 From: Steffen Eiden Date: Fri, 19 Jan 2024 14:55:43 +0100 Subject: [PATCH] rust/pv: Replace file-macros with functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Steffen Eiden Signed-off-by: Jan Höppner --- rust/pv/src/cli.rs | 5 ++-- rust/pv/src/lib.rs | 41 +++------------------------------ rust/pv/src/utils.rs | 35 +++++++++++++++++++++++++--- rust/pv/src/uvdevice.rs | 8 +++++-- rust/pvsecret/src/cmd/create.rs | 14 ++++------- 5 files changed, 48 insertions(+), 55 deletions(-) diff --git a/rust/pv/src/cli.rs b/rust/pv/src/cli.rs index 5cf5693b..6c673338 100644 --- a/rust/pv/src/cli.rs +++ b/rust/pv/src/cli.rs @@ -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> { 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> { 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)?)) } } diff --git a/rust/pv/src/lib.rs b/rust/pv/src/lib.rs index ed891408..0fd2a861 100644 --- a/rust/pv/src/lib.rs +++ b/rust/pv/src/lib.rs @@ -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))?, - ) - }; -} diff --git a/rust/pv/src/utils.rs b/rust/pv/src/utils.rs index 13dda2aa..84277c51 100644 --- a/rust/pv/src/utils.rs +++ b/rust/pv/src/utils.rs @@ -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 { Ok(u64::from_str_radix(hex_str, 16)?) } +/// Open a file. +/// +/// Wraps [`File::open`] +/// +/// * `path` - Path to file +pub fn open_file>(path: P) -> Result { + 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>(path: P) -> Result { + 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 diff --git a/rust/pv/src/uvdevice.rs b/rust/pv/src/uvdevice.rs index 01b6ba60..87cd6af6 100644 --- a/rust/pv/src/uvdevice.rs +++ b/rust/pv/src/uvdevice.rs @@ -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, + })?, )) } diff --git a/rust/pvsecret/src/cmd/create.rs b/rust/pvsecret/src/cmd/create.rs index a8beb564..9351e7d0 100644 --- a/rust/pvsecret/src/cmd/create.rs +++ b/rust/pvsecret/src/cmd/create.rs @@ -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>(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 { }); 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,