]> 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);
114
115 if (get_url_to_file(pkgsource.sites[n]->key + "/" +
116 pkgsource.Canonical (),
117 local + ".tmp", pkgsource.size, owner))
118 {
119 /* FIXME: note new source ? */
120 continue;
121 }
122 else
123 {
124 size_t size = get_file_size (local + ".tmp");
125 if (size == pkgsource.size)
126 {
127 log (LOG_PLAIN, String ("Downloaded ") + local);
128 if (_access (local.cstr_oneuse(), 0) == 0)
129 remove (local.cstr_oneuse());
130 rename ((local + ".tmp").cstr_oneuse(), local.cstr_oneuse());
131 success = 1;
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
135 continue;
136 }
137 else
138 {
139 log (LOG_PLAIN,
140 "Download %s wrong size (%u actual vs %d expected)",
141 local.cstr_oneuse(), size, pkgsource.size);
142 remove ((local + ".tmp").cstr_oneuse());
143 continue;
144 }
145 }
146 }
147 if (success)
148 return 0;
149 /* FIXME: Do we want to note this? if so how? */
150 return 1;
151 }
152
153 static void
154 do_download_thread (HINSTANCE h, HWND owner)
155 {
156 int errors = 0;
157 total_download_bytes = 0;
158 total_download_bytes_sofar = 0;
159
160 packagedb db;
161 /* calculate the amount needed */
162 for (size_t n = 1; n < db.packages.number (); n++)
163 {
164 packagemeta & pkg = *db.packages[n];
165 if (pkg.desired && (pkg.desired->srcpicked || pkg.desired->binpicked))
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 }
177 }
178
179 /* and do the download. FIXME: This here we assign a new name for the cached version
180 * and check that above.
181 */
182 for (size_t n = 1; n < db.packages.number (); n++)
183 {
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;
194 #if 0
195 if (e)
196 pkg->action = ACTION_ERROR;
197 #endif
198 }
199 }
200
201 if (errors)
202 {
203 if (yesno (owner, IDS_DOWNLOAD_INCOMPLETE) == IDYES)
204 {
205 next_dialog = IDD_SITE;
206 return;
207 }
208 }
209
210 if (source == IDC_SOURCE_DOWNLOAD)
211 {
212 if (errors)
213 exit_msg = IDS_DOWNLOAD_INCOMPLETE;
214 else
215 exit_msg = IDS_DOWNLOAD_COMPLETE;
216 next_dialog = 0;
217 }
218 else
219 next_dialog = IDD_S_INSTALL;
220 }
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 }
This page took 0.04503 seconds and 6 git commands to generate.