Wire up de-DE and pl-PL translations
[cygwin-setup.git] / download.cc
blob5bfcebf0957d26f122faa07324230946174d6c93
1 /*
2 * Copyright (c) 2000, 2001, Red Hat, Inc.
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.
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
12 * Written by DJ Delorie <dj@cygnus.com>
16 /* The purpose of this file is to download all the files we need to
17 do the installation. */
19 #include "csu_util/rfc1738.h"
21 #include "download.h"
23 #include "win32.h"
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <process.h>
28 #include <vector>
30 #include "resource.h"
31 #include "msg.h"
32 #include "dialog.h"
33 #include "geturl.h"
34 #include "state.h"
35 #include "LogFile.h"
36 #include "filemanip.h"
38 #include "io_stream.h"
40 #include "package_db.h"
41 #include "package_meta.h"
42 #include "package_source.h"
44 #include "threebar.h"
46 #include "Exception.h"
48 extern ThreeBarProgressPage Progress;
50 // Return true if selected checks pass, false if they don't and the
51 // user chooses to delete the file; otherwise throw an exception.
52 static bool
53 validateCachedPackage (const std::string& fullname, packagesource & pkgsource,
54 HWND owner, bool check_hash, bool check_size)
56 try
58 if (check_size)
59 pkgsource.check_size_and_cache (fullname);
60 if (check_hash)
61 pkgsource.check_hash ();
62 return true;
64 catch (Exception *e)
66 pkgsource.set_cached ("");
67 const char *filename = fullname.c_str ();
68 if (strncmp (filename, "file://", 7) == 0)
69 filename += 7;
70 if (e->errNo() == APPERR_CORRUPT_PACKAGE
71 && yesno (owner, IDS_QUERY_CORRUPT, filename) == IDYES)
72 remove (filename);
73 else
74 throw e;
76 return false;
79 /* 0 if not cached; may throw exception if validation fails.
81 int
82 check_for_cached (packagesource & pkgsource, HWND owner, bool mirror_mode,
83 bool check_hash)
85 /* If the packagesource doesn't have a filename, it can't possibly be in the
86 cache */
87 if (!pkgsource.Canonical())
89 return 0;
92 /* Note that the cache dir is represented by a mirror site of file://local_dir */
93 std::string prefix = "file://" + local_dir + "/";
94 std::string fullname = prefix + pkgsource.Canonical();
96 if (mirror_mode)
98 /* Just assume correctness of mirror. */
99 if (!pkgsource.Cached())
100 pkgsource.set_cached (fullname);
101 return 1;
104 // Already found one, which we can assume to have the right size.
105 if (pkgsource.Cached())
107 if (validateCachedPackage (pkgsource.Cached(), pkgsource, owner,
108 check_hash, false))
109 return 1;
110 // If we get here, pkgsource.Cached() was corrupt and deleted.
111 pkgsource.set_cached ("");
115 1) is there a legacy version in the cache dir available.
117 if (io_stream::exists (fullname))
119 if (validateCachedPackage (fullname, pkgsource, owner, check_hash, true))
120 return 1;
121 // If we get here, fullname was corrupt and deleted, but it
122 // might have been cached.
123 pkgsource.set_cached ("");
127 2) is there a version from one of the selected mirror sites available ?
129 for (packagesource::sitestype::const_iterator n = pkgsource.sites.begin();
130 n != pkgsource.sites.end(); ++n)
132 std::string fullname = prefix + rfc1738_escape_part (n->key) + "/" +
133 pkgsource.Canonical ();
134 if (io_stream::exists(fullname))
136 if (validateCachedPackage (fullname, pkgsource, owner, check_hash,
137 true))
138 return 1;
139 // If we get here, fullname was corrupt and deleted, but it
140 // might have been cached.
141 pkgsource.set_cached ("");
144 return 0;
147 /* download a file from a mirror site to the local cache. */
148 static int
149 download_one (packagesource & pkgsource, HWND owner)
153 if (check_for_cached (pkgsource, owner))
154 return 0;
156 catch (Exception * e)
158 // We know what to do with these..
159 if (e->errNo() == APPERR_CORRUPT_PACKAGE)
161 fatal (owner, IDS_CORRUPT_PACKAGE, pkgsource.Canonical());
162 return 1;
164 // Unexpected exception.
165 throw e;
167 /* try the download sites one after another */
169 int success = 0;
170 for (packagesource::sitestype::const_iterator n = pkgsource.sites.begin();
171 n != pkgsource.sites.end() && !success; ++n)
173 const std::string local = local_dir + "/" +
174 rfc1738_escape_part (n->key) + "/" +
175 pkgsource.Canonical ();
176 io_stream::mkpath_p (PATH_TO_FILE, "file://" + local, 0);
178 if (get_url_to_file(n->key + pkgsource.Canonical (),
179 local + ".tmp", pkgsource.size, owner))
181 /* FIXME: note new source ? */
182 continue;
184 else
188 if (_access (local.c_str(), 0) == 0)
189 remove (local.c_str());
190 rename ((local + ".tmp").c_str(), local.c_str());
191 pkgsource.check_size_and_cache ("file://" + local);
192 pkgsource.check_hash ();
193 Log (LOG_PLAIN) << "Downloaded " << local << endLog;
194 success = 1;
195 // FIXME: move the downloaded file to the
196 // original locations - without the mirror site dir in the way
197 continue;
199 catch (Exception *e)
201 remove (local.c_str());
202 pkgsource.set_cached ("");
203 if (e->errNo() == APPERR_CORRUPT_PACKAGE)
205 Log (LOG_PLAIN) << "Downloaded file " << local
206 << " is corrupt; deleting." << endLog;
207 continue;
209 else
211 Log (LOG_PLAIN) << "Unexpected exception while validating "
212 << "downloaded file " << local
213 << "; deleting." << endLog;
214 throw e;
219 if (success)
220 return 0;
221 return 1;
224 static std::vector <packageversion> download_failures;
225 static std::string download_warn_pkgs;
227 static INT_PTR CALLBACK
228 download_error_proc (HWND h, UINT message, WPARAM wParam, LPARAM lParam)
230 switch (message)
232 case WM_INITDIALOG:
233 eset (h, IDC_DOWNLOAD_EDIT, download_warn_pkgs);
234 SetFocus (GetDlgItem(h, IDRETRY));
235 return FALSE;
237 case WM_COMMAND:
238 switch (LOWORD (wParam))
240 case IDRETRY:
241 case IDC_BACK:
242 case IDIGNORE:
243 case IDABORT:
244 EndDialog (h, LOWORD (wParam));
245 default:
246 // Not reached.
247 return 0;
250 default:
251 // Not handled.
252 return FALSE;
254 return TRUE;
257 static int
258 query_download_errors (HINSTANCE h, HWND owner)
260 download_warn_pkgs = "";
261 Log (LOG_PLAIN) << "The following package(s) had download errors:" << endLog;
262 for (std::vector <packageversion>::const_iterator i = download_failures.begin (); i != download_failures.end (); i++)
264 packageversion pv = *i;
265 std::string pvs = pv.Name () + "-" + pv.Canonical_version ();
266 Log (LOG_PLAIN) << " " << pvs << endLog;
267 download_warn_pkgs += pvs + "\r\n";
269 return DialogBox (h, MAKEINTRESOURCE (IDD_DOWNLOAD_ERROR), owner,
270 download_error_proc);
273 static int
274 do_download_thread (HINSTANCE h, HWND owner)
276 int errors = 0;
277 total_download_bytes = 0;
278 total_download_bytes_sofar = 0;
279 download_failures.clear ();
281 Progress.SetText1 (IDS_PROGRESS_CHECKING);
282 Progress.SetText2 ("");
283 Progress.SetText3 ("");
285 packagedb db;
286 const SolverTransactionList &t = db.solution.transactions();
288 /* calculate the total size of the download */
289 for (SolverTransactionList::const_iterator i = t.begin (); i != t.end (); ++i)
291 if (i->type != SolverTransaction::transInstall)
292 continue;
293 packageversion version = i->version;
297 if (!check_for_cached (*version.source(), owner))
298 total_download_bytes += version.source()->size;
300 catch (Exception * e)
302 // We know what to do with these..
303 if (e->errNo() == APPERR_CORRUPT_PACKAGE)
304 fatal (owner, IDS_CORRUPT_PACKAGE, version.Name().c_str());
305 // Unexpected exception.
306 throw e;
308 Progress.SetBar2(std::distance(t.begin(), i) + 1, t.size());
311 /* and do the download. FIXME: This here we assign a new name for the cached version
312 * and check that above.
314 for (SolverTransactionList::const_iterator i = t.begin (); i != t.end (); ++i)
316 if (i->type != SolverTransaction::transInstall)
317 continue;
318 packageversion version = i->version;
321 int e = 0;
322 e += download_one (*version.source(), owner);
323 errors += e;
324 if (e)
325 download_failures.push_back (version);
326 #if 0
327 if (e)
328 pkg->action = ACTION_ERROR;
329 #endif
333 if (errors)
335 // In unattended mode we retry the download, but not forever.
336 static int retries = 5;
337 int rc;
338 if (unattended_mode && --retries <= 0)
340 Log (LOG_PLAIN) << "download error in unattended_mode: out of retries" << endLog;
341 rc = IDABORT;
343 else if (unattended_mode)
345 Log (LOG_PLAIN) << "download error in unattended_mode: " << retries
346 << (retries > 1 ? " retries" : " retry") << " remaining." << endLog;
347 rc = IDRETRY;
349 else
350 rc = query_download_errors (h, owner);
351 switch (rc)
353 case IDRETRY:
354 Progress.SetActivateTask (WM_APP_START_DOWNLOAD);
355 return IDD_INSTATUS;
356 case IDC_BACK:
357 return IDD_CHOOSE;
358 case IDABORT:
359 Logger ().setExitMsg (IDS_DOWNLOAD_INCOMPLETE_EXIT);
360 Logger ().exit (1);
361 case IDIGNORE:
362 break;
363 default:
364 break;
368 if (source == IDC_SOURCE_DOWNLOAD)
370 if (errors)
371 Logger ().setExitMsg (IDS_DOWNLOAD_INCOMPLETE_EXIT);
372 else if (!unattended_mode)
373 Logger ().setExitMsg (IDS_DOWNLOAD_COMPLETE);
374 return IDD_DESKTOP;
376 else
377 return IDD_S_INSTALL;
380 static DWORD WINAPI
381 do_download_reflector (void *p)
383 HANDLE *context;
384 context = (HANDLE *) p;
386 SetThreadUILanguage(langid);
390 int next_dialog =
391 do_download_thread ((HINSTANCE) context[0], (HWND) context[1]);
393 // Tell the progress page that we're done downloading
394 Progress.PostMessageNow (WM_APP_DOWNLOAD_THREAD_COMPLETE, 0, next_dialog);
396 TOPLEVEL_CATCH((HWND) context[1], "download");
398 ExitThread(0);
401 static HANDLE context[2];
403 void
404 do_download (HINSTANCE h, HWND owner)
406 context[0] = h;
407 context[1] = owner;
409 DWORD threadID;
410 CreateThread (NULL, 0, do_download_reflector, context, 0, &threadID);