|
1 |
| -use bitcoin_hashes::{sha256, Hash}; |
2 |
| -use flate2::read::GzDecoder; |
3 |
| -use std::fs::File; |
4 |
| -use std::io::{self, BufRead, BufReader, Cursor, Read}; |
5 |
| -use std::path::Path; |
6 |
| -use std::str::FromStr; |
7 |
| -use tar::Archive; |
8 |
| - |
9 |
| -include!("src/versions.rs"); |
10 |
| - |
11 |
| -#[cfg(all( |
12 |
| - target_os = "macos", |
13 |
| - any(target_arch = "x86_64", target_arch = "aarch64"), |
14 |
| -))] |
15 |
| -fn download_filename() -> String { |
16 |
| - if cfg!(any( |
17 |
| - feature = "22_0", |
18 |
| - feature = "0_21_1", |
19 |
| - feature = "0_21_0", |
20 |
| - feature = "0_20_1", |
21 |
| - feature = "0_20_0", |
22 |
| - feature = "0_19_1", |
23 |
| - feature = "0_19_0_1", |
24 |
| - feature = "0_18_1", |
25 |
| - feature = "0_18_0", |
26 |
| - feature = "0_17_1", |
27 |
| - )) { |
28 |
| - format!("bitcoin-{}-osx64.tar.gz", &VERSION) |
29 |
| - } else { |
30 |
| - format!("bitcoin-{}-x86_64-apple-darwin.tar.gz", &VERSION) |
31 |
| - } |
32 |
| -} |
| 1 | +#[cfg(not(feature = "download"))] |
| 2 | +mod download {} |
33 | 3 |
|
34 |
| -#[cfg(all(target_os = "linux", target_arch = "x86_64"))] |
35 |
| -fn download_filename() -> String { |
36 |
| - format!("bitcoin-{}-x86_64-linux-gnu.tar.gz", &VERSION) |
37 |
| -} |
| 4 | +#[cfg(not(feature = "download"))] |
| 5 | +fn main() {} |
38 | 6 |
|
39 |
| -#[cfg(all(target_os = "linux", target_arch = "aarch64"))] |
40 |
| -fn download_filename() -> String { |
41 |
| - format!("bitcoin-{}-aarch64-linux-gnu.tar.gz", &VERSION) |
| 7 | +#[cfg(feature = "download")] |
| 8 | +fn main() { |
| 9 | + download::start(); |
42 | 10 | }
|
43 | 11 |
|
44 |
| -#[cfg(all(target_os = "windows", target_arch = "x86_64"))] |
45 |
| -fn download_filename() -> String { |
46 |
| - format!("bitcoin-{}-win64.zip", &VERSION) |
47 |
| -} |
| 12 | +#[cfg(feature = "download")] |
| 13 | +mod download { |
| 14 | + |
| 15 | + use bitcoin_hashes::{sha256, Hash}; |
| 16 | + use flate2::read::GzDecoder; |
| 17 | + use std::fs::File; |
| 18 | + use std::io::{self, BufRead, BufReader, Cursor, Read}; |
| 19 | + use std::path::Path; |
| 20 | + use std::str::FromStr; |
| 21 | + use tar::Archive; |
| 22 | + |
| 23 | + include!("src/versions.rs"); |
48 | 24 |
|
49 |
| -fn get_expected_sha256(filename: &str) -> sha256::Hash { |
50 |
| - let sha256sums_filename = format!("sha256/bitcoin-core-{}-SHA256SUMS", &VERSION); |
51 |
| - #[cfg(any( |
52 |
| - feature = "0_21_1", |
53 |
| - feature = "0_21_0", |
54 |
| - feature = "0_20_1", |
55 |
| - feature = "0_20_0", |
56 |
| - feature = "0_19_1", |
57 |
| - feature = "0_19_0_1", |
58 |
| - feature = "0_18_1", |
59 |
| - feature = "0_18_0", |
60 |
| - feature = "0_17_1", |
| 25 | + #[cfg(all( |
| 26 | + target_os = "macos", |
| 27 | + any(target_arch = "x86_64", target_arch = "aarch64"), |
61 | 28 | ))]
|
62 |
| - let sha256sums_filename = format!("{}.asc", sha256sums_filename); |
63 |
| - let file = File::open(&sha256sums_filename).unwrap(); |
64 |
| - for line in BufReader::new(file).lines().flatten() { |
65 |
| - let tokens: Vec<_> = line.split(" ").collect(); |
66 |
| - if tokens.len() == 2 && filename == tokens[1] { |
67 |
| - return sha256::Hash::from_str(tokens[0]).unwrap(); |
| 29 | + fn download_filename() -> String { |
| 30 | + if cfg!(any( |
| 31 | + feature = "22_0", |
| 32 | + feature = "0_21_1", |
| 33 | + feature = "0_21_0", |
| 34 | + feature = "0_20_1", |
| 35 | + feature = "0_20_0", |
| 36 | + feature = "0_19_1", |
| 37 | + feature = "0_19_0_1", |
| 38 | + feature = "0_18_1", |
| 39 | + feature = "0_18_0", |
| 40 | + feature = "0_17_1", |
| 41 | + )) { |
| 42 | + format!("bitcoin-{}-osx64.tar.gz", &VERSION) |
| 43 | + } else { |
| 44 | + format!("bitcoin-{}-x86_64-apple-darwin.tar.gz", &VERSION) |
68 | 45 | }
|
69 | 46 | }
|
70 |
| - panic!( |
71 |
| - "Couldn't find hash for `{}` in `{}`:\n{}", |
72 |
| - filename, |
73 |
| - sha256sums_filename, |
74 |
| - std::fs::read_to_string(&sha256sums_filename).unwrap() |
75 |
| - ); |
76 |
| -} |
77 | 47 |
|
78 |
| -fn main() { |
79 |
| - if !HAS_FEATURE { |
80 |
| - return; |
| 48 | + #[cfg(all(target_os = "linux", target_arch = "x86_64"))] |
| 49 | + fn download_filename() -> String { |
| 50 | + format!("bitcoin-{}-x86_64-linux-gnu.tar.gz", &VERSION) |
| 51 | + } |
| 52 | + |
| 53 | + #[cfg(all(target_os = "linux", target_arch = "aarch64"))] |
| 54 | + fn download_filename() -> String { |
| 55 | + format!("bitcoin-{}-aarch64-linux-gnu.tar.gz", &VERSION) |
81 | 56 | }
|
82 |
| - let download_filename = download_filename(); |
83 |
| - let expected_hash = get_expected_sha256(&download_filename); |
84 |
| - let out_dir = std::env::var_os("OUT_DIR").unwrap(); |
85 |
| - let mut bitcoin_exe_home = Path::new(&out_dir).join("bitcoin"); |
86 |
| - if !bitcoin_exe_home.exists() { |
87 |
| - std::fs::create_dir(&bitcoin_exe_home).unwrap(); |
| 57 | + |
| 58 | + #[cfg(all(target_os = "windows", target_arch = "x86_64"))] |
| 59 | + fn download_filename() -> String { |
| 60 | + format!("bitcoin-{}-win64.zip", &VERSION) |
88 | 61 | }
|
89 |
| - let existing_filename = bitcoin_exe_home |
90 |
| - .join(format!("bitcoin-{}", VERSION)) |
91 |
| - .join("bin") |
92 |
| - .join("bitcoind"); |
93 |
| - |
94 |
| - if !existing_filename.exists() { |
95 |
| - println!( |
96 |
| - "filename:{} version:{} hash:{}", |
97 |
| - download_filename, VERSION, expected_hash |
98 |
| - ); |
99 | 62 |
|
100 |
| - let url = format!( |
101 |
| - "https://bitcoincore.org/bin/bitcoin-core-{}/{}", |
102 |
| - VERSION, download_filename |
| 63 | + fn get_expected_sha256(filename: &str) -> sha256::Hash { |
| 64 | + let sha256sums_filename = format!("sha256/bitcoin-core-{}-SHA256SUMS", &VERSION); |
| 65 | + #[cfg(any( |
| 66 | + feature = "0_21_1", |
| 67 | + feature = "0_21_0", |
| 68 | + feature = "0_20_1", |
| 69 | + feature = "0_20_0", |
| 70 | + feature = "0_19_1", |
| 71 | + feature = "0_19_0_1", |
| 72 | + feature = "0_18_1", |
| 73 | + feature = "0_18_0", |
| 74 | + feature = "0_17_1", |
| 75 | + ))] |
| 76 | + let sha256sums_filename = format!("{}.asc", sha256sums_filename); |
| 77 | + let file = File::open(&sha256sums_filename).unwrap(); |
| 78 | + for line in BufReader::new(file).lines().flatten() { |
| 79 | + let tokens: Vec<_> = line.split(" ").collect(); |
| 80 | + if tokens.len() == 2 && filename == tokens[1] { |
| 81 | + return sha256::Hash::from_str(tokens[0]).unwrap(); |
| 82 | + } |
| 83 | + } |
| 84 | + panic!( |
| 85 | + "Couldn't find hash for `{}` in `{}`:\n{}", |
| 86 | + filename, |
| 87 | + sha256sums_filename, |
| 88 | + std::fs::read_to_string(&sha256sums_filename).unwrap() |
103 | 89 | );
|
104 |
| - println!("url:{}", url); |
105 |
| - let mut downloaded_bytes = Vec::new(); |
106 |
| - let resp = ureq::get(&url).call().unwrap(); |
107 |
| - assert_eq!(resp.status(), 200, "url {} didn't return 200", url); |
108 |
| - |
109 |
| - let _size = resp |
110 |
| - .into_reader() |
111 |
| - .read_to_end(&mut downloaded_bytes) |
112 |
| - .unwrap(); |
113 |
| - let downloaded_hash = sha256::Hash::hash(&downloaded_bytes); |
114 |
| - assert_eq!(expected_hash, downloaded_hash); |
115 |
| - |
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(); |
| 90 | + } |
| 91 | + |
| 92 | + pub(crate) fn start() { |
| 93 | + let download_filename = download_filename(); |
| 94 | + let expected_hash = get_expected_sha256(&download_filename); |
| 95 | + let out_dir = std::env::var_os("OUT_DIR").unwrap(); |
| 96 | + let mut bitcoin_exe_home = Path::new(&out_dir).join("bitcoin"); |
| 97 | + if !bitcoin_exe_home.exists() { |
| 98 | + std::fs::create_dir(&bitcoin_exe_home).unwrap(); |
| 99 | + } |
| 100 | + let existing_filename = bitcoin_exe_home |
| 101 | + .join(format!("bitcoin-{}", VERSION)) |
| 102 | + .join("bin") |
| 103 | + .join("bitcoind"); |
| 104 | + |
| 105 | + if !existing_filename.exists() { |
| 106 | + println!( |
| 107 | + "filename:{} version:{} hash:{}", |
| 108 | + download_filename, VERSION, expected_hash |
| 109 | + ); |
| 110 | + |
| 111 | + let url = format!( |
| 112 | + "https://bitcoincore.org/bin/bitcoin-core-{}/{}", |
| 113 | + VERSION, download_filename |
| 114 | + ); |
| 115 | + println!("url:{}", url); |
| 116 | + let mut downloaded_bytes = Vec::new(); |
| 117 | + let resp = ureq::get(&url).call().unwrap(); |
| 118 | + assert_eq!(resp.status(), 200, "url {} didn't return 200", url); |
| 119 | + |
| 120 | + let _size = resp |
| 121 | + .into_reader() |
| 122 | + .read_to_end(&mut downloaded_bytes) |
| 123 | + .unwrap(); |
| 124 | + let downloaded_hash = sha256::Hash::hash(&downloaded_bytes); |
| 125 | + assert_eq!(expected_hash, downloaded_hash); |
| 126 | + |
| 127 | + if download_filename.ends_with(".tar.gz") { |
| 128 | + let d = GzDecoder::new(&downloaded_bytes[..]); |
| 129 | + |
| 130 | + let mut archive = Archive::new(d); |
| 131 | + for mut entry in archive.entries().unwrap().flatten() { |
| 132 | + if let Ok(file) = entry.path() { |
| 133 | + if file.ends_with("bitcoind") { |
| 134 | + entry.unpack_in(&bitcoin_exe_home).unwrap(); |
| 135 | + } |
124 | 136 | }
|
125 | 137 | }
|
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 |
| - for d in outpath.iter() { |
139 |
| - bitcoin_exe_home.push(d); |
| 138 | + } else if download_filename.ends_with(".zip") { |
| 139 | + let cursor = Cursor::new(downloaded_bytes); |
| 140 | + let mut archive = zip::ZipArchive::new(cursor).unwrap(); |
| 141 | + for i in 0..zip::ZipArchive::len(&archive) { |
| 142 | + let mut file = archive.by_index(i).unwrap(); |
| 143 | + let outpath = match file.enclosed_name() { |
| 144 | + Some(path) => path.to_owned(), |
| 145 | + None => continue, |
| 146 | + }; |
| 147 | + |
| 148 | + if outpath.file_name().map(|s| s.to_str()) == Some(Some("bitcoind.exe")) { |
| 149 | + for d in outpath.iter() { |
| 150 | + bitcoin_exe_home.push(d); |
| 151 | + } |
| 152 | + std::fs::create_dir_all(&bitcoin_exe_home.parent().unwrap()).unwrap(); |
| 153 | + println!("{:?}", bitcoin_exe_home); |
| 154 | + let mut outfile = std::fs::File::create(&bitcoin_exe_home).unwrap(); |
| 155 | + io::copy(&mut file, &mut outfile).unwrap(); |
| 156 | + break; |
140 | 157 | }
|
141 |
| - std::fs::create_dir_all(&bitcoin_exe_home.parent().unwrap()).unwrap(); |
142 |
| - println!("{:?}", bitcoin_exe_home); |
143 |
| - let mut outfile = std::fs::File::create(&bitcoin_exe_home).unwrap(); |
144 |
| - io::copy(&mut file, &mut outfile).unwrap(); |
145 |
| - break; |
146 | 158 | }
|
147 | 159 | }
|
148 | 160 | }
|
|
0 commit comments