Skip to content

Commit a602708

Browse files
committed
Rename CardanoDbUnpacker to CardanoDbDownloadChecker
As the responsability of the download has been shifted to the client api, the 'unpacked' does not handle the download anymore but only the checks.
1 parent cfc8307 commit a602708

File tree

4 files changed

+82
-50
lines changed

4 files changed

+82
-50
lines changed

mithril-client-cli/src/commands/cardano_db/download.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
commands::client_builder,
1515
configuration::ConfigParameters,
1616
utils::{
17-
CardanoDbUnpacker, CardanoDbUtils, ExpanderUtils, IndicatifFeedbackReceiver,
17+
CardanoDbDownloadChecker, CardanoDbUtils, ExpanderUtils, IndicatifFeedbackReceiver,
1818
ProgressOutputType, ProgressPrinter,
1919
},
2020
};
@@ -131,13 +131,13 @@ impl CardanoDbDownloadCommand {
131131
fn check_local_disk_info(
132132
step_number: u16,
133133
progress_printer: &ProgressPrinter,
134-
db_dir: &PathBuf,
134+
db_dir: &Path,
135135
cardano_db: &Snapshot,
136136
) -> MithrilResult<()> {
137137
progress_printer.report_step(step_number, "Checking local disk info…")?;
138138

139-
CardanoDbUnpacker::ensure_dir_exist(db_dir)?;
140-
if let Err(e) = CardanoDbUnpacker::check_prerequisites(
139+
CardanoDbDownloadChecker::ensure_dir_exist(db_dir)?;
140+
if let Err(e) = CardanoDbDownloadChecker::check_prerequisites(
141141
db_dir,
142142
cardano_db.size,
143143
cardano_db.compression_algorithm.unwrap_or_default(),

mithril-client-cli/src/utils/cardano_db.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use futures::Future;
33
use indicatif::{MultiProgress, ProgressBar};
44
use std::time::Duration;
55

6-
use super::CardanoDbUnpackerError;
6+
use super::CardanoDbDownloadCheckerError;
77
use mithril_client::{MithrilError, MithrilResult};
88

99
/// Utility functions for to the CardanoDb commands
@@ -12,11 +12,11 @@ pub struct CardanoDbUtils;
1212
impl CardanoDbUtils {
1313
/// Handle the error return by `check_prerequisites`
1414
pub fn check_disk_space_error(error: MithrilError) -> MithrilResult<String> {
15-
if let Some(CardanoDbUnpackerError::NotEnoughSpace {
15+
if let Some(CardanoDbDownloadCheckerError::NotEnoughSpace {
1616
left_space: _,
1717
pathdir: _,
1818
archive_size: _,
19-
}) = error.downcast_ref::<CardanoDbUnpackerError>()
19+
}) = error.downcast_ref::<CardanoDbDownloadCheckerError>()
2020
{
2121
Ok(format!("Warning: {}", error))
2222
} else {
@@ -51,7 +51,7 @@ mod test {
5151

5252
#[test]
5353
fn check_disk_space_error_should_return_warning_message_if_error_is_not_enough_space() {
54-
let not_enough_space_error = CardanoDbUnpackerError::NotEnoughSpace {
54+
let not_enough_space_error = CardanoDbDownloadCheckerError::NotEnoughSpace {
5555
left_space: 1_f64,
5656
pathdir: PathBuf::new(),
5757
archive_size: 2_f64,
@@ -66,15 +66,15 @@ mod test {
6666

6767
#[test]
6868
fn check_disk_space_error_should_return_error_if_error_is_not_error_not_enough_space() {
69-
let error = CardanoDbUnpackerError::UnpackDirectoryNotEmpty(PathBuf::new());
69+
let error = CardanoDbDownloadCheckerError::UnpackDirectoryNotEmpty(PathBuf::new());
7070

7171
let error = CardanoDbUtils::check_disk_space_error(anyhow!(error))
7272
.expect_err("check_disk_space_error should fail");
7373

7474
assert!(
7575
matches!(
76-
error.downcast_ref::<CardanoDbUnpackerError>(),
77-
Some(CardanoDbUnpackerError::UnpackDirectoryNotEmpty(_))
76+
error.downcast_ref::<CardanoDbDownloadCheckerError>(),
77+
Some(CardanoDbDownloadCheckerError::UnpackDirectoryNotEmpty(_))
7878
),
7979
"Unexpected error: {:?}",
8080
error

mithril-client-cli/src/utils/unpacker.rs renamed to mithril-client-cli/src/utils/cardano_db_download_checker.rs

Lines changed: 69 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ use thiserror::Error;
1010

1111
use mithril_client::{common::CompressionAlgorithm, MithrilError, MithrilResult};
1212

13-
/// Check and unpack a downloaded archive in a given directory.
14-
#[derive(Default)]
15-
pub struct CardanoDbUnpacker;
13+
/// Checks to apply before downloading a Cardano Db archive to a given directory.
14+
pub struct CardanoDbDownloadChecker;
1615

17-
/// Errors tied with the CardanoDbUnpacker.
16+
/// Errors tied with the [CardanoDbDownloadChecker].
1817
#[derive(Debug, Error)]
19-
pub enum CardanoDbUnpackerError {
18+
pub enum CardanoDbDownloadCheckerError {
2019
/// Not enough space on the disk. There should be at least the ratio given for the
2120
/// used algorithm (see [CompressionAlgorithm::free_space_snapshot_ratio]) times
2221
/// the size of the archive to download to ensure it could be unpacked safely.
@@ -43,12 +42,15 @@ pub enum CardanoDbUnpackerError {
4342
UnpackDirectoryIsNotWritable(PathBuf, #[source] MithrilError),
4443
}
4544

46-
impl CardanoDbUnpacker {
45+
impl CardanoDbDownloadChecker {
4746
/// Ensure that the given path exist, create it otherwise
4847
pub fn ensure_dir_exist(pathdir: &Path) -> MithrilResult<()> {
4948
if !pathdir.exists() {
5049
fs::create_dir_all(pathdir).map_err(|e| {
51-
CardanoDbUnpackerError::UnpackDirectoryIsNotWritable(pathdir.to_owned(), e.into())
50+
CardanoDbDownloadCheckerError::UnpackDirectoryIsNotWritable(
51+
pathdir.to_owned(),
52+
e.into(),
53+
)
5254
})?;
5355
}
5456

@@ -82,7 +84,9 @@ impl CardanoDbUnpacker {
8284
.next()
8385
.is_some()
8486
{
85-
return Err(CardanoDbUnpackerError::UnpackDirectoryNotEmpty(pathdir.to_owned()).into());
87+
return Err(
88+
CardanoDbDownloadCheckerError::UnpackDirectoryNotEmpty(pathdir.to_owned()).into(),
89+
);
8690
}
8791

8892
Ok(())
@@ -92,12 +96,18 @@ impl CardanoDbUnpacker {
9296
// Check if the directory is writable by creating a temporary file
9397
let temp_file_path = pathdir.join("temp_file");
9498
fs::File::create(&temp_file_path).map_err(|e| {
95-
CardanoDbUnpackerError::UnpackDirectoryIsNotWritable(pathdir.to_owned(), e.into())
99+
CardanoDbDownloadCheckerError::UnpackDirectoryIsNotWritable(
100+
pathdir.to_owned(),
101+
e.into(),
102+
)
96103
})?;
97104

98105
// Delete the temporary file
99106
fs::remove_file(temp_file_path).map_err(|e| {
100-
CardanoDbUnpackerError::UnpackDirectoryIsNotWritable(pathdir.to_owned(), e.into())
107+
CardanoDbDownloadCheckerError::UnpackDirectoryIsNotWritable(
108+
pathdir.to_owned(),
109+
e.into(),
110+
)
101111
})?;
102112

103113
Ok(())
@@ -110,7 +120,7 @@ impl CardanoDbUnpacker {
110120
) -> MithrilResult<()> {
111121
let free_space = fs2::available_space(pathdir)? as f64;
112122
if free_space < compression_algorithm.free_space_snapshot_ratio() * size as f64 {
113-
return Err(CardanoDbUnpackerError::NotEnoughSpace {
123+
return Err(CardanoDbDownloadCheckerError::NotEnoughSpace {
114124
left_space: free_space,
115125
pathdir: pathdir.to_owned(),
116126
archive_size: size as f64,
@@ -136,7 +146,8 @@ mod test {
136146
let pathdir =
137147
create_temporary_empty_directory("directory_does_not_exist").join("target_directory");
138148

139-
CardanoDbUnpacker::ensure_dir_exist(&pathdir).expect("ensure_dir_exist should not fail");
149+
CardanoDbDownloadChecker::ensure_dir_exist(&pathdir)
150+
.expect("ensure_dir_exist should not fail");
140151

141152
assert!(pathdir.exists());
142153
}
@@ -147,19 +158,27 @@ mod test {
147158
create_temporary_empty_directory("fail_if_pathdir_is_file").join("target_directory");
148159
fs::File::create(&pathdir).unwrap();
149160

150-
CardanoDbUnpacker::ensure_dir_exist(&pathdir).unwrap();
151-
CardanoDbUnpacker::check_prerequisites(&pathdir, 12, CompressionAlgorithm::default())
152-
.expect_err("check_prerequisites should fail");
161+
CardanoDbDownloadChecker::ensure_dir_exist(&pathdir).unwrap();
162+
CardanoDbDownloadChecker::check_prerequisites(
163+
&pathdir,
164+
12,
165+
CompressionAlgorithm::default(),
166+
)
167+
.expect_err("check_prerequisites should fail");
153168
}
154169

155170
#[test]
156171
fn return_ok_if_unpack_directory_does_not_exist() {
157172
let pathdir =
158173
create_temporary_empty_directory("directory_does_not_exist").join("target_directory");
159174

160-
CardanoDbUnpacker::ensure_dir_exist(&pathdir).unwrap();
161-
CardanoDbUnpacker::check_prerequisites(&pathdir, 12, CompressionAlgorithm::default())
162-
.expect("check_prerequisites should not fail");
175+
CardanoDbDownloadChecker::ensure_dir_exist(&pathdir).unwrap();
176+
CardanoDbDownloadChecker::check_prerequisites(
177+
&pathdir,
178+
12,
179+
CompressionAlgorithm::default(),
180+
)
181+
.expect("check_prerequisites should not fail");
163182
}
164183

165184
#[test]
@@ -168,9 +187,13 @@ mod test {
168187
create_temporary_empty_directory("existing_directory").join("target_directory");
169188
fs::create_dir_all(&pathdir).unwrap();
170189

171-
CardanoDbUnpacker::ensure_dir_exist(&pathdir).unwrap();
172-
CardanoDbUnpacker::check_prerequisites(&pathdir, 12, CompressionAlgorithm::default())
173-
.expect("check_prerequisites should not fail");
190+
CardanoDbDownloadChecker::ensure_dir_exist(&pathdir).unwrap();
191+
CardanoDbDownloadChecker::check_prerequisites(
192+
&pathdir,
193+
12,
194+
CompressionAlgorithm::default(),
195+
)
196+
.expect("check_prerequisites should not fail");
174197
}
175198

176199
#[test]
@@ -179,15 +202,18 @@ mod test {
179202
fs::create_dir_all(&pathdir).unwrap();
180203
fs::File::create(pathdir.join("file.txt")).unwrap();
181204

182-
CardanoDbUnpacker::ensure_dir_exist(&pathdir).unwrap();
183-
let error =
184-
CardanoDbUnpacker::check_prerequisites(&pathdir, 12, CompressionAlgorithm::default())
185-
.expect_err("check_prerequisites should fail");
205+
CardanoDbDownloadChecker::ensure_dir_exist(&pathdir).unwrap();
206+
let error = CardanoDbDownloadChecker::check_prerequisites(
207+
&pathdir,
208+
12,
209+
CompressionAlgorithm::default(),
210+
)
211+
.expect_err("check_prerequisites should fail");
186212

187213
assert!(
188214
matches!(
189-
error.downcast_ref::<CardanoDbUnpackerError>(),
190-
Some(CardanoDbUnpackerError::UnpackDirectoryNotEmpty(_))
215+
error.downcast_ref::<CardanoDbDownloadCheckerError>(),
216+
Some(CardanoDbDownloadCheckerError::UnpackDirectoryNotEmpty(_))
191217
),
192218
"Unexpected error: {:?}",
193219
error
@@ -200,8 +226,8 @@ mod test {
200226
create_temporary_empty_directory("enough_available_space").join("target_directory");
201227
let archive_size = u64::MAX;
202228

203-
CardanoDbUnpacker::ensure_dir_exist(&pathdir).unwrap();
204-
let error = CardanoDbUnpacker::check_prerequisites(
229+
CardanoDbDownloadChecker::ensure_dir_exist(&pathdir).unwrap();
230+
let error = CardanoDbDownloadChecker::check_prerequisites(
205231
&pathdir,
206232
archive_size,
207233
CompressionAlgorithm::default(),
@@ -210,8 +236,8 @@ mod test {
210236

211237
assert!(
212238
matches!(
213-
error.downcast_ref::<CardanoDbUnpackerError>(),
214-
Some(CardanoDbUnpackerError::NotEnoughSpace {
239+
error.downcast_ref::<CardanoDbDownloadCheckerError>(),
240+
Some(CardanoDbDownloadCheckerError::NotEnoughSpace {
215241
left_space: _,
216242
pathdir: _,
217243
archive_size: _
@@ -240,13 +266,16 @@ mod test {
240266
let targetdir = pathdir.join("target_directory");
241267
make_readonly(&pathdir);
242268

243-
let error = CardanoDbUnpacker::ensure_dir_exist(&targetdir)
269+
let error = CardanoDbDownloadChecker::ensure_dir_exist(&targetdir)
244270
.expect_err("ensure_dir_exist should fail");
245271

246272
assert!(
247273
matches!(
248-
error.downcast_ref::<CardanoDbUnpackerError>(),
249-
Some(CardanoDbUnpackerError::UnpackDirectoryIsNotWritable(_, _))
274+
error.downcast_ref::<CardanoDbDownloadCheckerError>(),
275+
Some(CardanoDbDownloadCheckerError::UnpackDirectoryIsNotWritable(
276+
_,
277+
_
278+
))
250279
),
251280
"Unexpected error: {:?}",
252281
error
@@ -262,7 +291,7 @@ mod test {
262291
fs::create_dir(&pathdir).unwrap();
263292
make_readonly(&pathdir);
264293

265-
let error = CardanoDbUnpacker::check_prerequisites(
294+
let error = CardanoDbDownloadChecker::check_prerequisites(
266295
&pathdir,
267296
12,
268297
CompressionAlgorithm::default(),
@@ -271,8 +300,11 @@ mod test {
271300

272301
assert!(
273302
matches!(
274-
error.downcast_ref::<CardanoDbUnpackerError>(),
275-
Some(CardanoDbUnpackerError::UnpackDirectoryIsNotWritable(_, _))
303+
error.downcast_ref::<CardanoDbDownloadCheckerError>(),
304+
Some(CardanoDbDownloadCheckerError::UnpackDirectoryIsNotWritable(
305+
_,
306+
_
307+
))
276308
),
277309
"Unexpected error: {:?}",
278310
error

mithril-client-cli/src/utils/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
//! This module contains tools needed for the commands layer.
33
44
mod cardano_db;
5+
mod cardano_db_download_checker;
56
mod expander;
67
mod feedback_receiver;
78
mod progress_reporter;
8-
mod unpacker;
99

1010
pub use cardano_db::*;
11+
pub use cardano_db_download_checker::*;
1112
pub use expander::*;
1213
pub use feedback_receiver::*;
1314
pub use progress_reporter::*;
14-
pub use unpacker::*;

0 commit comments

Comments
 (0)