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