@@ -10,13 +10,12 @@ use thiserror::Error;
10
10
11
11
use mithril_client:: { common:: CompressionAlgorithm , MithrilError , MithrilResult } ;
12
12
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 ;
16
15
17
- /// Errors tied with the CardanoDbUnpacker .
16
+ /// Errors tied with the [CardanoDbDownloadChecker] .
18
17
#[ derive( Debug , Error ) ]
19
- pub enum CardanoDbUnpackerError {
18
+ pub enum CardanoDbDownloadCheckerError {
20
19
/// Not enough space on the disk. There should be at least the ratio given for the
21
20
/// used algorithm (see [CompressionAlgorithm::free_space_snapshot_ratio]) times
22
21
/// the size of the archive to download to ensure it could be unpacked safely.
@@ -43,12 +42,15 @@ pub enum CardanoDbUnpackerError {
43
42
UnpackDirectoryIsNotWritable ( PathBuf , #[ source] MithrilError ) ,
44
43
}
45
44
46
- impl CardanoDbUnpacker {
45
+ impl CardanoDbDownloadChecker {
47
46
/// Ensure that the given path exist, create it otherwise
48
47
pub fn ensure_dir_exist ( pathdir : & Path ) -> MithrilResult < ( ) > {
49
48
if !pathdir. exists ( ) {
50
49
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
+ )
52
54
} ) ?;
53
55
}
54
56
@@ -82,7 +84,9 @@ impl CardanoDbUnpacker {
82
84
. next ( )
83
85
. is_some ( )
84
86
{
85
- return Err ( CardanoDbUnpackerError :: UnpackDirectoryNotEmpty ( pathdir. to_owned ( ) ) . into ( ) ) ;
87
+ return Err (
88
+ CardanoDbDownloadCheckerError :: UnpackDirectoryNotEmpty ( pathdir. to_owned ( ) ) . into ( ) ,
89
+ ) ;
86
90
}
87
91
88
92
Ok ( ( ) )
@@ -92,12 +96,18 @@ impl CardanoDbUnpacker {
92
96
// Check if the directory is writable by creating a temporary file
93
97
let temp_file_path = pathdir. join ( "temp_file" ) ;
94
98
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
+ )
96
103
} ) ?;
97
104
98
105
// Delete the temporary file
99
106
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
+ )
101
111
} ) ?;
102
112
103
113
Ok ( ( ) )
@@ -110,7 +120,7 @@ impl CardanoDbUnpacker {
110
120
) -> MithrilResult < ( ) > {
111
121
let free_space = fs2:: available_space ( pathdir) ? as f64 ;
112
122
if free_space < compression_algorithm. free_space_snapshot_ratio ( ) * size as f64 {
113
- return Err ( CardanoDbUnpackerError :: NotEnoughSpace {
123
+ return Err ( CardanoDbDownloadCheckerError :: NotEnoughSpace {
114
124
left_space : free_space,
115
125
pathdir : pathdir. to_owned ( ) ,
116
126
archive_size : size as f64 ,
@@ -136,7 +146,8 @@ mod test {
136
146
let pathdir =
137
147
create_temporary_empty_directory ( "directory_does_not_exist" ) . join ( "target_directory" ) ;
138
148
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" ) ;
140
151
141
152
assert ! ( pathdir. exists( ) ) ;
142
153
}
@@ -147,19 +158,27 @@ mod test {
147
158
create_temporary_empty_directory ( "fail_if_pathdir_is_file" ) . join ( "target_directory" ) ;
148
159
fs:: File :: create ( & pathdir) . unwrap ( ) ;
149
160
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" ) ;
153
168
}
154
169
155
170
#[ test]
156
171
fn return_ok_if_unpack_directory_does_not_exist ( ) {
157
172
let pathdir =
158
173
create_temporary_empty_directory ( "directory_does_not_exist" ) . join ( "target_directory" ) ;
159
174
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" ) ;
163
182
}
164
183
165
184
#[ test]
@@ -168,9 +187,13 @@ mod test {
168
187
create_temporary_empty_directory ( "existing_directory" ) . join ( "target_directory" ) ;
169
188
fs:: create_dir_all ( & pathdir) . unwrap ( ) ;
170
189
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" ) ;
174
197
}
175
198
176
199
#[ test]
@@ -179,15 +202,18 @@ mod test {
179
202
fs:: create_dir_all ( & pathdir) . unwrap ( ) ;
180
203
fs:: File :: create ( pathdir. join ( "file.txt" ) ) . unwrap ( ) ;
181
204
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" ) ;
186
212
187
213
assert ! (
188
214
matches!(
189
- error. downcast_ref:: <CardanoDbUnpackerError >( ) ,
190
- Some ( CardanoDbUnpackerError :: UnpackDirectoryNotEmpty ( _) )
215
+ error. downcast_ref:: <CardanoDbDownloadCheckerError >( ) ,
216
+ Some ( CardanoDbDownloadCheckerError :: UnpackDirectoryNotEmpty ( _) )
191
217
) ,
192
218
"Unexpected error: {:?}" ,
193
219
error
@@ -200,8 +226,8 @@ mod test {
200
226
create_temporary_empty_directory ( "enough_available_space" ) . join ( "target_directory" ) ;
201
227
let archive_size = u64:: MAX ;
202
228
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 (
205
231
& pathdir,
206
232
archive_size,
207
233
CompressionAlgorithm :: default ( ) ,
@@ -210,8 +236,8 @@ mod test {
210
236
211
237
assert ! (
212
238
matches!(
213
- error. downcast_ref:: <CardanoDbUnpackerError >( ) ,
214
- Some ( CardanoDbUnpackerError :: NotEnoughSpace {
239
+ error. downcast_ref:: <CardanoDbDownloadCheckerError >( ) ,
240
+ Some ( CardanoDbDownloadCheckerError :: NotEnoughSpace {
215
241
left_space: _,
216
242
pathdir: _,
217
243
archive_size: _
@@ -240,13 +266,16 @@ mod test {
240
266
let targetdir = pathdir. join ( "target_directory" ) ;
241
267
make_readonly ( & pathdir) ;
242
268
243
- let error = CardanoDbUnpacker :: ensure_dir_exist ( & targetdir)
269
+ let error = CardanoDbDownloadChecker :: ensure_dir_exist ( & targetdir)
244
270
. expect_err ( "ensure_dir_exist should fail" ) ;
245
271
246
272
assert ! (
247
273
matches!(
248
- error. downcast_ref:: <CardanoDbUnpackerError >( ) ,
249
- Some ( CardanoDbUnpackerError :: UnpackDirectoryIsNotWritable ( _, _) )
274
+ error. downcast_ref:: <CardanoDbDownloadCheckerError >( ) ,
275
+ Some ( CardanoDbDownloadCheckerError :: UnpackDirectoryIsNotWritable (
276
+ _,
277
+ _
278
+ ) )
250
279
) ,
251
280
"Unexpected error: {:?}" ,
252
281
error
@@ -262,7 +291,7 @@ mod test {
262
291
fs:: create_dir ( & pathdir) . unwrap ( ) ;
263
292
make_readonly ( & pathdir) ;
264
293
265
- let error = CardanoDbUnpacker :: check_prerequisites (
294
+ let error = CardanoDbDownloadChecker :: check_prerequisites (
266
295
& pathdir,
267
296
12 ,
268
297
CompressionAlgorithm :: default ( ) ,
@@ -271,8 +300,11 @@ mod test {
271
300
272
301
assert ! (
273
302
matches!(
274
- error. downcast_ref:: <CardanoDbUnpackerError >( ) ,
275
- Some ( CardanoDbUnpackerError :: UnpackDirectoryIsNotWritable ( _, _) )
303
+ error. downcast_ref:: <CardanoDbDownloadCheckerError >( ) ,
304
+ Some ( CardanoDbDownloadCheckerError :: UnpackDirectoryIsNotWritable (
305
+ _,
306
+ _
307
+ ) )
276
308
) ,
277
309
"Unexpected error: {:?}" ,
278
310
error
0 commit comments