pub fn evaluate_storage(
path: &Path,
dst: &Path,
expected_sha256: Option<&str>,
) -> Result<(bool, bool), DatasetError>Expand description
Evaluate the storage state for a dataset file.
This helper ensures the target directory exists and checks whether the destination
file is already present and valid. If expected_sha256 is None, any existing
file at dst is accepted without validation.
§Parameters
path- Directory path where the dataset will be stored.dst- Destination file path for the dataset.expected_sha256- Optional expected SHA256 hash for the dataset file. IfNone, any existing file atdstis accepted without validation.
§Returns
(need_acquire, need_overwrite)- Flags indicating whether a new file needs to be prepared and whether an existing file should be overwritten.
§Errors
DatasetError::IoError- Returned when creating the directory fails or when file I/O operations fail during hash verification.
§Example
use dataset_core::utils::evaluate_storage;
use std::path::Path;
use std::io::Write;
let test_dir = "./evaluate_storage_example";
let dir_path = Path::new(test_dir);
let file_path = dir_path.join("data.txt");
// Case 1: Directory doesn't exist yet
let (need_acquire, need_overwrite) = evaluate_storage(
dir_path,
&file_path,
None,
).unwrap();
assert!(need_acquire); // File doesn't exist, a new file must be prepared
assert!(!need_overwrite); // Nothing to overwrite
// Case 2: File exists with correct hash
let mut file = std::fs::File::create(&file_path).unwrap();
file.write_all(b"hello world").unwrap();
drop(file);
let correct_hash = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9";
let (need_acquire, need_overwrite) = evaluate_storage(
dir_path,
&file_path,
Some(correct_hash),
).unwrap();
assert!(!need_acquire); // File exists with correct hash
assert!(!need_overwrite);
// Case 3: File exists but hash doesn't match
let wrong_hash = "0000000000000000000000000000000000000000000000000000000000000000";
let (need_acquire, need_overwrite) = evaluate_storage(
dir_path,
&file_path,
Some(wrong_hash),
).unwrap();
assert!(need_acquire); // Hash mismatch, a new file must be prepared
assert!(need_overwrite); // Existing file needs to be replaced
// Clean up (dispensable)
std::fs::remove_dir_all(test_dir).unwrap();