]>
Commit | Line | Data |
---|---|---|
23c9e63c DD |
1 | /* |
2 | * Copyright (c) 2000, 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 intall all the packages selected in | |
17 | the install list (in ini.h). Note that we use a separate thread to | |
18 | maintain the progress dialog, so we avoid the complexity of | |
19 | handling two tasks in one thread. We also create or update all the | |
20 | files in /etc/setup/* and create the mount points. */ | |
21 | ||
8507f105 DD |
22 | static char *cvsid = "\n%%% $Id$\n"; |
23 | ||
23c9e63c DD |
24 | #include "win32.h" |
25 | #include "commctrl.h" | |
26 | ||
27 | #include <stdio.h> | |
904d24fe | 28 | #include <stdlib.h> |
23c9e63c DD |
29 | #include <sys/types.h> |
30 | #include <sys/stat.h> | |
31 | #include <errno.h> | |
32 | #include "zlib/zlib.h" | |
33 | ||
34 | #include "resource.h" | |
35 | #include "ini.h" | |
36 | #include "dialog.h" | |
37 | #include "concat.h" | |
38 | #include "geturl.h" | |
39 | #include "mkdir.h" | |
40 | #include "state.h" | |
41 | #include "tar.h" | |
42 | #include "diskfull.h" | |
43 | #include "msg.h" | |
44 | #include "mount.h" | |
89b1a15b | 45 | #include "log.h" |
23c9e63c DD |
46 | |
47 | static HWND ins_dialog = 0; | |
48 | static HWND ins_pkgname = 0; | |
49 | static HWND ins_filename = 0; | |
50 | static HWND ins_pprogress = 0; | |
51 | static HWND ins_iprogress = 0; | |
52 | static HWND ins_diskfull = 0; | |
53 | static HANDLE init_event; | |
54 | ||
55 | static int total_bytes = 0; | |
56 | static int total_bytes_sofar = 0; | |
57 | static int package_bytes = 0; | |
58 | ||
59 | static BOOL | |
60 | dialog_cmd (HWND h, int id, HWND hwndctl, UINT code) | |
61 | { | |
62 | switch (id) | |
63 | { | |
64 | case IDCANCEL: | |
89b1a15b | 65 | exit_setup (1); |
23c9e63c DD |
66 | } |
67 | } | |
68 | ||
69 | static BOOL CALLBACK | |
70 | dialog_proc (HWND h, UINT message, WPARAM wParam, LPARAM lParam) | |
71 | { | |
72 | int i, j; | |
73 | HWND listbox; | |
74 | switch (message) | |
75 | { | |
76 | case WM_INITDIALOG: | |
77 | ins_dialog = h; | |
78 | ins_pkgname = GetDlgItem (h, IDC_INS_PKG); | |
79 | ins_filename = GetDlgItem (h, IDC_INS_FILE); | |
80 | ins_pprogress = GetDlgItem (h, IDC_INS_PPROGRESS); | |
81 | ins_iprogress = GetDlgItem (h, IDC_INS_IPROGRESS); | |
82 | ins_diskfull = GetDlgItem (h, IDC_INS_DISKFULL); | |
83 | SetEvent (init_event); | |
84 | return FALSE; | |
85 | case WM_COMMAND: | |
1fd6d0a2 | 86 | return HANDLE_WM_COMMAND (h, wParam, lParam, dialog_cmd); |
23c9e63c DD |
87 | } |
88 | return FALSE; | |
89 | } | |
90 | ||
91 | static WINAPI DWORD | |
92 | dialog (void *) | |
93 | { | |
94 | int rv = 0; | |
95 | MSG m; | |
96 | HANDLE ins_dialog = CreateDialog (hinstance, MAKEINTRESOURCE (IDD_INSTATUS), | |
97 | 0, dialog_proc); | |
98 | if (ins_dialog == 0) | |
99 | fatal ("create dialog"); | |
100 | ShowWindow (ins_dialog, SW_SHOWNORMAL); | |
101 | UpdateWindow (ins_dialog); | |
102 | while (GetMessage (&m, 0, 0, 0) > 0) { | |
103 | TranslateMessage (&m); | |
104 | DispatchMessage (&m); | |
105 | } | |
106 | } | |
107 | ||
108 | static DWORD start_tics; | |
109 | ||
110 | static void | |
111 | init_dialog () | |
112 | { | |
113 | if (ins_dialog == 0) | |
114 | { | |
115 | DWORD tid; | |
116 | HANDLE thread; | |
117 | init_event = CreateEvent (0, 0, 0, 0); | |
118 | thread = CreateThread (0, 0, dialog, 0, 0, &tid); | |
119 | WaitForSingleObject (init_event, 10000); | |
120 | CloseHandle (init_event); | |
121 | SendMessage (ins_pprogress, PBM_SETRANGE, 0, MAKELPARAM (0, 100)); | |
122 | SendMessage (ins_iprogress, PBM_SETRANGE, 0, MAKELPARAM (0, 100)); | |
123 | SendMessage (ins_diskfull, PBM_SETRANGE, 0, MAKELPARAM (0, 100)); | |
124 | } | |
125 | ||
126 | SetWindowText (ins_pkgname, ""); | |
127 | SetWindowText (ins_filename, ""); | |
128 | SendMessage (ins_pprogress, PBM_SETPOS, (WPARAM) 0, 0); | |
129 | SendMessage (ins_iprogress, PBM_SETPOS, (WPARAM) 0, 0); | |
130 | SendMessage (ins_diskfull, PBM_SETPOS, (WPARAM) 0, 0); | |
131 | ShowWindow (ins_dialog, SW_SHOWNORMAL); | |
132 | SetForegroundWindow (ins_dialog); | |
133 | } | |
134 | ||
135 | static void | |
136 | progress (int bytes) | |
137 | { | |
138 | int perc; | |
139 | ||
bf1d5889 | 140 | if (package_bytes > 100) |
23c9e63c | 141 | { |
bf1d5889 | 142 | perc = bytes / (package_bytes / 100); |
23c9e63c DD |
143 | SendMessage (ins_pprogress, PBM_SETPOS, (WPARAM) perc, 0); |
144 | } | |
145 | ||
bf1d5889 | 146 | if (total_bytes > 100) |
23c9e63c | 147 | { |
bf1d5889 | 148 | perc = (total_bytes_sofar + bytes) / (total_bytes / 100); |
23c9e63c DD |
149 | SendMessage (ins_iprogress, PBM_SETPOS, (WPARAM) perc, 0); |
150 | } | |
151 | ||
152 | int df = diskfull (root_dir); | |
153 | SendMessage (ins_diskfull, PBM_SETPOS, (WPARAM) df, 0); | |
154 | } | |
155 | ||
156 | static void | |
157 | badrename (char *o, char *n) | |
158 | { | |
159 | char buf[1000]; | |
160 | char *err = strerror (errno); | |
161 | if (!err) | |
162 | err = "(unknown error)"; | |
163 | note (IDS_ERR_RENAME, o, n, err); | |
164 | } | |
165 | ||
904d24fe | 166 | static char *standard_dirs[] = { |
1a18aed7 | 167 | "/bin", |
904d24fe | 168 | "/etc", |
1a18aed7 | 169 | "/lib", |
904d24fe | 170 | "/tmp", |
1a18aed7 DD |
171 | "/usr/bin", |
172 | "/usr/lib", | |
173 | "/usr/local/bin", | |
174 | "/usr/local/etc", | |
175 | "/usr/local/lib", | |
904d24fe | 176 | "/usr/tmp", |
1a18aed7 | 177 | "/var/run", |
904d24fe DD |
178 | "/var/tmp", |
179 | 0 | |
180 | }; | |
181 | ||
23c9e63c DD |
182 | #define pi (package[i].info[package[i].trust]) |
183 | ||
184 | #define LOOP_PACKAGES \ | |
185 | for (i=0; i<npackages; i++) \ | |
b11b49f3 DD |
186 | if ((package[i].action == ACTION_NEW \ |
187 | || package[i].action == ACTION_UPGRADE) \ | |
188 | && pi.install) | |
23c9e63c DD |
189 | |
190 | void | |
191 | do_install (HINSTANCE h) | |
192 | { | |
193 | int i, num_installs = 0; | |
f57c332f | 194 | next_dialog = IDD_S_DESKTOP; |
23c9e63c | 195 | |
904d24fe DD |
196 | mkdir_p (1, root_dir); |
197 | ||
198 | for (i=0; standard_dirs[i]; i++) | |
199 | { | |
200 | char *p = concat (root_dir, standard_dirs[i], 0); | |
201 | mkdir_p (1, p); | |
202 | free (p); | |
203 | } | |
204 | ||
1a18aed7 DD |
205 | /* Create /var/run/utmp */ |
206 | char *utmp = concat (root_dir, "/var/run/utmp", 0); | |
207 | FILE *ufp = fopen (utmp, "wb"); | |
208 | if (ufp) | |
209 | fclose (ufp); | |
210 | free (utmp); | |
211 | ||
23c9e63c DD |
212 | dismiss_url_status_dialog (); |
213 | ||
214 | init_dialog (); | |
215 | ||
216 | total_bytes = 0; | |
217 | total_bytes_sofar = 0; | |
218 | ||
219 | LOOP_PACKAGES | |
220 | { | |
221 | total_bytes += pi.install_size; | |
222 | } | |
223 | ||
224 | LOOP_PACKAGES | |
225 | { | |
226 | char *local = pi.install, *cp, *fn, *base; | |
227 | ||
228 | base = local; | |
229 | for (cp=pi.install; *cp; cp++) | |
230 | if (*cp == '/' || *cp == '\\' || *cp == ':') | |
231 | base = cp+1; | |
232 | SetWindowText (ins_pkgname, base); | |
233 | ||
234 | gzFile lst = gzopen (concat (root_dir, "/etc/setup/", | |
235 | package[i].name, ".lst.gz", 0), | |
236 | "wb9"); | |
237 | ||
238 | package_bytes = pi.install_size; | |
239 | ||
89b1a15b | 240 | log (0, "Installing %s", local); |
23c9e63c DD |
241 | tar_open (local); |
242 | while (fn = tar_next_file ()) | |
243 | { | |
244 | char *dest_file; | |
245 | ||
246 | while (*fn == '/' || *fn == '\\') | |
247 | fn++; | |
248 | ||
249 | if (lst) | |
1fd6d0a2 | 250 | gzprintf (lst, "%s\n", fn); |
23c9e63c DD |
251 | |
252 | if (strncmp (fn, "usr/bin/", 8) == 0) | |
1fd6d0a2 | 253 | dest_file = concat (root_dir, "/bin/", fn+8, 0); |
23c9e63c | 254 | else if (strncmp (fn, "usr/lib/", 8) == 0) |
1fd6d0a2 | 255 | dest_file = concat (root_dir, "/lib/", fn+8, 0); |
23c9e63c | 256 | else |
1fd6d0a2 | 257 | dest_file = concat (root_dir, "/", fn, 0); |
23c9e63c DD |
258 | |
259 | SetWindowText (ins_filename, dest_file); | |
89b1a15b | 260 | log (LOG_BABBLE, "Installing file %s", dest_file); |
23c9e63c DD |
261 | tar_read_file (dest_file); |
262 | ||
263 | progress (tar_ftell ()); | |
264 | num_installs ++; | |
265 | } | |
266 | tar_close (); | |
267 | ||
268 | total_bytes_sofar += pi.install_size; | |
269 | progress (0); | |
270 | ||
271 | if (lst) | |
272 | gzclose (lst); | |
273 | } | |
274 | ||
275 | ShowWindow (ins_dialog, SW_HIDE); | |
276 | ||
277 | if (num_installs == 0) | |
278 | { | |
f57c332f | 279 | exit_msg = IDS_NOTHING_INSTALLED; |
23c9e63c DD |
280 | return; |
281 | } | |
282 | ||
283 | char *odbn = concat (root_dir, "/etc/setup/installed.db", 0); | |
284 | char *ndbn = concat (root_dir, "/etc/setup/installed.db.new", 0); | |
285 | char *sdbn = concat (root_dir, "/etc/setup/installed.db.old", 0); | |
286 | ||
287 | mkdir_p (0, ndbn); | |
288 | ||
289 | FILE *odb = fopen (odbn, "rt"); | |
290 | FILE *ndb = fopen (ndbn, "wb"); | |
291 | ||
292 | if (!ndb) | |
293 | { | |
294 | char *err = strerror (errno); | |
295 | if (!err) | |
296 | err = "(unknown error)"; | |
297 | fatal (IDS_ERR_OPEN_WRITE, ndb, err); | |
298 | } | |
299 | ||
300 | if (odb) | |
301 | { | |
302 | char line[1000], pkg[1000]; | |
303 | int printit; | |
304 | while (fgets (line, 1000, odb)) | |
305 | { | |
306 | printit = 1; | |
1fd6d0a2 | 307 | sscanf (line, "%s", pkg); |
23c9e63c DD |
308 | LOOP_PACKAGES |
309 | { | |
310 | if (strcmp (pkg, package[i].name) == 0) | |
311 | { | |
312 | printit = 0; | |
313 | break; | |
314 | } | |
315 | } | |
316 | if (printit) | |
317 | fputs (line, ndb); | |
318 | } | |
319 | ||
320 | } | |
321 | LOOP_PACKAGES | |
322 | { | |
323 | fprintf (ndb, "%s %s %d\n", package[i].name, | |
324 | pi.install, pi.install_size); | |
325 | } | |
326 | ||
327 | if (odb) | |
328 | fclose (odb); | |
329 | fclose (ndb); | |
330 | ||
331 | remove (sdbn); | |
332 | if (odb && rename (odbn, sdbn)) | |
333 | badrename (odbn, sdbn); | |
334 | ||
335 | remove (odbn); | |
336 | if (rename (ndbn, odbn)) | |
337 | badrename (ndbn, odbn); | |
338 | ||
24e259bb | 339 | remove_mount ("/"); |
23c9e63c | 340 | remove_mount ("/usr"); |
24e259bb DD |
341 | remove_mount ("/usr/bin"); |
342 | remove_mount ("/usr/lib"); | |
23c9e63c DD |
343 | remove_mount ("/var"); |
344 | remove_mount ("/lib"); | |
345 | remove_mount ("/bin"); | |
346 | remove_mount ("/etc"); | |
347 | ||
348 | int istext = (root_text == IDC_ROOT_TEXT) ? 1 : 0; | |
24e259bb | 349 | int issystem = (root_scope == IDC_ROOT_SYSTEM) ? 1 : 0; |
23c9e63c | 350 | |
24e259bb DD |
351 | create_mount ("/", root_dir, istext, issystem); |
352 | create_mount ("/usr/bin", concat (root_dir, "/bin", 0), istext, issystem); | |
353 | create_mount ("/usr/lib", concat (root_dir, "/lib", 0), istext, issystem); | |
23c9e63c | 354 | |
f57c332f | 355 | exit_msg = IDS_INSTALL_COMPLETE; |
23c9e63c | 356 | } |