Skip to content

Commit 2e6b781

Browse files
committed
feat(client): add a temporary folder provider in utils
1 parent 6a7107e commit 2e6b781

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

mithril-client/src/utils/mod.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22
//! This module contains tools needed mostly for the snapshot download and unpack.
33
44
cfg_fs! {
5-
pub const ANCILLARIES_NOT_SIGNED_BY_MITHRIL:&str = "Ancillary verification does not use the Mithril certification: as a mitigation, IOG owned keys are used to sign these files.";
5+
pub const ANCILLARIES_NOT_SIGNED_BY_MITHRIL: &str = "Ancillary verification does not use the Mithril certification: as a mitigation, IOG owned keys are used to sign these files.";
66

77
mod ancillary_verifier;
8+
mod bootstrap_files;
89
mod fs;
910
mod stream_reader;
10-
mod bootstrap_files;
11+
mod temp_dir_provider;
1112
mod unexpected_downloaded_file_verifier;
1213
mod vec_deque_extensions;
1314

14-
pub use fs::*;
15-
pub use vec_deque_extensions::VecDequeExtensions;
1615
pub use ancillary_verifier::AncillaryVerifier;
17-
pub(crate) use unexpected_downloaded_file_verifier::*;
18-
pub use stream_reader::*;
1916
pub use bootstrap_files::*;
17+
pub use fs::*;
18+
pub use stream_reader::*;
19+
pub(crate) use temp_dir_provider::{TempDirectoryProvider, TimestampTempDirectoryProvider};
20+
pub(crate) use unexpected_downloaded_file_verifier::*;
21+
pub use vec_deque_extensions::VecDequeExtensions;
2022
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use chrono::Utc;
2+
use std::path::PathBuf;
3+
4+
/// Trait to provide a temporary directory path
5+
#[cfg_attr(test, mockall::automock)]
6+
pub trait TempDirectoryProvider: Send + Sync {
7+
/// Provides a temporary directory path
8+
fn temp_dir(&self) -> PathBuf;
9+
}
10+
11+
/// Timestamp based temporary directory provider implementation
12+
pub struct TimestampTempDirectoryProvider {
13+
temp_dir: PathBuf,
14+
}
15+
16+
impl TimestampTempDirectoryProvider {
17+
/// Constructs a new `TimestampTempDirectoryProvider`.
18+
pub fn new(prefix: &str) -> Self {
19+
let temp_dir = std::env::temp_dir().join(format!(
20+
"mithril_client_{prefix}_{}",
21+
Utc::now().timestamp_micros()
22+
));
23+
Self { temp_dir }
24+
}
25+
}
26+
27+
impl TempDirectoryProvider for TimestampTempDirectoryProvider {
28+
fn temp_dir(&self) -> PathBuf {
29+
self.temp_dir.clone()
30+
}
31+
}

0 commit comments

Comments
 (0)