@@ -104,49 +104,62 @@ impl SnapshotConverterCommand {
104
104
)
105
105
} ) ?;
106
106
let distribution_dir = work_dir. join ( CARDANO_DISTRIBUTION_DIR ) ;
107
- create_dir ( & distribution_dir) . with_context ( || {
108
- format ! (
109
- "Failed to create distribution directory: {}" ,
110
- distribution_dir. display( )
107
+
108
+ let result = {
109
+ create_dir ( & distribution_dir) . with_context ( || {
110
+ format ! (
111
+ "Failed to create distribution directory: {}" ,
112
+ distribution_dir. display( )
113
+ )
114
+ } ) ?;
115
+
116
+ let archive_path = Self :: download_cardano_node_distribution (
117
+ ReqwestGitHubApiClient :: new ( ) ?,
118
+ ReqwestHttpDownloader :: new ( ) ?,
119
+ & self . cardano_node_version ,
120
+ & distribution_dir,
111
121
)
112
- } ) ?;
122
+ . await
123
+ . with_context ( || {
124
+ "Failed to download 'snapshot-converter' binary from Cardano node distribution"
125
+ } ) ?;
113
126
114
- let archive_path = Self :: download_cardano_node_distribution (
115
- ReqwestGitHubApiClient :: new ( ) ?,
116
- ReqwestHttpDownloader :: new ( ) ?,
117
- & self . cardano_node_version ,
118
- & distribution_dir,
119
- )
120
- . await
121
- . with_context ( || {
122
- "Failed to download 'snapshot-converter' binary from Cardano node distribution"
123
- } ) ?;
127
+ ArchiveUnpacker :: default ( )
128
+ . unpack ( & archive_path, & distribution_dir)
129
+ . with_context ( || {
130
+ format ! (
131
+ "Failed to unpack 'snapshot-converter' binary to directory: {}" ,
132
+ distribution_dir. display( )
133
+ )
134
+ } ) ?;
124
135
125
- ArchiveUnpacker :: default ( )
126
- . unpack ( & archive_path, & distribution_dir)
136
+ Self :: convert_ledger_state_snapshot (
137
+ & work_dir,
138
+ & self . db_directory ,
139
+ & distribution_dir,
140
+ & self . cardano_network ,
141
+ & self . utxo_hd_flavor ,
142
+ self . commit ,
143
+ )
127
144
. with_context ( || {
128
145
format ! (
129
- "Failed to unpack ' snapshot-converter' binary to directory : {}" ,
130
- distribution_dir . display ( )
146
+ "Failed to convert ledger snapshot to flavor : {}" ,
147
+ self . utxo_hd_flavor
131
148
)
132
149
} ) ?;
133
150
134
- Self :: convert_ledger_state_snapshot (
135
- & work_dir,
136
- & self . db_directory ,
137
- & distribution_dir,
138
- & self . cardano_network ,
139
- & self . utxo_hd_flavor ,
140
- self . commit ,
141
- )
142
- . with_context ( || {
143
- format ! (
144
- "Failed to convert ledger snapshot to flavor: {}" ,
145
- self . utxo_hd_flavor
146
- )
147
- } ) ?;
151
+ Ok ( ( ) )
152
+ } ;
148
153
149
- Ok ( ( ) )
154
+ if let Err ( e) = Self :: cleanup ( & work_dir, & distribution_dir, self . commit , result. is_ok ( ) ) {
155
+ eprintln ! (
156
+ "Failed to clean up temporary directory {} after execution: {}" ,
157
+ distribution_dir. display( ) ,
158
+ e
159
+ ) ;
160
+ }
161
+
162
+ result
150
163
}
151
164
152
165
async fn download_cardano_node_distribution (
@@ -421,6 +434,29 @@ impl SnapshotConverterCommand {
421
434
422
435
Ok ( ( ) )
423
436
}
437
+
438
+ fn cleanup (
439
+ work_dir : & Path ,
440
+ distribution_dir : & Path ,
441
+ commit : bool ,
442
+ success : bool ,
443
+ ) -> MithrilResult < ( ) > {
444
+ match ( success, commit) {
445
+ ( true , true ) => {
446
+ remove_dir_all ( distribution_dir) ?;
447
+ remove_dir_all ( work_dir) ?;
448
+ }
449
+ ( true , false ) => {
450
+ remove_dir_all ( distribution_dir) ?;
451
+ }
452
+ ( false , _) => {
453
+ remove_dir_all ( distribution_dir) ?;
454
+ remove_dir_all ( work_dir) ?;
455
+ }
456
+ }
457
+
458
+ Ok ( ( ) )
459
+ }
424
460
}
425
461
426
462
#[ cfg( test) ]
@@ -844,4 +880,78 @@ mod tests {
844
880
assert ! ( previous_snapshot. exists( ) ) ;
845
881
}
846
882
}
883
+
884
+ mod cleanup {
885
+ use super :: * ;
886
+
887
+ #[ test]
888
+ fn removes_both_dirs_on_success_when_commit_is_true ( ) {
889
+ let tmp = temp_dir_create ! ( ) ;
890
+ let work_dir = tmp. join ( "workdir_dir" ) ;
891
+ let distribution_dir = tmp. join ( "distribution_dir" ) ;
892
+ create_dir ( & work_dir) . unwrap ( ) ;
893
+ create_dir ( & distribution_dir) . unwrap ( ) ;
894
+
895
+ SnapshotConverterCommand :: cleanup ( & work_dir, & distribution_dir, true , true ) . unwrap ( ) ;
896
+
897
+ assert ! ( !distribution_dir. exists( ) ) ;
898
+ assert ! ( !work_dir. exists( ) ) ;
899
+ }
900
+
901
+ #[ test]
902
+ fn removes_only_distribution_on_success_when_commit_is_false ( ) {
903
+ let tmp = temp_dir_create ! ( ) ;
904
+ let work_dir = tmp. join ( "workdir_dir" ) ;
905
+ let distribution_dir = tmp. join ( "distribution_dir" ) ;
906
+ create_dir ( & work_dir) . unwrap ( ) ;
907
+ create_dir ( & distribution_dir) . unwrap ( ) ;
908
+
909
+ SnapshotConverterCommand :: cleanup ( & work_dir, & distribution_dir, false , true ) . unwrap ( ) ;
910
+
911
+ assert ! ( !distribution_dir. exists( ) ) ;
912
+ assert ! ( work_dir. exists( ) ) ;
913
+ }
914
+
915
+ #[ test]
916
+ fn removes_both_dirs_on_success_when_commit_is_true_and_distribution_is_nested ( ) {
917
+ let tmp = temp_dir_create ! ( ) ;
918
+ let work_dir = tmp. join ( "workdir_dir" ) ;
919
+ let distribution_dir = work_dir. join ( "distribution_dir" ) ;
920
+ create_dir ( & work_dir) . unwrap ( ) ;
921
+ create_dir ( & distribution_dir) . unwrap ( ) ;
922
+
923
+ SnapshotConverterCommand :: cleanup ( & work_dir, & distribution_dir, true , true ) . unwrap ( ) ;
924
+
925
+ assert ! ( !distribution_dir. exists( ) ) ;
926
+ assert ! ( !work_dir. exists( ) ) ;
927
+ }
928
+
929
+ #[ test]
930
+ fn removes_only_distribution_on_success_when_commit_is_false_and_distribution_is_nested ( ) {
931
+ let tmp = temp_dir_create ! ( ) ;
932
+ let work_dir = tmp. join ( "workdir_dir" ) ;
933
+ let distribution_dir = work_dir. join ( "distribution_dir" ) ;
934
+ create_dir ( & work_dir) . unwrap ( ) ;
935
+ create_dir ( & distribution_dir) . unwrap ( ) ;
936
+
937
+ SnapshotConverterCommand :: cleanup ( & work_dir, & distribution_dir, false , true ) . unwrap ( ) ;
938
+
939
+ assert ! ( !distribution_dir. exists( ) ) ;
940
+ assert ! ( work_dir. exists( ) ) ;
941
+ }
942
+
943
+ #[ test]
944
+ fn removes_both_dirs_on_failure ( ) {
945
+ let tmp = temp_dir_create ! ( ) ;
946
+ let work_dir = tmp. join ( "workdir_dir" ) ;
947
+ let distribution_dir = tmp. join ( "distribution_dir" ) ;
948
+ create_dir ( & work_dir) . unwrap ( ) ;
949
+ create_dir ( & distribution_dir) . unwrap ( ) ;
950
+
951
+ SnapshotConverterCommand :: cleanup ( & work_dir, & distribution_dir, false , false ) . unwrap ( ) ;
952
+
953
+ assert ! ( !distribution_dir. exists( ) ) ;
954
+ assert ! ( !work_dir. exists( ) ) ;
955
+ }
956
+ }
847
957
}
0 commit comments