2 * \file lib/transaction.c
8 #include <rpmmacro.h> /* XXX for rpmExpand */
16 #define _RPMFI_INTERNAL
19 #define _RPMTE_INTERNAL
22 #define _RPMTS_INTERNAL
27 #include "legacy.h" /* XXX domd5 */
28 #include "misc.h" /* XXX stripTrailingChar, splitString, currentDirectory */
32 /*@access FD_t @*/ /* XXX compared with NULL */
33 /*@access Header @*/ /* XXX compared with NULL */
34 /*@access rpmps @*/ /* XXX need rpmProblemSetOK() */
35 /*@access dbiIndexSet @*/
51 static int archOkay(/*@null@*/ const char * pkgArch)
54 if (pkgArch == NULL) return 0;
55 return (rpmMachineScore(RPM_MACHTABLE_INSTARCH, pkgArch) ? 1 : 0);
60 static int osOkay(/*@null@*/ const char * pkgOs)
63 if (pkgOs == NULL) return 0;
64 return (rpmMachineScore(RPM_MACHTABLE_INSTOS, pkgOs) ? 1 : 0);
69 static int sharedCmp(const void * one, const void * two)
72 sharedFileInfo a = (sharedFileInfo) one;
73 sharedFileInfo b = (sharedFileInfo) two;
75 if (a->otherPkg < b->otherPkg)
77 else if (a->otherPkg > b->otherPkg)
85 static fileAction decideFileFate(const rpmts ts,
86 const rpmfi ofi, rpmfi nfi)
87 /*@globals fileSystem @*/
88 /*@modifies nfi, fileSystem @*/
90 const char * fn = rpmfiFN(nfi);
91 int newFlags = rpmfiFFlags(nfi);
93 fileTypes dbWhat, newWhat, diskWhat;
95 int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
99 * The file doesn't exist on the disk. Create it unless the new
100 * package has marked it as missingok, or allfiles is requested.
102 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_ALLFILES)
103 && (newFlags & RPMFILE_MISSINGOK))
105 rpmMessage(RPMMESS_DEBUG, _("%s skipped due to missingok flag\n"),
113 diskWhat = whatis(sb.st_mode);
114 dbWhat = whatis(ofi->fmodes[ofi->i]);
115 newWhat = whatis(nfi->fmodes[nfi->i]);
118 * RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
119 * them in older packages as well.
124 if (diskWhat != newWhat)
126 else if (newWhat != dbWhat && diskWhat != dbWhat)
128 else if (dbWhat != newWhat)
130 else if (dbWhat != LINK && dbWhat != REG)
134 * This order matters - we'd prefer to CREATE the file if at all
135 * possible in case something else (like the timestamp) has changed.
139 if (ofi->md5s != NULL && nfi->md5s != NULL) {
141 const unsigned char * omd5 = ofi->md5s + (16 * ofi->i);
142 const unsigned char * nmd5 = nfi->md5s + (16 * nfi->i);
143 if (domd5(fn, buffer, 0))
144 return FA_CREATE; /* assume file has been removed */
145 if (!memcmp(omd5, buffer, 16))
146 return FA_CREATE; /* unmodified config file, replace. */
147 if (!memcmp(omd5, nmd5, 16))
148 return FA_SKIP; /* identical file, don't bother. */
151 const char * omd5 = ofi->fmd5s[ofi->i];
152 const char * nmd5 = nfi->fmd5s[nfi->i];
153 if (domd5(fn, buffer, 1))
154 return FA_CREATE; /* assume file has been removed */
155 if (!strcmp(omd5, buffer))
156 return FA_CREATE; /* unmodified config file, replace. */
157 if (!strcmp(omd5, nmd5))
158 return FA_SKIP; /* identical file, don't bother. */
161 } else /* dbWhat == LINK */ {
162 memset(buffer, 0, sizeof(buffer));
163 if (readlink(fn, buffer, sizeof(buffer) - 1) == -1)
164 return FA_CREATE; /* assume file has been removed */
165 if (!strcmp(ofi->flinks[ofi->i], buffer))
166 return FA_CREATE; /* unmodified config file, replace. */
167 if (!strcmp(ofi->flinks[ofi->i], nfi->flinks[nfi->i]))
168 return FA_SKIP; /* identical file, don't bother. */
172 * The config file on the disk has been modified, but
173 * the ones in the two packages are different. It would
174 * be nice if RPM was smart enough to at least try and
175 * merge the difference ala CVS, but...
182 static int filecmp(rpmfi afi, rpmfi bfi)
185 fileTypes awhat = whatis(afi->fmodes[afi->i]);
186 fileTypes bwhat = whatis(bfi->fmodes[bfi->i]);
188 if (awhat != bwhat) return 1;
191 const char * alink = afi->flinks[afi->i];
192 const char * blink = bfi->flinks[bfi->i];
193 return strcmp(alink, blink);
194 } else if (awhat == REG) {
196 if (afi->md5s != NULL && bfi->md5s != NULL) {
198 const unsigned char * amd5 = afi->md5s + (16 * afi->i);
199 const unsigned char * bmd5 = bfi->md5s + (16 * bfi->i);
200 return memcmp(amd5, bmd5, 16);
203 const char * amd5 = afi->fmd5s[afi->i];
204 const char * bmd5 = bfi->fmd5s[bfi->i];
205 return strcmp(amd5, bmd5);
215 /* XXX only ts->{probs,rpmdb} modified */
216 static int handleInstInstalledFiles(const rpmts ts,
218 sharedFileInfo shared,
219 int sharedCount, int reportConflicts)
220 /*@globals fileSystem @*/
221 /*@modifies ts, fi, fileSystem @*/
223 const char * altNEVR = NULL;
224 rpmfi otherFi = NULL;
229 { rpmdbMatchIterator mi;
233 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
234 &shared->otherPkg, sizeof(shared->otherPkg));
235 while ((h = rpmdbNextIterator(mi)) != NULL) {
236 altNEVR = hGetNEVR(h, NULL);
237 otherFi = rpmfiNew(ts, NULL, h, RPMTAG_BASENAMES, scareMem);
240 mi = rpmdbFreeIterator(mi);
246 fi->replaced = xcalloc(sharedCount, sizeof(*fi->replaced));
248 ps = rpmtsProblems(ts);
249 for (i = 0; i < sharedCount; i++, shared++) {
250 int otherFileNum, fileNum;
252 otherFileNum = shared->otherFileNum;
253 (void) rpmfiSetFX(otherFi, otherFileNum);
255 fileNum = shared->pkgFileNum;
256 (void) rpmfiSetFX(fi, fileNum);
259 /* XXX another tedious segfault, assume file state normal. */
260 if (otherStates && otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
264 if (XFA_SKIPPING(fi->actions[fileNum]))
267 if (filecmp(otherFi, fi)) {
268 if (reportConflicts) {
269 rpmpsAppend(ps, RPMPROB_FILE_CONFLICT,
270 rpmteNEVR(p), rpmteKey(p),
271 rpmfiDN(fi), rpmfiBN(fi),
275 if (!(rpmfiFFlags(otherFi) | rpmfiFFlags(fi)) & RPMFILE_CONFIG) {
276 /*@-assignexpose@*/ /* FIX: p->replaced, not fi */
277 if (!shared->isRemoved)
278 fi->replaced[numReplaced++] = *shared;
283 if ((rpmfiFFlags(otherFi) | rpmfiFFlags(fi)) & RPMFILE_CONFIG) {
285 action = decideFileFate(ts, otherFi, fi);
286 fi->actions[fileNum] = action;
288 fi->replacedSizes[fileNum] = otherFi->fsizes[otherFi->i];
293 altNEVR = _free(altNEVR);
294 otherFi = rpmfiFree(otherFi, 1);
296 fi->replaced = xrealloc(fi->replaced, /* XXX memory leak */
297 sizeof(*fi->replaced) * (numReplaced + 1));
298 fi->replaced[numReplaced].otherPkg = 0;
305 /* XXX only ts->rpmdb modified */
306 static int handleRmvdInstalledFiles(const rpmts ts, rpmfi fi,
307 sharedFileInfo shared, int sharedCount)
308 /*@globals fileSystem @*/
309 /*@modifies ts, fi, fileSystem @*/
313 const char * otherStates;
316 rpmdbMatchIterator mi;
318 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
319 &shared->otherPkg, sizeof(shared->otherPkg));
320 h = rpmdbNextIterator(mi);
322 mi = rpmdbFreeIterator(mi);
326 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
328 for (i = 0; i < sharedCount; i++, shared++) {
329 int otherFileNum, fileNum;
330 otherFileNum = shared->otherFileNum;
331 fileNum = shared->pkgFileNum;
333 if (otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
336 fi->actions[fileNum] = FA_SKIP;
339 mi = rpmdbFreeIterator(mi);
344 #define ISROOT(_d) (((_d)[0] == '/' && (_d)[1] == '\0') ? "" : (_d))
347 static int _fps_debug = 0;
349 static int fpsCompare (const void * one, const void * two)
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;
363 if (adnlen == 1 && asnlen != 0) adnlen = 0;
364 if (bdnlen == 1 && bsnlen != 0) bdnlen = 0;
366 afn = t = alloca(adnlen+asnlen+abnlen+2);
367 if (adnlen) t = stpcpy(t, a->entry->dirName);
369 if (a->subDir && asnlen) t = stpcpy(t, a->subDir);
370 if (abnlen) t = stpcpy(t, a->baseName);
371 if (afn[0] == '/' && afn[1] == '/') afn++;
373 bfn = t = alloca(bdnlen+bsnlen+bbnlen+2);
374 if (bdnlen) t = stpcpy(t, b->entry->dirName);
376 if (b->subDir && bsnlen) t = stpcpy(t, b->subDir);
377 if (bbnlen) t = stpcpy(t, b->baseName);
378 if (bfn[0] == '/' && bfn[1] == '/') bfn++;
380 rc = strcmp(afn, bfn);
383 fprintf(stderr, "\trc(%d) = strcmp(\"%s\", \"%s\")\n", rc, afn, bfn);
388 fprintf(stderr, "\t%s/%s%s\trc %d\n",
389 ISROOT(b->entry->dirName),
390 (b->subDir ? b->subDir : ""),
400 static int _linear_fps_search = 0;
402 static int findFps(const struct fingerPrint_s * fiFps,
403 const struct fingerPrint_s * otherFps,
411 fprintf(stderr, "==> %s/%s%s\n",
412 ISROOT(fiFps->entry->dirName),
413 (fiFps->subDir ? fiFps->subDir : ""),
417 if (_linear_fps_search) {
420 for (otherFileNum = 0; otherFileNum < otherFc; otherFileNum++, otherFps++) {
424 fprintf(stderr, "\t%4d %s/%s%s\n", otherFileNum,
425 ISROOT(otherFps->entry->dirName),
426 (otherFps->subDir ? otherFps->subDir : ""),
430 /* If the addresses are the same, so are the values. */
431 if (fiFps == otherFps)
434 /* Otherwise, compare fingerprints by value. */
435 /*@-nullpass@*/ /* LCL: looks good to me */
436 if (FP_EQUAL((*fiFps), (*otherFps)))
441 if (otherFileNum == otherFc) {
444 fprintf(stderr, "*** NULL %s/%s%s\n",
445 ISROOT(fiFps->entry->dirName),
446 (fiFps->subDir ? fiFps->subDir : ""),
455 const struct fingerPrint_s * bingoFps;
457 bingoFps = bsearch(fiFps, otherFps, otherFc, sizeof(*otherFps), fpsCompare);
458 if (bingoFps == NULL) {
460 fprintf(stderr, "*** NULL %s/%s%s\n",
461 ISROOT(fiFps->entry->dirName),
462 (fiFps->subDir ? fiFps->subDir : ""),
468 /* If the addresses are the same, so are the values. */
469 /*@-nullpass@*/ /* LCL: looks good to me */
470 if (!(fiFps == bingoFps || FP_EQUAL((*fiFps), (*bingoFps)))) {
472 fprintf(stderr, "*** BAD %s/%s%s\n",
473 ISROOT(bingoFps->entry->dirName),
474 (bingoFps->subDir ? bingoFps->subDir : ""),
480 otherFileNum = (bingoFps != NULL ? (bingoFps - otherFps) : 0);
488 * Update disk space needs on each partition for this package's files.
490 /* XXX only ts->{probs,di} modified */
491 static void handleOverlappedFiles(const rpmts ts,
492 const rpmte p, rpmfi fi)
493 /*@globals fileSystem @*/
494 /*@modifies ts, fi, fileSystem @*/
496 uint_32 fixupSize = 0;
501 ps = rpmtsProblems(ts);
502 fi = rpmfiInit(fi, 0);
504 while ((i = rpmfiNext(fi)) >= 0) {
505 struct fingerPrint_s * fiFps;
506 int otherPkgNum, otherFileNum;
511 if (XFA_SKIPPING(fi->actions[i]))
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.
525 (void) htGetEntry(ts->ht, fiFps,
526 (const void ***) &recs, &numRecs, NULL);
529 * If this package is being added, look only at other packages
530 * being added -- removed packages dance to a different tune.
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.
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.
549 /* Locate this overlapped file in the set of added/removed packages. */
550 for (j = 0; j < numRecs && recs[j] != fi; j++)
553 /* Find what the previous disposition of this file was. */
554 otherFileNum = -1; /* keep gcc quiet */
556 for (otherPkgNum = j - 1; otherPkgNum >= 0; otherPkgNum--) {
557 struct fingerPrint_s * otherFps;
560 otherFi = recs[otherPkgNum];
562 /* Added packages need only look at other added packages. */
563 if (rpmteType(p) == TR_ADDED && rpmteType(otherFi->te) != TR_ADDED)
564 /*@innercontinue@*/ continue;
566 otherFps = otherFi->fps;
567 otherFc = rpmfiFC(otherFi);
569 otherFileNum = findFps(fiFps, otherFps, otherFc);
570 (void) rpmfiSetFX(otherFi, otherFileNum);
572 /* XXX is this test still necessary? */
573 assert(otherFi->actions[otherFileNum] != FA_UNKNOWN);
574 if (otherFi->actions[otherFileNum] != FA_UNKNOWN)
575 /*@innerbreak@*/ break;
578 switch (rpmteType(p)) {
581 if (otherPkgNum < 0) {
582 /* XXX is this test still necessary? */
583 if (fi->actions[i] != FA_UNKNOWN)
584 /*@switchbreak@*/ break;
585 if ((rpmfiFFlags(fi) & RPMFILE_CONFIG) &&
587 /* Here is a non-overlapped pre-existing config file. */
588 fi->actions[i] = (rpmfiFFlags(fi) & RPMFILE_NOREPLACE)
589 ? FA_ALTNAME : FA_BACKUP;
591 fi->actions[i] = FA_CREATE;
593 /*@switchbreak@*/ break;
596 assert(otherFi != NULL);
597 /* Mark added overlapped non-identical files as a conflict. */
598 if ((rpmtsFilterFlags(ts) & RPMPROB_FILTER_REPLACENEWFILES)
599 && filecmp(otherFi, fi))
601 rpmpsAppend(ps, RPMPROB_NEW_FILE_CONFLICT,
602 rpmteNEVR(p), rpmteKey(p),
604 rpmteNEVR(otherFi->te),
608 /* Try to get the disk accounting correct even if a conflict. */
609 fixupSize = otherFi->fsizes[otherFileNum];
611 if ((rpmfiFFlags(fi) & RPMFILE_CONFIG) && !lstat(fn, &sb)) {
612 /* Here is an overlapped pre-existing config file. */
613 fi->actions[i] = (rpmfiFFlags(fi) & RPMFILE_NOREPLACE)
614 ? FA_ALTNAME : FA_SKIP;
616 fi->actions[i] = FA_CREATE;
618 } /*@switchbreak@*/ break;
621 if (otherPkgNum >= 0) {
622 assert(otherFi != NULL);
623 /* Here is an overlapped added file we don't want to nuke. */
624 if (otherFi->actions[otherFileNum] != FA_ERASE) {
625 /* On updates, don't remove files. */
626 fi->actions[i] = FA_SKIP;
627 /*@switchbreak@*/ break;
629 /* Here is an overlapped removed file: skip in previous. */
630 otherFi->actions[otherFileNum] = FA_SKIP;
632 if (XFA_SKIPPING(fi->actions[i]))
633 /*@switchbreak@*/ break;
634 if (fi->fstates && fi->fstates[i] != RPMFILE_STATE_NORMAL)
635 /*@switchbreak@*/ break;
636 if (!(S_ISREG(fi->fmodes[i]) && (rpmfiFFlags(fi) & RPMFILE_CONFIG))) {
637 fi->actions[i] = FA_ERASE;
638 /*@switchbreak@*/ break;
641 /* Here is a pre-existing modified config file that needs saving. */
643 const unsigned char * md5 = fi->md5s + (16 * i);
644 if (!domd5(fn, md5sum, 0) && memcmp(md5, md5sum, 16)) {
645 fi->actions[i] = FA_BACKUP;
646 /*@switchbreak@*/ break;
649 fi->actions[i] = FA_ERASE;
650 /*@switchbreak@*/ break;
653 /* Update disk space info for a file. */
654 rpmtsUpdateDSI(ts, fi->fps[i].entry->dev,
655 fi->fsizes[i], fi->replacedSizes[i], fixupSize, fi->actions[i]);
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
668 static int ensureOlder(rpmts ts,
669 const rpmte p, const Header h)
672 int_32 reqFlags = (RPMSENSE_LESS | RPMSENSE_EQUAL);
679 if (p == NULL || h == NULL)
682 nb = strlen(rpmteNEVR(p)) + (rpmteE(p) != NULL ? strlen(rpmteE(p)) : 0) + 1;
686 if (rpmteE(p) != NULL) t = stpcpy( stpcpy(t, rpmteE(p)), ":");
687 if (rpmteV(p) != NULL) t = stpcpy(t, rpmteV(p));
689 if (rpmteR(p) != NULL) t = stpcpy(t, rpmteR(p));
691 req = rpmdsSingle(RPMTAG_REQUIRENAME, rpmteN(p), reqEVR, reqFlags);
692 rc = headerMatchesDepFlags(h, req);
693 req = rpmdsFree(req);
696 rpmps ps = rpmtsProblems(ts);
697 const char * altNEVR = hGetNEVR(h, NULL);
698 rpmpsAppend(ps, RPMPROB_OLDPACKAGE,
699 rpmteNEVR(p), rpmteKey(p),
703 altNEVR = _free(altNEVR);
714 /*@-mustmod@*/ /* FIX: fi->actions is modified. */
715 static void skipFiles(const rpmts ts, rpmfi fi)
716 /*@globals rpmGlobalMacroContext @*/
717 /*@modifies fi, rpmGlobalMacroContext @*/
719 int noDocs = (rpmtsFlags(ts) & RPMTRANS_FLAG_NODOCS);
720 char ** netsharedPaths = NULL;
721 const char ** languages;
722 const char * dn, * bn;
723 int dnlen, bnlen, ix;
731 noDocs = rpmExpandNumeric("%{_excludedocs}");
733 { const char *tmpPath = rpmExpand("%{_netsharedpath}", NULL);
735 if (tmpPath && *tmpPath != '%')
736 netsharedPaths = splitString(tmpPath, strlen(tmpPath), ':');
738 tmpPath = _free(tmpPath);
741 s = rpmExpand("%{_install_langs}", NULL);
743 if (!(s && *s != '%'))
746 languages = (const char **) splitString(s, strlen(s), ':');
752 /* Compute directory refcount, skip directory if now empty. */
754 drc = alloca(dc * sizeof(*drc));
755 memset(drc, 0, dc * sizeof(*drc));
756 dff = alloca(dc * sizeof(*dff));
757 memset(dff, 0, dc * sizeof(*dff));
759 fi = rpmfiInit(fi, 0);
760 if (fi != NULL) /* XXX lclint */
761 while ((i = rpmfiNext(fi)) >= 0)
771 continue; /* XXX can't happen */
775 /* Don't bother with skipped files */
776 if (XFA_SKIPPING(fi->actions[i])) {
782 * Skip net shared paths.
783 * Net shared paths are not relative to the current root (though
784 * they do need to take package relocations into account).
786 for (nsp = netsharedPaths; nsp && *nsp; nsp++) {
791 if (strncmp(dn, *nsp, len))
792 /*@innercontinue@*/ continue;
793 /* Only directories or complete file paths can be net shared */
794 if (!(dn[len] == '/' || dn[len] == '\0'))
795 /*@innercontinue@*/ continue;
797 if (len < (dnlen + bnlen))
798 /*@innercontinue@*/ continue;
799 if (strncmp(dn, *nsp, dnlen))
800 /*@innercontinue@*/ continue;
801 if (strncmp(bn, (*nsp) + dnlen, bnlen))
802 /*@innercontinue@*/ continue;
804 /* Only directories or complete file paths can be net shared */
805 if (!((*nsp)[len] == '/' || (*nsp)[len] == '\0'))
806 /*@innercontinue@*/ continue;
809 /*@innerbreak@*/ break;
813 drc[ix]--; dff[ix] = 1;
814 fi->actions[i] = FA_SKIPNETSHARED;
819 * Skip i18n language specific files.
821 if (fi->flangs && languages && *fi->flangs[i]) {
822 const char **lang, *l, *le;
823 for (lang = languages; *lang != NULL; lang++) {
824 if (!strcmp(*lang, "all"))
825 /*@innerbreak@*/ break;
826 for (l = fi->flangs[i]; *l != '\0'; l = le) {
827 for (le = l; *le != '\0' && *le != '|'; le++)
829 if ((le-l) > 0 && !strncmp(*lang, l, (le-l)))
830 /*@innerbreak@*/ break;
831 if (*le == '|') le++; /* skip over | */
834 /*@innerbreak@*/ break;
837 drc[ix]--; dff[ix] = 1;
838 fi->actions[i] = FA_SKIPNSTATE;
844 * Skip documentation if requested.
846 if (noDocs && (rpmfiFFlags(fi) & RPMFILE_DOC)) {
847 drc[ix]--; dff[ix] = 1;
848 fi->actions[i] = FA_SKIPNSTATE;
853 /* Skip (now empty) directories that had skipped files. */
855 if (fi != NULL) /* XXX can't happen */
856 for (j = 0; j < dc; j++)
858 if ((fi = rpmfiInitD(fi)) != NULL)
859 while (j = rpmfiNextD(fi) >= 0)
863 if (drc[j]) continue; /* dir still has files. */
864 if (!dff[j]) continue; /* dir was not emptied here. */
866 /* Find parent directory and basename. */
867 dn = fi->dnl[j]; dnlen = strlen(dn) - 1;
868 bn = dn + dnlen; bnlen = 0;
869 while (bn > dn && bn[-1] != '/') {
875 /* If explicitly included in the package, skip the directory. */
876 fi = rpmfiInit(fi, 0);
877 if (fi != NULL) /* XXX lclint */
878 while ((i = rpmfiNext(fi)) >= 0) {
881 if (XFA_SKIPPING(fi->actions[i]))
882 /*@innercontinue@*/ continue;
883 if (whatis(fi->fmodes[i]) != XDIR)
884 /*@innercontinue@*/ continue;
885 dir = fi->dnl[fi->dil[i]];
886 if (strlen(dir) != dnlen)
887 /*@innercontinue@*/ continue;
888 if (strncmp(dir, dn, dnlen))
889 /*@innercontinue@*/ continue;
890 if (strlen(fi->bnl[i]) != bnlen)
891 /*@innercontinue@*/ continue;
892 if (strncmp(fi->bnl[i], bn, bnlen))
893 /*@innercontinue@*/ continue;
894 rpmMessage(RPMMESS_DEBUG, _("excluding directory %s\n"), dn);
895 fi->actions[i] = FA_SKIPNSTATE;
896 /*@innerbreak@*/ break;
900 if (netsharedPaths) freeSplitString(netsharedPaths);
901 #ifdef DYING /* XXX freeFi will deal with this later. */
902 fi->flangs = _free(fi->flangs);
904 if (languages) freeSplitString((char **)languages);
909 * Return transaction element's file info.
910 * @todo Take a rpmfi refcount here.
911 * @param tsi transaction element iterator
912 * @return transaction element file info
915 rpmfi rpmtsiFi(const rpmtsi tsi)
920 if (tsi != NULL && tsi->ocsave != -1) {
921 /*@-type -abstract@*/ /* FIX: rpmte not opaque */
922 rpmte te = rpmtsElement(tsi->ts, tsi->ocsave);
924 if (te != NULL && (fi = te->fi) != NULL)
927 /*@=type =abstract@*/
929 /*@-compdef -refcounttrans -usereleased @*/
931 /*@=compdef =refcounttrans =usereleased @*/
934 #define NOTIFY(_ts, _al) /*@i@*/ if ((_ts)->notify) (void) (_ts)->notify _al
936 int rpmtsRun(rpmts ts, rpmps okProbs, rpmprobFilterFlags ignoreSet)
940 int totalFileCount = 0;
942 sharedFileInfo shared, sharedList;
946 fingerPrintCache fpc;
948 PSM_t psm = memset(alloca(sizeof(*psm)), 0, sizeof(*psm));
953 /* FIXME: what if the same package is included in ts twice? */
955 if (rpmtsFlags(ts) & RPMTRANS_FLAG_NOSCRIPTS)
956 (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | _noTransScripts | _noTransTriggers));
957 if (rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERS)
958 (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | _noTransTriggers));
960 /* XXX MULTILIB is broken, as packages can and do execute /sbin/ldconfig. */
961 if (rpmtsFlags(ts) & (RPMTRANS_FLAG_JUSTDB | RPMTRANS_FLAG_MULTILIB))
962 (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | _noTransScripts | _noTransTriggers));
964 ts->probs = rpmpsFree(ts->probs);
965 ts->probs = rpmpsCreate();
967 ts->ignoreSet = ignoreSet;
968 { const char * currDir = currentDirectory();
969 rpmtsSetCurrDir(ts, currDir);
970 currDir = _free(currDir);
973 (void) rpmtsSetChrootDone(ts, 0);
975 { int_32 tid = (int_32) time(NULL);
976 (void) rpmtsSetTid(ts, tid);
979 memset(psm, 0, sizeof(*psm));
980 psm->ts = rpmtsLink(ts, "tsRun");
982 /* Get available space on mounted file systems. */
983 xx = rpmtsInitDSI(ts);
985 /* ===============================================
986 * For packages being installed:
987 * - verify package arch/os.
988 * - verify package epoch:version-release is newer.
990 * For packages being removed:
993 ps = rpmtsProblems(ts);
994 /* The ordering doesn't matter here */
996 while ((p = rpmtsiNext(pi, TR_ADDED)) != NULL) {
997 rpmdbMatchIterator mi;
1000 if ((fi = rpmtsiFi(pi)) == NULL)
1001 continue; /* XXX can't happen */
1004 if (!(rpmtsFilterFlags(ts) & RPMPROB_FILTER_IGNOREARCH))
1005 if (!archOkay(rpmteA(p)))
1006 rpmpsAppend(ps, RPMPROB_BADARCH,
1007 rpmteNEVR(p), rpmteKey(p),
1011 if (!(rpmtsFilterFlags(ts) & RPMPROB_FILTER_IGNOREOS))
1012 if (!osOkay(rpmteO(p)))
1013 rpmpsAppend(ps, RPMPROB_BADOS,
1014 rpmteNEVR(p), rpmteKey(p),
1018 if (!(rpmtsFilterFlags(ts) & RPMPROB_FILTER_OLDPACKAGE)) {
1020 mi = rpmtsInitIterator(ts, RPMTAG_NAME, rpmteN(p), 0);
1021 while ((h = rpmdbNextIterator(mi)) != NULL)
1022 xx = ensureOlder(ts, p, h);
1023 mi = rpmdbFreeIterator(mi);
1026 /* XXX multilib should not display "already installed" problems */
1027 if (!(rpmtsFilterFlags(ts) & RPMPROB_FILTER_REPLACEPKG) && !rpmteMultiLib(p)) {
1028 mi = rpmtsInitIterator(ts, RPMTAG_NAME, rpmteN(p), 0);
1029 xx = rpmdbSetIteratorRE(mi, RPMTAG_VERSION, RPMMIRE_DEFAULT,
1031 xx = rpmdbSetIteratorRE(mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT,
1034 while (rpmdbNextIterator(mi) != NULL) {
1035 rpmpsAppend(ps, RPMPROB_PKG_INSTALLED,
1036 rpmteNEVR(p), rpmteKey(p),
1039 /*@innerbreak@*/ break;
1041 mi = rpmdbFreeIterator(mi);
1044 /* Count no. of files (if any). */
1045 totalFileCount += fc;
1048 pi = rpmtsiFree(pi);
1051 /* The ordering doesn't matter here */
1052 pi = rpmtsiInit(ts);
1053 while ((p = rpmtsiNext(pi, TR_REMOVED)) != NULL) {
1056 if ((fi = rpmtsiFi(pi)) == NULL)
1057 continue; /* XXX can't happen */
1060 totalFileCount += fc;
1062 pi = rpmtsiFree(pi);
1064 /* ===============================================
1065 * Initialize transaction element file info for package:
1069 * FIXME?: we'd be better off assembling one very large file list and
1070 * calling fpLookupList only once. I'm not sure that the speedup is
1071 * worth the trouble though.
1073 pi = rpmtsiInit(ts);
1074 while ((p = rpmtsiNext(pi, 0)) != NULL) {
1077 if ((fi = rpmtsiFi(pi)) == NULL)
1078 continue; /* XXX can't happen */
1081 #ifdef DYING /* XXX W2DO? this is now done in rpmtsiFi, okay ??? */
1082 fi->magic = RPMFIMAGIC;
1087 switch (rpmteType(p)) {
1090 /* Skip netshared paths, not our i18n files, and excluded docs */
1093 /*@switchbreak@*/ break;
1095 fi->record = rpmteDBOffset(p);
1096 /*@switchbreak@*/ break;
1100 fi->fps = (fc > 0 ? xmalloc(fc * sizeof(*fi->fps)) : NULL);
1102 pi = rpmtsiFree(pi);
1104 if (!rpmtsChrootDone(ts)) {
1105 const char * rootDir = rpmtsRootDir(ts);
1107 /*@-superuser -noeffect @*/
1108 if (rootDir != NULL)
1109 xx = chroot(rootDir);
1110 /*@=superuser =noeffect @*/
1111 (void) rpmtsSetChrootDone(ts, 1);
1114 ts->ht = htCreate(totalFileCount * 2, 0, 0, fpHashFunction, fpEqual);
1115 fpc = fpCacheCreate(totalFileCount);
1117 /* ===============================================
1118 * Add fingerprint for each file not skipped.
1120 pi = rpmtsiInit(ts);
1121 while ((p = rpmtsiNext(pi, 0)) != NULL) {
1124 if ((fi = rpmtsiFi(pi)) == NULL)
1125 continue; /* XXX can't happen */
1128 fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fc, fi->fps);
1130 fi = rpmfiInit(fi, 0);
1131 if (fi != NULL) /* XXX lclint */
1132 while ((i = rpmfiNext(fi)) >= 0) {
1133 if (XFA_SKIPPING(fi->actions[i]))
1134 /*@innercontinue@*/ continue;
1135 /*@-dependenttrans@*/
1136 htAddEntry(ts->ht, fi->fps + i, (void *) fi);
1137 /*@=dependenttrans@*/
1141 pi = rpmtsiFree(pi);
1143 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_START, 6, ts->orderCount,
1144 NULL, ts->notifyData));
1146 /* ===============================================
1147 * Compute file disposition for each package in transaction set.
1149 ps = rpmtsProblems(ts);
1150 pi = rpmtsiInit(ts);
1151 while ((p = rpmtsiNext(pi, 0)) != NULL) {
1152 dbiIndexSet * matches;
1156 if ((fi = rpmtsiFi(pi)) == NULL)
1157 continue; /* XXX can't happen */
1160 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_PROGRESS, rpmtsiOc(pi),
1161 ts->orderCount, NULL, ts->notifyData));
1163 if (fc == 0) continue;
1165 /* Extract file info for all files in this package from the database. */
1166 matches = xcalloc(fc, sizeof(*matches));
1167 if (rpmdbFindFpList(rpmtsGetRdb(ts), fi->fps, matches, fc)) {
1168 psm->ts = rpmtsUnlink(ts, "tsRun (rpmFindFpList fail)");
1170 return 1; /* XXX WTFO? */
1174 fi = rpmfiInit(fi, 0);
1175 while ((i = rpmfiNext(fi)) >= 0)
1176 numShared += dbiIndexSetCount(matches[i]);
1178 /* Build sorted file info list for this package. */
1179 shared = sharedList = xcalloc((numShared + 1), sizeof(*sharedList));
1181 fi = rpmfiInit(fi, 0);
1182 while ((i = rpmfiNext(fi)) >= 0) {
1184 * Take care not to mark files as replaced in packages that will
1185 * have been removed before we will get here.
1187 for (j = 0; j < dbiIndexSetCount(matches[i]); j++) {
1189 ro = dbiIndexRecordOffset(matches[i], j);
1191 qi = rpmtsiInit(ts);
1192 while ((q = rpmtsiNext(qi, TR_REMOVED)) != NULL) {
1194 /*@innerbreak@*/ break;
1195 if (rpmteDBOffset(q) == ro)
1198 qi = rpmtsiFree(qi);
1200 shared->pkgFileNum = i;
1201 shared->otherPkg = dbiIndexRecordOffset(matches[i], j);
1202 shared->otherFileNum = dbiIndexRecordFileNumber(matches[i], j);
1203 shared->isRemoved = (knownBad == ro);
1206 matches[i] = dbiFreeIndexSet(matches[i]);
1208 numShared = shared - sharedList;
1209 shared->otherPkg = -1;
1210 matches = _free(matches);
1212 /* Sort file info by other package index (otherPkg) */
1213 qsort(sharedList, numShared, sizeof(*shared), sharedCmp);
1215 /* For all files from this package that are in the database ... */
1217 for (i = 0; i < numShared; i = nexti) {
1220 shared = sharedList + i;
1222 /* Find the end of the files in the other package. */
1223 for (nexti = i + 1; nexti < numShared; nexti++) {
1224 if (sharedList[nexti].otherPkg != shared->otherPkg)
1225 /*@innerbreak@*/ break;
1228 /* Is this file from a package being removed? */
1230 if (ts->removedPackages != NULL)
1231 for (j = 0; j < ts->numRemovedPackages; j++) {
1232 if (ts->removedPackages[j] != shared->otherPkg)
1233 /*@innercontinue@*/ continue;
1235 /*@innerbreak@*/ break;
1238 /* Determine the fate of each file. */
1239 switch (rpmteType(p)) {
1241 xx = handleInstInstalledFiles(ts, p, fi, shared, nexti - i,
1242 !(beingRemoved || (rpmtsFilterFlags(ts) & RPMPROB_FILTER_REPLACEOLDFILES)));
1243 /*@switchbreak@*/ break;
1246 xx = handleRmvdInstalledFiles(ts, fi, shared, nexti - i);
1247 /*@switchbreak@*/ break;
1254 /* Update disk space needs on each partition for this package. */
1255 handleOverlappedFiles(ts, p, fi);
1257 /* Check added package has sufficient space on each partition used. */
1258 switch (rpmteType(p)) {
1260 rpmtsCheckDSIProblems(ts, p);
1261 /*@switchbreak@*/ break;
1263 /*@switchbreak@*/ break;
1266 pi = rpmtsiFree(pi);
1269 if (rpmtsChrootDone(ts)) {
1270 const char * currDir = rpmtsCurrDir(ts);
1271 /*@-superuser -noeffect @*/
1273 /*@=superuser =noeffect @*/
1274 (void) rpmtsSetChrootDone(ts, 0);
1275 if (currDir != NULL)
1276 xx = chdir(currDir);
1279 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_STOP, 6, ts->orderCount,
1280 NULL, ts->notifyData));
1282 /* ===============================================
1283 * Free unused memory as soon as possible.
1285 pi = rpmtsiInit(ts);
1286 while ((p = rpmtsiNext(pi, 0)) != NULL) {
1287 if ((fi = rpmtsiFi(pi)) == NULL)
1288 continue; /* XXX can't happen */
1289 if (rpmfiFC(fi) == 0)
1291 fi->fps = _free(fi->fps);
1293 pi = rpmtsiFree(pi);
1295 fpc = fpCacheFree(fpc);
1296 ts->ht = htFree(ts->ht);
1298 /* ===============================================
1299 * If unfiltered problems exist, free memory and return.
1301 if ((rpmtsFlags(ts) & RPMTRANS_FLAG_BUILD_PROBS)
1302 || (ts->probs->numProblems &&
1303 (okProbs != NULL || rpmpsTrim(ts->probs, okProbs)))
1306 if (psm->ts != NULL)
1307 psm->ts = rpmtsUnlink(psm->ts, "tsRun (problems)");
1308 return ts->orderCount;
1311 /* ===============================================
1312 * Save removed files before erasing.
1314 if (rpmtsFlags(ts) & (RPMTRANS_FLAG_DIRSTASH | RPMTRANS_FLAG_REPACKAGE)) {
1315 pi = rpmtsiInit(ts);
1316 while ((p = rpmtsiNext(pi, 0)) != NULL) {
1318 switch (rpmteType(p)) {
1320 /*@switchbreak@*/ break;
1322 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_REPACKAGE))
1323 /*@switchbreak@*/ break;
1325 psm->fi = rpmfiLink(fi, "tsRepackage");
1326 xx = psmStage(psm, PSM_PKGSAVE);
1327 (void) rpmfiUnlink(fi, "tsRepackage");
1330 /*@switchbreak@*/ break;
1333 pi = rpmtsiFree(pi);
1336 /* ===============================================
1337 * Install and remove packages.
1339 lastKey = (alKey)-2; /* erased packages have -1 */
1340 pi = rpmtsiInit(ts);
1341 /*@-branchstate@*/ /* FIX: fi reload needs work */
1342 while ((p = rpmtsiNext(pi, 0)) != NULL) {
1347 if ((fi = rpmtsiFi(pi)) == NULL)
1348 continue; /* XXX can't happen */
1351 psm->fi = rpmfiLink(fi, "tsInstall");
1352 switch (rpmteType(p)) {
1355 pkgKey = rpmteAddedKey(p);
1357 rpmMessage(RPMMESS_DEBUG, "========== +++ %s\n", rpmteNEVR(p));
1359 /*@-type@*/ /* FIX: rpmte not opaque */
1361 /*@-noeffectuncon@*/ /* FIX: notify annotations */
1362 p->fd = ts->notify(fi->h, RPMCALLBACK_INST_OPEN_FILE, 0, 0,
1363 rpmteKey(p), ts->notifyData);
1364 /*@=noeffectuncon@*/
1365 if (rpmteFd(p) != NULL) {
1368 rpmrc = rpmReadPackageFile(ts, rpmteFd(p),
1371 if (!(rpmrc == RPMRC_OK || rpmrc == RPMRC_BADSIZE)) {
1372 /*@-noeffectuncon@*/ /* FIX: notify annotations */
1373 p->fd = ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE,
1375 rpmteKey(p), ts->notifyData);
1376 /*@=noeffectuncon@*/
1380 if (rpmteFd(p) != NULL) gotfd = 1;
1385 if (rpmteFd(p) != NULL) {
1387 char * fstates = fi->fstates;
1388 fileAction * actions = fi->actions;
1392 psm->fi = rpmfiFree(psm->fi, 1);
1393 (void) rpmfiFree(fi, 0);
1395 fi->magic = RPMFIMAGIC;
1398 (void) rpmfiNew(ts, fi, p->h, RPMTAG_BASENAMES, 1);
1399 psm->fi = rpmfiLink(fi, "tsInstall");
1400 fi->fstates = _free(fi->fstates);
1401 fi->fstates = fstates;
1402 fi->actions = _free(fi->actions);
1403 fi->actions = actions;
1407 if (rpmteMultiLib(p))
1408 (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | RPMTRANS_FLAG_MULTILIB));
1410 (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) & ~RPMTRANS_FLAG_MULTILIB));
1412 if (psmStage(psm, PSM_PKGINSTALL)) {
1416 fi->h = headerFree(fi->h, "TR_ADDED fi->h free");
1422 p->h = headerFree(p->h, "TR_ADDED h free");
1425 /*@-noeffectuncon @*/ /* FIX: check rc */
1426 (void) ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE, 0, 0,
1427 rpmteKey(p), ts->notifyData);
1428 /*@=noeffectuncon @*/
1433 /*@switchbreak@*/ break;
1435 rpmMessage(RPMMESS_DEBUG, "========== --- %s\n", rpmteNEVR(p));
1436 /* If install failed, then we shouldn't erase. */
1437 if (rpmteDependsOnKey(p) != lastKey) {
1438 if (psmStage(psm, PSM_PKGERASE))
1441 /*@switchbreak@*/ break;
1443 xx = rpmdbSync(rpmtsGetRdb(ts));
1444 (void) rpmfiUnlink(psm->fi, "tsInstall");
1447 /*@-type@*/ /* FIX: p is almost opaque */
1448 p->fi = rpmfiFree(fi, 1);
1452 pi = rpmtsiFree(pi);
1454 psm->ts = rpmtsUnlink(psm->ts, "tsRun");
1456 /*@-nullstate@*/ /* FIX: ts->flList may be NULL */