]> cygwin.com Git - cygwin-apps/setup.git/blob - download.cc
2002-02-19 Robert Collins <rbtcollins@hotmail.com>
[cygwin-apps/setup.git] / download.cc
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 "win32.h"
25
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <process.h>
29
30 #include "resource.h"
31 #include "msg.h"
32 #include "ini.h"
33 #include "dialog.h"
34 #include "String++.h"
35 #include "geturl.h"
36 #include "state.h"
37 #include "mkdir.h"
38 #include "log.h"
39 #include "filemanip.h"
40 #include "port.h"
41
42 #include "io_stream.h"
43
44 #include "package_db.h"
45 #include "package_meta.h"
46 #include "package_version.h"
47 #include "package_source.h"
48
49 #include "rfc1738.h"
50
51 #include "threebar.h"
52 extern ThreeBarProgressPage Progress;
53
54 /* 0 on failure
55 */
56 static int
57 check_for_cached (packagesource & pkgsource)
58 {
59 /* search algo:
60 1) is there a legacy version in the cache dir available.
61 (Note that the cache dir is represented by a mirror site of
62 file://local_dir
63 */
64
65 DWORD size;
66 if ((size =
67 get_file_size (local_dir + "/" + pkgsource.Canonical ())) >
68 0)
69 if (size == pkgsource.size)
70 {
71 pkgsource.
72 set_cached (String ("file://") + local_dir + "/" + pkgsource.Canonical ());
73 return 1;
74 }
75
76 /*
77 2) is there a version from one of the selected mirror sites available ?
78 */
79 for (size_t n = 1; n <= pkgsource.sites.number (); n++)
80 if ((size =
81 get_file_size (local_dir + "/" +
82 rfc1738_escape_part (pkgsource.sites[n]->key) + "/" +
83 pkgsource.Canonical ())) > 0)
84 if (size == pkgsource.size)
85 {
86 pkgsource.
87 set_cached (String ("file://") + local_dir + "/" +
88 rfc1738_escape_part (pkgsource.sites[n]->
89 key) + "/" +
90 pkgsource.Canonical ());
91 return 1;
92 }
93
94 return 0;
95 }
96
97 /* download a file from a mirror site to the local cache. */
98 static int
99 download_one (packagesource & pkgsource, HWND owner)
100 {
101 if (check_for_cached (pkgsource) && source != IDC_SOURCE_DOWNLOAD)
102 return 0;
103
104 /* try the download sites one after another */
105
106 int success = 0;
107 for (size_t n = 1; n <= pkgsource.sites.number () && !success; n++)
108 {
109 String const local = local_dir + "/" +
110 rfc1738_escape_part (pkgsource.sites[n]->
111 key) + "/" +
112 pkgsource.Canonical ();
113 io_stream::mkpath_p (PATH_TO_FILE, (String ("file://") + local).cstr_oneuse());
114
115 if (get_url_to_file(String (pkgsource.sites[n]->key) + "/" + pkgsource.Canonical (),
116 local + ".tmp", pkgsource.size, owner))
117 {
118 /* FIXME: note new source ? */
119 continue;
120 }
121 else
122 {
123 size_t size = get_file_size (local + ".tmp");
124 if (size == pkgsource.size)
125 {
126 log (LOG_TIMESTAMP, "Downloaded %s", local.cstr_oneuse());
127 if (_access (local.cstr_oneuse(), 0) == 0)
128 remove (local.cstr_oneuse());
129 rename ((local + ".tmp").cstr_oneuse(), local.cstr_oneuse());
130 success = 1;
131 pkgsource.set_cached (String ("file://") + local);
132 // FIXME: move the downloaded file to the
133 // original locations - without the mirror site dir in the way
134 continue;
135 }
136 else
137 {
138 log (LOG_TIMESTAMP, "Download %s wrong size (%u actual vs %d expected)",
139 local.cstr_oneuse(), size, pkgsource.size);
140 remove ((local + ".tmp").cstr_oneuse());
141 continue;
142 }
143 }
144 }
145 if (success)
146 return 0;
147 /* FIXME: Do we want to note this? if so how? */
148 return 1;
149 }
150
151 static void
152 do_download_thread (HINSTANCE h, HWND owner)
153 {
154 int errors = 0;
155 total_download_bytes = 0;
156 total_download_bytes_sofar = 0;
157
158 packagedb db;
159 /* calculate the amount needed */
160 for (size_t n = 1; n < db.packages.number (); n++)
161 {
162 packagemeta & pkg = *db.packages[n];
163 if (pkg.desired && (pkg.desired->srcpicked || pkg.desired->binpicked))
164 {
165 packageversion *version = pkg.desired;
166 if (!
167 (check_for_cached (version->bin)
168 && source != IDC_SOURCE_DOWNLOAD) && pkg.desired->binpicked)
169 total_download_bytes += version->bin.size;
170 if (!
171 (check_for_cached (version->src)
172 && source != IDC_SOURCE_DOWNLOAD) && pkg.desired->srcpicked)
173 total_download_bytes += version->src.size;
174 }
175 }
176
177 /* and do the download. FIXME: This here we assign a new name for the cached version
178 * and check that above.
179 */
180 for (size_t n = 1; n < db.packages.number (); n++)
181 {
182 packagemeta & pkg = *db.packages[n];
183 if (pkg.desired && (pkg.desired->srcpicked || pkg.desired->binpicked))
184 {
185 int e = 0;
186 packageversion *version = pkg.desired;
187 if (version->binpicked)
188 e += download_one (version->bin, owner);
189 if (version->srcpicked)
190 e += download_one (version->src, owner);
191 errors += e;
192 #if 0
193 if (e)
194 pkg->action = ACTION_ERROR;
195 #endif
196 }
197 }
198
199 if (errors)
200 {
201 if (yesno (owner, IDS_DOWNLOAD_INCOMPLETE) == IDYES)
202 {
203 next_dialog = IDD_SITE;
204 return;
205 }
206 }
207
208 if (source == IDC_SOURCE_DOWNLOAD)
209 {
210 if (errors)
211 exit_msg = IDS_DOWNLOAD_INCOMPLETE;
212 else
213 exit_msg = IDS_DOWNLOAD_COMPLETE;
214 next_dialog = 0;
215 }
216 else
217 next_dialog = IDD_S_INSTALL;
218 }
219
220 static void
221 do_download_reflector (void *p)
222 {
223 HANDLE *context;
224 context = (HANDLE *) p;
225
226 do_download_thread ((HINSTANCE) context[0], (HWND) context[1]);
227
228 // Tell the progress page that we're done downloading
229 Progress.PostMessage (WM_APP_DOWNLOAD_THREAD_COMPLETE, 0, next_dialog);
230
231 _endthread ();
232 }
233
234 static HANDLE context[2];
235
236 void
237 do_download (HINSTANCE h, HWND owner)
238 {
239 context[0] = h;
240 context[1] = owner;
241
242 _beginthread (do_download_reflector, 0, context);
243 }
This page took 0.043159 seconds and 5 git commands to generate.