Deleted Added
full compact
plist.c (96388) plist.c (96392)
1/*
2 * FreeBSD install - a package for the installation and maintainance
3 * of non-core utilities.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * Jordan K. Hubbard
15 * 18 July 1993
16 *
17 * General packing list routines.
18 *
19 */
20
21#include <sys/cdefs.h>
1/*
2 * FreeBSD install - a package for the installation and maintainance
3 * of non-core utilities.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * Jordan K. Hubbard
15 * 18 July 1993
16 *
17 * General packing list routines.
18 *
19 */
20
21#include <sys/cdefs.h>
22__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/lib/plist.c 96388 2002-05-11 03:48:49Z alfred $");
22__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/lib/plist.c 96392 2002-05-11 04:17:55Z alfred $");
23
24#include "lib.h"
25#include <err.h>
26#include <md5.h>
27
28/* Add an item to a packing list */
29void
30add_plist(Package *p, plist_t type, const char *arg)
31{
32 PackingList tmp;
33
34 tmp = new_plist_entry();
35 tmp->name = copy_string(arg);
36 tmp->type = type;
37
38 if (!p->head)
39 p->head = p->tail = tmp;
40 else {
41 tmp->prev = p->tail;
42 p->tail->next = tmp;
43 p->tail = tmp;
44 }
45 switch (type) {
46 case PLIST_NAME:
47 p->name = tmp->name;
48 break;
49
50 case PLIST_ORIGIN:
51 p->origin = tmp->name;
52 break;
53
54 default:
55 break;
56 }
57}
58
59void
60add_plist_top(Package *p, plist_t type, const char *arg)
61{
62 PackingList tmp;
63
64 tmp = new_plist_entry();
65 tmp->name = copy_string(arg);
66 tmp->type = type;
67
68 if (!p->head)
69 p->head = p->tail = tmp;
70 else {
71 tmp->next = p->head;
72 p->head->prev = tmp;
73 p->head = tmp;
74 }
75}
76
77/* Return the last (most recent) entry in a packing list */
78PackingList
79last_plist(Package *p)
80{
81 return p->tail;
82}
83
84/* Mark all items in a packing list to prevent iteration over them */
85void
86mark_plist(Package *pkg)
87{
88 PackingList p = pkg->head;
89
90 while (p) {
91 p->marked = TRUE;
92 p = p->next;
93 }
94}
95
96/* Find a given item in a packing list and, if so, return it (else NULL) */
97PackingList
98find_plist(Package *pkg, plist_t type)
99{
100 PackingList p = pkg->head;
101
102 while (p) {
103 if (p->type == type)
104 return p;
105 p = p->next;
106 }
107 return NULL;
108}
109
110/* Look for a specific boolean option argument in the list */
111char *
112find_plist_option(Package *pkg, const char *name)
113{
114 PackingList p = pkg->head;
115
116 while (p) {
117 if (p->type == PLIST_OPTION && !strcmp(p->name, name))
118 return p->name;
119 p = p->next;
120 }
121 return NULL;
122}
123
124/*
125 * Delete plist item 'type' in the list (if 'name' is non-null, match it
126 * too.) If 'all' is set, delete all items, not just the first occurance.
127 */
128void
129delete_plist(Package *pkg, Boolean all, plist_t type, const char *name)
130{
131 PackingList p = pkg->head;
132
133 while (p) {
134 PackingList pnext = p->next;
135
136 if (p->type == type && (!name || !strcmp(name, p->name))) {
137 free(p->name);
138 if (p->prev)
139 p->prev->next = pnext;
140 else
141 pkg->head = pnext;
142 if (pnext)
143 pnext->prev = p->prev;
144 else
145 pkg->tail = p->prev;
146 free(p);
147 if (!all)
148 return;
149 p = pnext;
150 }
151 else
152 p = p->next;
153 }
154}
155
156/* Allocate a new packing list entry */
157PackingList
158new_plist_entry(void)
159{
160 PackingList ret;
161
162 ret = (PackingList)malloc(sizeof(struct _plist));
163 bzero(ret, sizeof(struct _plist));
164 return ret;
165}
166
167/* Free an entire packing list */
168void
169free_plist(Package *pkg)
170{
171 PackingList p = pkg->head;
172
173 while (p) {
174 PackingList p1 = p->next;
175
176 free(p->name);
177 free(p);
178 p = p1;
179 }
180 pkg->head = pkg->tail = NULL;
181}
182
183/*
184 * For an ascii string denoting a plist command, return its code and
185 * optionally its argument(s)
186 */
187int
188plist_cmd(const char *s, char **arg)
189{
190 char cmd[FILENAME_MAX + 20]; /* 20 == fudge for max cmd len */
191 char *cp;
192 const char *sp;
193
194 strcpy(cmd, s);
195 str_lowercase(cmd);
196 cp = cmd;
197 sp = s;
198 while (*cp) {
199 if (isspace(*cp)) {
200 *cp = '\0';
201 while (isspace(*sp)) /* Never sure if macro, increment later */
202 ++sp;
203 break;
204 }
205 ++cp, ++sp;
206 }
207 if (arg)
208 (const char *)*arg = sp;
209 if (!strcmp(cmd, "cwd"))
210 return PLIST_CWD;
211 else if (!strcmp(cmd, "srcdir"))
212 return PLIST_SRC;
213 else if (!strcmp(cmd, "cd"))
214 return PLIST_CWD;
215 else if (!strcmp(cmd, "exec"))
216 return PLIST_CMD;
217 else if (!strcmp(cmd, "unexec"))
218 return PLIST_UNEXEC;
219 else if (!strcmp(cmd, "mode"))
220 return PLIST_CHMOD;
221 else if (!strcmp(cmd, "owner"))
222 return PLIST_CHOWN;
223 else if (!strcmp(cmd, "group"))
224 return PLIST_CHGRP;
225 else if (!strcmp(cmd, "comment")) {
226 if (!strncmp(*arg, "ORIGIN:", 7)) {
227 *arg += 7;
228 return PLIST_ORIGIN;
229 } else if (!strncmp(*arg, "DEPORIGIN:", 10)) {
230 *arg += 10;
231 return PLIST_DEPORIGIN;
232 }
233 return PLIST_COMMENT;
234 } else if (!strcmp(cmd, "ignore"))
235 return PLIST_IGNORE;
236 else if (!strcmp(cmd, "ignore_inst"))
237 return PLIST_IGNORE_INST;
238 else if (!strcmp(cmd, "name"))
239 return PLIST_NAME;
240 else if (!strcmp(cmd, "display"))
241 return PLIST_DISPLAY;
242 else if (!strcmp(cmd, "pkgdep"))
243 return PLIST_PKGDEP;
244 else if (!strcmp(cmd, "mtree"))
245 return PLIST_MTREE;
246 else if (!strcmp(cmd, "dirrm"))
247 return PLIST_DIR_RM;
248 else if (!strcmp(cmd, "option"))
249 return PLIST_OPTION;
250 else
251 return FAIL;
252}
253
254/* Read a packing list from a file */
255void
256read_plist(Package *pkg, FILE *fp)
257{
258 char *cp, pline[FILENAME_MAX];
259 int cmd, major, minor;
260
261 pkg->fmtver_maj = 1;
262 pkg->fmtver_mnr = 0;
263 while (fgets(pline, FILENAME_MAX, fp)) {
264 int len = strlen(pline);
265
266 while (len && isspace(pline[len - 1]))
267 pline[--len] = '\0';
268 if (!len)
269 continue;
270 cp = pline;
271 if (pline[0] != CMD_CHAR) {
272 cmd = PLIST_FILE;
273 goto bottom;
274 }
275 cmd = plist_cmd(pline + 1, &cp);
276 if (cmd == FAIL) {
277 cleanup(0);
23
24#include "lib.h"
25#include <err.h>
26#include <md5.h>
27
28/* Add an item to a packing list */
29void
30add_plist(Package *p, plist_t type, const char *arg)
31{
32 PackingList tmp;
33
34 tmp = new_plist_entry();
35 tmp->name = copy_string(arg);
36 tmp->type = type;
37
38 if (!p->head)
39 p->head = p->tail = tmp;
40 else {
41 tmp->prev = p->tail;
42 p->tail->next = tmp;
43 p->tail = tmp;
44 }
45 switch (type) {
46 case PLIST_NAME:
47 p->name = tmp->name;
48 break;
49
50 case PLIST_ORIGIN:
51 p->origin = tmp->name;
52 break;
53
54 default:
55 break;
56 }
57}
58
59void
60add_plist_top(Package *p, plist_t type, const char *arg)
61{
62 PackingList tmp;
63
64 tmp = new_plist_entry();
65 tmp->name = copy_string(arg);
66 tmp->type = type;
67
68 if (!p->head)
69 p->head = p->tail = tmp;
70 else {
71 tmp->next = p->head;
72 p->head->prev = tmp;
73 p->head = tmp;
74 }
75}
76
77/* Return the last (most recent) entry in a packing list */
78PackingList
79last_plist(Package *p)
80{
81 return p->tail;
82}
83
84/* Mark all items in a packing list to prevent iteration over them */
85void
86mark_plist(Package *pkg)
87{
88 PackingList p = pkg->head;
89
90 while (p) {
91 p->marked = TRUE;
92 p = p->next;
93 }
94}
95
96/* Find a given item in a packing list and, if so, return it (else NULL) */
97PackingList
98find_plist(Package *pkg, plist_t type)
99{
100 PackingList p = pkg->head;
101
102 while (p) {
103 if (p->type == type)
104 return p;
105 p = p->next;
106 }
107 return NULL;
108}
109
110/* Look for a specific boolean option argument in the list */
111char *
112find_plist_option(Package *pkg, const char *name)
113{
114 PackingList p = pkg->head;
115
116 while (p) {
117 if (p->type == PLIST_OPTION && !strcmp(p->name, name))
118 return p->name;
119 p = p->next;
120 }
121 return NULL;
122}
123
124/*
125 * Delete plist item 'type' in the list (if 'name' is non-null, match it
126 * too.) If 'all' is set, delete all items, not just the first occurance.
127 */
128void
129delete_plist(Package *pkg, Boolean all, plist_t type, const char *name)
130{
131 PackingList p = pkg->head;
132
133 while (p) {
134 PackingList pnext = p->next;
135
136 if (p->type == type && (!name || !strcmp(name, p->name))) {
137 free(p->name);
138 if (p->prev)
139 p->prev->next = pnext;
140 else
141 pkg->head = pnext;
142 if (pnext)
143 pnext->prev = p->prev;
144 else
145 pkg->tail = p->prev;
146 free(p);
147 if (!all)
148 return;
149 p = pnext;
150 }
151 else
152 p = p->next;
153 }
154}
155
156/* Allocate a new packing list entry */
157PackingList
158new_plist_entry(void)
159{
160 PackingList ret;
161
162 ret = (PackingList)malloc(sizeof(struct _plist));
163 bzero(ret, sizeof(struct _plist));
164 return ret;
165}
166
167/* Free an entire packing list */
168void
169free_plist(Package *pkg)
170{
171 PackingList p = pkg->head;
172
173 while (p) {
174 PackingList p1 = p->next;
175
176 free(p->name);
177 free(p);
178 p = p1;
179 }
180 pkg->head = pkg->tail = NULL;
181}
182
183/*
184 * For an ascii string denoting a plist command, return its code and
185 * optionally its argument(s)
186 */
187int
188plist_cmd(const char *s, char **arg)
189{
190 char cmd[FILENAME_MAX + 20]; /* 20 == fudge for max cmd len */
191 char *cp;
192 const char *sp;
193
194 strcpy(cmd, s);
195 str_lowercase(cmd);
196 cp = cmd;
197 sp = s;
198 while (*cp) {
199 if (isspace(*cp)) {
200 *cp = '\0';
201 while (isspace(*sp)) /* Never sure if macro, increment later */
202 ++sp;
203 break;
204 }
205 ++cp, ++sp;
206 }
207 if (arg)
208 (const char *)*arg = sp;
209 if (!strcmp(cmd, "cwd"))
210 return PLIST_CWD;
211 else if (!strcmp(cmd, "srcdir"))
212 return PLIST_SRC;
213 else if (!strcmp(cmd, "cd"))
214 return PLIST_CWD;
215 else if (!strcmp(cmd, "exec"))
216 return PLIST_CMD;
217 else if (!strcmp(cmd, "unexec"))
218 return PLIST_UNEXEC;
219 else if (!strcmp(cmd, "mode"))
220 return PLIST_CHMOD;
221 else if (!strcmp(cmd, "owner"))
222 return PLIST_CHOWN;
223 else if (!strcmp(cmd, "group"))
224 return PLIST_CHGRP;
225 else if (!strcmp(cmd, "comment")) {
226 if (!strncmp(*arg, "ORIGIN:", 7)) {
227 *arg += 7;
228 return PLIST_ORIGIN;
229 } else if (!strncmp(*arg, "DEPORIGIN:", 10)) {
230 *arg += 10;
231 return PLIST_DEPORIGIN;
232 }
233 return PLIST_COMMENT;
234 } else if (!strcmp(cmd, "ignore"))
235 return PLIST_IGNORE;
236 else if (!strcmp(cmd, "ignore_inst"))
237 return PLIST_IGNORE_INST;
238 else if (!strcmp(cmd, "name"))
239 return PLIST_NAME;
240 else if (!strcmp(cmd, "display"))
241 return PLIST_DISPLAY;
242 else if (!strcmp(cmd, "pkgdep"))
243 return PLIST_PKGDEP;
244 else if (!strcmp(cmd, "mtree"))
245 return PLIST_MTREE;
246 else if (!strcmp(cmd, "dirrm"))
247 return PLIST_DIR_RM;
248 else if (!strcmp(cmd, "option"))
249 return PLIST_OPTION;
250 else
251 return FAIL;
252}
253
254/* Read a packing list from a file */
255void
256read_plist(Package *pkg, FILE *fp)
257{
258 char *cp, pline[FILENAME_MAX];
259 int cmd, major, minor;
260
261 pkg->fmtver_maj = 1;
262 pkg->fmtver_mnr = 0;
263 while (fgets(pline, FILENAME_MAX, fp)) {
264 int len = strlen(pline);
265
266 while (len && isspace(pline[len - 1]))
267 pline[--len] = '\0';
268 if (!len)
269 continue;
270 cp = pline;
271 if (pline[0] != CMD_CHAR) {
272 cmd = PLIST_FILE;
273 goto bottom;
274 }
275 cmd = plist_cmd(pline + 1, &cp);
276 if (cmd == FAIL) {
277 cleanup(0);
278 errx(2, "%s: bad command '%s'", __FUNCTION__, pline);
278 errx(2, "%s: bad command '%s'", __func__, pline);
279 }
280 if (*cp == '\0') {
281 cp = NULL;
282 goto bottom;
283 }
284 if (cmd == PLIST_COMMENT && sscanf(cp, "PKG_FORMAT_REVISION:%d.%d\n",
285 &major, &minor) == 2) {
286 pkg->fmtver_maj = major;
287 pkg->fmtver_mnr = minor;
288 if (verscmp(pkg, PLIST_FMT_VER_MAJOR, PLIST_FMT_VER_MINOR) <= 0)
289 goto bottom;
290
291 warnx("plist format revision (%d.%d) is higher than supported"
292 "(%d.%d)", pkg->fmtver_maj, pkg->fmtver_mnr,
293 PLIST_FMT_VER_MAJOR, PLIST_FMT_VER_MINOR);
294 if (pkg->fmtver_maj > PLIST_FMT_VER_MAJOR) {
295 cleanup(0);
296 exit(2);
297 }
298 }
299bottom:
300 add_plist(pkg, cmd, cp);
301 }
302}
303
304/* Write a packing list to a file, converting commands to ascii equivs */
305void
306write_plist(Package *pkg, FILE *fp)
307{
308 PackingList plist = pkg->head;
309
310 while (plist) {
311 switch(plist->type) {
312 case PLIST_FILE:
313 fprintf(fp, "%s\n", plist->name);
314 break;
315
316 case PLIST_CWD:
317 fprintf(fp, "%ccwd %s\n", CMD_CHAR, plist->name);
318 break;
319
320 case PLIST_SRC:
321 fprintf(fp, "%csrcdir %s\n", CMD_CHAR, plist->name);
322 break;
323
324 case PLIST_CMD:
325 fprintf(fp, "%cexec %s\n", CMD_CHAR, plist->name);
326 break;
327
328 case PLIST_UNEXEC:
329 fprintf(fp, "%cunexec %s\n", CMD_CHAR, plist->name);
330 break;
331
332 case PLIST_CHMOD:
333 fprintf(fp, "%cmode %s\n", CMD_CHAR, plist->name ? plist->name : "");
334 break;
335
336 case PLIST_CHOWN:
337 fprintf(fp, "%cowner %s\n", CMD_CHAR, plist->name ? plist->name : "");
338 break;
339
340 case PLIST_CHGRP:
341 fprintf(fp, "%cgroup %s\n", CMD_CHAR, plist->name ? plist->name : "");
342 break;
343
344 case PLIST_COMMENT:
345 fprintf(fp, "%ccomment %s\n", CMD_CHAR, plist->name);
346 break;
347
348 case PLIST_IGNORE:
349 case PLIST_IGNORE_INST: /* a one-time non-ignored file */
350 fprintf(fp, "%cignore\n", CMD_CHAR);
351 break;
352
353 case PLIST_NAME:
354 fprintf(fp, "%cname %s\n", CMD_CHAR, plist->name);
355 break;
356
357 case PLIST_DISPLAY:
358 fprintf(fp, "%cdisplay %s\n", CMD_CHAR, plist->name);
359 break;
360
361 case PLIST_PKGDEP:
362 fprintf(fp, "%cpkgdep %s\n", CMD_CHAR, plist->name);
363 break;
364
365 case PLIST_MTREE:
366 fprintf(fp, "%cmtree %s\n", CMD_CHAR, plist->name);
367 break;
368
369 case PLIST_DIR_RM:
370 fprintf(fp, "%cdirrm %s\n", CMD_CHAR, plist->name);
371 break;
372
373 case PLIST_OPTION:
374 fprintf(fp, "%coption %s\n", CMD_CHAR, plist->name);
375 break;
376
377 case PLIST_ORIGIN:
378 fprintf(fp, "%ccomment ORIGIN:%s\n", CMD_CHAR, plist->name);
379 break;
380
381 case PLIST_DEPORIGIN:
382 fprintf(fp, "%ccomment DEPORIGIN:%s\n", CMD_CHAR, plist->name);
383 break;
384
385 default:
386 cleanup(0);
279 }
280 if (*cp == '\0') {
281 cp = NULL;
282 goto bottom;
283 }
284 if (cmd == PLIST_COMMENT && sscanf(cp, "PKG_FORMAT_REVISION:%d.%d\n",
285 &major, &minor) == 2) {
286 pkg->fmtver_maj = major;
287 pkg->fmtver_mnr = minor;
288 if (verscmp(pkg, PLIST_FMT_VER_MAJOR, PLIST_FMT_VER_MINOR) <= 0)
289 goto bottom;
290
291 warnx("plist format revision (%d.%d) is higher than supported"
292 "(%d.%d)", pkg->fmtver_maj, pkg->fmtver_mnr,
293 PLIST_FMT_VER_MAJOR, PLIST_FMT_VER_MINOR);
294 if (pkg->fmtver_maj > PLIST_FMT_VER_MAJOR) {
295 cleanup(0);
296 exit(2);
297 }
298 }
299bottom:
300 add_plist(pkg, cmd, cp);
301 }
302}
303
304/* Write a packing list to a file, converting commands to ascii equivs */
305void
306write_plist(Package *pkg, FILE *fp)
307{
308 PackingList plist = pkg->head;
309
310 while (plist) {
311 switch(plist->type) {
312 case PLIST_FILE:
313 fprintf(fp, "%s\n", plist->name);
314 break;
315
316 case PLIST_CWD:
317 fprintf(fp, "%ccwd %s\n", CMD_CHAR, plist->name);
318 break;
319
320 case PLIST_SRC:
321 fprintf(fp, "%csrcdir %s\n", CMD_CHAR, plist->name);
322 break;
323
324 case PLIST_CMD:
325 fprintf(fp, "%cexec %s\n", CMD_CHAR, plist->name);
326 break;
327
328 case PLIST_UNEXEC:
329 fprintf(fp, "%cunexec %s\n", CMD_CHAR, plist->name);
330 break;
331
332 case PLIST_CHMOD:
333 fprintf(fp, "%cmode %s\n", CMD_CHAR, plist->name ? plist->name : "");
334 break;
335
336 case PLIST_CHOWN:
337 fprintf(fp, "%cowner %s\n", CMD_CHAR, plist->name ? plist->name : "");
338 break;
339
340 case PLIST_CHGRP:
341 fprintf(fp, "%cgroup %s\n", CMD_CHAR, plist->name ? plist->name : "");
342 break;
343
344 case PLIST_COMMENT:
345 fprintf(fp, "%ccomment %s\n", CMD_CHAR, plist->name);
346 break;
347
348 case PLIST_IGNORE:
349 case PLIST_IGNORE_INST: /* a one-time non-ignored file */
350 fprintf(fp, "%cignore\n", CMD_CHAR);
351 break;
352
353 case PLIST_NAME:
354 fprintf(fp, "%cname %s\n", CMD_CHAR, plist->name);
355 break;
356
357 case PLIST_DISPLAY:
358 fprintf(fp, "%cdisplay %s\n", CMD_CHAR, plist->name);
359 break;
360
361 case PLIST_PKGDEP:
362 fprintf(fp, "%cpkgdep %s\n", CMD_CHAR, plist->name);
363 break;
364
365 case PLIST_MTREE:
366 fprintf(fp, "%cmtree %s\n", CMD_CHAR, plist->name);
367 break;
368
369 case PLIST_DIR_RM:
370 fprintf(fp, "%cdirrm %s\n", CMD_CHAR, plist->name);
371 break;
372
373 case PLIST_OPTION:
374 fprintf(fp, "%coption %s\n", CMD_CHAR, plist->name);
375 break;
376
377 case PLIST_ORIGIN:
378 fprintf(fp, "%ccomment ORIGIN:%s\n", CMD_CHAR, plist->name);
379 break;
380
381 case PLIST_DEPORIGIN:
382 fprintf(fp, "%ccomment DEPORIGIN:%s\n", CMD_CHAR, plist->name);
383 break;
384
385 default:
386 cleanup(0);
387 errx(2, "%s: unknown command type %d (%s)", __FUNCTION__,
387 errx(2, "%s: unknown command type %d (%s)", __func__,
388 plist->type, plist->name);
389 break;
390 }
391 plist = plist->next;
392 }
393}
394
395/*
396 * Delete the results of a package installation.
397 *
398 * This is here rather than in the pkg_delete code because pkg_add needs to
399 * run it too in cases of failure.
400 */
401int
402delete_package(Boolean ign_err, Boolean nukedirs, Package *pkg)
403{
404 PackingList p;
405 const char *Where = ".", *last_file = "";
406 Boolean fail = SUCCESS;
407 Boolean preserve;
408 char tmp[FILENAME_MAX], *name = NULL;
409
410 preserve = find_plist_option(pkg, "preserve") ? TRUE : FALSE;
411 for (p = pkg->head; p; p = p->next) {
412 switch (p->type) {
413 case PLIST_NAME:
414 name = p->name;
415 break;
416
417 case PLIST_IGNORE:
418 p = p->next;
419 break;
420
421 case PLIST_CWD:
422 Where = p->name;
423 if (Verbose)
424 printf("Change working directory to %s\n", Where);
425 break;
426
427 case PLIST_UNEXEC:
428 format_cmd(tmp, p->name, Where, last_file);
429 if (Verbose)
430 printf("Execute '%s'\n", tmp);
431 if (!Fake && system(tmp)) {
432 warnx("unexec command for '%s' failed", tmp);
433 fail = FAIL;
434 }
435 break;
436
437 case PLIST_FILE:
438 last_file = p->name;
439 sprintf(tmp, "%s/%s", Where, p->name);
440 if (isdir(tmp) && fexists(tmp) && !issymlink(tmp)) {
441 warnx("cannot delete specified file '%s' - it is a directory!\n"
442 "this packing list is incorrect - ignoring delete request", tmp);
443 }
444 else {
445 if (p->next && p->next->type == PLIST_COMMENT && !strncmp(p->next->name, "MD5:", 4)) {
446 char *cp = NULL, buf[33];
447
448 /*
449 * For packing lists whose version is 1.1 or greater, the md5
450 * hash for a symlink is calculated on the string returned
451 * by readlink().
452 */
453 if (issymlink(tmp) && verscmp(pkg, 1, 0) > 0) {
454 int len;
455 char linkbuf[FILENAME_MAX];
456
457 if ((len = readlink(tmp, linkbuf, FILENAME_MAX)) > 0)
458 cp = MD5Data((unsigned char *)linkbuf, len, buf);
459 } else if (isfile(tmp) || verscmp(pkg, 1, 1) < 0)
460 cp = MD5File(tmp, buf);
461
462 if (cp != NULL) {
463 /* Mismatch? */
464 if (strcmp(cp, p->next->name + 4)) {
465 warnx("'%s' fails original MD5 checksum - %s",
466 tmp, Force ? "deleted anyway." : "not deleted.");
467 if (!Force) {
468 fail = FAIL;
469 continue;
470 }
471 }
472 }
473 }
474 if (Verbose)
475 printf("Delete file %s\n", tmp);
476 if (!Fake) {
477 if (delete_hierarchy(tmp, ign_err, nukedirs))
478 fail = FAIL;
479 if (preserve && name) {
480 char tmp2[FILENAME_MAX];
481
482 if (make_preserve_name(tmp2, FILENAME_MAX, name, tmp)) {
483 if (fexists(tmp2)) {
484 if (rename(tmp2, tmp))
485 warn("preserve: unable to restore %s as %s",
486 tmp2, tmp);
487 }
488 }
489 }
490 }
491 }
492 break;
493
494 case PLIST_DIR_RM:
495 sprintf(tmp, "%s/%s", Where, p->name);
496 if (!isdir(tmp) && fexists(tmp)) {
497 warnx("cannot delete specified directory '%s' - it is a file!\n"
498 "this packing list is incorrect - ignoring delete request", tmp);
499 }
500 else {
501 if (Verbose)
502 printf("Delete directory %s\n", tmp);
503 if (!Fake && delete_hierarchy(tmp, ign_err, FALSE)) {
504 warnx("unable to completely remove directory '%s'", tmp);
505 fail = FAIL;
506 }
507 }
508 last_file = p->name;
509 break;
510
511 default:
512 break;
513 }
514 }
515 return fail;
516}
517
518#ifdef DEBUG
519#define RMDIR(dir) vsystem("%s %s", RMDIR_CMD, dir)
520#define REMOVE(dir,ie) vsystem("%s %s%s", REMOVE_CMD, (ie ? "-f " : ""), dir)
521#else
522#define RMDIR rmdir
523#define REMOVE(file,ie) (remove(file) && !(ie))
524#endif
525
526/* Selectively delete a hierarchy */
527int
528delete_hierarchy(const char *dir, Boolean ign_err, Boolean nukedirs)
529{
530 char *cp1, *cp2;
531
532 cp1 = cp2 = strdup(dir);
533 if (!fexists(dir)) {
534 if (!ign_err)
535 warnx("%s '%s' doesn't really exist",
536 isdir(dir) ? "directory" : "file", dir);
537 return !ign_err;
538 }
539 else if (nukedirs) {
540 if (vsystem("%s -r%s %s", REMOVE_CMD, (ign_err ? "f" : ""), dir))
541 return 1;
542 }
543 else if (isdir(dir) && !issymlink(dir)) {
544 if (RMDIR(dir) && !ign_err)
545 return 1;
546 }
547 else {
548 if (REMOVE(dir, ign_err))
549 return 1;
550 }
551
552 if (!nukedirs)
553 return 0;
554 while (cp2) {
555 if ((cp2 = strrchr(cp1, '/')) != NULL)
556 *cp2 = '\0';
557 if (!isemptydir(dir))
558 return 0;
559 if (RMDIR(dir) && !ign_err) {
560 if (!fexists(dir))
561 warnx("directory '%s' doesn't really exist", dir);
562 else
563 return 1;
564 }
565 /* back up the pathname one component */
566 if (cp2) {
567 cp1 = strdup(dir);
568 }
569 }
570 return 0;
571}
388 plist->type, plist->name);
389 break;
390 }
391 plist = plist->next;
392 }
393}
394
395/*
396 * Delete the results of a package installation.
397 *
398 * This is here rather than in the pkg_delete code because pkg_add needs to
399 * run it too in cases of failure.
400 */
401int
402delete_package(Boolean ign_err, Boolean nukedirs, Package *pkg)
403{
404 PackingList p;
405 const char *Where = ".", *last_file = "";
406 Boolean fail = SUCCESS;
407 Boolean preserve;
408 char tmp[FILENAME_MAX], *name = NULL;
409
410 preserve = find_plist_option(pkg, "preserve") ? TRUE : FALSE;
411 for (p = pkg->head; p; p = p->next) {
412 switch (p->type) {
413 case PLIST_NAME:
414 name = p->name;
415 break;
416
417 case PLIST_IGNORE:
418 p = p->next;
419 break;
420
421 case PLIST_CWD:
422 Where = p->name;
423 if (Verbose)
424 printf("Change working directory to %s\n", Where);
425 break;
426
427 case PLIST_UNEXEC:
428 format_cmd(tmp, p->name, Where, last_file);
429 if (Verbose)
430 printf("Execute '%s'\n", tmp);
431 if (!Fake && system(tmp)) {
432 warnx("unexec command for '%s' failed", tmp);
433 fail = FAIL;
434 }
435 break;
436
437 case PLIST_FILE:
438 last_file = p->name;
439 sprintf(tmp, "%s/%s", Where, p->name);
440 if (isdir(tmp) && fexists(tmp) && !issymlink(tmp)) {
441 warnx("cannot delete specified file '%s' - it is a directory!\n"
442 "this packing list is incorrect - ignoring delete request", tmp);
443 }
444 else {
445 if (p->next && p->next->type == PLIST_COMMENT && !strncmp(p->next->name, "MD5:", 4)) {
446 char *cp = NULL, buf[33];
447
448 /*
449 * For packing lists whose version is 1.1 or greater, the md5
450 * hash for a symlink is calculated on the string returned
451 * by readlink().
452 */
453 if (issymlink(tmp) && verscmp(pkg, 1, 0) > 0) {
454 int len;
455 char linkbuf[FILENAME_MAX];
456
457 if ((len = readlink(tmp, linkbuf, FILENAME_MAX)) > 0)
458 cp = MD5Data((unsigned char *)linkbuf, len, buf);
459 } else if (isfile(tmp) || verscmp(pkg, 1, 1) < 0)
460 cp = MD5File(tmp, buf);
461
462 if (cp != NULL) {
463 /* Mismatch? */
464 if (strcmp(cp, p->next->name + 4)) {
465 warnx("'%s' fails original MD5 checksum - %s",
466 tmp, Force ? "deleted anyway." : "not deleted.");
467 if (!Force) {
468 fail = FAIL;
469 continue;
470 }
471 }
472 }
473 }
474 if (Verbose)
475 printf("Delete file %s\n", tmp);
476 if (!Fake) {
477 if (delete_hierarchy(tmp, ign_err, nukedirs))
478 fail = FAIL;
479 if (preserve && name) {
480 char tmp2[FILENAME_MAX];
481
482 if (make_preserve_name(tmp2, FILENAME_MAX, name, tmp)) {
483 if (fexists(tmp2)) {
484 if (rename(tmp2, tmp))
485 warn("preserve: unable to restore %s as %s",
486 tmp2, tmp);
487 }
488 }
489 }
490 }
491 }
492 break;
493
494 case PLIST_DIR_RM:
495 sprintf(tmp, "%s/%s", Where, p->name);
496 if (!isdir(tmp) && fexists(tmp)) {
497 warnx("cannot delete specified directory '%s' - it is a file!\n"
498 "this packing list is incorrect - ignoring delete request", tmp);
499 }
500 else {
501 if (Verbose)
502 printf("Delete directory %s\n", tmp);
503 if (!Fake && delete_hierarchy(tmp, ign_err, FALSE)) {
504 warnx("unable to completely remove directory '%s'", tmp);
505 fail = FAIL;
506 }
507 }
508 last_file = p->name;
509 break;
510
511 default:
512 break;
513 }
514 }
515 return fail;
516}
517
518#ifdef DEBUG
519#define RMDIR(dir) vsystem("%s %s", RMDIR_CMD, dir)
520#define REMOVE(dir,ie) vsystem("%s %s%s", REMOVE_CMD, (ie ? "-f " : ""), dir)
521#else
522#define RMDIR rmdir
523#define REMOVE(file,ie) (remove(file) && !(ie))
524#endif
525
526/* Selectively delete a hierarchy */
527int
528delete_hierarchy(const char *dir, Boolean ign_err, Boolean nukedirs)
529{
530 char *cp1, *cp2;
531
532 cp1 = cp2 = strdup(dir);
533 if (!fexists(dir)) {
534 if (!ign_err)
535 warnx("%s '%s' doesn't really exist",
536 isdir(dir) ? "directory" : "file", dir);
537 return !ign_err;
538 }
539 else if (nukedirs) {
540 if (vsystem("%s -r%s %s", REMOVE_CMD, (ign_err ? "f" : ""), dir))
541 return 1;
542 }
543 else if (isdir(dir) && !issymlink(dir)) {
544 if (RMDIR(dir) && !ign_err)
545 return 1;
546 }
547 else {
548 if (REMOVE(dir, ign_err))
549 return 1;
550 }
551
552 if (!nukedirs)
553 return 0;
554 while (cp2) {
555 if ((cp2 = strrchr(cp1, '/')) != NULL)
556 *cp2 = '\0';
557 if (!isemptydir(dir))
558 return 0;
559 if (RMDIR(dir) && !ign_err) {
560 if (!fexists(dir))
561 warnx("directory '%s' doesn't really exist", dir);
562 else
563 return 1;
564 }
565 /* back up the pathname one component */
566 if (cp2) {
567 cp1 = strdup(dir);
568 }
569 }
570 return 0;
571}