@@ -104,49 +104,62 @@ impl SnapshotConverterCommand {
104104 )
105105 } ) ?;
106106 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,
111121 )
112- } ) ?;
122+ . await
123+ . with_context ( || {
124+ "Failed to download 'snapshot-converter' binary from Cardano node distribution"
125+ } ) ?;
113126
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+ } ) ?;
124135
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+ )
127144 . with_context ( || {
128145 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
131148 )
132149 } ) ?;
133150
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+ } ;
148153
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
150163 }
151164
152165 async fn download_cardano_node_distribution (
@@ -421,6 +434,29 @@ impl SnapshotConverterCommand {
421434
422435 Ok ( ( ) )
423436 }
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+ }
424460}
425461
426462#[ cfg( test) ]
@@ -844,4 +880,78 @@ mod tests {
844880 assert ! ( previous_snapshot. exists( ) ) ;
845881 }
846882 }
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+ }
847957}
0 commit comments