]>
Commit | Line | Data |
---|---|---|
23c9e63c | 1 | /* |
9fe1181b | 2 | * Copyright (c) 2000, 2001, Red Hat, Inc. |
23c9e63c DD |
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 | ||
b24c88b3 RC |
19 | #if 0 |
20 | static const char *cvsid = | |
21 | "\n%%% $Id$\n"; | |
22 | #endif | |
8507f105 | 23 | |
23c9e63c DD |
24 | #include "win32.h" |
25 | ||
26 | #include <stdio.h> | |
42bf5b92 | 27 | #include <unistd.h> |
ab57ceaa | 28 | #include <process.h> |
23c9e63c DD |
29 | |
30 | #include "resource.h" | |
31 | #include "msg.h" | |
32 | #include "ini.h" | |
33 | #include "dialog.h" | |
3c054baf | 34 | #include "String++.h" |
23c9e63c DD |
35 | #include "geturl.h" |
36 | #include "state.h" | |
37 | #include "mkdir.h" | |
89b1a15b | 38 | #include "log.h" |
341988b9 | 39 | #include "filemanip.h" |
42bf5b92 | 40 | #include "port.h" |
23c9e63c | 41 | |
bb849dbd RC |
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 | ||
ab57ceaa RC |
51 | #include "threebar.h" |
52 | extern ThreeBarProgressPage Progress; | |
53 | ||
bb849dbd RC |
54 | /* 0 on failure |
55 | */ | |
3b9077d4 | 56 | static int |
bb849dbd | 57 | check_for_cached (packagesource & pkgsource) |
3b9077d4 | 58 | { |
bb849dbd RC |
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 | */ | |
3b9077d4 | 64 | |
88a77116 | 65 | DWORD size; |
bb849dbd | 66 | if ((size = |
3c054baf | 67 | get_file_size (local_dir + "/" + pkgsource.Canonical ())) > |
bb849dbd RC |
68 | 0) |
69 | if (size == pkgsource.size) | |
70 | { | |
71 | pkgsource. | |
3c054baf | 72 | set_cached (String ("file://") + local_dir + "/" + pkgsource.Canonical ()); |
bb849dbd RC |
73 | return 1; |
74 | } | |
3b9077d4 | 75 | |
bb849dbd RC |
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 = | |
3c054baf RC |
81 | get_file_size (local_dir + "/" + |
82 | rfc1738_escape_part (pkgsource.sites[n]->key) + "/" + | |
83 | pkgsource.Canonical ())) > 0) | |
bb849dbd RC |
84 | if (size == pkgsource.size) |
85 | { | |
86 | pkgsource. | |
3c054baf | 87 | set_cached (String ("file://") + local_dir + "/" + |
341988b9 | 88 | rfc1738_escape_part (pkgsource.sites[n]-> |
3c054baf RC |
89 | key) + "/" + |
90 | pkgsource.Canonical ()); | |
bb849dbd RC |
91 | return 1; |
92 | } | |
3b9077d4 | 93 | |
bb849dbd RC |
94 | return 0; |
95 | } | |
96 | ||
97 | /* download a file from a mirror site to the local cache. */ | |
98 | static int | |
ab57ceaa | 99 | download_one (packagesource & pkgsource, HWND owner) |
bb849dbd RC |
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++) | |
d4a4527d | 108 | { |
3c054baf | 109 | String const local = local_dir + "/" + |
ab57ceaa | 110 | rfc1738_escape_part (pkgsource.sites[n]-> |
3c054baf RC |
111 | key) + "/" + |
112 | pkgsource.Canonical (); | |
1ac649ed | 113 | io_stream::mkpath_p (PATH_TO_FILE, String ("file://") + local); |
3c054baf | 114 | |
1ac649ed RC |
115 | if (get_url_to_file(pkgsource.sites[n]->key + "/" + |
116 | pkgsource.Canonical (), | |
117 | local + ".tmp", pkgsource.size, owner)) | |
3b9077d4 | 118 | { |
bb849dbd RC |
119 | /* FIXME: note new source ? */ |
120 | continue; | |
3b9077d4 DD |
121 | } |
122 | else | |
123 | { | |
3c054baf | 124 | size_t size = get_file_size (local + ".tmp"); |
bb849dbd RC |
125 | if (size == pkgsource.size) |
126 | { | |
1ac649ed | 127 | log (LOG_PLAIN, String ("Downloaded ") + local); |
3c054baf RC |
128 | if (_access (local.cstr_oneuse(), 0) == 0) |
129 | remove (local.cstr_oneuse()); | |
130 | rename ((local + ".tmp").cstr_oneuse(), local.cstr_oneuse()); | |
bb849dbd | 131 | success = 1; |
3c054baf RC |
132 | pkgsource.set_cached (String ("file://") + local); |
133 | // FIXME: move the downloaded file to the | |
134 | // original locations - without the mirror site dir in the way | |
bb849dbd RC |
135 | continue; |
136 | } | |
137 | else | |
138 | { | |
1ac649ed RC |
139 | log (LOG_PLAIN, |
140 | "Download %s wrong size (%u actual vs %d expected)", | |
3c054baf RC |
141 | local.cstr_oneuse(), size, pkgsource.size); |
142 | remove ((local + ".tmp").cstr_oneuse()); | |
bb849dbd RC |
143 | continue; |
144 | } | |
3b9077d4 DD |
145 | } |
146 | } | |
bb849dbd RC |
147 | if (success) |
148 | return 0; | |
149 | /* FIXME: Do we want to note this? if so how? */ | |
150 | return 1; | |
3b9077d4 DD |
151 | } |
152 | ||
ab57ceaa RC |
153 | static void |
154 | do_download_thread (HINSTANCE h, HWND owner) | |
23c9e63c | 155 | { |
2a1a01e0 | 156 | int errors = 0; |
2f9645a1 CV |
157 | total_download_bytes = 0; |
158 | total_download_bytes_sofar = 0; | |
159 | ||
bb849dbd RC |
160 | packagedb db; |
161 | /* calculate the amount needed */ | |
3bab9a49 | 162 | for (size_t n = 1; n <= db.packages.number (); n++) |
cbfc4215 | 163 | { |
ab57ceaa | 164 | packagemeta & pkg = *db.packages[n]; |
cbfc4215 | 165 | if (pkg.desired && (pkg.desired->srcpicked || pkg.desired->binpicked)) |
ab57ceaa RC |
166 | { |
167 | packageversion *version = pkg.desired; | |
168 | if (! | |
169 | (check_for_cached (version->bin) | |
170 | && source != IDC_SOURCE_DOWNLOAD) && pkg.desired->binpicked) | |
171 | total_download_bytes += version->bin.size; | |
172 | if (! | |
173 | (check_for_cached (version->src) | |
174 | && source != IDC_SOURCE_DOWNLOAD) && pkg.desired->srcpicked) | |
175 | total_download_bytes += version->src.size; | |
176 | } | |
cbfc4215 | 177 | } |
23c9e63c | 178 | |
bb849dbd RC |
179 | /* and do the download. FIXME: This here we assign a new name for the cached version |
180 | * and check that above. | |
181 | */ | |
3bab9a49 | 182 | for (size_t n = 1; n <= db.packages.number (); n++) |
cbfc4215 | 183 | { |
ab57ceaa RC |
184 | packagemeta & pkg = *db.packages[n]; |
185 | if (pkg.desired && (pkg.desired->srcpicked || pkg.desired->binpicked)) | |
186 | { | |
187 | int e = 0; | |
188 | packageversion *version = pkg.desired; | |
189 | if (version->binpicked) | |
190 | e += download_one (version->bin, owner); | |
191 | if (version->srcpicked) | |
192 | e += download_one (version->src, owner); | |
193 | errors += e; | |
bb849dbd | 194 | #if 0 |
ab57ceaa RC |
195 | if (e) |
196 | pkg->action = ACTION_ERROR; | |
bb849dbd | 197 | #endif |
ab57ceaa | 198 | } |
cbfc4215 | 199 | } |
23c9e63c | 200 | |
2a1a01e0 DD |
201 | if (errors) |
202 | { | |
ab57ceaa | 203 | if (yesno (owner, IDS_DOWNLOAD_INCOMPLETE) == IDYES) |
2a1a01e0 DD |
204 | { |
205 | next_dialog = IDD_SITE; | |
206 | return; | |
207 | } | |
208 | } | |
209 | ||
bf1d5889 DD |
210 | if (source == IDC_SOURCE_DOWNLOAD) |
211 | { | |
2a1a01e0 DD |
212 | if (errors) |
213 | exit_msg = IDS_DOWNLOAD_INCOMPLETE; | |
214 | else | |
215 | exit_msg = IDS_DOWNLOAD_COMPLETE; | |
bf1d5889 DD |
216 | next_dialog = 0; |
217 | } | |
218 | else | |
219 | next_dialog = IDD_S_INSTALL; | |
23c9e63c | 220 | } |
ab57ceaa RC |
221 | |
222 | static void | |
223 | do_download_reflector (void *p) | |
224 | { | |
225 | HANDLE *context; | |
226 | context = (HANDLE *) p; | |
227 | ||
228 | do_download_thread ((HINSTANCE) context[0], (HWND) context[1]); | |
229 | ||
230 | // Tell the progress page that we're done downloading | |
231 | Progress.PostMessage (WM_APP_DOWNLOAD_THREAD_COMPLETE, 0, next_dialog); | |
232 | ||
233 | _endthread (); | |
234 | } | |
235 | ||
236 | static HANDLE context[2]; | |
237 | ||
238 | void | |
239 | do_download (HINSTANCE h, HWND owner) | |
240 | { | |
241 | context[0] = h; | |
242 | context[1] = owner; | |
243 | ||
244 | _beginthread (do_download_reflector, 0, context); | |
245 | } |