Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hdf5::Dir API update. #301

Merged
merged 4 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 48 additions & 37 deletions hermes/bin/src/hdf5/dir.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! A Hermes HDF5 directory abstraction over the HDF5 Group object.

use std::io::Read;

use super::{
resources::{Hdf5Resource, ResourceTrait},
File, Path,
Expand Down Expand Up @@ -38,38 +36,41 @@ impl Dir {
Ok(())
}

/// Create a new empty file in the provided path.
pub(crate) fn create_file(&self, mut path: Path) -> anyhow::Result<File> {
let file_name = path.pop_elem()?;
let dir = self.get_dir(&path)?;
let file = File::create(&dir.0, file_name.as_str())?;
Ok(file)
}

/// Copy resource file to the provided path.
pub(crate) fn copy_resource_file(
&self, resource: &impl ResourceTrait, mut path: Path,
&self, resource: &impl ResourceTrait, path: Path,
) -> anyhow::Result<()> {
let file_name = path.pop_elem()?;

let mut file = self.create_file(path)?;
let mut reader = resource.get_reader()?;
let mut resource_data = Vec::new();
reader.read_to_end(&mut resource_data)?;
if resource_data.is_empty() {
anyhow::bail!("Resource {} is empty", resource.to_string());
}

let dir = self.create_dir(&path)?;
File::create(&dir.0, file_name.as_str(), &resource_data)?;
std::io::copy(&mut reader, &mut file)?;
Ok(())
}

/// Copy resource dir recursively to the provided path.
pub(crate) fn copy_resource_dir(
&self, resource: &impl ResourceTrait, path: &Path,
) -> anyhow::Result<()> {
let dir = self.create_dir(path)?;
let dir = self.get_dir(path)?;

let mut errors = Errors::new();
for resource in resource.get_directory_content()? {
let path: Path = resource.name()?.into();
if resource.is_dir() {
dir.copy_resource_dir(&resource, &resource.name()?.into())
dir.create_dir(path.clone())?;
dir.copy_resource_dir(&resource, &path)
.unwrap_or_else(errors.get_add_err_fn());
}
if resource.is_file() {
dir.copy_resource_file(&resource, resource.name()?.into())
dir.copy_resource_file(&resource, path)
.unwrap_or_else(errors.get_add_err_fn());
}
}
Expand All @@ -86,17 +87,14 @@ impl Dir {
/// Create dir recursively from path related to current dir.
/// If some dir already exists it will be skipped, if some dir does not exist it will
/// be created.
/// If path is empty it will return cloned `Dir`.
pub(crate) fn create_dir(&self, path: &Path) -> anyhow::Result<Self> {
let mut dir = self.0.clone();
for path_element in path.iter() {
if let Ok(known_dir) = dir.group(path_element) {
dir = known_dir;
} else {
dir = dir.create_group(path_element)?;
}
}
Ok(Self(dir))
pub(crate) fn create_dir(&self, mut path: Path) -> anyhow::Result<Self> {
let dir_name = path.pop_elem()?;
let dir = self.get_dir(&path)?;
let new_dir = dir
.0
.create_group(&dir_name)
.map_err(|_| anyhow::anyhow!("Dir `{path}/{dir_name}` already exists"))?;
Ok(Self(new_dir))
}

/// Remove file by the provided path.
Expand Down Expand Up @@ -156,7 +154,7 @@ impl Dir {
for path_element in path.iter() {
dir = dir
.group(path_element)
.map_err(|_| anyhow::anyhow!("Dir {path} not found"))?;
.map_err(|_| anyhow::anyhow!("Dir `{path}` not found"))?;
}
Ok(Self(dir))
}
Expand All @@ -171,6 +169,8 @@ impl Dir {

#[cfg(test)]
mod tests {
use std::io::Read;

use temp_dir::TempDir;

use super::*;
Expand All @@ -183,18 +183,27 @@ mod tests {
let package = hdf5::File::create(package_name).expect("Failed to create a new package.");
let dir = Dir::new(package.as_group().expect("Failed to create a root group."));

let path = Path::from_str("dir_1/dir_2/dir_3/dir_4");
dir.create_dir(&path)
let dir_1 = "dir_1";
let dir_2 = "dir_2";
let dir_3 = "dir_3";
let new_dir = dir
.create_dir(dir_1.into())
.expect("Failed to create directories in package.");
let new_dir = new_dir
.create_dir(dir_2.into())
.expect("Failed to create directories in package.");
new_dir
.create_dir(dir_3.into())
.expect("Failed to create directories in package.");

assert!(dir.get_dir(&Path::from_str("dir_1")).is_ok());
assert!(dir.get_dir(&Path::from_str("dir_1/dir_2")).is_ok());
assert!(dir.get_dir(&Path::from_str("dir_1/dir_2/dir_3")).is_ok());
assert!(dir.get_dir(&path).is_ok());
assert!(dir.get_dir(&dir_1.into()).is_ok());
assert!(dir.get_dir(&format!("{dir_1}/{dir_2}").into()).is_ok());
assert!(dir
.get_dir(&format!("{dir_1}/{dir_2}/{dir_3}").into())
.is_ok());
assert!(dir.get_dir(&Path::from_str("not_created_dir")).is_err());

dir.create_dir(&path)
.expect("Failed to create directories in package.");
assert!(dir.create_dir(dir_1.into()).is_err());
}

#[test]
Expand All @@ -212,10 +221,10 @@ mod tests {
let child_dir_path = Path::from_str(child_dir_name);

let child_dir = dir2
.create_dir(&child_dir_path)
.create_dir(child_dir_path.clone())
.expect("Failed to create dir.");
child_dir
.create_dir(&child_dir_path)
.create_dir(child_dir_path)
.expect("Failed to create dir.");

let mounted_dir_name = "mounted_dir";
Expand Down Expand Up @@ -314,6 +323,8 @@ mod tests {
let file_3 = fs_child_dir.join(file_3_name);
std::fs::write(file_3, file_content).expect("Failed to create file_3 file.");

dir.create_dir(base_dir_name.into())
.expect("Failed to create dir.");
dir.copy_resource_dir(&FsResource::new(fs_base_dir), &base_dir_name.into())
.expect("Failed to copy dir to package.");

Expand Down
17 changes: 9 additions & 8 deletions hermes/bin/src/hdf5/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@ pub(crate) struct File {

impl File {
/// Create a new file.
pub(crate) fn create(
group: &hdf5::Group, file_name: &str, data: &[u8],
) -> anyhow::Result<Self> {
pub(crate) fn create(group: &hdf5::Group, file_name: &str) -> anyhow::Result<Self> {
let builder = group.new_dataset_builder();
let shape = hdf5::SimpleExtents::resizable([data.len()]);
let shape = hdf5::SimpleExtents::resizable([0]);
let hdf5_ds = enable_compression(builder)
.empty::<u8>()
.shape(shape)
.create(file_name)?;
hdf5_ds.write(data)?;
Ok(Self { hdf5_ds, pos: 0 })
}

Expand Down Expand Up @@ -152,13 +149,17 @@ mod tests {
.expect("Failed to get a root group from package.");

let file_name = "test.txt";
let file_content = b"file_content";

assert!(group.dataset(file_name).is_err());
let mut file =
File::create(&group, file_name, file_content).expect("Failed to create a new file.");
let mut file = File::create(&group, file_name).expect("Failed to create a new file.");
assert!(group.dataset(file_name).is_ok());

let file_content = b"file_content";
file.write_all(file_content)
.expect("Failed to write to file.");

file.seek(std::io::SeekFrom::Start(0))
.expect("Failed to seek.");
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)
.expect("Failed to read from file.");
Expand Down
Loading
Loading