xref: /titanic_50/usr/src/cmd/svr4pkg/pkgremove/delmap.c (revision 5c51f1241dbbdf2656d0e10011981411ed0c9673)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 
30 
31 #include <stdio.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <sys/types.h>
38 #include <pkgstrct.h>
39 #include <locale.h>
40 #include <libintl.h>
41 #include <pkglib.h>
42 #include <libadm.h>
43 #include <libinst.h>
44 
45 extern int	dbchg, warnflag, otherstoo;
46 extern char	*pkginst;
47 
48 #define	EPTMALLOC	128
49 
50 #define	ERR_WRENT	"write of entry failed, errno=%d"
51 #define	ERR_MEMORY	"no memory, errno=%d"
52 #define	ERR_READ_C	"bad read of contents file"
53 #define	ERR_READ_DB	"bad read of the database"
54 
55 extern struct cfent	**eptlist;
56 extern int	eptnum;
57 
58 int
59 delmap(int flag, char *pkginst)
60 {
61 	struct cfent	*ept;
62 	struct pinfo	*pinfo;
63 	VFP_T		*vfp;
64 	VFP_T		*vfpo;
65 	int		n;
66 	char		*unknown = "Unknown";
67 
68 
69 	if (!ocfile(&vfp, &vfpo, 0L)) {
70 		quit(99);
71 	}
72 
73 	/* re-use any memory used to store pathnames */
74 	(void) pathdup(NULL);
75 
76 	if (eptlist != NULL)
77 		free(eptlist);
78 	eptlist = (struct cfent **)calloc(EPTMALLOC,
79 					sizeof (struct cfent *));
80 	if (eptlist == NULL) {
81 		progerr(gettext(ERR_MEMORY), errno);
82 		quit(99);
83 	}
84 
85 	ept = (struct cfent *)calloc(1,
86 				(unsigned)sizeof (struct cfent));
87 	if (!ept) {
88 		progerr(gettext(ERR_MEMORY), errno);
89 		quit(99);
90 	}
91 
92 	eptnum = 0;
93 	while (n = srchcfile(ept, "*", vfp, (VFP_T *)NULL)) {
94 		if (n < 0) {
95 			char	*errstr = getErrstr();
96 			progerr(gettext("bad read of contents file"));
97 			progerr(gettext("pathname=%s"),
98 				(ept->path && *ept->path) ? ept->path :
99 				unknown);
100 			progerr(gettext("problem=%s"),
101 				(errstr && *errstr) ? errstr : unknown);
102 			exit(99);
103 		}
104 		pinfo = eptstat(ept, pkginst, (flag ? '@' : '-'));
105 		if (ept->npkgs > 0) {
106 			if (putcvfpfile(ept, vfpo)) {
107 				progerr(gettext(ERR_WRENT), errno);
108 				quit(99);
109 			}
110 		}
111 
112 		if (flag || (pinfo == NULL))
113 			continue;
114 
115 		dbchg++;
116 
117 		/*
118 		 * If (otherstoo > 0), more than one package has an
119 		 * interest in the ept entry in the database. Setting
120 		 * ept->ftype = '\0' effectively marks the file as being
121 		 * "shared", thus ensuring the ept entry will not
122 		 * subsequently be removed. Shared editable files (ftype
123 		 * 'e') are a special case: they should be passed to a
124 		 * class action script if present. Setting ept->ftype =
125 		 * '^' indicates this special case of shared editable
126 		 * file, allowing the distinction to be made later.
127 		 */
128 		if (!pinfo->editflag && otherstoo)
129 			ept->ftype = (ept->ftype == 'e') ? '^' : '\0';
130 		if (*pinfo->aclass)
131 			(void) strcpy(ept->pkg_class, pinfo->aclass);
132 		eptlist[eptnum] = ept;
133 
134 		ept->path = pathdup(ept->path);
135 		if (ept->ainfo.local != NULL)
136 			ept->ainfo.local = pathdup(ept->ainfo.local);
137 
138 		ept = (struct cfent *)calloc(1, sizeof (struct cfent));
139 		if ((++eptnum % EPTMALLOC) == 0) {
140 			eptlist = (struct cfent **)realloc(eptlist,
141 			(eptnum+EPTMALLOC)*sizeof (struct cfent *));
142 			if (eptlist == NULL) {
143 				progerr(gettext(ERR_MEMORY), errno);
144 				quit(99);
145 			}
146 		}
147 	}
148 
149 	eptlist[eptnum] = (struct cfent *)NULL;
150 
151 	n = swapcfile(&vfp, &vfpo, pkginst, dbchg);
152 	if (n == RESULT_WRN) {
153 		warnflag++;
154 	} else if (n == RESULT_ERR) {
155 		quit(99);
156 	}
157 
158 	return (0);
159 }
160