420 remove patch (sparse package) support from svr4 pkg
[illumos-gate.git] / usr / src / cmd / svr4pkg / libinst / dockdeps.c
blob7f469f7d724e22296a132ca3636260a058d2f009
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
29 #include <stdio.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include <dirent.h>
37 #include <sys/types.h>
38 #include <pkgstrct.h>
39 #include <pkginfo.h>
40 #include <locale.h>
41 #include <libintl.h>
42 #include <pkglib.h>
43 #include "libinst.h"
44 #include "libadm.h"
45 #include "messages.h"
47 #define LSIZE 256
48 #define NVERS 50
51 * internal global variables
54 static struct pkginfo info;
56 static char type;
57 static char *alist[NVERS];
58 static char *rmpkginst;
59 static char *vlist[NVERS];
60 static char file[128];
61 static char name[128];
62 static char rmpkg[PKGSIZ+1];
63 static char wabbrev[128];
65 static int errflg = 0;
66 static int nlist;
67 static int pkgexist;
68 static int pkgokay;
69 static int is_update;
72 * IMPORTANT NOTE: THE SIZE OF 'abbrev' IS HARD CODED INTO THE CHARACTER
73 * ARRAY SSCANF_FORMAT -- YOU MUST UPDATE BOTH VALUES AT THE SAME TIME!!
76 static char abbrev[128+1];
77 static char *SSCANF_FORMAT = "%c %128s %[^\n]";
80 * forward declarations
83 static void ckrdeps(boolean_t a_preinstallCheck);
84 static void ckpreq(FILE *fp, char *dname, boolean_t a_preinstallCheck);
85 static void deponme(char *pkginst, char *pkgname,
86 boolean_t a_preinstallCheck);
87 static void prereq(char *pkginst, char *pkgname,
88 boolean_t a_preinstallCheck);
89 static void incompat(char *pkginst, char *pkgname,
90 boolean_t a_preinstallCheck);
91 static int getaline(FILE *fp);
94 * *****************************************************************************
95 * global external (public) functions
96 * *****************************************************************************
99 int
100 dockdeps(char *a_depfile, int a_removeFlag, boolean_t a_preinstallCheck)
102 FILE *fp;
103 int i;
104 char *inst;
106 if (a_removeFlag) {
107 /* check removal dependencies */
108 rmpkginst = a_depfile;
109 (void) strncpy(rmpkg, rmpkginst, PKGSIZ);
110 (void) strtok(rmpkg, ".");
111 (void) snprintf(file, sizeof (file),
112 "%s/%s/%s", pkgdir, rmpkginst, DEPEND_FILE);
113 if ((fp = fopen(file, "r")) == NULL)
114 goto done;
115 } else {
116 if ((fp = fopen(a_depfile, "r")) == NULL) {
117 progerr(ERR_CANNOT_OPEN_DEPEND_FILE, a_depfile,
118 strerror(errno));
119 quit(99);
123 while (getaline(fp)) {
124 switch (type) {
125 case 'I':
126 case 'P':
127 if (a_removeFlag) {
128 continue;
130 break;
132 case 'R':
133 if (!a_removeFlag) {
134 continue;
136 break;
138 default:
139 errflg++;
140 progerr(ERR_UNKNOWN_DEPENDENCY, type);
141 break;
144 /* check to see if any versions listed are installed */
145 pkgexist = pkgokay = 0;
146 i = 0;
147 if (strchr(abbrev, '.')) {
148 progerr(ERR_PKGABRV, abbrev);
150 (void) snprintf(wabbrev, sizeof (wabbrev), "%s.*", abbrev);
152 do {
153 inst = fpkginst(wabbrev, alist[i], vlist[i]);
154 if (inst && (pkginfo(&info, inst, NULL, NULL) == 0)) {
155 pkgexist++;
156 if (info.status == PI_INSTALLED)
157 pkgokay++;
159 } while (++i < nlist);
160 (void) fpkginst(NULL); /* force closing/rewind of files */
162 if (!info.name) {
163 info.name = name;
166 switch (type) {
167 case 'I':
168 incompat(abbrev, info.name, a_preinstallCheck);
169 break;
171 case 'P':
172 prereq(abbrev, name, a_preinstallCheck);
173 break;
175 case 'R':
176 deponme(abbrev, info.name, a_preinstallCheck);
179 (void) fclose(fp);
181 done:
182 if (a_removeFlag) {
183 ckrdeps(a_preinstallCheck);
186 return (errflg);
189 void
190 setUpdate(void)
192 is_update = 1;
196 isUpdate(void)
198 return ((is_update) ? 1 : 0);
202 * *****************************************************************************
203 * static internal (private) functions
204 * *****************************************************************************
207 static void
208 incompat(char *pkginst, char *pkgname, boolean_t a_preinstallCheck)
210 char buf[512];
212 if (!pkgexist)
213 return;
215 errflg++;
216 if (a_preinstallCheck == B_TRUE) {
217 (void) fprintf(stdout, "incompat=%s\n", pkginst);
218 return;
221 logerr(ERR_WARNING);
222 (void) snprintf(buf, sizeof (buf), ERR_INCOMP_VERS, pkginst, pkgname);
223 puttext(stderr, buf, 4, 0);
224 (void) putc('\n', stderr);
227 static void
228 prereq(char *pkginst, char *pkgname, boolean_t a_preinstallCheck)
230 register int i;
231 char buf[512];
233 if (pkgokay) {
234 return;
237 errflg++;
239 if (a_preinstallCheck == B_TRUE) {
240 if (pkgexist) {
241 (void) fprintf(stdout,
242 "prerequisite-incomplete=%s\n", pkginst);
243 } else {
244 (void) fprintf(stdout,
245 "prerequisite-installed=%s\n", pkginst);
247 return;
250 logerr(ERR_WARNING);
251 if (pkgexist) {
252 (void) snprintf(buf, sizeof (buf), ERR_PRENCI, pkginst,
253 pkgname);
254 puttext(stderr, buf, 4, 0);
255 (void) putc('\n', stderr);
256 } else {
257 (void) snprintf(buf, sizeof (buf), ERR_PREREQ, pkginst,
258 pkgname);
259 if (nlist) {
260 (void) strcat(buf, ERR_VALINST);
262 puttext(stderr, buf, 4, 0);
263 (void) putc('\n', stderr);
264 for (i = 0; i < nlist; i++) {
265 (void) printf(" ");
266 if (alist[i])
267 (void) printf("(%s) ", alist[i]);
268 if (vlist[i])
269 (void) printf("%s", vlist[i]);
270 (void) printf("\n");
275 static void
276 deponme(char *pkginst, char *pkgname, boolean_t a_preinstallCheck)
278 char buf[512];
280 if (!pkgexist)
281 return;
283 errflg++;
285 if (a_preinstallCheck == B_TRUE) {
286 if (!pkgname || !pkgname[0]) {
287 (void) snprintf(buf, sizeof (buf),
288 "dependonme=%s", pkginst);
289 } else {
290 (void) snprintf(buf, sizeof (buf),
291 "dependsonme=%s:%s", pkginst, pkgname);
293 (void) fprintf(stdout, "%s\n", buf);
294 return;
297 logerr(ERR_WARNING);
298 if (!pkgname || !pkgname[0]) {
299 (void) snprintf(buf, sizeof (buf), ERR_DEPONME, pkginst);
300 } else {
301 (void) snprintf(buf, sizeof (buf), ERR_DEPNAM, pkginst,
302 pkgname);
304 puttext(stderr, buf, 4, 0);
305 (void) putc('\n', stderr);
308 static int
309 getaline(FILE *fp)
311 register int i, c, found;
312 char *pt, *new, line[LSIZE];
314 abbrev[0] = name[0] = type = '\0';
316 for (i = 0; i < nlist; i++) {
317 if (alist[i]) {
318 free(alist[i]);
319 alist[i] = NULL;
321 if (vlist[i]) {
322 free(vlist[i]);
323 vlist[i] = NULL;
326 alist[0] = vlist[0] = NULL;
328 found = (-1);
329 nlist = 0;
330 while ((c = getc(fp)) != EOF) {
331 (void) ungetc(c, fp);
332 if ((found >= 0) && !isspace(c))
333 return (1);
335 if (!fgets(line, LSIZE, fp))
336 break;
338 for (pt = line; isspace(*pt); /* void */)
339 pt++;
340 if (!*pt || (*pt == '#'))
341 continue;
343 if (pt == line) {
344 /* begin new definition */
345 /* LINTED variable format specifier to sscanf(): */
346 (void) sscanf(line, SSCANF_FORMAT, &type, abbrev, name);
347 found++;
348 continue;
350 if (found < 0)
351 return (0);
353 if (*pt == '(') {
354 /* architecture is specified */
355 if (new = strchr(pt, ')'))
356 *new++ = '\0';
357 else
358 return (-1); /* bad specification */
359 alist[found] = qstrdup(pt+1);
360 pt = new;
362 while (isspace(*pt))
363 pt++;
364 if (*pt) {
365 vlist[found] = qstrdup(pt);
366 if (pt = strchr(vlist[found], '\n'))
367 *pt = '\0';
369 found++;
370 nlist++;
372 return ((found >= 0) ? 1 : 0);
375 static void
376 ckrdeps(boolean_t a_preinstallCheck)
378 struct dirent *drp;
379 DIR *dirfp;
380 FILE *fp;
381 char depfile[PATH_MAX+1];
383 if ((dirfp = opendir(pkgdir)) == NULL)
384 return;
386 while ((drp = readdir(dirfp)) != NULL) {
387 if (drp->d_name[0] == '.')
388 continue;
390 if (strcmp(drp->d_name, rmpkginst) == 0)
391 continue; /* others don't include me */
392 (void) snprintf(depfile, sizeof (depfile),
393 "%s/%s/%s", pkgdir, drp->d_name, DEPEND_FILE);
394 if ((fp = fopen(depfile, "r")) == NULL)
395 continue;
397 ckpreq(fp, drp->d_name, a_preinstallCheck);
399 (void) closedir(dirfp);
402 static void
403 ckpreq(FILE *fp, char *dname, boolean_t a_preinstallCheck)
405 register int i;
406 char *inst;
408 while (getaline(fp)) {
409 if (type != 'P')
410 continue;
412 if (strcmp(abbrev, rmpkg))
413 continue;
415 /* see if package is installed */
416 i = 0;
417 if (strchr(abbrev, '.') == 0) {
418 (void) strcat(abbrev, ".*");
420 pkgexist = 1;
422 do {
423 if (inst = fpkginst(abbrev, alist[i], vlist[i])) {
424 if (strcmp(inst, rmpkginst) == 0) {
425 deponme(dname, "", a_preinstallCheck);
426 (void) fclose(fp);
427 (void) fpkginst(NULL);
428 return;
431 } while (++i < nlist);
432 (void) fpkginst(NULL);
434 (void) fclose(fp);