1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
|
/* $NetBSD: perform.c,v 1.4 2003/09/01 16:27:13 jlam Exp $ */
#include <nbcompat.h>
#if HAVE_CONFIG_H
#include "config.h"
#endif
#if HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#endif
#ifndef lint
#if 0
static const char *rcsid = "from FreeBSD Id: perform.c,v 1.15 1997/10/13 15:03:52 jkh Exp";
#else
__RCSID("$NetBSD: perform.c,v 1.4 2003/09/01 16:27:13 jlam Exp $");
#endif
#endif
/*
* FreeBSD install - a package for the installation and maintainance
* of non-core utilities.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Jordan K. Hubbard
* 18 July 1993
*
* This is the main body of the delete module.
*
*/
/*
* Copyright (c) 1999 Christian E. Hopps
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Added the require find and require delete code
*/
#if HAVE_ERR_H
#include <err.h>
#endif
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include "lib.h"
#include "delete.h"
/* In which direction to search in require_find() */
typedef enum {
FIND_UP, FIND_DOWN
} rec_find_t;
static int require_find_recursive_up(lpkg_t *);
static int require_find_recursive_down(lpkg_t *, package_t *);
static int require_find(char *, rec_find_t);
static int require_delete(char *, int);
static void require_print(void);
static int undepend(const char *, void *);
static char LogDir[FILENAME_MAX];
static char linebuf[FILENAME_MAX];
static char pkgdir[FILENAME_MAX];
static package_t Plist;
static lpkg_head_t lpfindq;
static lpkg_head_t lpdelq;
static void
sanity_check(char *pkg)
{
if (!fexists(CONTENTS_FNAME)) {
cleanup(0);
errx(2, "installed package %s has no %s file!",
pkg, CONTENTS_FNAME);
}
}
void
cleanup(int sig)
{
/* Nothing to do */
if (sig) /* in case this is ever used as a signal handler */
exit(1);
}
/*
* deppkgname is the pkg from which's +REQUIRED_BY file we are
* about to remove pkg2delname. This function is called from
* findmatchingname(), deppkgname is expanded from a (possible) pattern.
*/
static int
undepend(const char *deppkgname, void *vp)
{
char *pkg2delname = vp;
char fname[FILENAME_MAX], ftmp[FILENAME_MAX];
char fbuf[FILENAME_MAX];
FILE *fp, *fpwr;
int s;
(void) snprintf(fname, sizeof(fname), "%s/%s/%s",
_pkgdb_getPKGDB_DIR(), deppkgname, REQUIRED_BY_FNAME);
fp = fopen(fname, "r");
if (fp == NULL) {
warnx("couldn't open dependency file `%s'", fname);
return 0;
}
(void) snprintf(ftmp, sizeof(ftmp), "%s.XXXXXX", fname);
s = mkstemp(ftmp);
if (s == -1) {
fclose(fp);
warnx("couldn't open temp file `%s'", ftmp);
return 0;
}
fpwr = fdopen(s, "w");
if (fpwr == NULL) {
close(s);
fclose(fp);
warnx("couldn't fdopen temp file `%s'", ftmp);
remove(ftmp);
return 0;
}
while (fgets(fbuf, sizeof(fbuf), fp) != NULL) {
if (fbuf[strlen(fbuf) - 1] == '\n')
fbuf[strlen(fbuf) - 1] = '\0';
if (strcmp(fbuf, pkg2delname)) /* no match */
fputs(fbuf, fpwr), putc('\n', fpwr);
}
(void) fclose(fp);
if (fchmod(s, 0644) == FAIL) {
warnx("error changing permission of temp file `%s'", ftmp);
fclose(fpwr);
remove(ftmp);
return 0;
}
if (fclose(fpwr) == EOF) {
warnx("error closing temp file `%s'", ftmp);
remove(ftmp);
return 0;
}
if (rename(ftmp, fname) == -1)
warnx("error renaming `%s' to `%s'", ftmp, fname);
remove(ftmp); /* just in case */
return 0;
}
/*
* Remove the current view's package dbdir from the +VIEWS file of the
* depoted package named by pkgname.
*/
static int
unview(const char *pkgname)
{
char fname[FILENAME_MAX], ftmp[FILENAME_MAX];
char fbuf[FILENAME_MAX];
char dbdir[FILENAME_MAX];
FILE *fp, *fpwr;
int s;
int cc;
(void) snprintf(dbdir, sizeof(dbdir), "%s", _pkgdb_getPKGDB_DIR());
/* Get the depot directory. */
(void) snprintf(fname, sizeof(fname), "%s/%s/%s",
dbdir, pkgname, DEPOT_FNAME);
if ((fp = fopen(fname, "r")) == NULL) {
warnx("unable to open `%s' file", fname);
return -1;
}
if (fgets(fbuf, sizeof(fbuf), fp) == NULL) {
(void) fclose(fp);
warnx("empty depot file `%s'", fname);
return -1;
}
if (fbuf[cc = strlen(fbuf) - 1] == '\n') {
fbuf[cc] = 0;
}
fclose(fp);
/*
* Copy the contents of the +VIEWS file into a temp file, but
* skip copying the name of the current view's package dbdir.
*/
(void) snprintf(fname, sizeof(fname), "%s/%s", fbuf, VIEWS_FNAME);
if ((fp = fopen(fname, "r")) == NULL) {
warnx("unable to open `%s' file", fname);
return -1;
}
(void) snprintf(ftmp, sizeof(ftmp), "%s.XXXXXX", fname);
if ((s = mkstemp(ftmp)) == -1) {
(void) fclose(fp);
warnx("unable to open `%s' temp file", ftmp);
return -1;
}
if ((fpwr = fdopen(s, "w")) == NULL) {
(void) close(s);
(void) remove(ftmp);
(void) fclose(fp);
warnx("unable to fdopen `%s' temp file", ftmp);
return -1;
}
while (fgets(fbuf, sizeof(fbuf), fp) != NULL) {
if (fbuf[cc = strlen(fbuf) - 1] == '\n') {
fbuf[cc] = 0;
}
if (strcmp(fbuf, dbdir) != 0) {
(void) fputs(fbuf, fpwr);
(void) putc('\n', fpwr);
}
}
(void) fclose(fp);
if (fchmod(s, 0644) == FAIL) {
(void) fclose(fpwr);
(void) remove(ftmp);
warnx("unable to change permissions of `%s' temp file", ftmp);
return -1;
}
if (fclose(fpwr) == EOF) {
(void) remove(ftmp);
warnx("unable to close `%s' temp file", ftmp);
return -1;
}
/* Rename the temp file to the +VIEWS file */
if (rename(ftmp, fname) == -1) {
(void) remove(ftmp);
warnx("unable to rename `%s' to `%s'", ftmp, fname);
return -1;
}
return 0;
}
/*
* Delete from directory 'home' all packages on lpkg_list.
* If tryall is set, ignore errors from pkg_delete(1).
*/
int
require_delete(char *home, int tryall)
{
lpkg_t *lpp;
int rv, fail;
int oldcwd;
/* save cwd */
oldcwd = open(".", O_RDONLY, 0);
if (oldcwd == -1)
err(EXIT_FAILURE, "cannot open \".\"");
(void) snprintf(pkgdir, sizeof(pkgdir), "%s", _pkgdb_getPKGDB_DIR());
/* walk list of things to delete */
fail = 0;
lpp = TAILQ_FIRST(&lpdelq);
for (; lpp; lpp = TAILQ_NEXT(lpp, lp_link)) {
int rm_installed; /* delete expanded pkg, not @pkgdep value */
char installed[FILENAME_MAX];
/* go to the db dir */
if (chdir(pkgdir) == FAIL) {
warnx("unable to change directory to %s, deinstall failed (1)",
pkgdir);
fail = 1;
break;
}
/* look to see if package was already deleted */
rm_installed = 0;
if (ispkgpattern(lpp->lp_name)) {
if (findmatchingname(".", lpp->lp_name, note_whats_installed, installed) != 1) {
warnx("%s appears to have been deleted", lpp->lp_name);
continue;
}
rm_installed = 1;
} else {
if (!fexists(lpp->lp_name)) {
warnx("%s appears to have been deleted", lpp->lp_name);
continue;
}
}
/* return home for execution of command */
if (chdir(home) == FAIL) {
warnx("unable to change directory to %s, deinstall failed (2)", home);
fail = 1;
break;
}
if (Verbose)
printf("deinstalling %s\n", rm_installed?installed:lpp->lp_name);
/* delete the package */
if (Fake)
rv = 0;
else
rv = vsystem("%s %s %s %s %s %s %s %s %s", ProgramPath,
Prefix ? "-p" : "",
Prefix ? Prefix : "",
Verbose ? "-v" : "",
(Force > 1) ? "-f -f" : (Force == 1) ? "-f" : "",
NoDeInstall ? "-D" : "",
CleanDirs ? "-d" : "",
Fake ? "-n" : "",
rm_installed?installed:lpp->lp_name);
/* check for delete failure */
if (rv && !tryall) {
fail = 1;
warnx("had problem removing %s%s", rm_installed?installed:lpp->lp_name,
Force ? ", continuing" : "");
if (!Force)
break;
}
}
/* cleanup list */
while ((lpp = TAILQ_FIRST(&lpdelq))) {
TAILQ_REMOVE(&lpdelq, lpp, lp_link);
free_lpkg(lpp);
}
/* return to the log dir */
if (fchdir(oldcwd) == FAIL) {
warnx("unable to change to previous directory, deinstall failed");
fail = 1;
}
close(oldcwd);
return (fail);
}
/*
* Recursively find all packages "up" the tree (follow +REQUIRED_BY).
* Return 1 on errors
*/
int
require_find_recursive_up(lpkg_t *thislpp)
{
lpkg_head_t reqq;
lpkg_t *lpp = NULL;
FILE *cfile;
char *nl;
/* see if we are on the find queue -- circular dependency */
if ((lpp = find_on_queue(&lpfindq, thislpp->lp_name))) {
warnx("circular dependency found for pkg %s", lpp->lp_name);
return (1);
}
TAILQ_INIT(&reqq);
(void) snprintf(pkgdir, sizeof(pkgdir), "%s/%s",
_pkgdb_getPKGDB_DIR(), thislpp->lp_name);
/* change to package's dir */
if (chdir(pkgdir) == FAIL) {
warnx("unable to change directory to %s! deinstall failed", pkgdir);
return (1);
}
/* terminate recursion if no required by's */
if (isemptyfile(REQUIRED_BY_FNAME))
return (0);
/* get packages that directly require us */
cfile = fopen(REQUIRED_BY_FNAME, "r");
if (!cfile) {
warnx("cannot open requirements file `%s'", REQUIRED_BY_FNAME);
return (1);
}
while (fgets(linebuf, sizeof(linebuf), cfile)) {
if ((nl = strrchr(linebuf, '\n')))
*nl = 0;
lpp = alloc_lpkg(linebuf);
TAILQ_INSERT_TAIL(&reqq, lpp, lp_link);
}
fclose(cfile);
/* put ourselves on the top of the find queue */
TAILQ_INSERT_HEAD(&lpfindq, thislpp, lp_link);
while ((lpp = TAILQ_FIRST(&reqq))) {
/* remove a direct req from our queue */
TAILQ_REMOVE(&reqq, lpp, lp_link);
/* find direct required requires */
if (require_find_recursive_up(lpp))
goto fail;
/* all requires taken care of, add to tail of delete queue
* if not already there */
if (find_on_queue(&lpdelq, lpp->lp_name))
free_lpkg(lpp);
else
TAILQ_INSERT_TAIL(&lpdelq, lpp, lp_link);
}
/* take ourselves off the find queue */
TAILQ_REMOVE(&lpfindq, thislpp, lp_link);
return (0);
fail:
while ((lpp = TAILQ_FIRST(&reqq))) {
TAILQ_REMOVE(&reqq, lpp, lp_link);
free_lpkg(lpp);
}
return (1);
}
/*
* Recursively find all packages "down" the tree (follow @pkgdep).
* Return 1 on errors
*/
int
require_find_recursive_down(lpkg_t *thislpp, package_t *plist)
{
plist_t *p;
lpkg_t *lpp, *lpp2;
lpkg_head_t reqq;
int rc, fail = 0;
/* see if we are on the find queue -- circular dependency */
if ((lpp = find_on_queue(&lpfindq, thislpp->lp_name))) {
warnx("circular dependency found for pkg %s", lpp->lp_name);
return (1);
}
TAILQ_INIT(&reqq);
/* width-first scan */
/* first enqueue all @pkgdep's to lpdelq, then (further below)
* go in recursively */
for (p = plist->head; p; p = p->next) {
switch (p->type) {
case PLIST_PKGDEP:
lpp = alloc_lpkg(p->name);
TAILQ_INSERT_TAIL(&reqq, lpp, lp_link);
lpp2 = find_on_queue(&lpdelq, p->name);
if (lpp2) {
TAILQ_REMOVE(&lpdelq, lpp2, lp_link);
free_lpkg(lpp2);
}
lpp = alloc_lpkg(p->name);
TAILQ_INSERT_TAIL(&lpdelq, lpp, lp_link);
break;
default:
break;
}
}
while ((lpp = TAILQ_FIRST(&reqq))) {
FILE *cfile;
package_t rPlist;
/* remove a direct req from our queue */
TAILQ_REMOVE(&reqq, lpp, lp_link);
/* Reset some state */
rPlist.head = NULL;
rPlist.tail = NULL;
/* prepare for recursion */
chdir(_pkgdb_getPKGDB_DIR());
if (ispkgpattern(lpp->lp_name)) {
char installed[FILENAME_MAX];
if (findmatchingname(".", lpp->lp_name, note_whats_installed, installed) != 1) {
warnx("cannot remove dependency for pkg-pattern %s", lpp->lp_name);
fail = 1;
goto fail;
}
if (chdir(installed) == -1) {
warnx("can't chdir to %s", installed);
fail = 1;
goto fail;
}
sanity_check(installed);
} else {
if (chdir(lpp->lp_name) == -1) {
warnx("cannot remove dependency from %s", lpp->lp_name);
fail = 1;
goto fail;
}
sanity_check(lpp->lp_name);
}
cfile = fopen(CONTENTS_FNAME, "r");
if (!cfile) {
warn("unable to open '%s' file", CONTENTS_FNAME);
fail = 1;
goto fail;
}
/* If we have a prefix, add it now */
if (Prefix)
add_plist(&rPlist, PLIST_CWD, Prefix);
read_plist(&rPlist, cfile);
fclose(cfile);
p = find_plist(&rPlist, PLIST_CWD);
if (!p) {
warnx("package '%s' doesn't have a prefix", lpp->lp_name);
free_plist(&rPlist);
fail = 1;
goto fail;
}
/* put ourselves on the top of the find queue */
TAILQ_INSERT_HEAD(&lpfindq, thislpp, lp_link);
rc = require_find_recursive_down(lpp, &rPlist);
free_plist(&rPlist);
if (rc) {
fail = 1;
goto fail;
}
/* take ourselves off the find queue */
TAILQ_REMOVE(&lpfindq, thislpp, lp_link);
free_lpkg(lpp);
}
fail:
/* Clean out reqq */
while ((lpp = TAILQ_FIRST(&reqq))) {
TAILQ_REMOVE(&reqq, lpp, lp_link);
free_lpkg(lpp);
}
return fail;
}
/*
* Start recursion in the one or other direction.
*/
int
require_find(char *pkg, rec_find_t updown)
{
lpkg_t *lpp;
int rv = 0;
TAILQ_INIT(&lpfindq);
TAILQ_INIT(&lpdelq);
lpp = alloc_lpkg(pkg);
switch (updown) {
case FIND_UP:
rv = require_find_recursive_up(lpp);
break;
case FIND_DOWN:
rv = require_find_recursive_down(lpp, &Plist);
break;
}
free_lpkg(lpp);
return (rv);
}
void
require_print(void)
{
lpkg_t *lpp;
/* print all but last -- deleting if requested */
while ((lpp = TAILQ_FIRST(&lpdelq))) {
TAILQ_REMOVE(&lpdelq, lpp, lp_link);
fprintf(stderr, "\t%s\n", lpp->lp_name);
free_lpkg(lpp);
}
}
/*
* This is seriously ugly code following. Written very fast!
*/
static int
pkg_do(char *pkg)
{
plist_t *p;
FILE *cfile;
FILE *fp;
char home[FILENAME_MAX];
char view[FILENAME_MAX];
int cc;
Boolean is_depoted_pkg = FALSE;
/* Reset some state */
if (Plist.head)
free_plist(&Plist);
(void) snprintf(LogDir, sizeof(LogDir), "%s/%s",
_pkgdb_getPKGDB_DIR(), pkg);
if (!fexists(LogDir) || !isdir(LogDir)) {
{
/* Check if the given package name matches something
* with 'pkg-[0-9]*' */
char try[FILENAME_MAX];
snprintf(try, FILENAME_MAX, "%s-[0-9]*", pkg);
if (findmatchingname(_pkgdb_getPKGDB_DIR(), try,
add_to_list_fn, &pkgs) != 0) {
return 0; /* we've just appended some names to the pkgs list,
* they will be processed after this package. */
}
}
/* No match */
warnx("package '%s' not installed", pkg);
return 1;
}
if (!getcwd(home, FILENAME_MAX)) {
cleanup(0);
errx(2, "unable to get current working directory!");
}
if (chdir(LogDir) == FAIL) {
warnx("unable to change directory to %s! deinstall failed", LogDir);
return 1;
}
if (!fexists(CONTENTS_FNAME)) {
warnx("package '%s' is not installed, %s missing", pkg, CONTENTS_FNAME);
if (!Force)
return 1;
}
if (fexists(PRESERVE_FNAME)) {
printf("Package `%s' is marked as not for deletion\n", pkg);
if (Force <= 1) {
return 1;
}
}
if (!isemptyfile(REQUIRED_BY_FNAME)) {
/* This package is required by others. Either nuke
* them (-r), or stop. */
if (!Recurse_up)
warnx("package `%s' is required by other packages:", pkg);
else if (Verbose)
printf("Building list of packages that require `%s'"
" to deinstall\n", pkg);
if (require_find(pkg, FIND_UP)) {
if (!Force || Recurse_up)
return (1);
}
chdir(LogDir); /* CWD was changed by require_find() */
if (!Recurse_up) {
require_print();
if (!Force)
return 1;
} else
require_delete(home, 0);
}
if (!isemptyfile(VIEWS_FNAME)) {
/* This package has instances in other views */
/* Delete them from the views */
if ((fp = fopen(VIEWS_FNAME, "r")) == NULL) {
warnx("unable to open '%s' file", VIEWS_FNAME);
return 1;
}
while (fgets(view, sizeof(view), fp) != NULL) {
if (view[cc = strlen(view) - 1] == '\n') {
view[cc] = 0;
}
if (Verbose) {
printf("Deleting package %s instance from `%s' view\n", pkg, view);
}
if (vsystem("%s -K %s %s", ProgramPath, view, pkg) != 0) {
warnx("unable to delete package %s from view %s", pkg, view);
(void) fclose(fp);
return 1;
}
}
(void) fclose(fp);
}
sanity_check(LogDir);
cfile = fopen(CONTENTS_FNAME, "r");
if (!cfile) {
warnx("unable to open '%s' file", CONTENTS_FNAME);
return 1;
}
/* If we have a prefix, add it now */
if (Prefix)
add_plist(&Plist, PLIST_CWD, Prefix);
read_plist(&Plist, cfile);
fclose(cfile);
p = find_plist(&Plist, PLIST_CWD);
if (!p) {
warnx("package '%s' doesn't have a prefix", pkg);
return 1;
}
setenv(PKG_PREFIX_VNAME, p->name, 1);
if (fexists(REQUIRE_FNAME)) {
if (Verbose)
printf("Executing 'require' script.\n");
vsystem("%s +x %s", CHMOD_CMD, REQUIRE_FNAME); /* be sure */
if (vsystem("./%s %s DEINSTALL", REQUIRE_FNAME, pkg)) {
warnx("package %s fails requirements %s", pkg,
Force ? "" : "- not deleted");
if (!Force)
return 1;
}
}
/*
* Ensure that we don't do VIEW-DEINSTALL action for old packages
* or for the package in its depot directory.
*/
if (!NoDeInstall && fexists(DEINSTALL_FNAME) && fexists(DEPOT_FNAME)) {
if (Fake) {
printf("Would execute view de-install script at this point (arg: VIEW-DEINSTALL).\n");
} else {
vsystem("%s +x %s", CHMOD_CMD, DEINSTALL_FNAME); /* make sure */
if (vsystem("./%s %s VIEW-DEINSTALL", DEINSTALL_FNAME, pkg)) {
warnx("view deinstall script returned error status");
if (!Force) {
return 1;
}
}
}
}
if (!NoDeInstall && fexists(DEINSTALL_FNAME) && !fexists(DEPOT_FNAME)) {
if (Fake)
printf("Would execute de-install script at this point (arg: DEINSTALL).\n");
else {
vsystem("%s +x %s", CHMOD_CMD, DEINSTALL_FNAME); /* make sure */
if (vsystem("./%s %s DEINSTALL", DEINSTALL_FNAME, pkg)) {
warnx("deinstall script returned error status");
if (!Force)
return 1;
}
}
}
if (!Fake) {
/* Some packages aren't packed right, so we need to just ignore delete_package()'s status. Ugh! :-( */
if (delete_package(FALSE, CleanDirs, &Plist) == FAIL)
warnx(
"couldn't entirely delete package `%s'\n"
"(perhaps the packing list is incorrectly specified?)", pkg);
}
if (!isemptyfile(DEPOT_FNAME)) {
if (Verbose)
printf("Attempting to remove the %s registration on package `%s'\n", VIEWS_FNAME, pkg);
if (!Fake)
(void) unview(pkg);
}
/*
* If this isn't a package in a view, then remove this package
* from the +REQUIRED_BY list of the packages this depends on.
*/
if (!fexists(DEPOT_FNAME)) {
for (p = Plist.head; p; p = p->next) {
if (p->type != PLIST_PKGDEP)
continue;
if (Verbose)
printf("Attempting to remove dependency on package `%s'\n", p->name);
if (!Fake)
findmatchingname(_pkgdb_getPKGDB_DIR(),
p->name, undepend, pkg);
}
}
if (Recurse_down) {
/* Also remove the packages further down, now that there's
* (most likely) nothing left which requires them. */
if (Verbose)
printf("Building list of packages that `%s' required\n", pkg);
if (require_find(pkg, FIND_DOWN))
return (1);
require_delete(home, 1);
}
if (!NoDeInstall && fexists(DEINSTALL_FNAME) && !fexists(DEPOT_FNAME)) {
if (Fake)
printf("Would execute post-de-install script at this point (arg: POST-DEINSTALL).\n");
else {
vsystem("%s +x %s", CHMOD_CMD, DEINSTALL_FNAME); /* make sure */
if (vsystem("./%s %s POST-DEINSTALL", DEINSTALL_FNAME, pkg)) {
warnx("post-deinstall script returned error status");
if (!Force)
return 1;
}
}
}
if (fexists(VIEWS_FNAME))
is_depoted_pkg = TRUE;
/* Change out of LogDir before we remove it.
* Do not fail here, as the package is not yet completely deleted! */
if (chdir(home) == FAIL)
warnx("Oops - removed current working directory. Oh, well.");
if (!Fake) {
/* Finally nuke the +-files and the pkgdb-dir (/var/db/pkg/foo) */
if (is_depoted_pkg) {
(void) vsystem("%s %s/+*", REMOVE_CMD, LogDir);
if (isemptydir(LogDir))
(void) vsystem("%s %s", RMDIR_CMD, LogDir);
else
warnx("%s is not empty", LogDir);
return 0;
} else {
if (vsystem("%s -r %s", REMOVE_CMD, LogDir)) {
warnx("couldn't remove log entry in %s, deinstall failed", LogDir);
if (!Force)
return 1;
}
}
}
return 0;
}
int
pkg_perform(lpkg_head_t *pkghead)
{
int err_cnt = 0;
int oldcwd;
lpkg_t *lpp;
/* save cwd */
oldcwd = open(".", O_RDONLY, 0);
if (oldcwd == -1)
err(EXIT_FAILURE, "cannot open \".\"");
while ((lpp = TAILQ_FIRST(pkghead))) {
err_cnt += pkg_do(lpp->lp_name);
TAILQ_REMOVE(pkghead, lpp, lp_link);
free_lpkg(lpp);
if (fchdir(oldcwd) == FAIL)
err(EXIT_FAILURE, "unable to change to previous directory");
}
close(oldcwd);
return err_cnt;
}
|