fix: restore upgrade functionality with colors.
[platform/upstream/rpm.git] / lib / transaction.c
1 /** \ingroup rpmts
2  * \file lib/transaction.c
3  */
4
5 #include "system.h"
6 #include <rpmlib.h>
7
8 #include <rpmmacro.h>   /* XXX for rpmExpand */
9
10 #include "fsm.h"
11 #include "psm.h"
12
13 #include "rpmdb.h"
14
15 #include "rpmds.h"
16
17 #define _RPMFI_INTERNAL
18 #include "rpmfi.h"
19
20 #define _RPMTE_INTERNAL
21 #include "rpmte.h"
22
23 #define _RPMTS_INTERNAL
24 #include "rpmts.h"
25
26 #include "cpio.h"
27 #include "fprint.h"
28 #include "legacy.h"     /* XXX domd5 */
29 #include "misc.h" /* XXX stripTrailingChar, splitString, currentDirectory */
30
31 #include "debug.h"
32
33 /*@access FD_t @*/              /* XXX compared with NULL */
34 /*@access Header @*/            /* XXX compared with NULL */
35 /*@access rpmps @*/     /* XXX need rpmProblemSetOK() */
36 /*@access dbiIndexSet @*/
37 /*@access rpmdb @*/
38
39 /*@access rpmpsm @*/
40
41 /*@access alKey @*/
42 /*@access fnpyKey @*/
43
44 /*@access rpmfi @*/
45
46 /*@access rpmte @*/
47 /*@access rpmtsi @*/
48 /*@access rpmts @*/
49
50 /**
51  */
52 static int archOkay(/*@null@*/ const char * pkgArch)
53         /*@*/
54 {
55     if (pkgArch == NULL) return 0;
56     return (rpmMachineScore(RPM_MACHTABLE_INSTARCH, pkgArch) ? 1 : 0);
57 }
58
59 /**
60  */
61 static int osOkay(/*@null@*/ const char * pkgOs)
62         /*@*/
63 {
64     if (pkgOs == NULL) return 0;
65     return (rpmMachineScore(RPM_MACHTABLE_INSTOS, pkgOs) ? 1 : 0);
66 }
67
68 /**
69  */
70 static int sharedCmp(const void * one, const void * two)
71         /*@*/
72 {
73     sharedFileInfo a = (sharedFileInfo) one;
74     sharedFileInfo b = (sharedFileInfo) two;
75
76     if (a->otherPkg < b->otherPkg)
77         return -1;
78     else if (a->otherPkg > b->otherPkg)
79         return 1;
80
81     return 0;
82 }
83
84 /**
85  */
86 /*@-boundsread@*/
87 static fileAction decideFileFate(const rpmts ts,
88                 const rpmfi ofi, rpmfi nfi)
89         /*@globals fileSystem, internalState @*/
90         /*@modifies nfi, fileSystem, internalState @*/
91 {
92     const char * fn = rpmfiFN(nfi);
93     int newFlags = rpmfiFFlags(nfi);
94     char buffer[1024];
95     fileTypes dbWhat, newWhat, diskWhat;
96     struct stat sb;
97     int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
98
99     if (lstat(fn, &sb)) {
100         /*
101          * The file doesn't exist on the disk. Create it unless the new
102          * package has marked it as missingok, or allfiles is requested.
103          */
104         if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_ALLFILES)
105          && (newFlags & RPMFILE_MISSINGOK))
106         {
107             rpmMessage(RPMMESS_DEBUG, _("%s skipped due to missingok flag\n"),
108                         fn);
109             return FA_SKIP;
110         } else {
111             return FA_CREATE;
112         }
113     }
114
115     diskWhat = whatis((int_16)sb.st_mode);
116     dbWhat = whatis(rpmfiFMode(ofi));
117     newWhat = whatis(rpmfiFMode(nfi));
118
119     /*
120      * RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
121      * them in older packages as well.
122      */
123     if (newWhat == XDIR)
124         return FA_CREATE;
125
126     if (diskWhat != newWhat)
127         return save;
128     else if (newWhat != dbWhat && diskWhat != dbWhat)
129         return save;
130     else if (dbWhat != newWhat)
131         return FA_CREATE;
132     else if (dbWhat != LINK && dbWhat != REG)
133         return FA_CREATE;
134
135     /*
136      * This order matters - we'd prefer to CREATE the file if at all
137      * possible in case something else (like the timestamp) has changed.
138      */
139     if (dbWhat == REG) {
140         const unsigned char * omd5, * nmd5;
141         if (domd5(fn, buffer, 0, NULL))
142             return FA_CREATE;   /* assume file has been removed */
143         omd5 = rpmfiMD5(ofi);
144         if (omd5 && !memcmp(omd5, buffer, 16))
145             return FA_CREATE;   /* unmodified config file, replace. */
146         nmd5 = rpmfiMD5(nfi);
147 /*@-nullpass@*/
148         if (omd5 && nmd5 && !memcmp(omd5, nmd5, 16))
149             return FA_SKIP;     /* identical file, don't bother. */
150 /*@=nullpass@*/
151     } else /* dbWhat == LINK */ {
152         const char * oFLink, * nFLink;
153         memset(buffer, 0, sizeof(buffer));
154         if (readlink(fn, buffer, sizeof(buffer) - 1) == -1)
155             return FA_CREATE;   /* assume file has been removed */
156         oFLink = rpmfiFLink(ofi);
157         if (oFLink && !strcmp(oFLink, buffer))
158             return FA_CREATE;   /* unmodified config file, replace. */
159         nFLink = rpmfiFLink(nfi);
160 /*@-nullpass@*/
161         if (oFLink && nFLink && !strcmp(oFLink, nFLink))
162             return FA_SKIP;     /* identical file, don't bother. */
163 /*@=nullpass@*/
164      }
165
166     /*
167      * The config file on the disk has been modified, but
168      * the ones in the two packages are different. It would
169      * be nice if RPM was smart enough to at least try and
170      * merge the difference ala CVS, but...
171      */
172     return save;
173 }
174 /*@=boundsread@*/
175
176 /**
177  */
178 /*@-boundsread@*/
179 static int filecmp(rpmfi afi, rpmfi bfi)
180         /*@*/
181 {
182     fileTypes awhat = whatis(rpmfiFMode(afi));
183     fileTypes bwhat = whatis(rpmfiFMode(bfi));
184
185     if (awhat != bwhat) return 1;
186
187     if (awhat == LINK) {
188         const char * alink = rpmfiFLink(afi);
189         const char * blink = rpmfiFLink(bfi);
190         if (alink == blink) return 0;
191         if (alink == NULL) return 1;
192         if (blink == NULL) return -1;
193         return strcmp(alink, blink);
194     } else if (awhat == REG) {
195         const unsigned char * amd5 = rpmfiMD5(afi);
196         const unsigned char * bmd5 = rpmfiMD5(bfi);
197         if (amd5 == bmd5) return 0;
198         if (amd5 == NULL) return 1;
199         if (bmd5 == NULL) return -1;
200         return memcmp(amd5, bmd5, 16);
201     }
202
203     return 0;
204 }
205 /*@=boundsread@*/
206
207 /**
208  */
209 /* XXX only ts->{probs,rpmdb} modified */
210 /*@-bounds@*/
211 static int handleInstInstalledFiles(const rpmts ts,
212                 rpmte p, rpmfi fi,
213                 sharedFileInfo shared,
214                 int sharedCount, int reportConflicts)
215         /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
216         /*@modifies ts, fi, rpmGlobalMacroContext, fileSystem, internalState @*/
217 {
218     const char * altNEVR = NULL;
219     rpmfi otherFi = NULL;
220     int numReplaced = 0;
221     rpmps ps;
222     int i;
223
224     {   rpmdbMatchIterator mi;
225         Header h;
226         int scareMem = 0;
227
228         mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
229                         &shared->otherPkg, sizeof(shared->otherPkg));
230         while ((h = rpmdbNextIterator(mi)) != NULL) {
231             altNEVR = hGetNEVR(h, NULL);
232             otherFi = rpmfiNew(ts, h, RPMTAG_BASENAMES, scareMem);
233             break;
234         }
235         mi = rpmdbFreeIterator(mi);
236     }
237
238     if (otherFi == NULL)
239         return 1;
240
241     fi->replaced = xcalloc(sharedCount, sizeof(*fi->replaced));
242
243     ps = rpmtsProblems(ts);
244     for (i = 0; i < sharedCount; i++, shared++) {
245         int otherFileNum, fileNum;
246         int isCfgFile;
247
248         otherFileNum = shared->otherFileNum;
249         (void) rpmfiSetFX(otherFi, otherFileNum);
250
251         fileNum = shared->pkgFileNum;
252         (void) rpmfiSetFX(fi, fileNum);
253
254         isCfgFile = ((rpmfiFFlags(otherFi) | rpmfiFFlags(fi)) & RPMFILE_CONFIG);
255
256 #ifdef  DYING
257         /* XXX another tedious segfault, assume file state normal. */
258         if (otherStates && otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
259             continue;
260 #endif
261
262         if (XFA_SKIPPING(fi->actions[fileNum]))
263             continue;
264
265         if (filecmp(otherFi, fi)) {
266             if (reportConflicts) {
267                 rpmpsAppend(ps, RPMPROB_FILE_CONFLICT,
268                         rpmteNEVR(p), rpmteKey(p),
269                         rpmfiDN(fi), rpmfiBN(fi),
270                         altNEVR,
271                         0);
272             }
273             if (!isCfgFile) {
274                 /*@-assignexpose@*/ /* FIX: p->replaced, not fi */
275                 if (!shared->isRemoved)
276                     fi->replaced[numReplaced++] = *shared;
277                 /*@=assignexpose@*/
278             }
279         }
280
281         if (isCfgFile) {
282             fileAction action;
283             action = decideFileFate(ts, otherFi, fi);
284             fi->actions[fileNum] = action;
285         }
286         fi->replacedSizes[fileNum] = rpmfiFSize(otherFi);
287     }
288     ps = rpmpsFree(ps);
289
290     altNEVR = _free(altNEVR);
291     otherFi = rpmfiFree(otherFi);
292
293     fi->replaced = xrealloc(fi->replaced,       /* XXX memory leak */
294                            sizeof(*fi->replaced) * (numReplaced + 1));
295     fi->replaced[numReplaced].otherPkg = 0;
296
297     return 0;
298 }
299 /*@=bounds@*/
300
301 /**
302  */
303 /* XXX only ts->rpmdb modified */
304 static int handleRmvdInstalledFiles(const rpmts ts, rpmfi fi,
305                 sharedFileInfo shared, int sharedCount)
306         /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
307         /*@modifies ts, fi, rpmGlobalMacroContext, fileSystem, internalState @*/
308 {
309     HGE_t hge = fi->hge;
310     Header h;
311     const char * otherStates;
312     int i, xx;
313    
314     rpmdbMatchIterator mi;
315
316     mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
317                         &shared->otherPkg, sizeof(shared->otherPkg));
318     h = rpmdbNextIterator(mi);
319     if (h == NULL) {
320         mi = rpmdbFreeIterator(mi);
321         return 1;
322     }
323
324     xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
325
326 /*@-boundswrite@*/
327     for (i = 0; i < sharedCount; i++, shared++) {
328         int otherFileNum, fileNum;
329         otherFileNum = shared->otherFileNum;
330         fileNum = shared->pkgFileNum;
331
332         if (otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
333             continue;
334
335         fi->actions[fileNum] = FA_SKIP;
336     }
337 /*@=boundswrite@*/
338
339     mi = rpmdbFreeIterator(mi);
340
341     return 0;
342 }
343
344 #define ISROOT(_d)      (((_d)[0] == '/' && (_d)[1] == '\0') ? "" : (_d))
345
346 /*@unchecked@*/
347 int _fps_debug = 0;
348
349 static int fpsCompare (const void * one, const void * two)
350         /*@*/
351 {
352     const struct fingerPrint_s * a = (const struct fingerPrint_s *)one;
353     const struct fingerPrint_s * b = (const struct fingerPrint_s *)two;
354     int adnlen = strlen(a->entry->dirName);
355     int asnlen = (a->subDir ? strlen(a->subDir) : 0);
356     int abnlen = strlen(a->baseName);
357     int bdnlen = strlen(b->entry->dirName);
358     int bsnlen = (b->subDir ? strlen(b->subDir) : 0);
359     int bbnlen = strlen(b->baseName);
360     char * afn, * bfn, * t;
361     int rc = 0;
362
363     if (adnlen == 1 && asnlen != 0) adnlen = 0;
364     if (bdnlen == 1 && bsnlen != 0) bdnlen = 0;
365
366 /*@-boundswrite@*/
367     afn = t = alloca(adnlen+asnlen+abnlen+2);
368     if (adnlen) t = stpcpy(t, a->entry->dirName);
369     *t++ = '/';
370     if (a->subDir && asnlen) t = stpcpy(t, a->subDir);
371     if (abnlen) t = stpcpy(t, a->baseName);
372     if (afn[0] == '/' && afn[1] == '/') afn++;
373
374     bfn = t = alloca(bdnlen+bsnlen+bbnlen+2);
375     if (bdnlen) t = stpcpy(t, b->entry->dirName);
376     *t++ = '/';
377     if (b->subDir && bsnlen) t = stpcpy(t, b->subDir);
378     if (bbnlen) t = stpcpy(t, b->baseName);
379     if (bfn[0] == '/' && bfn[1] == '/') bfn++;
380 /*@=boundswrite@*/
381
382     rc = strcmp(afn, bfn);
383 /*@-modfilesys@*/
384 if (_fps_debug)
385 fprintf(stderr, "\trc(%d) = strcmp(\"%s\", \"%s\")\n", rc, afn, bfn);
386 /*@=modfilesys@*/
387
388 /*@-modfilesys@*/
389 if (_fps_debug)
390 fprintf(stderr, "\t%s/%s%s\trc %d\n",
391 ISROOT(b->entry->dirName),
392 (b->subDir ? b->subDir : ""),
393 b->baseName,
394 rc
395 );
396 /*@=modfilesys@*/
397
398     return rc;
399 }
400
401 /*@unchecked@*/
402 static int _linear_fps_search = 0;
403
404 static int findFps(const struct fingerPrint_s * fiFps,
405                 const struct fingerPrint_s * otherFps,
406                 int otherFc)
407         /*@*/
408 {
409     int otherFileNum;
410
411 /*@-modfilesys@*/
412 if (_fps_debug)
413 fprintf(stderr, "==> %s/%s%s\n",
414 ISROOT(fiFps->entry->dirName),
415 (fiFps->subDir ? fiFps->subDir : ""),
416 fiFps->baseName);
417 /*@=modfilesys@*/
418
419   if (_linear_fps_search) {
420
421 linear:
422     for (otherFileNum = 0; otherFileNum < otherFc; otherFileNum++, otherFps++) {
423
424 /*@-modfilesys@*/
425 if (_fps_debug)
426 fprintf(stderr, "\t%4d %s/%s%s\n", otherFileNum,
427 ISROOT(otherFps->entry->dirName),
428 (otherFps->subDir ? otherFps->subDir : ""),
429 otherFps->baseName);
430 /*@=modfilesys@*/
431
432         /* If the addresses are the same, so are the values. */
433         if (fiFps == otherFps)
434             break;
435
436         /* Otherwise, compare fingerprints by value. */
437         /*@-nullpass@*/ /* LCL: looks good to me */
438         if (FP_EQUAL((*fiFps), (*otherFps)))
439             break;
440         /*@=nullpass@*/
441     }
442
443 if (otherFileNum == otherFc) {
444 /*@-modfilesys@*/
445 if (_fps_debug)
446 fprintf(stderr, "*** FP_EQUAL NULL %s/%s%s\n",
447 ISROOT(fiFps->entry->dirName),
448 (fiFps->subDir ? fiFps->subDir : ""),
449 fiFps->baseName);
450 /*@=modfilesys@*/
451 }
452
453     return otherFileNum;
454
455   } else {
456
457     const struct fingerPrint_s * bingoFps;
458
459 /*@-boundswrite@*/
460     bingoFps = bsearch(fiFps, otherFps, otherFc, sizeof(*otherFps), fpsCompare);
461 /*@=boundswrite@*/
462     if (bingoFps == NULL) {
463 /*@-modfilesys@*/
464 if (_fps_debug)
465 fprintf(stderr, "*** bingoFps NULL %s/%s%s\n",
466 ISROOT(fiFps->entry->dirName),
467 (fiFps->subDir ? fiFps->subDir : ""),
468 fiFps->baseName);
469 /*@=modfilesys@*/
470         goto linear;
471     }
472
473     /* If the addresses are the same, so are the values. */
474     /*@-nullpass@*/     /* LCL: looks good to me */
475     if (!(fiFps == bingoFps || FP_EQUAL((*fiFps), (*bingoFps)))) {
476 /*@-modfilesys@*/
477 if (_fps_debug)
478 fprintf(stderr, "***  BAD %s/%s%s\n",
479 ISROOT(bingoFps->entry->dirName),
480 (bingoFps->subDir ? bingoFps->subDir : ""),
481 bingoFps->baseName);
482 /*@=modfilesys@*/
483         goto linear;
484     }
485
486     otherFileNum = (bingoFps != NULL ? (bingoFps - otherFps) : 0);
487
488   }
489
490     return otherFileNum;
491 }
492
493 /**
494  * Update disk space needs on each partition for this package's files.
495  */
496 /* XXX only ts->{probs,di} modified */
497 static void handleOverlappedFiles(const rpmts ts,
498                 const rpmte p, rpmfi fi)
499         /*@globals fileSystem, internalState @*/
500         /*@modifies ts, fi, fileSystem, internalState @*/
501 {
502     uint_32 fixupSize = 0;
503     rpmps ps;
504     const char * fn;
505     int i, j;
506   
507     ps = rpmtsProblems(ts);
508     fi = rpmfiInit(fi, 0);
509     if (fi != NULL)
510     while ((i = rpmfiNext(fi)) >= 0) {
511         struct fingerPrint_s * fiFps;
512         int otherPkgNum, otherFileNum;
513         rpmfi otherFi;
514         int_32 FFlags;
515         int_16 FMode;
516         const rpmfi * recs;
517         int numRecs;
518
519         if (XFA_SKIPPING(fi->actions[i]))
520             continue;
521
522         fn = rpmfiFN(fi);
523         fiFps = fi->fps + i;
524         FFlags = rpmfiFFlags(fi);
525         FMode = rpmfiFMode(fi);
526
527         fixupSize = 0;
528
529         /*
530          * Retrieve all records that apply to this file. Note that the
531          * file info records were built in the same order as the packages
532          * will be installed and removed so the records for an overlapped
533          * files will be sorted in exactly the same order.
534          */
535         (void) htGetEntry(ts->ht, fiFps,
536                         (const void ***) &recs, &numRecs, NULL);
537
538         /*
539          * If this package is being added, look only at other packages
540          * being added -- removed packages dance to a different tune.
541          *
542          * If both this and the other package are being added, overlapped
543          * files must be identical (or marked as a conflict). The
544          * disposition of already installed config files leads to
545          * a small amount of extra complexity.
546          *
547          * If this package is being removed, then there are two cases that
548          * need to be worried about:
549          * If the other package is being added, then skip any overlapped files
550          * so that this package removal doesn't nuke the overlapped files
551          * that were just installed.
552          * If both this and the other package are being removed, then each
553          * file removal from preceding packages needs to be skipped so that
554          * the file removal occurs only on the last occurence of an overlapped
555          * file in the transaction set.
556          *
557          */
558
559         /* Locate this overlapped file in the set of added/removed packages. */
560         for (j = 0; j < numRecs && recs[j] != fi; j++)
561             {};
562
563         /* Find what the previous disposition of this file was. */
564         otherFileNum = -1;                      /* keep gcc quiet */
565         otherFi = NULL;
566         for (otherPkgNum = j - 1; otherPkgNum >= 0; otherPkgNum--) {
567             struct fingerPrint_s * otherFps;
568             int otherFc;
569
570             otherFi = recs[otherPkgNum];
571
572             /* Added packages need only look at other added packages. */
573             if (rpmteType(p) == TR_ADDED && rpmteType(otherFi->te) != TR_ADDED)
574                 /*@innercontinue@*/ continue;
575
576             otherFps = otherFi->fps;
577             otherFc = rpmfiFC(otherFi);
578
579             otherFileNum = findFps(fiFps, otherFps, otherFc);
580             (void) rpmfiSetFX(otherFi, otherFileNum);
581
582             /* XXX Happens iff fingerprint for incomplete package install. */
583             if (otherFi->actions[otherFileNum] != FA_UNKNOWN)
584                 /*@innerbreak@*/ break;
585         }
586
587 /*@-boundswrite@*/
588         switch (rpmteType(p)) {
589         case TR_ADDED:
590           { struct stat sb;
591             if (otherPkgNum < 0) {
592                 /* XXX is this test still necessary? */
593                 if (fi->actions[i] != FA_UNKNOWN)
594                     /*@switchbreak@*/ break;
595                 if ((FFlags & RPMFILE_CONFIG) && !lstat(fn, &sb)) {
596                     /* Here is a non-overlapped pre-existing config file. */
597                     fi->actions[i] = (FFlags & RPMFILE_NOREPLACE)
598                         ? FA_ALTNAME : FA_BACKUP;
599                 } else {
600                     fi->actions[i] = FA_CREATE;
601                 }
602                 /*@switchbreak@*/ break;
603             }
604
605 assert(otherFi != NULL);
606             /* Mark added overlapped non-identical files as a conflict. */
607             if ((rpmtsFilterFlags(ts) & RPMPROB_FILTER_REPLACENEWFILES)
608              && filecmp(otherFi, fi))
609             {
610                 rpmpsAppend(ps, RPMPROB_NEW_FILE_CONFLICT,
611                         rpmteNEVR(p), rpmteKey(p),
612                         fn, NULL,
613                         rpmteNEVR(otherFi->te),
614                         0);
615             }
616
617             /* Try to get the disk accounting correct even if a conflict. */
618             fixupSize = rpmfiFSize(otherFi);
619
620             if ((FFlags & RPMFILE_CONFIG) && !lstat(fn, &sb)) {
621                 /* Here is an overlapped  pre-existing config file. */
622                 fi->actions[i] = (FFlags & RPMFILE_NOREPLACE)
623                         ? FA_ALTNAME : FA_SKIP;
624             } else {
625                 fi->actions[i] = FA_CREATE;
626             }
627           } /*@switchbreak@*/ break;
628
629         case TR_REMOVED:
630             if (otherPkgNum >= 0) {
631 assert(otherFi != NULL);
632                 /* Here is an overlapped added file we don't want to nuke. */
633                 if (otherFi->actions[otherFileNum] != FA_ERASE) {
634                     /* On updates, don't remove files. */
635                     fi->actions[i] = FA_SKIP;
636                     /*@switchbreak@*/ break;
637                 }
638                 /* Here is an overlapped removed file: skip in previous. */
639                 otherFi->actions[otherFileNum] = FA_SKIP;
640             }
641             if (XFA_SKIPPING(fi->actions[i]))
642                 /*@switchbreak@*/ break;
643             if (rpmfiFState(fi) != RPMFILE_STATE_NORMAL)
644                 /*@switchbreak@*/ break;
645             if (!(S_ISREG(FMode) && (FFlags & RPMFILE_CONFIG))) {
646                 fi->actions[i] = FA_ERASE;
647                 /*@switchbreak@*/ break;
648             }
649                 
650             /* Here is a pre-existing modified config file that needs saving. */
651             {   char md5sum[50];
652                 const unsigned char * MD5 = rpmfiMD5(fi);
653                 if (!domd5(fn, md5sum, 0, NULL) && memcmp(MD5, md5sum, 16)) {
654                     fi->actions[i] = FA_BACKUP;
655                     /*@switchbreak@*/ break;
656                 }
657             }
658             fi->actions[i] = FA_ERASE;
659             /*@switchbreak@*/ break;
660         }
661 /*@=boundswrite@*/
662
663         /* Update disk space info for a file. */
664         rpmtsUpdateDSI(ts, fiFps->entry->dev,
665                 rpmfiFSize(fi), fi->replacedSizes[i], fixupSize, fi->actions[i]);
666
667     }
668     ps = rpmpsFree(ps);
669 }
670
671 /**
672  * Ensure that current package is newer than installed package.
673  * @param ts            transaction set
674  * @param p             current transaction element
675  * @param h             installed header
676  * @return              0 if not newer, 1 if okay
677  */
678 static int ensureOlder(rpmts ts,
679                 const rpmte p, const Header h)
680         /*@modifies ts @*/
681 {
682     int_32 reqFlags = (RPMSENSE_LESS | RPMSENSE_EQUAL);
683     const char * reqEVR;
684     rpmds req;
685     char * t;
686     int nb;
687     int rc;
688
689     if (p == NULL || h == NULL)
690         return 1;
691
692 /*@-boundswrite@*/
693     nb = strlen(rpmteNEVR(p)) + (rpmteE(p) != NULL ? strlen(rpmteE(p)) : 0) + 1;
694     t = alloca(nb);
695     *t = '\0';
696     reqEVR = t;
697     if (rpmteE(p) != NULL)      t = stpcpy( stpcpy(t, rpmteE(p)), ":");
698     if (rpmteV(p) != NULL)      t = stpcpy(t, rpmteV(p));
699     *t++ = '-';
700     if (rpmteR(p) != NULL)      t = stpcpy(t, rpmteR(p));
701 /*@=boundswrite@*/
702     
703     req = rpmdsSingle(RPMTAG_REQUIRENAME, rpmteN(p), reqEVR, reqFlags);
704     rc = rpmdsNVRMatchesDep(h, req, _rpmds_nopromote);
705     req = rpmdsFree(req);
706
707     if (rc == 0) {
708         rpmps ps = rpmtsProblems(ts);
709         const char * altNEVR = hGetNEVR(h, NULL);
710         rpmpsAppend(ps, RPMPROB_OLDPACKAGE,
711                 rpmteNEVR(p), rpmteKey(p),
712                 NULL, NULL,
713                 altNEVR,
714                 0);
715         altNEVR = _free(altNEVR);
716         ps = rpmpsFree(ps);
717         rc = 1;
718     } else
719         rc = 0;
720
721     return rc;
722 }
723
724 /**
725  * Skip any files that do not match install policies.
726  * @param ts            transaction set
727  * @param fi            file info set
728  */
729 /*@-mustmod@*/ /* FIX: fi->actions is modified. */
730 /*@-bounds@*/
731 static void skipFiles(const rpmts ts, rpmfi fi)
732         /*@globals rpmGlobalMacroContext @*/
733         /*@modifies fi, rpmGlobalMacroContext @*/
734 {
735     uint_32 tscolor = rpmtsColor(ts);
736     uint_32 ficolor;
737     int noConfigs = (rpmtsFlags(ts) & RPMTRANS_FLAG_NOCONFIGS);
738     int noDocs = (rpmtsFlags(ts) & RPMTRANS_FLAG_NODOCS);
739     char ** netsharedPaths = NULL;
740     const char ** languages;
741     const char * dn, * bn;
742     int dnlen, bnlen, ix;
743     const char * s;
744     int * drc;
745     char * dff;
746     int dc;
747     int i, j;
748
749     if (!noDocs)
750         noDocs = rpmExpandNumeric("%{_excludedocs}");
751
752     {   const char *tmpPath = rpmExpand("%{_netsharedpath}", NULL);
753         /*@-branchstate@*/
754         if (tmpPath && *tmpPath != '%')
755             netsharedPaths = splitString(tmpPath, strlen(tmpPath), ':');
756         /*@=branchstate@*/
757         tmpPath = _free(tmpPath);
758     }
759
760     s = rpmExpand("%{_install_langs}", NULL);
761     /*@-branchstate@*/
762     if (!(s && *s != '%'))
763         s = _free(s);
764     if (s) {
765         languages = (const char **) splitString(s, strlen(s), ':');
766         s = _free(s);
767     } else
768         languages = NULL;
769     /*@=branchstate@*/
770
771     /* Compute directory refcount, skip directory if now empty. */
772     dc = rpmfiDC(fi);
773     drc = alloca(dc * sizeof(*drc));
774     memset(drc, 0, dc * sizeof(*drc));
775     dff = alloca(dc * sizeof(*dff));
776     memset(dff, 0, dc * sizeof(*dff));
777
778     fi = rpmfiInit(fi, 0);
779     if (fi != NULL)     /* XXX lclint */
780     while ((i = rpmfiNext(fi)) >= 0)
781     {
782         char ** nsp;
783
784         bn = rpmfiBN(fi);
785         bnlen = strlen(bn);
786         ix = rpmfiDX(fi);
787         dn = rpmfiDN(fi);
788         dnlen = strlen(dn);
789         if (dn == NULL)
790             continue;   /* XXX can't happen */
791
792         drc[ix]++;
793
794         /* Don't bother with skipped files */
795         if (XFA_SKIPPING(fi->actions[i])) {
796             drc[ix]--;  dff[ix] = 1;
797             continue;
798         }
799
800         /* Ignore colored files not in our rainbow. */
801         ficolor = rpmfiFColor(fi);
802         if (tscolor && ficolor && !(tscolor & ficolor)) {
803             drc[ix]--;  dff[ix] = 1;
804             fi->actions[i] = FA_SKIPCOLOR;
805             continue;
806         }
807
808         /*
809          * Skip net shared paths.
810          * Net shared paths are not relative to the current root (though
811          * they do need to take package relocations into account).
812          */
813         for (nsp = netsharedPaths; nsp && *nsp; nsp++) {
814             int len;
815
816             len = strlen(*nsp);
817             if (dnlen >= len) {
818                 if (strncmp(dn, *nsp, len))
819                     /*@innercontinue@*/ continue;
820                 /* Only directories or complete file paths can be net shared */
821                 if (!(dn[len] == '/' || dn[len] == '\0'))
822                     /*@innercontinue@*/ continue;
823             } else {
824                 if (len < (dnlen + bnlen))
825                     /*@innercontinue@*/ continue;
826                 if (strncmp(dn, *nsp, dnlen))
827                     /*@innercontinue@*/ continue;
828                 if (strncmp(bn, (*nsp) + dnlen, bnlen))
829                     /*@innercontinue@*/ continue;
830                 len = dnlen + bnlen;
831                 /* Only directories or complete file paths can be net shared */
832                 if (!((*nsp)[len] == '/' || (*nsp)[len] == '\0'))
833                     /*@innercontinue@*/ continue;
834             }
835
836             /*@innerbreak@*/ break;
837         }
838
839         if (nsp && *nsp) {
840             drc[ix]--;  dff[ix] = 1;
841             fi->actions[i] = FA_SKIPNETSHARED;
842             continue;
843         }
844
845         /*
846          * Skip i18n language specific files.
847          */
848         if (fi->flangs && languages && *fi->flangs[i]) {
849             const char **lang, *l, *le;
850             for (lang = languages; *lang != NULL; lang++) {
851                 if (!strcmp(*lang, "all"))
852                     /*@innerbreak@*/ break;
853                 for (l = fi->flangs[i]; *l != '\0'; l = le) {
854                     for (le = l; *le != '\0' && *le != '|'; le++)
855                         {};
856                     if ((le-l) > 0 && !strncmp(*lang, l, (le-l)))
857                         /*@innerbreak@*/ break;
858                     if (*le == '|') le++;       /* skip over | */
859                 }
860                 if (*l != '\0')
861                     /*@innerbreak@*/ break;
862             }
863             if (*lang == NULL) {
864                 drc[ix]--;      dff[ix] = 1;
865                 fi->actions[i] = FA_SKIPNSTATE;
866                 continue;
867             }
868         }
869
870         /*
871          * Skip config files if requested.
872          */
873         if (noConfigs && (rpmfiFFlags(fi) & RPMFILE_CONFIG)) {
874             drc[ix]--;  dff[ix] = 1;
875             fi->actions[i] = FA_SKIPNSTATE;
876             continue;
877         }
878
879         /*
880          * Skip documentation if requested.
881          */
882         if (noDocs && (rpmfiFFlags(fi) & RPMFILE_DOC)) {
883             drc[ix]--;  dff[ix] = 1;
884             fi->actions[i] = FA_SKIPNSTATE;
885             continue;
886         }
887     }
888
889     /* Skip (now empty) directories that had skipped files. */
890 #ifndef NOTYET
891     if (fi != NULL)     /* XXX can't happen */
892     for (j = 0; j < dc; j++)
893 #else
894     if ((fi = rpmfiInitD(fi)) != NULL)
895     while (j = rpmfiNextD(fi) >= 0)
896 #endif
897     {
898
899         if (drc[j]) continue;   /* dir still has files. */
900         if (!dff[j]) continue;  /* dir was not emptied here. */
901         
902         /* Find parent directory and basename. */
903         dn = fi->dnl[j];        dnlen = strlen(dn) - 1;
904         bn = dn + dnlen;        bnlen = 0;
905         while (bn > dn && bn[-1] != '/') {
906                 bnlen++;
907                 dnlen--;
908                 bn--;
909         }
910
911         /* If explicitly included in the package, skip the directory. */
912         fi = rpmfiInit(fi, 0);
913         if (fi != NULL)         /* XXX lclint */
914         while ((i = rpmfiNext(fi)) >= 0) {
915             const char * fdn, * fbn;
916             int_16 fFMode;
917
918             if (XFA_SKIPPING(fi->actions[i]))
919                 /*@innercontinue@*/ continue;
920
921             fFMode = rpmfiFMode(fi);
922
923             if (whatis(fFMode) != XDIR)
924                 /*@innercontinue@*/ continue;
925             fdn = rpmfiDN(fi);
926             if (strlen(fdn) != dnlen)
927                 /*@innercontinue@*/ continue;
928             if (strncmp(fdn, dn, dnlen))
929                 /*@innercontinue@*/ continue;
930             fbn = rpmfiBN(fi);
931             if (strlen(fbn) != bnlen)
932                 /*@innercontinue@*/ continue;
933             if (strncmp(fbn, bn, bnlen))
934                 /*@innercontinue@*/ continue;
935             rpmMessage(RPMMESS_DEBUG, _("excluding directory %s\n"), dn);
936             fi->actions[i] = FA_SKIPNSTATE;
937             /*@innerbreak@*/ break;
938         }
939     }
940
941     if (netsharedPaths) freeSplitString(netsharedPaths);
942 #ifdef  DYING   /* XXX freeFi will deal with this later. */
943     fi->flangs = _free(fi->flangs);
944 #endif
945     if (languages) freeSplitString((char **)languages);
946 }
947 /*@=bounds@*/
948 /*@=mustmod@*/
949
950 /**
951  * Return transaction element's file info.
952  * @todo Take a rpmfi refcount here.
953  * @param tsi           transaction element iterator
954  * @return              transaction element file info
955  */
956 static /*@null@*/
957 rpmfi rpmtsiFi(const rpmtsi tsi)
958         /*@*/
959 {
960     rpmfi fi = NULL;
961
962     if (tsi != NULL && tsi->ocsave != -1) {
963         /*@-type -abstract@*/ /* FIX: rpmte not opaque */
964         rpmte te = rpmtsElement(tsi->ts, tsi->ocsave);
965         /*@-assignexpose@*/
966         if (te != NULL && (fi = te->fi) != NULL)
967             fi->te = te;
968         /*@=assignexpose@*/
969         /*@=type =abstract@*/
970     }
971     /*@-compdef -refcounttrans -usereleased @*/
972     return fi;
973     /*@=compdef =refcounttrans =usereleased @*/
974 }
975
976 #define NOTIFY(_ts, _al) /*@i@*/ if ((_ts)->notify) (void) (_ts)->notify _al
977
978 int rpmtsRun(rpmts ts, rpmps okProbs, rpmprobFilterFlags ignoreSet)
979 {
980     uint_32 tscolor = rpmtsColor(ts);
981     int i, j;
982     int ourrc = 0;
983     int totalFileCount = 0;
984     rpmfi fi;
985     sharedFileInfo shared, sharedList;
986     int numShared;
987     int nexti;
988     alKey lastFailKey;
989     fingerPrintCache fpc;
990     rpmps ps;
991     rpmpsm psm;
992     rpmtsi pi;  rpmte p;
993     rpmtsi qi;  rpmte q;
994     int numAdded;
995     int numRemoved;
996     int xx;
997
998     /* FIXME: what if the same package is included in ts twice? */
999
1000     if (rpmtsFlags(ts) & RPMTRANS_FLAG_NOSCRIPTS)
1001         (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | _noTransScripts | _noTransTriggers));
1002     if (rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERS)
1003         (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | _noTransTriggers));
1004
1005     /* XXX MULTILIB is broken, as packages can and do execute /sbin/ldconfig. */
1006     if (rpmtsFlags(ts) & RPMTRANS_FLAG_JUSTDB)
1007         (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | _noTransScripts | _noTransTriggers));
1008
1009     ts->probs = rpmpsFree(ts->probs);
1010     ts->probs = rpmpsCreate();
1011
1012     /* XXX Make sure the database is open RDWR for package install/erase. */
1013     {   int dbmode = (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)
1014                 ? O_RDONLY : (O_RDWR|O_CREAT);
1015
1016         /* Open database RDWR for installing packages. */
1017         if (rpmtsOpenDB(ts, dbmode))
1018             return -1;  /* XXX W2DO? */
1019     }
1020
1021     ts->ignoreSet = ignoreSet;
1022     {   const char * currDir = currentDirectory();
1023         rpmtsSetCurrDir(ts, currDir);
1024         currDir = _free(currDir);
1025     }
1026
1027     (void) rpmtsSetChrootDone(ts, 0);
1028
1029     {   int_32 tid = (int_32) time(NULL);
1030         (void) rpmtsSetTid(ts, tid);
1031     }
1032
1033     /* Get available space on mounted file systems. */
1034     xx = rpmtsInitDSI(ts);
1035
1036     /* ===============================================
1037      * For packages being installed:
1038      * - verify package arch/os.
1039      * - verify package epoch:version-release is newer.
1040      * - count files.
1041      * For packages being removed:
1042      * - count files.
1043      */
1044
1045 rpmMessage(RPMMESS_DEBUG, _("sanity checking %d elements\n"), rpmtsNElements(ts));
1046     ps = rpmtsProblems(ts);
1047     /* The ordering doesn't matter here */
1048     pi = rpmtsiInit(ts);
1049     while ((p = rpmtsiNext(pi, TR_ADDED)) != NULL) {
1050         rpmdbMatchIterator mi;
1051         int fc;
1052
1053         if ((fi = rpmtsiFi(pi)) == NULL)
1054             continue;   /* XXX can't happen */
1055         fc = rpmfiFC(fi);
1056
1057         if (!(rpmtsFilterFlags(ts) & RPMPROB_FILTER_IGNOREARCH) && !tscolor)
1058             if (!archOkay(rpmteA(p)))
1059                 rpmpsAppend(ps, RPMPROB_BADARCH,
1060                         rpmteNEVR(p), rpmteKey(p),
1061                         rpmteA(p), NULL,
1062                         NULL, 0);
1063
1064         if (!(rpmtsFilterFlags(ts) & RPMPROB_FILTER_IGNOREOS))
1065             if (!osOkay(rpmteO(p)))
1066                 rpmpsAppend(ps, RPMPROB_BADOS,
1067                         rpmteNEVR(p), rpmteKey(p),
1068                         rpmteO(p), NULL,
1069                         NULL, 0);
1070
1071         if (!(rpmtsFilterFlags(ts) & RPMPROB_FILTER_OLDPACKAGE)) {
1072             Header h;
1073             mi = rpmtsInitIterator(ts, RPMTAG_NAME, rpmteN(p), 0);
1074             while ((h = rpmdbNextIterator(mi)) != NULL)
1075                 xx = ensureOlder(ts, p, h);
1076             mi = rpmdbFreeIterator(mi);
1077         }
1078
1079         if (!(rpmtsFilterFlags(ts) & RPMPROB_FILTER_REPLACEPKG)) {
1080             mi = rpmtsInitIterator(ts, RPMTAG_NAME, rpmteN(p), 0);
1081             xx = rpmdbSetIteratorRE(mi, RPMTAG_EPOCH, RPMMIRE_DEFAULT,
1082                                 rpmteE(p));
1083             xx = rpmdbSetIteratorRE(mi, RPMTAG_VERSION, RPMMIRE_DEFAULT,
1084                                 rpmteV(p));
1085             xx = rpmdbSetIteratorRE(mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT,
1086                                 rpmteR(p));
1087
1088             while (rpmdbNextIterator(mi) != NULL) {
1089                 rpmpsAppend(ps, RPMPROB_PKG_INSTALLED,
1090                         rpmteNEVR(p), rpmteKey(p),
1091                         NULL, NULL,
1092                         NULL, 0);
1093                 /*@innerbreak@*/ break;
1094             }
1095             mi = rpmdbFreeIterator(mi);
1096         }
1097
1098         /* Count no. of files (if any). */
1099         totalFileCount += fc;
1100
1101     }
1102     pi = rpmtsiFree(pi);
1103     ps = rpmpsFree(ps);
1104
1105     /* The ordering doesn't matter here */
1106     pi = rpmtsiInit(ts);
1107     while ((p = rpmtsiNext(pi, TR_REMOVED)) != NULL) {
1108         int fc;
1109
1110         if ((fi = rpmtsiFi(pi)) == NULL)
1111             continue;   /* XXX can't happen */
1112         fc = rpmfiFC(fi);
1113
1114         totalFileCount += fc;
1115     }
1116     pi = rpmtsiFree(pi);
1117
1118     /* ===============================================
1119      * Initialize transaction element file info for package:
1120      */
1121
1122     /*
1123      * FIXME?: we'd be better off assembling one very large file list and
1124      * calling fpLookupList only once. I'm not sure that the speedup is
1125      * worth the trouble though.
1126      */
1127 rpmMessage(RPMMESS_DEBUG, _("computing %d file fingerprints\n"), totalFileCount);
1128     numAdded = numRemoved = 0;
1129     pi = rpmtsiInit(ts);
1130     while ((p = rpmtsiNext(pi, 0)) != NULL) {
1131         int fc;
1132
1133         if ((fi = rpmtsiFi(pi)) == NULL)
1134             continue;   /* XXX can't happen */
1135         fc = rpmfiFC(fi);
1136
1137         /*@-branchstate@*/
1138         switch (rpmteType(p)) {
1139         case TR_ADDED:
1140             numAdded++;
1141             fi->record = 0;
1142             /* Skip netshared paths, not our i18n files, and excluded docs */
1143             if (fc > 0)
1144                 skipFiles(ts, fi);
1145             /*@switchbreak@*/ break;
1146         case TR_REMOVED:
1147             numRemoved++;
1148             fi->record = rpmteDBOffset(p);
1149             /*@switchbreak@*/ break;
1150         }
1151         /*@=branchstate@*/
1152
1153         fi->fps = (fc > 0 ? xmalloc(fc * sizeof(*fi->fps)) : NULL);
1154     }
1155     pi = rpmtsiFree(pi);
1156
1157     if (!rpmtsChrootDone(ts)) {
1158         const char * rootDir = rpmtsRootDir(ts);
1159         xx = chdir("/");
1160         /*@-superuser -noeffect @*/
1161         if (rootDir != NULL)
1162             xx = chroot(rootDir);
1163         /*@=superuser =noeffect @*/
1164         (void) rpmtsSetChrootDone(ts, 1);
1165     }
1166
1167     ts->ht = htCreate(totalFileCount * 2, 0, 0, fpHashFunction, fpEqual);
1168     fpc = fpCacheCreate(totalFileCount);
1169
1170     /* ===============================================
1171      * Add fingerprint for each file not skipped.
1172      */
1173     pi = rpmtsiInit(ts);
1174     while ((p = rpmtsiNext(pi, 0)) != NULL) {
1175         int fc;
1176
1177         (void) rpmdbCheckSignals();
1178
1179         if ((fi = rpmtsiFi(pi)) == NULL)
1180             continue;   /* XXX can't happen */
1181         fc = rpmfiFC(fi);
1182
1183         fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fc, fi->fps);
1184         /*@-branchstate@*/
1185         fi = rpmfiInit(fi, 0);
1186         if (fi != NULL)         /* XXX lclint */
1187         while ((i = rpmfiNext(fi)) >= 0) {
1188             if (XFA_SKIPPING(fi->actions[i]))
1189                 /*@innercontinue@*/ continue;
1190             /*@-dependenttrans@*/
1191             htAddEntry(ts->ht, fi->fps + i, (void *) fi);
1192             /*@=dependenttrans@*/
1193         }
1194         /*@=branchstate@*/
1195     }
1196     pi = rpmtsiFree(pi);
1197
1198     NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_START, 6, ts->orderCount,
1199         NULL, ts->notifyData));
1200
1201     /* ===============================================
1202      * Compute file disposition for each package in transaction set.
1203      */
1204 rpmMessage(RPMMESS_DEBUG, _("computing file dispositions\n"));
1205     ps = rpmtsProblems(ts);
1206     pi = rpmtsiInit(ts);
1207     while ((p = rpmtsiNext(pi, 0)) != NULL) {
1208         dbiIndexSet * matches;
1209         int knownBad;
1210         int fc;
1211
1212         (void) rpmdbCheckSignals();
1213
1214         if ((fi = rpmtsiFi(pi)) == NULL)
1215             continue;   /* XXX can't happen */
1216         fc = rpmfiFC(fi);
1217
1218         NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_PROGRESS, rpmtsiOc(pi),
1219                         ts->orderCount, NULL, ts->notifyData));
1220
1221         if (fc == 0) continue;
1222
1223         /* Extract file info for all files in this package from the database. */
1224         matches = xcalloc(fc, sizeof(*matches));
1225         if (rpmdbFindFpList(rpmtsGetRdb(ts), fi->fps, matches, fc)) {
1226             ps = rpmpsFree(ps);
1227             return 1;   /* XXX WTFO? */
1228         }
1229
1230         numShared = 0;
1231         fi = rpmfiInit(fi, 0);
1232         while ((i = rpmfiNext(fi)) >= 0)
1233             numShared += dbiIndexSetCount(matches[i]);
1234
1235         /* Build sorted file info list for this package. */
1236         shared = sharedList = xcalloc((numShared + 1), sizeof(*sharedList));
1237
1238         fi = rpmfiInit(fi, 0);
1239         while ((i = rpmfiNext(fi)) >= 0) {
1240             /*
1241              * Take care not to mark files as replaced in packages that will
1242              * have been removed before we will get here.
1243              */
1244             for (j = 0; j < dbiIndexSetCount(matches[i]); j++) {
1245                 int ro;
1246                 ro = dbiIndexRecordOffset(matches[i], j);
1247                 knownBad = 0;
1248                 qi = rpmtsiInit(ts);
1249                 while ((q = rpmtsiNext(qi, TR_REMOVED)) != NULL) {
1250                     if (ro == knownBad)
1251                         /*@innerbreak@*/ break;
1252                     if (rpmteDBOffset(q) == ro)
1253                         knownBad = ro;
1254                 }
1255                 qi = rpmtsiFree(qi);
1256
1257                 shared->pkgFileNum = i;
1258                 shared->otherPkg = dbiIndexRecordOffset(matches[i], j);
1259                 shared->otherFileNum = dbiIndexRecordFileNumber(matches[i], j);
1260                 shared->isRemoved = (knownBad == ro);
1261                 shared++;
1262             }
1263             matches[i] = dbiFreeIndexSet(matches[i]);
1264         }
1265         numShared = shared - sharedList;
1266         shared->otherPkg = -1;
1267         matches = _free(matches);
1268
1269         /* Sort file info by other package index (otherPkg) */
1270         qsort(sharedList, numShared, sizeof(*shared), sharedCmp);
1271
1272         /* For all files from this package that are in the database ... */
1273         /*@-branchstate@*/
1274         for (i = 0; i < numShared; i = nexti) {
1275             int beingRemoved;
1276
1277             shared = sharedList + i;
1278
1279             /* Find the end of the files in the other package. */
1280             for (nexti = i + 1; nexti < numShared; nexti++) {
1281                 if (sharedList[nexti].otherPkg != shared->otherPkg)
1282                     /*@innerbreak@*/ break;
1283             }
1284
1285             /* Is this file from a package being removed? */
1286             beingRemoved = 0;
1287             if (ts->removedPackages != NULL)
1288             for (j = 0; j < ts->numRemovedPackages; j++) {
1289                 if (ts->removedPackages[j] != shared->otherPkg)
1290                     /*@innercontinue@*/ continue;
1291                 beingRemoved = 1;
1292                 /*@innerbreak@*/ break;
1293             }
1294
1295             /* Determine the fate of each file. */
1296             switch (rpmteType(p)) {
1297             case TR_ADDED:
1298                 xx = handleInstInstalledFiles(ts, p, fi, shared, nexti - i,
1299         !(beingRemoved || (rpmtsFilterFlags(ts) & RPMPROB_FILTER_REPLACEOLDFILES)));
1300                 /*@switchbreak@*/ break;
1301             case TR_REMOVED:
1302                 if (!beingRemoved)
1303                     xx = handleRmvdInstalledFiles(ts, fi, shared, nexti - i);
1304                 /*@switchbreak@*/ break;
1305             }
1306         }
1307         /*@=branchstate@*/
1308
1309         free(sharedList);
1310
1311         /* Update disk space needs on each partition for this package. */
1312         handleOverlappedFiles(ts, p, fi);
1313
1314         /* Check added package has sufficient space on each partition used. */
1315         switch (rpmteType(p)) {
1316         case TR_ADDED:
1317             rpmtsCheckDSIProblems(ts, p);
1318             /*@switchbreak@*/ break;
1319         case TR_REMOVED:
1320             /*@switchbreak@*/ break;
1321         }
1322     }
1323     pi = rpmtsiFree(pi);
1324     ps = rpmpsFree(ps);
1325
1326     if (rpmtsChrootDone(ts)) {
1327         const char * currDir = rpmtsCurrDir(ts);
1328         /*@-superuser -noeffect @*/
1329         xx = chroot(".");
1330         /*@=superuser =noeffect @*/
1331         (void) rpmtsSetChrootDone(ts, 0);
1332         if (currDir != NULL)
1333             xx = chdir(currDir);
1334     }
1335
1336     NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_STOP, 6, ts->orderCount,
1337         NULL, ts->notifyData));
1338
1339     /* ===============================================
1340      * Free unused memory as soon as possible.
1341      */
1342     pi = rpmtsiInit(ts);
1343     while ((p = rpmtsiNext(pi, 0)) != NULL) {
1344         if ((fi = rpmtsiFi(pi)) == NULL)
1345             continue;   /* XXX can't happen */
1346         if (rpmfiFC(fi) == 0)
1347             continue;
1348         fi->fps = _free(fi->fps);
1349     }
1350     pi = rpmtsiFree(pi);
1351
1352     fpc = fpCacheFree(fpc);
1353     ts->ht = htFree(ts->ht);
1354
1355     /* ===============================================
1356      * If unfiltered problems exist, free memory and return.
1357      */
1358     if ((rpmtsFlags(ts) & RPMTRANS_FLAG_BUILD_PROBS)
1359      || (ts->probs->numProblems &&
1360                 (okProbs != NULL || rpmpsTrim(ts->probs, okProbs)))
1361        )
1362     {
1363         return ts->orderCount;
1364     }
1365
1366     /* ===============================================
1367      * Save removed files before erasing.
1368      */
1369     if (rpmtsFlags(ts) & (RPMTRANS_FLAG_DIRSTASH | RPMTRANS_FLAG_REPACKAGE)) {
1370         int progress;
1371         progress = 0;
1372         pi = rpmtsiInit(ts);
1373         while ((p = rpmtsiNext(pi, 0)) != NULL) {
1374
1375             (void) rpmdbCheckSignals();
1376
1377             if ((fi = rpmtsiFi(pi)) == NULL)
1378                 continue;       /* XXX can't happen */
1379             switch (rpmteType(p)) {
1380             case TR_ADDED:
1381                 /*@switchbreak@*/ break;
1382             case TR_REMOVED:
1383                 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_REPACKAGE))
1384                     /*@switchbreak@*/ break;
1385                 if (!progress)
1386                     NOTIFY(ts, (NULL, RPMCALLBACK_REPACKAGE_START,
1387                                 7, numRemoved, NULL, ts->notifyData));
1388
1389                 NOTIFY(ts, (NULL, RPMCALLBACK_REPACKAGE_PROGRESS, progress,
1390                         numRemoved, NULL, ts->notifyData));
1391                 progress++;
1392
1393         /* XXX TR_REMOVED needs CPIO_MAP_{ABSOLUTE,ADDDOT} CPIO_ALL_HARDLINKS */
1394                 fi->mapflags |= CPIO_MAP_ABSOLUTE;
1395                 fi->mapflags |= CPIO_MAP_ADDDOT;
1396                 fi->mapflags |= CPIO_ALL_HARDLINKS;
1397                 psm = rpmpsmNew(ts, p, fi);
1398                 xx = rpmpsmStage(psm, PSM_PKGSAVE);
1399                 psm = rpmpsmFree(psm);
1400                 fi->mapflags &= ~CPIO_MAP_ABSOLUTE;
1401                 fi->mapflags &= ~CPIO_MAP_ADDDOT;
1402                 fi->mapflags &= ~CPIO_ALL_HARDLINKS;
1403
1404                 /*@switchbreak@*/ break;
1405             }
1406         }
1407         pi = rpmtsiFree(pi);
1408         if (progress) {
1409             NOTIFY(ts, (NULL, RPMCALLBACK_REPACKAGE_STOP, 7, numRemoved,
1410                         NULL, ts->notifyData));
1411         }
1412     }
1413
1414     /* ===============================================
1415      * Install and remove packages.
1416      */
1417     lastFailKey = (alKey)-2;    /* erased packages have -1 */
1418     pi = rpmtsiInit(ts);
1419     /*@-branchstate@*/ /* FIX: fi reload needs work */
1420     while ((p = rpmtsiNext(pi, 0)) != NULL) {
1421         alKey pkgKey;
1422         int gotfd;
1423
1424         (void) rpmdbCheckSignals();
1425
1426         gotfd = 0;
1427         if ((fi = rpmtsiFi(pi)) == NULL)
1428             continue;   /* XXX can't happen */
1429         
1430         psm = rpmpsmNew(ts, p, fi);
1431         psm->unorderedSuccessor =
1432                 (rpmtsiOc(pi) >= rpmtsUnorderedSuccessors(ts, -1) ? 1 : 0);
1433
1434         switch (rpmteType(p)) {
1435         case TR_ADDED:
1436
1437             pkgKey = rpmteAddedKey(p);
1438
1439             rpmMessage(RPMMESS_DEBUG, "========== +++ %s\n", rpmteNEVR(p));
1440             p->h = NULL;
1441             /*@-type@*/ /* FIX: rpmte not opaque */
1442             {
1443                 /*@-noeffectuncon@*/ /* FIX: notify annotations */
1444                 p->fd = ts->notify(p->h, RPMCALLBACK_INST_OPEN_FILE, 0, 0,
1445                                 rpmteKey(p), ts->notifyData);
1446                 /*@=noeffectuncon@*/
1447                 if (rpmteFd(p) != NULL) {
1448                     rpmVSFlags ovsflags = rpmtsVSFlags(ts);
1449                     rpmVSFlags vsflags = ovsflags | RPMVSF_NEEDPAYLOAD;
1450                     rpmRC rpmrc;
1451
1452                     ovsflags = rpmtsSetVSFlags(ts, vsflags);
1453                     rpmrc = rpmReadPackageFile(ts, rpmteFd(p),
1454                                 rpmteNEVR(p), &p->h);
1455                     vsflags = rpmtsSetVSFlags(ts, ovsflags);
1456
1457                     switch (rpmrc) {
1458                     default:
1459                         /*@-noeffectuncon@*/ /* FIX: notify annotations */
1460                         p->fd = ts->notify(p->h, RPMCALLBACK_INST_CLOSE_FILE,
1461                                         0, 0,
1462                                         rpmteKey(p), ts->notifyData);
1463                         /*@=noeffectuncon@*/
1464                         p->fd = NULL;
1465                         ourrc++;
1466                         /*@innerbreak@*/ break;
1467                     case RPMRC_NOTTRUSTED:
1468                     case RPMRC_NOKEY:
1469                     case RPMRC_OK:
1470                         /*@innerbreak@*/ break;
1471                     }
1472                     if (rpmteFd(p) != NULL) gotfd = 1;
1473                 }
1474             }
1475             /*@=type@*/
1476
1477             if (rpmteFd(p) != NULL) {
1478                 /*
1479                  * XXX Sludge necessary to tranfer existing fstates/actions
1480                  * XXX around a recreated file info set.
1481                  */
1482                 psm->fi = rpmfiFree(psm->fi);
1483                 {
1484                     char * fstates = fi->fstates;
1485                     fileAction * actions = fi->actions;
1486                     rpmte savep;
1487
1488                     fi->fstates = NULL;
1489                     fi->actions = NULL;
1490                     fi = rpmfiFree(fi);
1491
1492                     savep = rpmtsSetRelocateElement(ts, p);
1493                     fi = rpmfiNew(ts, p->h, RPMTAG_BASENAMES, 1);
1494                     (void) rpmtsSetRelocateElement(ts, savep);
1495
1496                     if (fi != NULL) {   /* XXX can't happen */
1497                         fi->te = p;
1498                         fi->fstates = _free(fi->fstates);
1499                         fi->fstates = fstates;
1500                         fi->actions = _free(fi->actions);
1501                         fi->actions = actions;
1502                         p->fi = fi;
1503                     }
1504                 }
1505                 psm->fi = rpmfiLink(p->fi, NULL);
1506
1507 /*@-nullstate@*/ /* FIX: psm->fi may be NULL */
1508                 if (rpmpsmStage(psm, PSM_PKGINSTALL)) {
1509                     ourrc++;
1510                     lastFailKey = pkgKey;
1511                 }
1512 /*@=nullstate@*/
1513             } else {
1514                 ourrc++;
1515                 lastFailKey = pkgKey;
1516             }
1517
1518             if (gotfd) {
1519                 /*@-noeffectuncon @*/ /* FIX: check rc */
1520                 (void) ts->notify(p->h, RPMCALLBACK_INST_CLOSE_FILE, 0, 0,
1521                         rpmteKey(p), ts->notifyData);
1522                 /*@=noeffectuncon @*/
1523                 /*@-type@*/
1524                 p->fd = NULL;
1525                 /*@=type@*/
1526             }
1527
1528             p->h = headerFree(p->h);
1529
1530             /*@switchbreak@*/ break;
1531         case TR_REMOVED:
1532             rpmMessage(RPMMESS_DEBUG, "========== --- %s\n", rpmteNEVR(p));
1533             /*
1534              * XXX This has always been a hack, now mostly broken.
1535              * If install failed, then we shouldn't erase.
1536              */
1537             if (rpmteDependsOnKey(p) != lastFailKey) {
1538                 if (rpmpsmStage(psm, PSM_PKGERASE))
1539                     ourrc++;
1540             }
1541             /*@switchbreak@*/ break;
1542         }
1543         xx = rpmdbSync(rpmtsGetRdb(ts));
1544
1545 /*@-nullstate@*/ /* FIX: psm->fi may be NULL */
1546         psm = rpmpsmFree(psm);
1547 /*@=nullstate@*/
1548
1549 /*@-type@*/ /* FIX: p is almost opaque */
1550         p->fi = rpmfiFree(p->fi);
1551 /*@=type@*/
1552
1553     }
1554     /*@=branchstate@*/
1555     pi = rpmtsiFree(pi);
1556
1557     /*@-nullstate@*/ /* FIX: ts->flList may be NULL */
1558     if (ourrc)
1559         return -1;
1560     else
1561         return 0;
1562     /*@=nullstate@*/
1563 }