rust/pv_core: Add misc::read_file_string

Like read_file, this is a function that reads from a file with error
handling conveniences, but that reads to a trimmed string instead of a
byte vector.

Signed-off-by: Jakob Naucke <naucke@linux.ibm.com>
Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Jakob Naucke
2025-01-31 16:32:28 +01:00
committed by Jan Höppner
parent 66a10d5e3e
commit f9050799bb
2 changed files with 13 additions and 1 deletions

View File

@@ -20,9 +20,10 @@ pub mod attest {
/// Miscellaneous functions and definitions
pub mod misc {
pub use crate::utils::pv_guest_bit_set;
pub use crate::utils::{create_file, open_file, read_exact_file, read_file, write_file};
pub use crate::utils::{create_file, open_file};
pub use crate::utils::{decode_hex, encode_hex, parse_hex};
pub use crate::utils::{read, write};
pub use crate::utils::{read_exact_file, read_file, read_file_string, write_file};
pub use crate::utils::{to_u16, to_u32, try_parse_u128, try_parse_u64};
pub use crate::utils::{Flags, Lsb0Flags64, Msb0Flags64};
}

View File

@@ -262,6 +262,17 @@ pub fn read_exact_file<P: AsRef<Path>, const COUNT: usize>(
Ok(buf)
}
/// Read content from a file as string and add context in case of an error
///
/// * `path` - Path to file
/// * `ctx` - Error context string in case of an error
///
/// # Errors
/// Passes through any kind of error `std::fs::read` produces
pub fn read_file_string<P: AsRef<Path>>(path: P, ctx: &str) -> Result<String> {
std::fs::read_to_string(&path).map_err(|e| file_error!(Read, ctx, path, e))
}
/// Read content from a file and add context in case of an error
///
/// * `path` - Path to file