Merge from vendor branch LIBSTDC++:
[dragonfly.git] / release / sysinstall / package.c
1 /*
2  * The new sysinstall program.
3  *
4  * This is probably the last program in the `sysinstall' line - the next
5  * generation being essentially a complete rewrite.
6  *
7  * $FreeBSD: src/release/sysinstall/package.c,v 1.90.2.9 2002/04/23 20:35:08 obrien Exp $
8  * $DragonFly: src/release/sysinstall/Attic/package.c,v 1.2 2003/06/17 04:27:21 dillon Exp $
9  *
10  * Copyright (c) 1995
11  *      Jordan Hubbard.  All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer,
18  *    verbatim and that no modifications are made prior to this
19  *    point in the file.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  */
37
38 #include "sysinstall.h"
39 #include <sys/errno.h>
40 #include <sys/time.h>
41 #include <sys/param.h>
42 #include <sys/mount.h>
43 #include <sys/stat.h>
44
45 static Boolean sigpipe_caught;
46
47 static void
48 catch_pipe(int sig)
49 {
50     sigpipe_caught = TRUE;
51 }
52
53 extern PkgNode Top;
54
55 /* Like package_extract, but assumes current media device and chases deps */
56 int
57 package_add(char *name)
58 {
59     PkgNodePtr tmp;
60     int i;
61
62     if (!mediaVerify())
63         return DITEM_FAILURE;
64
65     if (!DEVICE_INIT(mediaDevice))
66         return DITEM_FAILURE;    
67
68     i = index_initialize("packages/INDEX");
69     if (DITEM_STATUS(i) != DITEM_SUCCESS)
70         return i;
71
72     tmp = index_search(&Top, name, &tmp);
73     if (tmp)
74         return index_extract(mediaDevice, &Top, tmp, FALSE);
75     else {
76         msgConfirm("Sorry, package %s was not found in the INDEX.", name);
77         return DITEM_FAILURE;
78     }
79 }
80
81 /* For use by dispatch */
82 int
83 packageAdd(dialogMenuItem *self)
84 {
85     char *cp;
86
87     cp = variable_get(VAR_PACKAGE);
88     if (!cp) {
89         msgDebug("packageAdd:  No package name passed in package variable\n");
90         return DITEM_FAILURE;
91     }
92     else
93         return package_add(cp);
94 }
95
96 Boolean
97 package_exists(char *name)
98 {
99     char fname[FILENAME_MAX];
100     int status /* = vsystem("pkg_info -e %s", name) */;
101
102     /* XXX KLUDGE ALERT!  This makes evil assumptions about how XXX
103      * packages register themselves and should *really be done with
104      * `pkg_info -e <name>' except that this it's too slow for an
105      * item check routine.. :-(
106      */
107     snprintf(fname, FILENAME_MAX, "/var/db/pkg/%s", name);
108     status = access(fname, R_OK);
109     if (isDebug())
110         msgDebug("package check for %s returns %s.\n", name, status ? "failure" : "success");
111     return !status;
112 }
113
114 /* Extract a package based on a namespec and a media device */
115 int
116 package_extract(Device *dev, char *name, Boolean depended)
117 {
118     char path[511];
119     int ret, last_msg = 0;
120     FILE *fp;
121
122     /* Check to make sure it's not already there */
123     if (package_exists(name))
124         return DITEM_SUCCESS;
125
126     if (!DEVICE_INIT(dev)) {
127         msgConfirm("Unable to initialize media type for package extract.");
128         return DITEM_FAILURE;
129     }
130
131     /* If necessary, initialize the ldconfig hints */
132     if (!file_readable("/var/run/ld-elf.so.hints"))
133         vsystem("ldconfig /usr/lib /usr/lib/compat /usr/local/lib /usr/X11R6/lib");
134     if (!file_readable("/var/run/ld.so.hints"))
135         vsystem("ldconfig -aout /usr/lib/aout /usr/lib/compat/aout /usr/local/lib/aout /usr/X11R6/lib/aout");
136
137     /* Be initially optimistic */
138     ret = DITEM_SUCCESS;
139     /* Make a couple of paranoid locations for temp files to live if user specified none */
140     if (!variable_get(VAR_PKG_TMPDIR)) {
141         /* Set it to a location with as much space as possible */
142         variable_set2(VAR_PKG_TMPDIR, "/var/tmp", 0);
143     }
144     Mkdir(variable_get(VAR_PKG_TMPDIR));
145     vsystem("chmod 1777 %s", variable_get(VAR_PKG_TMPDIR));
146
147     if (!index(name, '/')) {
148         if (!strpbrk(name, "-_"))
149             sprintf(path, "packages/Latest/%s.tgz", name);
150         else
151             sprintf(path, "packages/All/%s%s", name, strstr(name, ".tgz") ? "" : ".tgz");
152     }
153     else
154         sprintf(path, "%s%s", name, strstr(name, ".tgz") ? "" : ".tgz");
155
156     /* We have a path, call the device strategy routine to get the file */
157     fp = DEVICE_GET(dev, path, TRUE);
158     if (fp) {
159         int i = 0, tot, pfd[2];
160         pid_t pid;
161         WINDOW *w = savescr();
162
163         sigpipe_caught = FALSE;
164         signal(SIGPIPE, catch_pipe);
165
166         dialog_clear_norefresh();
167         msgNotify("Adding %s%s\nfrom %s", path, depended ? " (as a dependency)" : "", dev->name);
168         pipe(pfd);
169         pid = fork();
170         if (!pid) {
171             dup2(pfd[0], 0); close(pfd[0]);
172             dup2(DebugFD, 1); dup2(1, 2);
173             close(pfd[1]);
174
175             /* Prevent pkg_add from wanting to interact in bad ways */
176             setenv("PACKAGE_BUILDING", "t", 1);
177             setenv("BATCH", "t", 1);
178
179             if (isDebug())
180                 i = execl("/usr/sbin/pkg_add", "/usr/sbin/pkg_add", "-v", "-",
181                     (char *)0);
182             else
183                 i = execl("/usr/sbin/pkg_add", "/usr/sbin/pkg_add", "-",
184                     (char *)0);
185         }
186         else {
187             char buf[BUFSIZ];
188             struct timeval start, stop;
189
190             close(pfd[0]);
191             tot = 0;
192             (void)gettimeofday(&start, (struct timezone *)0);
193
194             while (!sigpipe_caught && (i = fread(buf, 1, BUFSIZ, fp)) > 0) {
195                 int seconds;
196
197                 tot += i;
198                 /* Print statistics about how we're doing */
199                 (void) gettimeofday(&stop, (struct timezone *)0);
200                 stop.tv_sec = stop.tv_sec - start.tv_sec;
201                 stop.tv_usec = stop.tv_usec - start.tv_usec;
202                 if (stop.tv_usec < 0)
203                     stop.tv_sec--, stop.tv_usec += 1000000;
204                 seconds = stop.tv_sec + (stop.tv_usec / 1000000.0);
205                 if (!seconds)
206                     seconds = 1;
207                 if (seconds != last_msg) {
208                     last_msg = seconds;
209                     msgInfo("%10d bytes read from package %s @ %4.1f KBytes/second", tot, name, (tot / seconds) / 1000.0);
210                 }
211                 /* Write it out */
212                 if (sigpipe_caught || write(pfd[1], buf, i) != i) {
213                     msgInfo("Write failure to pkg_add!  Package may be corrupt.");
214                     break;
215                 }
216             }
217             close(pfd[1]);
218             fclose(fp);
219             if (sigpipe_caught)
220                 msgInfo("pkg_add(1) apparently did not like the %s package.", name);
221             else if (i == -1)
222                 msgInfo("I/O error while reading in the %s package.", name);
223             else
224                 msgInfo("Package %s read successfully - waiting for pkg_add(1)", name);
225             refresh();
226             i = waitpid(pid, &tot, 0);
227             dialog_clear_norefresh();
228             if (sigpipe_caught || i < 0 || WEXITSTATUS(tot)) {
229                 ret = DITEM_FAILURE;
230                 if (variable_get(VAR_NO_CONFIRM))
231                     msgNotify("Add of package %s aborted, error code %d -\n"
232                                "Please check the debug screen for more info.", name, WEXITSTATUS(tot));
233                 else
234                     msgConfirm("Add of package %s aborted, error code %d -\n"
235                                "Please check the debug screen for more info.", name, WEXITSTATUS(tot));
236             }
237             else
238                 msgNotify("Package %s was added successfully", name);
239
240             /* Now catch any stragglers */
241             while (wait3(&tot, WNOHANG, NULL) > 0);
242
243             sleep(1);
244             restorescr(w);
245         }
246     }
247     else {
248         dialog_clear_norefresh();
249         if (variable_get(VAR_NO_CONFIRM))
250             msgNotify("Unable to fetch package %s from selected media.\n"
251                       "No package add will be done.", name);
252         else
253             msgConfirm("Unable to fetch package %s from selected media.\n"
254                        "No package add will be done.", name);
255         ret = DITEM_FAILURE;
256     }
257     signal(SIGPIPE, SIG_IGN);
258     return ret;
259 }