use object_store::path::Path;
pub(crate) fn prefixed_path(base: &Path, location: &Path) -> Path {
if base.as_ref().is_empty() {
return location.clone();
}
if location.as_ref().is_empty() {
return base.clone();
}
Path::from_iter(base.parts().chain(location.parts()))
}
pub(crate) fn relativize_path(
base_dir: &Path,
full_path: Path, store_name_for_error: &'static str,
) -> object_store::Result<Path> {
if base_dir.as_ref().is_empty() {
return Ok(full_path);
}
match full_path.prefix_match(base_dir) {
Some(iter) => Ok(Path::from_iter(iter)),
None => Err(object_store::Error::Generic {
store: store_name_for_error,
source: format!(
"Internal logic error: expected base_dir '{}' to be a prefix of '{}', but it was not. Cannot relativize path.",
base_dir, full_path
).into(),
}),
}
}
#[cfg(all(test, feature = "local"))]
mod tests {
use super::*;
use crate::providers::storage::LocalStorage;
use bytes::Bytes;
use futures::TryStreamExt;
use object_store::ObjectStore;
#[tokio::test]
async fn local_url_binding_preserves_literal_keys_through_write_list_copy_and_delete() {
let directory = tempfile::tempdir().unwrap();
let store =
LocalStorage::new_from_url(&format!("file://{}/", directory.path().display())).unwrap();
let key = "nested/quote\"é*%2F space.json";
let path = Path::parse(key).unwrap();
store
.put(&path, Bytes::from_static(b"payload").into())
.await
.unwrap();
assert_eq!(
std::fs::read(directory.path().join(key)).unwrap(),
b"payload"
);
assert_eq!(
store.get(&path).await.unwrap().bytes().await.unwrap(),
"payload"
);
let listed: Vec<_> = store.list(None).try_collect().await.unwrap();
assert_eq!(listed.len(), 1);
assert_eq!(listed[0].location.as_ref(), key);
let target = Path::parse("copy/é%2F.json").unwrap();
store.copy(&path, &target).await.unwrap();
assert_eq!(
std::fs::read(directory.path().join(target.as_ref())).unwrap(),
b"payload"
);
store.delete(&path).await.unwrap();
store.delete(&target).await.unwrap();
assert!(store
.list(None)
.try_collect::<Vec<_>>()
.await
.unwrap()
.is_empty());
}
}