1
- use std:: env:: current_dir;
1
+ use std:: { env:: current_dir, fs , ops :: Not as _ } ;
2
2
3
3
use snm_atom:: { atom:: AtomTrait as _, node_atom:: NodeAtom } ;
4
4
use snm_config:: parse_snm_config;
5
+ use snm_download_builder:: { DownloadBuilder , WriteStrategy } ;
5
6
use snm_utils:: snm_error:: SnmError ;
6
7
use tracing:: { instrument, Level } ;
7
8
@@ -14,7 +15,6 @@ pub async fn get_node_bin_dir() -> Result<String, SnmError> {
14
15
let snm_config = parse_snm_config ( & dir) ?;
15
16
16
17
let snm_node = NodeAtom :: new ( snm_config. clone ( ) ) ;
17
-
18
18
let version = if let Some ( version) = snm_config. get_runtime_node_version ( ) {
19
19
version
20
20
} else if snm_config. get_strict ( ) {
@@ -24,6 +24,63 @@ pub async fn get_node_bin_dir() -> Result<String, SnmError> {
24
24
version. ok_or ( SnmError :: NoDefaultNodeBinary ) ?
25
25
} ;
26
26
27
+ if snm_node
28
+ . get_anchor_file_path_buf ( version. as_str ( ) ) ?
29
+ . exists ( )
30
+ . not ( )
31
+ {
32
+ if snm_node. get_snm_config ( ) . get_strict ( ) {
33
+ return Err ( SnmError :: UnsupportedNodeVersionError {
34
+ version : version. to_string ( ) ,
35
+ } ) ;
36
+ } else {
37
+ let download_url = snm_node. get_download_url ( & version) ;
38
+
39
+ let downloaded_file_path_buf = snm_node. get_downloaded_file_path_buf ( & version) ?;
40
+
41
+ DownloadBuilder :: new ( )
42
+ . retries ( 3 )
43
+ . timeout ( snm_node. get_snm_config ( ) . get_download_timeout_secs ( ) )
44
+ . write_strategy ( WriteStrategy :: WriteAfterDelete )
45
+ . download ( & download_url, & downloaded_file_path_buf)
46
+ . await ?;
47
+
48
+ let runtime_dir_path_buf = snm_node. get_runtime_dir_path_buf ( & version) ?;
49
+
50
+ let downloaded_file_path_buf = snm_node. get_downloaded_file_path_buf ( & version) ?;
51
+
52
+ let expect = snm_node. get_expect_shasum ( & version) . await ?;
53
+
54
+ let actual = snm_node
55
+ . get_actual_shasum ( & downloaded_file_path_buf)
56
+ . await ?;
57
+
58
+ if actual. is_none ( ) || expect. is_none ( ) {
59
+ fs:: remove_file ( & downloaded_file_path_buf) ?;
60
+ return Err ( SnmError :: ShasumError {
61
+ file_path : downloaded_file_path_buf. display ( ) . to_string ( ) ,
62
+ expect : "None" . to_string ( ) ,
63
+ actual : "None" . to_string ( ) ,
64
+ } ) ;
65
+ }
66
+
67
+ if actual. eq ( & expect) . not ( ) {
68
+ fs:: remove_file ( & downloaded_file_path_buf) ?;
69
+ return Err ( SnmError :: ShasumError {
70
+ file_path : downloaded_file_path_buf. display ( ) . to_string ( ) ,
71
+ expect : expect. unwrap_or ( "None" . to_string ( ) ) ,
72
+ actual : actual. unwrap_or ( "None" . to_string ( ) ) ,
73
+ } ) ;
74
+ }
75
+
76
+ snm_node. decompress_download_file ( & downloaded_file_path_buf, & runtime_dir_path_buf) ?;
77
+
78
+ if let Some ( parent) = downloaded_file_path_buf. parent ( ) {
79
+ fs:: remove_dir_all ( parent) ?;
80
+ }
81
+ }
82
+ }
83
+
27
84
let binary_dir_string = ensure_binary_path ( & snm_node, & version, true ) . await ?;
28
85
29
86
Ok ( binary_dir_string)
0 commit comments