From 30ea695518508e77cb9e17dde76624360053079b Mon Sep 17 00:00:00 2001 From: Christian Ebner Date: Fri, 7 Jun 2024 13:37:44 +0200 Subject: [PATCH] api: datastore: factor out path decoding for catalog The file path passed to the catalog is base64 encoded, with an exception for the root. Factor this check and decoding step out into a helper function to make it reusable when doing the same for lookups via the metadata archive instead of the catalog. Signed-off-by: Christian Ebner --- src/api2/admin/datastore.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs index 34a9105d..85430299 100644 --- a/src/api2/admin/datastore.rs +++ b/src/api2/admin/datastore.rs @@ -1636,6 +1636,14 @@ pub fn upload_backup_log( .boxed() } +fn decode_path(path: &str) -> Result, Error> { + if path != "root" && path != "/" { + base64::decode(path).map_err(|err| format_err!("base64 decoding of path failed - {err}")) + } else { + Ok(vec![b'/']) + } +} + #[api( input: { properties: { @@ -1709,12 +1717,7 @@ pub async fn catalog( let mut catalog_reader = CatalogReader::new(reader); - let path = if filepath != "root" && filepath != "/" { - base64::decode(filepath)? - } else { - vec![b'/'] - }; - + let path = decode_path(&filepath)?; catalog_reader.list_dir_contents(&path) }) .await?