1
1
use bitcoin_hashes:: { sha256, Hash } ;
2
2
use flate2:: read:: GzDecoder ;
3
3
use std:: fs:: File ;
4
- use std:: io:: { BufRead , BufReader , Read } ;
4
+ use std:: io:: { self , BufRead , BufReader , Cursor , Read } ;
5
5
use std:: path:: Path ;
6
6
use std:: str:: FromStr ;
7
7
use tar:: Archive ;
@@ -43,7 +43,7 @@ fn download_filename() -> String {
43
43
44
44
#[ cfg( all( target_os = "windows" , target_arch = "x86_64" ) ) ]
45
45
fn download_filename ( ) -> String {
46
- format ! ( "bitcoin-{}-win64-unsigned.tar.gz " , & VERSION )
46
+ format ! ( "bitcoin-{}-win64.zip " , & VERSION )
47
47
}
48
48
49
49
fn get_expected_sha256 ( filename : & str ) -> sha256:: Hash {
@@ -82,7 +82,7 @@ fn main() {
82
82
let download_filename = download_filename ( ) ;
83
83
let expected_hash = get_expected_sha256 ( & download_filename) ;
84
84
let out_dir = std:: env:: var_os ( "OUT_DIR" ) . unwrap ( ) ;
85
- let bitcoin_exe_home = Path :: new ( & out_dir) . join ( "bitcoin" ) ;
85
+ let mut bitcoin_exe_home = Path :: new ( & out_dir) . join ( "bitcoin" ) ;
86
86
if !bitcoin_exe_home. exists ( ) {
87
87
std:: fs:: create_dir ( & bitcoin_exe_home) . unwrap ( ) ;
88
88
}
@@ -101,23 +101,46 @@ fn main() {
101
101
"https://bitcoincore.org/bin/bitcoin-core-{}/{}" ,
102
102
VERSION , download_filename
103
103
) ;
104
+ println ! ( "url:{}" , url) ;
104
105
let mut downloaded_bytes = Vec :: new ( ) ;
105
106
let resp = ureq:: get ( & url) . call ( ) ;
106
- assert_eq ! ( resp. status( ) , 200 ) ;
107
+ assert_eq ! ( resp. status( ) , 200 , "url {} didn't return 200" , url ) ;
107
108
108
109
let _size = resp
109
110
. into_reader ( )
110
111
. read_to_end ( & mut downloaded_bytes)
111
112
. unwrap ( ) ;
112
113
let downloaded_hash = sha256:: Hash :: hash ( & downloaded_bytes) ;
113
114
assert_eq ! ( expected_hash, downloaded_hash) ;
114
- let d = GzDecoder :: new ( & downloaded_bytes[ ..] ) ;
115
115
116
- let mut archive = Archive :: new ( d) ;
117
- for mut entry in archive. entries ( ) . unwrap ( ) . flatten ( ) {
118
- if let Ok ( file) = entry. path ( ) {
119
- if file. ends_with ( "bitcoind" ) {
120
- entry. unpack_in ( & bitcoin_exe_home) . unwrap ( ) ;
116
+ if download_filename. ends_with ( ".tar.gz" ) {
117
+ let d = GzDecoder :: new ( & downloaded_bytes[ ..] ) ;
118
+
119
+ let mut archive = Archive :: new ( d) ;
120
+ for mut entry in archive. entries ( ) . unwrap ( ) . flatten ( ) {
121
+ if let Ok ( file) = entry. path ( ) {
122
+ if file. ends_with ( "bitcoind" ) {
123
+ entry. unpack_in ( & bitcoin_exe_home) . unwrap ( ) ;
124
+ }
125
+ }
126
+ }
127
+ } else if download_filename. ends_with ( ".zip" ) {
128
+ let cursor = Cursor :: new ( downloaded_bytes) ;
129
+ let mut archive = zip:: ZipArchive :: new ( cursor) . unwrap ( ) ;
130
+ for i in 0 ..zip:: ZipArchive :: len ( & archive) {
131
+ let mut file = archive. by_index ( i) . unwrap ( ) ;
132
+ let outpath = match file. enclosed_name ( ) {
133
+ Some ( path) => path. to_owned ( ) ,
134
+ None => continue ,
135
+ } ;
136
+
137
+ if outpath. file_name ( ) . map ( |s| s. to_str ( ) ) == Some ( Some ( "bitcoind.exe" ) ) {
138
+ bitcoin_exe_home. push ( outpath) ;
139
+ std:: fs:: create_dir_all ( & bitcoin_exe_home) . unwrap ( ) ;
140
+ println ! ( "{:?}" , bitcoin_exe_home) ;
141
+ let mut outfile = std:: fs:: File :: create ( & bitcoin_exe_home) . unwrap ( ) ;
142
+ io:: copy ( & mut file, & mut outfile) . unwrap ( ) ;
143
+ break ;
121
144
}
122
145
}
123
146
}
0 commit comments