]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000, 2001, Red Hat, Inc. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * A copy of the GNU General Public License can be found at | |
10 | * http://www.gnu.org/ | |
11 | * | |
12 | * Written by DJ Delorie <dj@cygnus.com> | |
13 | * | |
14 | */ | |
15 | ||
16 | /* The purpose of this file is to download all the files we need to | |
17 | do the installation. */ | |
18 | ||
19 | #if 0 | |
20 | static const char *cvsid = | |
21 | "\n%%% $Id$\n"; | |
22 | #endif | |
23 | ||
24 | #include "download.h" | |
25 | ||
26 | #include "win32.h" | |
27 | ||
28 | #include <stdio.h> | |
29 | #include <unistd.h> | |
30 | #include <process.h> | |
31 | ||
32 | #include "resource.h" | |
33 | #include "msg.h" | |
34 | #include "dialog.h" | |
35 | #include "String++.h" | |
36 | #include "geturl.h" | |
37 | #include "state.h" | |
38 | #include "LogSingleton.h" | |
39 | #include "filemanip.h" | |
40 | ||
41 | #include "io_stream.h" | |
42 | ||
43 | #include "package_db.h" | |
44 | #include "package_meta.h" | |
45 | #include "package_version.h" | |
46 | #include "package_source.h" | |
47 | ||
48 | #include "rfc1738.h" | |
49 | ||
50 | #include "threebar.h" | |
51 | ||
52 | #include "md5.h" | |
53 | ||
54 | #include "Exception.h" | |
55 | ||
56 | using namespace std; | |
57 | ||
58 | extern ThreeBarProgressPage Progress; | |
59 | ||
60 | ||
61 | bool | |
62 | validateCachedPackage (String const &fullname, packagesource & pkgsource) | |
63 | { | |
64 | if (pkgsource.md5.isSet()) | |
65 | { | |
66 | // check the MD5 sum of the cached file here | |
67 | io_stream *thefile = io_stream::open (fullname, "rb"); | |
68 | if (!thefile) | |
69 | return 0; | |
70 | md5_state_t pns; | |
71 | md5_init (&pns); | |
72 | ||
73 | log (LOG_BABBLE) << "Checking MD5 for " << fullname << endLog; | |
74 | ||
75 | Progress.SetText1 ((String ("Checking MD5 for ") + pkgsource.Base()).cstr_oneuse()); | |
76 | Progress.SetText4 ("Progress:"); | |
77 | Progress.SetBar1 (0); | |
78 | ||
79 | unsigned char buffer[16384]; | |
80 | ssize_t count; | |
81 | while ((count = thefile->read (buffer, 16384)) > 0) | |
82 | { | |
83 | md5_append (&pns, buffer, count); | |
84 | Progress.SetBar1 (thefile->tell(), thefile->get_size()); | |
85 | } | |
86 | delete thefile; | |
87 | if (count < 0) | |
88 | throw new Exception ("__LINE__ __FILE__", String ("IO Error reading ") + pkgsource.Cached(), APPERR_IO_ERROR); | |
89 | ||
90 | md5_byte_t tempdigest[16]; | |
91 | md5_finish(&pns, tempdigest); | |
92 | md5 tempMD5; | |
93 | tempMD5.set (tempdigest); | |
94 | ||
95 | log (LOG_BABBLE) << "For file '" << fullname << | |
96 | " ini digest is " << pkgsource.md5.print() << | |
97 | " file digest is " << tempMD5.print() << endLog; | |
98 | ||
99 | if (pkgsource.md5 != tempMD5) | |
100 | return false; | |
101 | } | |
102 | return true; | |
103 | } | |
104 | ||
105 | /* 0 on failure | |
106 | */ | |
107 | int | |
108 | check_for_cached (packagesource & pkgsource) | |
109 | { | |
110 | /* search algo: | |
111 | 1) is there a legacy version in the cache dir available. | |
112 | (Note that the cache dir is represented by a mirror site of | |
113 | file://local_dir | |
114 | */ | |
115 | ||
116 | // Already found one. | |
117 | if (pkgsource.Cached()) | |
118 | return 1; | |
119 | ||
120 | String prefix = String ("file://") + local_dir + "/"; | |
121 | DWORD size; | |
122 | if ((size = get_file_size (prefix + pkgsource.Canonical ())) > 0) | |
123 | if (size == pkgsource.size) | |
124 | { | |
125 | if (validateCachedPackage (prefix + pkgsource.Canonical (), pkgsource)) | |
126 | pkgsource.set_cached (prefix + pkgsource.Canonical ()); | |
127 | else | |
128 | throw new Exception ("__LINE__ __FILE__", String ("Package validation failure for ") + prefix + pkgsource.Canonical (), APPERR_CORRUPT_PACKAGE); | |
129 | return 1; | |
130 | } | |
131 | ||
132 | /* | |
133 | 2) is there a version from one of the selected mirror sites available ? | |
134 | */ | |
135 | for (packagesource::sitestype::const_iterator n = pkgsource.sites.begin(); | |
136 | n != pkgsource.sites.end(); ++n) | |
137 | { | |
138 | String fullname = prefix + rfc1738_escape_part (n->key) + "/" + | |
139 | pkgsource.Canonical (); | |
140 | if ((size = get_file_size (fullname)) > 0) | |
141 | if (size == pkgsource.size) | |
142 | { | |
143 | if (validateCachedPackage (fullname, pkgsource)) | |
144 | pkgsource.set_cached (fullname ); | |
145 | else | |
146 | throw new Exception ("__LINE__ __FILE__", String ("Package validation failure for ") + fullname, APPERR_CORRUPT_PACKAGE); | |
147 | return 1; | |
148 | } | |
149 | } | |
150 | return 0; | |
151 | } | |
152 | ||
153 | /* download a file from a mirror site to the local cache. */ | |
154 | static int | |
155 | download_one (packagesource & pkgsource, HWND owner) | |
156 | { | |
157 | try | |
158 | { | |
159 | if (check_for_cached (pkgsource)) | |
160 | return 0; | |
161 | } | |
162 | catch (Exception * e) | |
163 | { | |
164 | // We know what to do with these.. | |
165 | if (e->errNo() == APPERR_CORRUPT_PACKAGE) | |
166 | { | |
167 | fatal (owner, IDS_CORRUPT_PACKAGE, pkgsource.Canonical()); | |
168 | return 1; | |
169 | } | |
170 | // Unexpected exception. | |
171 | throw e; | |
172 | } | |
173 | /* try the download sites one after another */ | |
174 | ||
175 | int success = 0; | |
176 | for (packagesource::sitestype::const_iterator n = pkgsource.sites.begin(); | |
177 | n != pkgsource.sites.end() && !success; ++n) | |
178 | { | |
179 | String const local = local_dir + "/" + | |
180 | rfc1738_escape_part (n->key) + "/" + | |
181 | pkgsource.Canonical (); | |
182 | io_stream::mkpath_p (PATH_TO_FILE, String ("file://") + local); | |
183 | ||
184 | if (get_url_to_file(n->key + "/" + pkgsource.Canonical (), | |
185 | local + ".tmp", pkgsource.size, owner)) | |
186 | { | |
187 | /* FIXME: note new source ? */ | |
188 | continue; | |
189 | } | |
190 | else | |
191 | { | |
192 | size_t size = get_file_size (String("file://") + local + ".tmp"); | |
193 | if (size == pkgsource.size) | |
194 | { | |
195 | log (LOG_PLAIN) << "Downloaded " << local << endLog; | |
196 | if (_access (local.cstr_oneuse(), 0) == 0) | |
197 | remove (local.cstr_oneuse()); | |
198 | rename ((local + ".tmp").cstr_oneuse(), local.cstr_oneuse()); | |
199 | success = 1; | |
200 | pkgsource.set_cached (String ("file://") + local); | |
201 | // FIXME: move the downloaded file to the | |
202 | // original locations - without the mirror site dir in the way | |
203 | continue; | |
204 | } | |
205 | else | |
206 | { | |
207 | log (LOG_PLAIN) << "Download " << local << " wrong size (" << | |
208 | size << " actual vs " << pkgsource.size << " expected)" << | |
209 | endLog; | |
210 | remove ((local + ".tmp").cstr_oneuse()); | |
211 | continue; | |
212 | } | |
213 | } | |
214 | } | |
215 | if (success) | |
216 | return 0; | |
217 | /* FIXME: Do we want to note this? if so how? */ | |
218 | return 1; | |
219 | } | |
220 | ||
221 | static void | |
222 | do_download_thread (HINSTANCE h, HWND owner) | |
223 | { | |
224 | int errors = 0; | |
225 | total_download_bytes = 0; | |
226 | total_download_bytes_sofar = 0; | |
227 | ||
228 | packagedb db; | |
229 | /* calculate the amount needed */ | |
230 | for (vector <packagemeta *>::iterator i = db.packages.begin (); | |
231 | i != db.packages.end (); ++i) | |
232 | { | |
233 | packagemeta & pkg = **i; | |
234 | if (pkg.desired.changeRequested()) | |
235 | { | |
236 | packageversion version = pkg.desired; | |
237 | packageversion sourceversion = version.sourcePackage(); | |
238 | try | |
239 | { | |
240 | if (version.picked()) | |
241 | { | |
242 | for (vector<packagesource>::iterator i = | |
243 | version.sources ()->begin(); | |
244 | i != version.sources ()->end(); ++i) | |
245 | if (!check_for_cached (*i)) | |
246 | total_download_bytes += i->size; | |
247 | } | |
248 | if (sourceversion.picked ()) | |
249 | { | |
250 | for (vector<packagesource>::iterator i = | |
251 | sourceversion.sources ()->begin(); | |
252 | i != sourceversion.sources ()->end(); ++i) | |
253 | if (!check_for_cached (*i)) | |
254 | total_download_bytes += i->size; | |
255 | } | |
256 | } | |
257 | catch (Exception * e) | |
258 | { | |
259 | // We know what to do with these.. | |
260 | if (e->errNo() == APPERR_CORRUPT_PACKAGE) | |
261 | fatal (owner, IDS_CORRUPT_PACKAGE, pkg.name.cstr_oneuse()); | |
262 | // Unexpected exception. | |
263 | throw e; | |
264 | } | |
265 | } | |
266 | } | |
267 | ||
268 | /* and do the download. FIXME: This here we assign a new name for the cached version | |
269 | * and check that above. | |
270 | */ | |
271 | for (vector <packagemeta *>::iterator i = db.packages.begin (); | |
272 | i != db.packages.end (); ++i) | |
273 | { | |
274 | packagemeta & pkg = **i; | |
275 | if (pkg.desired.changeRequested()) | |
276 | { | |
277 | int e = 0; | |
278 | packageversion version = pkg.desired; | |
279 | packageversion sourceversion = version.sourcePackage(); | |
280 | if (version.picked()) | |
281 | { | |
282 | for (vector<packagesource>::iterator i = | |
283 | version.sources ()->begin(); | |
284 | i != version.sources ()->end(); ++i) | |
285 | e += download_one (*i, owner); | |
286 | } | |
287 | if (sourceversion && sourceversion.picked()) | |
288 | { | |
289 | for (vector<packagesource>::iterator i = | |
290 | sourceversion.sources ()->begin(); | |
291 | i != sourceversion.sources ()->end(); ++i) | |
292 | e += download_one (*i, owner); | |
293 | } | |
294 | errors += e; | |
295 | #if 0 | |
296 | if (e) | |
297 | pkg->action = ACTION_ERROR; | |
298 | #endif | |
299 | } | |
300 | } | |
301 | ||
302 | if (errors) | |
303 | { | |
304 | if (yesno (owner, IDS_DOWNLOAD_INCOMPLETE) == IDYES) | |
305 | { | |
306 | next_dialog = IDD_SITE; | |
307 | return; | |
308 | } | |
309 | } | |
310 | ||
311 | if (source == IDC_SOURCE_DOWNLOAD) | |
312 | { | |
313 | if (errors) | |
314 | exit_msg = IDS_DOWNLOAD_INCOMPLETE; | |
315 | else if (!unattended_mode) | |
316 | exit_msg = IDS_DOWNLOAD_COMPLETE; | |
317 | next_dialog = 0; | |
318 | } | |
319 | else | |
320 | next_dialog = IDD_S_INSTALL; | |
321 | } | |
322 | ||
323 | static DWORD WINAPI | |
324 | do_download_reflector (void *p) | |
325 | { | |
326 | HANDLE *context; | |
327 | context = (HANDLE *) p; | |
328 | ||
329 | do_download_thread ((HINSTANCE) context[0], (HWND) context[1]); | |
330 | ||
331 | // Tell the progress page that we're done downloading | |
332 | Progress.PostMessage (WM_APP_DOWNLOAD_THREAD_COMPLETE, 0, next_dialog); | |
333 | ||
334 | ExitThread(0); | |
335 | } | |
336 | ||
337 | static HANDLE context[2]; | |
338 | ||
339 | void | |
340 | do_download (HINSTANCE h, HWND owner) | |
341 | { | |
342 | context[0] = h; | |
343 | context[1] = owner; | |
344 | ||
345 | DWORD threadID; | |
346 | CreateThread (NULL, 0, do_download_reflector, context, 0, &threadID); | |
347 | } |