summaryrefslogtreecommitdiff
path: root/lib/uninstall.c
blob: 95099641e2ded8e95f6228e203fe34ae210d4071 (plain)
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
#include "system.h"
#include "miscfn.h"

#include <sys/time.h>
#include <sys/resource.h>

#include "dbindex.h"
#include "depends.h"
#include "install.h"
#include "intl.h"
#include "messages.h"
#include "md5.h"
#include "misc.h"
#include "rpmdb.h"
#include "rpmlib.h"

static char * SCRIPT_PATH = "PATH=/sbin:/bin:/usr/sbin:/usr/bin:"
			                 "/usr/X11R6/bin";

enum fileActions { REMOVE, BACKUP, KEEP };

static int sharedFileCmp(const void * one, const void * two);
static int handleSharedFiles(rpmdb db, int offset, char ** fileList, 
			     char ** fileMd5List, int fileCount, 
			     enum fileActions * fileActions);
static int removeFile(char * file, char state, unsigned int flags, char * md5, 
		      short mode, enum fileActions action, 
		      int brokenMd5, int test);
static int runScript(Header h, char * root, int progArgc, char ** progArgv, 
		     char * script, int arg1, int arg2, int errfd);

static int sharedFileCmp(const void * one, const void * two) {
    if (((struct sharedFile *) one)->secRecOffset <
	((struct sharedFile *) two)->secRecOffset)
	return -1;
    else if (((struct sharedFile *) one)->secRecOffset ==
	((struct sharedFile *) two)->secRecOffset)
	return 0;
    else 
	return 1;
}

int findSharedFiles(rpmdb db, int offset, char ** fileList, int fileCount,
		    struct sharedFile ** listPtr, int * listCountPtr) {
    int i, j;
    struct sharedFile * list = NULL;
    int itemsUsed = 0;
    int itemsAllocated = 0;
    dbiIndexSet matches;

    itemsAllocated = 5;
    list = malloc(sizeof(struct sharedFile) * itemsAllocated);

    for (i = 0; i < fileCount; i++) {
	if (!rpmdbFindByFile(db, fileList[i], &matches)) {
	    for (j = 0; j < matches.count; j++) {
		if (matches.recs[j].recOffset != offset) {
		    if (itemsUsed == itemsAllocated) {
			itemsAllocated += 10;
			list = realloc(list, sizeof(struct sharedFile) * 
					    itemsAllocated);
		    }
		    list[itemsUsed].mainFileNumber = i;
		    list[itemsUsed].secRecOffset = matches.recs[j].recOffset;
		    list[itemsUsed].secFileNumber = matches.recs[j].fileNumber;
		    itemsUsed++;
		}
	    }

	    dbiFreeIndexRecord(matches);
	}
    }

    if (itemsUsed) {
	qsort(list, itemsUsed, sizeof(struct sharedFile), sharedFileCmp);
	*listPtr = list;
	*listCountPtr = itemsUsed;
    } else {
	free(list);
	*listPtr = NULL;
	*listCountPtr = 0;
    }

    return 0;
}

static int handleSharedFiles(rpmdb db, int offset, char ** fileList, 
			     char ** fileMd5List, int fileCount, 
			     enum fileActions * fileActions) {
    Header sech = NULL;
    int secOffset = 0;
    struct sharedFile * sharedList;
    int sharedCount;
    char * name, * version, * release;
    int secFileCount;
    char ** secFileMd5List, ** secFileList;
    char * secFileStatesList;
    int type;
    int i;
    int rc = 0;

    if (findSharedFiles(db, offset, fileList, fileCount, &sharedList, 
			&sharedCount)) {
	return 1;
    }

    if (!sharedCount) {
	return 0;
    }

    for (i = 0; i < sharedCount; i++) {
	if (secOffset != sharedList[i].secRecOffset) {
	    if (secOffset) {
		headerFree(sech);
		free(secFileMd5List);
		free(secFileList);
	    }

	    secOffset = sharedList[i].secRecOffset;
	    sech = rpmdbGetRecord(db, secOffset);
	    if (!sech) {
		rpmError(RPMERR_DBCORRUPT, 
			 _("cannot read header at %d for uninstall"), offset);
		rc = 1;
		break;
	    }

	    headerGetEntry(sech, RPMTAG_NAME, &type, (void **) &name, 
		     &secFileCount);
	    headerGetEntry(sech, RPMTAG_VERSION, &type, (void **) &version, 
		     &secFileCount);
	    headerGetEntry(sech, RPMTAG_RELEASE, &type, (void **) &release, 
		     &secFileCount);

	    rpmMessage(RPMMESS_DEBUG, 
			_("package %s-%s-%s contain shared files\n"), 
		    	name, version, release);

	    if (!headerGetEntry(sech, RPMTAG_FILENAMES, &type, 
			  (void **) &secFileList, &secFileCount)) {
		rpmError(RPMERR_DBCORRUPT, "package %s contains no files",
		      name);
		headerFree(sech);
		rc = 1;
		break;
	    }

	    if (!headerGetEntry(sech, RPMTAG_FILESTATES, &type, 
		                (void **) &secFileStatesList, NULL)) {
		/* This shouldn't happen, but some versions of RPM didn't
		   implement --justdb properly, and chose to leave this stuff
		   out. */
		rpmMessage(RPMMESS_DEBUG, 
			    "package is missing FILESTATES\n");
		secFileStatesList = alloca(secFileCount);
		memset(secFileStatesList, RPMFILE_STATE_NOTINSTALLED,
			secFileCount);
	    }

	    headerGetEntry(sech, RPMTAG_FILEMD5S, &type, 
		     (void **) &secFileMd5List, &secFileCount);
	}

	rpmMessage(RPMMESS_DEBUG, "file %s is shared\n",
		fileList[sharedList[i].mainFileNumber]);
	
	switch (secFileStatesList[sharedList[i].secFileNumber]) {
	  case RPMFILE_STATE_REPLACED:
	    rpmMessage(RPMMESS_DEBUG, "     file has already been replaced\n");
	    break;

	  case RPMFILE_STATE_NOTINSTALLED:
	    rpmMessage(RPMMESS_DEBUG, "     file was never installed\n");
	    break;
    
	  case RPMFILE_STATE_NETSHARED:
	    rpmMessage(RPMMESS_DEBUG, "     file is netshared (so don't touch it)\n");
	    fileActions[sharedList[i].mainFileNumber] = KEEP;
	    break;
    
	  case RPMFILE_STATE_NORMAL:
	    if (!strcmp(fileMd5List[sharedList[i].mainFileNumber],
			secFileMd5List[sharedList[i].secFileNumber])) {
		rpmMessage(RPMMESS_DEBUG, "    file is truely shared - saving\n");
	    }
	    fileActions[sharedList[i].mainFileNumber] = KEEP;
	    break;
	}
    }

    if (secOffset) {
	headerFree(sech);
	free(secFileMd5List);
	free(secFileList);
    }
    free(sharedList);

    return rc;
}

int rpmRemovePackage(char * prefix, rpmdb db, unsigned int offset, int flags) {
    Header h;
    int i, j;
    int fileCount;
    char * name, * version, * release;
    char * fnbuffer = NULL;
    dbiIndexSet matches;
    int fnbuffersize = 0;
    int prefixLength = strlen(prefix);
    char ** fileList, ** fileMd5List;
    int type, count;
    uint_32 * fileFlagsList;
    int_16 * fileModesList;
    char * fileStatesList;
    enum { REMOVE, BACKUP, KEEP } * fileActions;
    int scriptArg;

    if (flags & RPMUNINSTALL_JUSTDB)
	flags |= RPMUNINSTALL_NOSCRIPTS;

    h = rpmdbGetRecord(db, offset);
    if (!h) {
	rpmError(RPMERR_DBCORRUPT, "cannot read header at %d for uninstall",
	      offset);
	return 1;
    }

    headerGetEntry(h, RPMTAG_NAME, &type, (void **) &name,  &count);
    headerGetEntry(h, RPMTAG_VERSION, &type, (void **) &version,  &count);
    headerGetEntry(h, RPMTAG_RELEASE, &type, (void **) &release,  &count);
    /* when we run scripts, we pass an argument which is the number of 
       versions of this package that will be installed when we are finished */
    if (rpmdbFindPackage(db, name, &matches)) {
	rpmError(RPMERR_DBCORRUPT, "cannot read packages named %s for uninstall",
	      name);
	return 1;
    }
 
    scriptArg = matches.count - 1;
    dbiFreeIndexRecord(matches);

    if (!(flags & RPMUNINSTALL_NOTRIGGERS)) {
	/* run triggers from this package which are keyed on installed 
	   packages */
	if (runImmedTriggers(prefix, db, RPMSENSE_TRIGGERUN, h, -1)) {
	    return 2;
	}

	/* run triggers which are set off by the removal of this package */
	if (runTriggers(prefix, db, RPMSENSE_TRIGGERUN, h, -1))
	    return 1;
    }

    if (!(flags & RPMUNINSTALL_TEST)) {
	if (runInstScript(prefix, h, RPMTAG_PREUN, RPMTAG_PREUNPROG, scriptArg, 
		          flags & RPMUNINSTALL_NOSCRIPTS, 0)) {
	    headerFree(h);
	    return 1;
	}
    }
    
    rpmMessage(RPMMESS_DEBUG, "will remove files test = %d\n", 
		flags & RPMUNINSTALL_TEST);
    if (!(flags & RPMUNINSTALL_JUSTDB) &&
	headerGetEntry(h, RPMTAG_FILENAMES, &type, (void **) &fileList, 
	               &fileCount)) {
	if (prefix[0]) {
	    fnbuffersize = 1024;
	    fnbuffer = alloca(fnbuffersize);
	}

	headerGetEntry(h, RPMTAG_FILEMD5S, &type, (void **) &fileMd5List, 
		 &fileCount);
	headerGetEntry(h, RPMTAG_FILEFLAGS, &type, (void **) &fileFlagsList, 
		 &fileCount);
	headerGetEntry(h, RPMTAG_FILEMODES, &type, (void **) &fileModesList, 
		 &fileCount);

	if (!headerGetEntry(h, RPMTAG_FILESTATES, &type, 
			    (void **) &fileStatesList, NULL)) {
	    /* This shouldn't happen, but some versions of RPM didn't
	       implement --justdb properly, and chose to leave this stuff
	       out. */
	    rpmMessage(RPMMESS_DEBUG, "package is missing FILESTATES\n");
	    fileStatesList = alloca(fileCount);
	    memset(fileStatesList, RPMFILE_STATE_NOTINSTALLED,
		    fileCount);
	}

	fileActions = alloca(sizeof(*fileActions) * fileCount);
	for (i = 0; i < fileCount; i++) 
	    if (fileStatesList[i] == RPMFILE_STATE_NOTINSTALLED ||
		fileStatesList[i] == RPMFILE_STATE_NETSHARED) 
		fileActions[i] = KEEP;
	    else
		fileActions[i] = REMOVE;

	if (rpmGetVar(RPMVAR_NETSHAREDPATH)) {
	    char ** netsharedPaths, ** nsp;

	    netsharedPaths = splitString(rpmGetVar(RPMVAR_NETSHAREDPATH),
			strlen(rpmGetVar(RPMVAR_NETSHAREDPATH)), ':');

	    for (nsp = netsharedPaths; nsp && *nsp; nsp++) {
		j = strlen(*nsp);

		for (i = 0; i < fileCount; i++) 
		if (!strncmp(fileList[i], *nsp, j) &&
		    (fileList[i][j] == '\0' || fileList[i][j] == '/')) {
		    rpmMessage(RPMMESS_DEBUG, "%s has a netshared override\n",
				fileList[i]);
		    fileActions[i] = KEEP;
		}
	    }

	    freeSplitString(netsharedPaths);
	}

	handleSharedFiles(db, offset, fileList, fileMd5List, fileCount, 
			  fileActions);

	/* go through the filelist backwards to help insure that rmdir()
	   will work */
	for (i = fileCount - 1; i >= 0; i--) {
	    if (strcmp(prefix, "/")) {
		if ((strlen(fileList[i]) + prefixLength + 1) > fnbuffersize) {
		    fnbuffersize = (strlen(fileList[i]) + prefixLength) * 2;
		    fnbuffer = alloca(fnbuffersize);
		}
		strcpy(fnbuffer, prefix);
		strcat(fnbuffer, "/");
		strcat(fnbuffer, fileList[i]);
	    } else {
		fnbuffer = fileList[i];
	    }

	    removeFile(fnbuffer, fileStatesList[i], fileFlagsList[i],
		       fileMd5List[i], fileModesList[i], fileActions[i], 
		       !headerIsEntry(h, RPMTAG_RPMVERSION),
		       flags & RPMUNINSTALL_TEST);
	}

	free(fileList);
	free(fileMd5List);
    }

    if (!(flags & RPMUNINSTALL_TEST)) {
	rpmMessage(RPMMESS_DEBUG, "running postuninstall script (if any)\n");
	runInstScript(prefix, h, RPMTAG_POSTUN, RPMTAG_POSTUNPROG, scriptArg, 
		      flags & RPMUNINSTALL_NOSCRIPTS, 0);
    }

    if (!(flags & RPMUNINSTALL_NOTRIGGERS)) {
	/* Run postun triggers which are set off by this package's removal */
	if (runTriggers(prefix, db, RPMSENSE_TRIGGERPOSTUN, h, 0)) {
	    return 2;
	}
    }

    headerFree(h);

    rpmMessage(RPMMESS_DEBUG, "removing database entry\n");
    if (!(flags & RPMUNINSTALL_TEST))
	rpmdbRemove(db, offset, 0);

    return 0;
}

static int runScript(Header h, char * root, int progArgc, char ** progArgv, 
		     char * script, int arg1, int arg2, int errfd) {
    char ** argv;
    int argc;
    char ** prefixes = NULL;
    int numPrefixes;
    char * oldPrefix;
    int maxPrefixLength;
    int len;
    char * prefixBuf;
    pid_t child;
    int status;
    char * fn;
    int fd;
    int i;
    int freePrefixes = 0;
    int pipes[2];
    int out = 1;

    if (!progArgv && !script)
	return 0;
    else if (!progArgv) {
	argv = alloca(5 * sizeof(char *));
	argv[0] = "/bin/sh";
	argc = 1;
    } else {
	argv = progArgv;
	argv = alloca((progArgc + 4) * sizeof(char *));
	memcpy(argv, progArgv, progArgc * sizeof(char *));
	argc = progArgc;
    }

    if (headerGetEntry(h, RPMTAG_INSTPREFIXES, NULL, (void *) &prefixes,
		       &numPrefixes)) {
	freePrefixes = 1;
    } else if (headerGetEntry(h, RPMTAG_INSTALLPREFIX, NULL, 
			      (void **) &oldPrefix,
	    	       NULL)) {
	prefixes = &oldPrefix;
	numPrefixes = 1;
    } else {
	numPrefixes = 0;
    }

    maxPrefixLength = 0;
    for (i = 0; i < numPrefixes; i++) {
	len = strlen(prefixes[i]);
	if (len > maxPrefixLength) maxPrefixLength = len;
    }
    prefixBuf = alloca(maxPrefixLength + 50);

    if (script) {
	if (makeTempFile(root, &fn, &fd)) {
	    free(argv);
	    if (freePrefixes) free(prefixes);
	    return 1;
	}

	if (rpmIsDebug() &&
	    (!strcmp(argv[0], "/bin/sh") || !strcmp(argv[0], "/bin/bash")))
	    write(fd, "set -x\n", 7);

	write(fd, script, strlen(script));
	close(fd);

	argv[argc++] = fn + strlen(root);
	if (arg1 >= 0) {
	    argv[argc] = alloca(20);
	    sprintf(argv[argc++], "%d", arg1);
	}
	if (arg2 >= 0) {
	    argv[argc] = alloca(20);
	    sprintf(argv[argc++], "%d", arg2);
	}
    }

    argv[argc] = NULL;

    if (errfd) {
	if (rpmIsVerbose()) {
	    out = errfd;
	} else {
	    out = open("/dev/null", O_WRONLY);
	    if (out < 0) {
		out = errfd;
	    }
	}
    }
    
    if (!(child = fork())) {
	/* make stdin inaccessible */
	pipe(pipes);
	close(pipes[1]);
	dup2(pipes[0], 0);
	close(pipes[0]);

	if (errfd) {
	    if (errfd != 2) dup2(errfd, 2);
	    if (out != 1) dup2(out, 1);
	    /* make sure we don't close stdin/stderr/stdout by mistake! */
	    if (errfd > 2) close (errfd);
	    if (out > 2 && out != errfd) close (out); 
	}

	doputenv(SCRIPT_PATH);

	for (i = 0; i < numPrefixes; i++) {
	    sprintf(prefixBuf, "RPM_INSTALL_PREFIX%d=%s", i, prefixes[i]);
	    doputenv(prefixBuf);

	    /* backwards compatibility */
	    if (!i) {
		sprintf(prefixBuf, "RPM_INSTALL_PREFIX=%s", prefixes[i]);
		doputenv(prefixBuf);
	    }
	}

	if (strcmp(root, "/")) {
	    chroot(root);
	}

	chdir("/");

	execv(argv[0], argv);
 	_exit(-1);
    }

    waitpid(child, &status, 0);

    if (freePrefixes) free(prefixes);

    if (errfd) {
	if (out > 2) close(out);
	if (errfd > 2) close(errfd);
    }
    
    if (script) {
	if (!rpmIsDebug()) unlink(fn);
	free(fn);
    }

    if (!WIFEXITED(status) || WEXITSTATUS(status)) {
	rpmError(RPMERR_SCRIPT, _("execution of script failed"));
	return 1;
    }

    return 0;
}

int runInstScript(char * root, Header h, int scriptTag, int progTag,
	          int arg, int norunScripts, int err) {
    void ** programArgv;
    int programArgc;
    char ** argv;
    int programType;
    char * script;
    int rc;

    if (norunScripts) return 0;

    /* headerGetEntry() sets the data pointer to NULL if the entry does
       not exist */
    headerGetEntry(h, progTag, &programType, (void **) &programArgv,
		   &programArgc);
    headerGetEntry(h, scriptTag, NULL, (void **) &script, NULL);

    if (programArgv && programType == RPM_STRING_TYPE) {
	argv = alloca(sizeof(char *));
	*argv = (char *) programArgv;
    } else {
	argv = (char **) programArgv;
    }

    rc = runScript(h, root, programArgc, argv, script, arg, -1, err);
    if (programType == RPM_STRING_ARRAY_TYPE) free(programArgv);
    return rc;
}

static int removeFile(char * file, char state, unsigned int flags, char * md5, 
		      short mode, enum fileActions action, 
		      int brokenMd5, int test) {
    char currentMd5[40];
    int rc = 0;
    char * newfile;
	
    switch (state) {
      case RPMFILE_STATE_REPLACED:
	rpmMessage(RPMMESS_DEBUG, "%s has already been replaced\n", file);
	break;

      case RPMFILE_STATE_NORMAL:
	if ((action == REMOVE) && (flags & RPMFILE_CONFIG)) {
	    /* if it's a config file, we may not want to remove it */
	    rpmMessage(RPMMESS_DEBUG, "finding md5sum of %s\n", file);
	    if (brokenMd5)
		rc = mdfileBroken(file, currentMd5);
	    else
		rc = mdfile(file, currentMd5);

	    if (mdfile(file, currentMd5)) 
		rpmMessage(RPMMESS_DEBUG, 
				"    failed - assuming file removed\n");
	    else {
		if (strcmp(currentMd5, md5)) {
		    rpmMessage(RPMMESS_DEBUG, "    file changed - will save\n");
		    action = BACKUP;
		} else {
		    rpmMessage(RPMMESS_DEBUG, 
				"    file unchanged - will remove\n");
		}
	    }
	}

	switch (action) {

	  case KEEP:
	    rpmMessage(RPMMESS_DEBUG, "keeping %s\n", file);
	    break;

	  case BACKUP:
	    rpmMessage(RPMMESS_DEBUG, "saving %s as %s.rpmsave\n", file, file);
	    if (!test) {
		newfile = alloca(strlen(file) + 20);
		strcpy(newfile, file);
		strcat(newfile, ".rpmsave");
		if (rename(file, newfile)) {
		    rpmError(RPMERR_RENAME, _("rename of %s to %s failed: %s"),
				file, newfile, strerror(errno));
		    rc = 1;
		}
	    }
	    break;

	  case REMOVE:
	    rpmMessage(RPMMESS_DEBUG, "%s - removing\n", file);
	    if (S_ISDIR(mode)) {
		if (!test) {
		    if (rmdir(file)) {
			if (errno == ENOTEMPTY)
			    rpmError(RPMERR_RMDIR, 
				_("cannot remove %s - directory not empty"), 
				file);
			else
			    rpmError(RPMERR_RMDIR, _("rmdir of %s failed: %s"),
					file, strerror(errno));
			rc = 1;
		    }
		}
	    } else {
		if (!test) {
		    if (unlink(file)) {
			if (errno != ENOENT || !(flags & RPMFILE_MISSINGOK)) {
			    rpmError(RPMERR_UNLINK, 
				      _("removal of %s failed: %s"),
					file, strerror(errno));
			}
			rc = 1;
		    }
		}
	    }
	    break;
	}
    }
 
    return 0;
}

static int handleOneTrigger(char * root, rpmdb db, int sense, Header sourceH,
			    Header triggeredH, int arg1correction, int arg2,
			    char * triggersAlreadyRun) {
    char ** triggerNames, ** triggerVersions;
    char ** triggerScripts, ** triggerProgs;
    int_32 * triggerFlags, * triggerIndices;
    char * triggerPackageName, * sourceName;
    int numTriggers;
    int rc = 0;
    int i;
    int index;
    dbiIndexSet matches;
    int skip;

    if (!headerGetEntry(triggeredH, RPMTAG_TRIGGERNAME, NULL, 
			(void **) &triggerNames, &numTriggers)) {
	headerFree(triggeredH);
	return 0;
    }

    headerGetEntry(sourceH, RPMTAG_NAME, NULL, (void **) &sourceName, NULL);

    headerGetEntry(triggeredH, RPMTAG_TRIGGERFLAGS, NULL, 
		   (void **) &triggerFlags, NULL);
    headerGetEntry(triggeredH, RPMTAG_TRIGGERVERSION, NULL, 
		   (void **) &triggerVersions, NULL);

    for (i = 0; i < numTriggers; i++) {
	if (!(triggerFlags[i] & sense)) continue;
	if (strcmp(triggerNames[i], sourceName)) continue;

	/* For some reason, the TRIGGERVERSION stuff includes the name
	   of the package which the trigger is based on. We need to skip
	   over that here. I suspect that we'll change our minds on this
	   and remove that, so I'm going to just 'do the right thing'. */
	skip = strlen(triggerNames[i]);
	if (!strncmp(triggerVersions[i], triggerNames[i], skip) &&
	    (triggerVersions[i][skip] == '-'))
	    skip++;
	else
	    skip = 0;

	if (!headerMatchesDepFlags(sourceH, triggerVersions[i] + skip, 
				   triggerFlags[i])) continue;

	headerGetEntry(triggeredH, RPMTAG_TRIGGERINDEX, NULL,
		       (void **) &triggerIndices, NULL);
	headerGetEntry(triggeredH, RPMTAG_TRIGGERSCRIPTS, NULL,
		       (void **) &triggerScripts, NULL);
	headerGetEntry(triggeredH, RPMTAG_TRIGGERSCRIPTPROG, NULL,
		       (void **) &triggerProgs, NULL);

	headerGetEntry(triggeredH, RPMTAG_NAME, NULL, 
		       (void **) &triggerPackageName, NULL);
	rpmdbFindPackage(db, triggerPackageName, &matches);
	dbiFreeIndexRecord(matches);

	index = triggerIndices[i];
	if (!triggersAlreadyRun || !triggersAlreadyRun[index]) {
	    rc = runScript(triggeredH, root, 1, triggerProgs + index,
			    triggerScripts[index], 
			    matches.count + arg1correction, arg2, 0);
	    if (triggersAlreadyRun) triggersAlreadyRun[index] = 1;
	}

	free(triggerScripts);
	free(triggerProgs);

	/* each target/source header pair can only result in a single
	   script being run */
	break;
    }

    free(triggerNames);

    return rc;
}

int runTriggers(char * root, rpmdb db, int sense, Header h,
		int countCorrection) {
    char * packageName;
    dbiIndexSet matches, otherMatches;
    Header triggeredH;
    int numPackage;
    int rc;
    int i;

    headerGetEntry(h, RPMTAG_NAME, NULL, (void **) &packageName, NULL);

    if ((rc = rpmdbFindByTriggeredBy(db, packageName, &matches)) < 0)
	return 1;
    else if (rc)
	return 0;

    rpmdbFindPackage(db, packageName, &otherMatches);
    numPackage = otherMatches.count + countCorrection;
    dbiFreeIndexRecord(otherMatches);

    rc = 0;
    for (i = 0; i < matches.count; i++) {
	if (!(triggeredH = rpmdbGetRecord(db, matches.recs[i].recOffset))) 
	    return 1;

	rc |= handleOneTrigger(root, db, sense, h, triggeredH, 0, numPackage, 
			       NULL);
	
	headerFree(triggeredH);
    }

    dbiFreeIndexRecord(matches);

    return rc;
    
}

int runImmedTriggers(char * root, rpmdb db, int sense, Header h,
		     int countCorrection) {
    int rc = 0;
    dbiIndexSet matches;
    char ** triggerNames;
    int numTriggers;
    int i, j;
    int_32 * triggerIndices;
    char * triggersRun;
    Header sourceH;

    if (!headerGetEntry(h, RPMTAG_TRIGGERNAME, NULL, (void **) &triggerNames, 
			&numTriggers))
	return 0;
    headerGetEntry(h, RPMTAG_TRIGGERINDEX, NULL, (void **) &triggerIndices, 
		   &i);
    triggersRun = alloca(sizeof(*triggersRun) * i);
    memset(triggersRun, 0, sizeof(*triggersRun) * i);

    for (i = 0; i < numTriggers; i++) {
	if (triggersRun[triggerIndices[i]]) continue;
	
        if ((j = rpmdbFindPackage(db, triggerNames[i], &matches))) {
	    if (j < 0) rc |= 1;	
	    continue;
	} 

	for (j = 0; j < matches.count; j++) {
	    if (!(sourceH = rpmdbGetRecord(db, matches.recs[j].recOffset))) 
		return 1;
	    rc |= handleOneTrigger(root, db, sense, sourceH, h, 
				   countCorrection, matches.count, triggersRun);
	    headerFree(sourceH);
	    if (triggersRun[triggerIndices[i]]) break;
	}

	dbiFreeIndexRecord(matches);
    }

    return rc;
}