summaryrefslogtreecommitdiff
path: root/src/data.rs
blob: 20691104e67fa300a43839d9bec232efbf0521d4 (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use crate::specification::{Create, FileCount};
use log::debug;
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)?;
        debug!("created temporary directory {}", live.path().display());
        Ok(Self { live })
    }

    pub(crate) fn create(&self, create: &Create) -> Result<(), DataError> {
        debug!(
            "creating {} files in {}",
            create.files,
            self.live.path().display()
        );
        for i in 0..create.files {
            let filename = self.live.path().join(format!("{}", i));
            debug!("creating {}", filename.display());
            File::create(&filename).map_err(|err| DataError::CreateFile(filename, err))?;
        }
        Ok(())
    }

    pub(crate) fn rename(&self, count: &FileCount) -> Result<(), DataError> {
        debug!("renaming {} files", count.files);
        Ok(())
    }

    pub(crate) fn delete(&self, count: &FileCount) -> Result<(), DataError> {
        debug!("deleting {}", count.files);
        Ok(())
    }

    pub(crate) fn file_count(&self) -> Result<u64, DataError> {
        debug!("counting files in {}", self.live.path().display());
        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;
        }
        debug!("found {} files in {}", n, self.live.path().display());
        Ok(n)
    }

    pub(crate) fn file_size(&self) -> Result<u64, DataError> {
        debug!(
            "counting combined slze of files in {}",
            self.live.path().display()
        );
        let n = 0;
        debug!(
            "found {} bytes of data in {}",
            n,
            self.live.path().display()
        );
        Ok(n)
    }
}