1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
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.
///
/// Each backup in a benchmark backs up some test data created for
/// that backup. That data is later cleaned up. Is all handled by this
/// structure.
#[derive(Debug)]
pub struct Data {
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 {
live,
})
}
pub(crate) fn create(&self, create: &Create) -> Result<(), DataError> {
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(())
}
pub(crate) fn rename(&self, count: &FileCount) -> Result<(), DataError> {
println!("rename {:?}", count);
Ok(())
}
pub(crate) fn delete(&self, count: &FileCount) -> Result<(), DataError> {
println!("delete {:?}", count);
Ok(())
}
pub(crate) fn file_count(&self) -> Result<u64, DataError> {
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> {
Ok(0)
}
}
|