diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-12-05 21:33:44 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-12-05 21:33:44 +0200 |
commit | 7845a6633953d46bf8bcb84af06f746fc5e427c6 (patch) | |
tree | e72e07f26f0e8d07f9fe58bb8bb66566287911e1 | |
parent | 34f660a6399eacb4847138f9caf7e2cf7a28124e (diff) | |
download | obnam-benchmark-7845a6633953d46bf8bcb84af06f746fc5e427c6.tar.gz |
feat: create and count test data files
They're empty, for now.
Sponsored-by: author
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/data.rs | 38 |
3 files changed, 36 insertions, 4 deletions
@@ -589,6 +589,7 @@ dependencies = [ "subplotlib", "tempfile", "thiserror", + "walkdir", ] [[package]] @@ -14,6 +14,7 @@ serde_yaml = "0.8.21" structopt = "0.3.25" tempfile = "3.2.0" thiserror = "1.0.30" +walkdir = "2.3.2" [build-dependencies] anyhow = "1.0.51" diff --git a/src/data.rs b/src/data.rs index f997885..dbea40f 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,5 +1,8 @@ use crate::specification::{Create, FileCount}; +use std::fs::File; use std::path::{Path, PathBuf}; +use tempfile::{tempdir_in, TempDir}; +use walkdir::WalkDir; /// Test data management for Obnam benchmarks. /// @@ -8,23 +11,43 @@ use std::path::{Path, PathBuf}; /// structure. #[derive(Debug)] pub struct Data { - tempdir: PathBuf, + live: TempDir, } /// Possible errors from managing data. #[derive(Debug, thiserror::Error)] pub enum DataError { + /// Directory creation failed. + #[error("Failed to create directory {0}: {1}")] + CreateDirectory(PathBuf, std::io::Error), + + /// File creation failed. + #[error("Failed to create file {0}: {1}")] + CreateFile(PathBuf, std::io::Error), + + /// File counting failed. + #[error("Failed to count files in {0}: {1}")] + CountFiles(PathBuf, walkdir::Error), + + /// Failed to create a temporary directory. + #[error(transparent)] + CreateTemp(#[from] std::io::Error), } impl Data { pub(crate) fn new(tempdir: &Path) -> Result<Self, DataError> { + let live = tempdir_in(&tempdir)?; Ok(Self { - tempdir: tempdir.to_path_buf(), + live, }) } pub(crate) fn create(&self, create: &Create) -> Result<(), DataError> { - println!("create {:?} in {}", create, self.tempdir.display()); + println!("create {:?} in {}", create, self.live.path().display()); + for i in 0..create.files { + let filename = self.live.path().join(format!("{}", i)); + File::create(&filename).map_err(|err| DataError::CreateFile(filename, err))?; + } Ok(()) } @@ -39,7 +62,14 @@ impl Data { } pub(crate) fn file_count(&self) -> Result<u64, DataError> { - Ok(0) + let mut n = 0; + for entry in WalkDir::new(&self.live).into_iter() { + if let Err(err) = entry { + return Err(DataError::CountFiles(self.live.path().to_path_buf(), err)); + } + n += 1; + } + Ok(n) } pub(crate) fn file_size(&self) -> Result<u64, DataError> { |