2 * \file lib/transaction.c
8 #include <rpmmacro.h> /* XXX for rpmExpand */
17 #define _RPMFI_INTERNAL
20 #define _RPMTE_INTERNAL
23 #define _RPMTS_INTERNAL
28 #include "legacy.h" /* XXX domd5 */
29 #include "misc.h" /* XXX stripTrailingChar, splitString, currentDirectory */
33 /*@access FD_t @*/ /* XXX compared with NULL */
34 /*@access Header @*/ /* XXX compared with NULL */
35 /*@access rpmps @*/ /* XXX need rpmProblemSetOK() */
36 /*@access dbiIndexSet @*/
52 static int archOkay(/*@null@*/ const char * pkgArch)
55 if (pkgArch == NULL) return 0;
56 return (rpmMachineScore(RPM_MACHTABLE_INSTARCH, pkgArch) ? 1 : 0);
61 static int osOkay(/*@null@*/ const char * pkgOs)
64 if (pkgOs == NULL) return 0;
65 return (rpmMachineScore(RPM_MACHTABLE_INSTOS, pkgOs) ? 1 : 0);
70 static int sharedCmp(const void * one, const void * two)
73 sharedFileInfo a = (sharedFileInfo) one;
74 sharedFileInfo b = (sharedFileInfo) two;
76 if (a->otherPkg < b->otherPkg)
78 else if (a->otherPkg > b->otherPkg)
87 static fileAction decideFileFate(const rpmts ts,
88 const rpmfi ofi, rpmfi nfi)
89 /*@globals fileSystem, internalState @*/
90 /*@modifies nfi, fileSystem, internalState @*/
92 const char * fn = rpmfiFN(nfi);
93 int newFlags = rpmfiFFlags(nfi);
95 fileTypes dbWhat, newWhat, diskWhat;
97 int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
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.
104 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_ALLFILES)
105 && (newFlags & RPMFILE_MISSINGOK))
107 rpmMessage(RPMMESS_DEBUG, _("%s skipped due to missingok flag\n"),
115 diskWhat = whatis((int_16)sb.st_mode);
116 dbWhat = whatis(rpmfiFMode(ofi));
117 newWhat = whatis(rpmfiFMode(nfi));
120 * RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
121 * them in older packages as well.
126 if (diskWhat != newWhat)
128 else if (newWhat != dbWhat && diskWhat != dbWhat)
130 else if (dbWhat != newWhat)
132 else if (dbWhat != LINK && dbWhat != REG)
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.
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. */
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...
175 static int filecmp(rpmfi afi, rpmfi bfi)
178 fileTypes awhat = whatis(rpmfiFMode(afi));
179 fileTypes bwhat = whatis(rpmfiFMode(bfi));
181 if (awhat != bwhat) return 1;
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);
199 /* XXX only ts->{probs,rpmdb} modified */
201 static int handleInstInstalledFiles(const rpmts ts,
203 sharedFileInfo shared,
204 int sharedCount, int reportConflicts)
205 /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
206 /*@modifies ts, fi, rpmGlobalMacroContext, fileSystem, internalState @*/
208 const char * altNEVR = NULL;
209 rpmfi otherFi = NULL;
214 { rpmdbMatchIterator mi;
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);
225 mi = rpmdbFreeIterator(mi);
231 fi->replaced = xcalloc(sharedCount, sizeof(*fi->replaced));
233 ps = rpmtsProblems(ts);
234 for (i = 0; i < sharedCount; i++, shared++) {
235 int otherFileNum, fileNum;
238 otherFileNum = shared->otherFileNum;
239 (void) rpmfiSetFX(otherFi, otherFileNum);
241 fileNum = shared->pkgFileNum;
242 (void) rpmfiSetFX(fi, fileNum);
244 isCfgFile = ((rpmfiFFlags(otherFi) | rpmfiFFlags(fi)) & RPMFILE_CONFIG);
247 /* XXX another tedious segfault, assume file state normal. */
248 if (otherStates && otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
252 if (XFA_SKIPPING(fi->actions[fileNum]))
255 if (filecmp(otherFi, fi)) {
256 if (reportConflicts) {
257 rpmpsAppend(ps, RPMPROB_FILE_CONFLICT,
258 rpmteNEVR(p), rpmteKey(p),
259 rpmfiDN(fi), rpmfiBN(fi),
264 /*@-assignexpose@*/ /* FIX: p->replaced, not fi */
265 if (!shared->isRemoved)
266 fi->replaced[numReplaced++] = *shared;
273 action = decideFileFate(ts, otherFi, fi);
274 fi->actions[fileNum] = action;
276 fi->replacedSizes[fileNum] = rpmfiFSize(otherFi);
280 altNEVR = _free(altNEVR);
281 otherFi = rpmfiFree(otherFi, 1);
283 fi->replaced = xrealloc(fi->replaced, /* XXX memory leak */
284 sizeof(*fi->replaced) * (numReplaced + 1));
285 fi->replaced[numReplaced].otherPkg = 0;
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 @*/
301 const char * otherStates;
304 rpmdbMatchIterator mi;
306 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
307 &shared->otherPkg, sizeof(shared->otherPkg));
308 h = rpmdbNextIterator(mi);
310 mi = rpmdbFreeIterator(mi);
314 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
317 for (i = 0; i < sharedCount; i++, shared++) {
318 int otherFileNum, fileNum;
319 otherFileNum = shared->otherFileNum;
320 fileNum = shared->pkgFileNum;
322 if (otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
325 fi->actions[fileNum] = FA_SKIP;
329 mi = rpmdbFreeIterator(mi);
334 #define ISROOT(_d) (((_d)[0] == '/' && (_d)[1] == '\0') ? "" : (_d))
337 static int _fps_debug = 0;
339 static int fpsCompare (const void * one, const void * two)
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;
353 if (adnlen == 1 && asnlen != 0) adnlen = 0;
354 if (bdnlen == 1 && bsnlen != 0) bdnlen = 0;
357 afn = t = alloca(adnlen+asnlen+abnlen+2);
358 if (adnlen) t = stpcpy(t, a->entry->dirName);
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++;
364 bfn = t = alloca(bdnlen+bsnlen+bbnlen+2);
365 if (bdnlen) t = stpcpy(t, b->entry->dirName);
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++;
372 rc = strcmp(afn, bfn);
375 fprintf(stderr, "\trc(%d) = strcmp(\"%s\", \"%s\")\n", rc, afn, bfn);
380 fprintf(stderr, "\t%s/%s%s\trc %d\n",
381 ISROOT(b->entry->dirName),
382 (b->subDir ? b->subDir : ""),
392 static int _linear_fps_search = 0;
394 static int findFps(const struct fingerPrint_s * fiFps,
395 const struct fingerPrint_s * otherFps,
403 fprintf(stderr, "==> %s/%s%s\n",
404 ISROOT(fiFps->entry->dirName),
405 (fiFps->subDir ? fiFps->subDir : ""),
409 if (_linear_fps_search) {
412 for (otherFileNum = 0; otherFileNum < otherFc; otherFileNum++, otherFps++) {
416 fprintf(stderr, "\t%4d %s/%s%s\n", otherFileNum,
417 ISROOT(otherFps->entry->dirName),
418 (otherFps->subDir ? otherFps->subDir : ""),
422 /* If the addresses are the same, so are the values. */
423 if (fiFps == otherFps)
426 /* Otherwise, compare fingerprints by value. */
427 /*@-nullpass@*/ /* LCL: looks good to me */
428 if (FP_EQUAL((*fiFps), (*otherFps)))
433 if (otherFileNum == otherFc) {
436 fprintf(stderr, "*** FP_EQUAL NULL %s/%s%s\n",
437 ISROOT(fiFps->entry->dirName),
438 (fiFps->subDir ? fiFps->subDir : ""),
447 const struct fingerPrint_s * bingoFps;
450 bingoFps = bsearch(fiFps, otherFps, otherFc, sizeof(*otherFps), fpsCompare);
452 if (bingoFps == NULL) {
455 fprintf(stderr, "*** bingoFps NULL %s/%s%s\n",
456 ISROOT(fiFps->entry->dirName),
457 (fiFps->subDir ? fiFps->subDir : ""),
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)))) {
468 fprintf(stderr, "*** BAD %s/%s%s\n",
469 ISROOT(bingoFps->entry->dirName),
470 (bingoFps->subDir ? bingoFps->subDir : ""),
476 otherFileNum = (bingoFps != NULL ? (bingoFps - otherFps) : 0);
484 * Update disk space needs on each partition for this package's files.
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 @*/
492 uint_32 fixupSize = 0;
497 ps = rpmtsProblems(ts);
498 fi = rpmfiInit(fi, 0);
500 while ((i = rpmfiNext(fi)) >= 0) {
501 struct fingerPrint_s * fiFps;
502 int otherPkgNum, otherFileNum;
509 if (XFA_SKIPPING(fi->actions[i]))
514 FFlags = rpmfiFFlags(fi);
515 FMode = rpmfiFMode(fi);
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 Happens iff fingerprint for incomplete package install. */
573 if (otherFi->actions[otherFileNum] != FA_UNKNOWN)
574 /*@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 ((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;
590 fi->actions[i] = FA_CREATE;
592 /*@switchbreak@*/ break;
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))
600 rpmpsAppend(ps, RPMPROB_NEW_FILE_CONFLICT,
601 rpmteNEVR(p), rpmteKey(p),
603 rpmteNEVR(otherFi->te),
607 /* Try to get the disk accounting correct even if a conflict. */
608 fixupSize = rpmfiFSize(otherFi);
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;
615 fi->actions[i] = FA_CREATE;
617 } /*@switchbreak@*/ break;
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;
628 /* Here is an overlapped removed file: skip in previous. */
629 otherFi->actions[otherFileNum] = FA_SKIP;
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;
640 /* Here is a pre-existing modified config file that needs saving. */
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;
648 fi->actions[i] = FA_ERASE;
649 /*@switchbreak@*/ break;
653 /* Update disk space info for a file. */
654 rpmtsUpdateDSI(ts, fiFps->entry->dev,
655 rpmfiFSize(fi), 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)
683 nb = strlen(rpmteNEVR(p)) + (rpmteE(p) != NULL ? strlen(rpmteE(p)) : 0) + 1;
687 if (rpmteE(p) != NULL) t = stpcpy( stpcpy(t, rpmteE(p)), ":");
688 if (rpmteV(p) != NULL) t = stpcpy(t, rpmteV(p));
690 if (rpmteR(p) != NULL) t = stpcpy(t, rpmteR(p));
693 req = rpmdsSingle(RPMTAG_REQUIRENAME, rpmteN(p), reqEVR, reqFlags);
694 rc = headerMatchesDepFlags(h, req);
695 req = rpmdsFree(req);
698 rpmps ps = rpmtsProblems(ts);
699 const char * altNEVR = hGetNEVR(h, NULL);
700 rpmpsAppend(ps, RPMPROB_OLDPACKAGE,
701 rpmteNEVR(p), rpmteKey(p),
705 altNEVR = _free(altNEVR);
716 /*@-mustmod@*/ /* FIX: fi->actions is modified. */
718 static void skipFiles(const rpmts ts, rpmfi fi)
719 /*@globals rpmGlobalMacroContext @*/
720 /*@modifies fi, rpmGlobalMacroContext @*/
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;
734 noDocs = rpmExpandNumeric("%{_excludedocs}");
736 { const char *tmpPath = rpmExpand("%{_netsharedpath}", NULL);
738 if (tmpPath && *tmpPath != '%')
739 netsharedPaths = splitString(tmpPath, strlen(tmpPath), ':');
741 tmpPath = _free(tmpPath);
744 s = rpmExpand("%{_install_langs}", NULL);
746 if (!(s && *s != '%'))
749 languages = (const char **) splitString(s, strlen(s), ':');
755 /* Compute directory refcount, skip directory if now empty. */
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));
762 fi = rpmfiInit(fi, 0);
763 if (fi != NULL) /* XXX lclint */
764 while ((i = rpmfiNext(fi)) >= 0)
774 continue; /* XXX can't happen */
778 /* Don't bother with skipped files */
779 if (XFA_SKIPPING(fi->actions[i])) {
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).
789 for (nsp = netsharedPaths; nsp && *nsp; nsp++) {
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;
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;
807 /* Only directories or complete file paths can be net shared */
808 if (!((*nsp)[len] == '/' || (*nsp)[len] == '\0'))
809 /*@innercontinue@*/ continue;
812 /*@innerbreak@*/ break;
816 drc[ix]--; dff[ix] = 1;
817 fi->actions[i] = FA_SKIPNETSHARED;
822 * Skip i18n language specific files.
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++)
832 if ((le-l) > 0 && !strncmp(*lang, l, (le-l)))
833 /*@innerbreak@*/ break;
834 if (*le == '|') le++; /* skip over | */
837 /*@innerbreak@*/ break;
840 drc[ix]--; dff[ix] = 1;
841 fi->actions[i] = FA_SKIPNSTATE;
847 * Skip documentation if requested.
849 if (noDocs && (rpmfiFFlags(fi) & RPMFILE_DOC)) {
850 drc[ix]--; dff[ix] = 1;
851 fi->actions[i] = FA_SKIPNSTATE;
856 /* Skip (now empty) directories that had skipped files. */
858 if (fi != NULL) /* XXX can't happen */
859 for (j = 0; j < dc; j++)
861 if ((fi = rpmfiInitD(fi)) != NULL)
862 while (j = rpmfiNextD(fi) >= 0)
866 if (drc[j]) continue; /* dir still has files. */
867 if (!dff[j]) continue; /* dir was not emptied here. */
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] != '/') {
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;
885 if (XFA_SKIPPING(fi->actions[i]))
886 /*@innercontinue@*/ continue;
888 fFMode = rpmfiFMode(fi);
890 if (whatis(fFMode) != XDIR)
891 /*@innercontinue@*/ continue;
893 if (strlen(fdn) != dnlen)
894 /*@innercontinue@*/ continue;
895 if (strncmp(fdn, dn, dnlen))
896 /*@innercontinue@*/ continue;
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;
908 if (netsharedPaths) freeSplitString(netsharedPaths);
909 #ifdef DYING /* XXX freeFi will deal with this later. */
910 fi->flangs = _free(fi->flangs);
912 if (languages) freeSplitString((char **)languages);
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
924 rpmfi rpmtsiFi(const rpmtsi tsi)
929 if (tsi != NULL && tsi->ocsave != -1) {
930 /*@-type -abstract@*/ /* FIX: rpmte not opaque */
931 rpmte te = rpmtsElement(tsi->ts, tsi->ocsave);
933 if (te != NULL && (fi = te->fi) != NULL)
936 /*@=type =abstract@*/
938 /*@-compdef -refcounttrans -usereleased @*/
940 /*@=compdef =refcounttrans =usereleased @*/
943 #define NOTIFY(_ts, _al) /*@i@*/ if ((_ts)->notify) (void) (_ts)->notify _al
945 int rpmtsRun(rpmts ts, rpmps okProbs, rpmprobFilterFlags ignoreSet)
949 int totalFileCount = 0;
951 sharedFileInfo shared, sharedList;
955 fingerPrintCache fpc;
957 PSM_t psm = memset(alloca(sizeof(*psm)), 0, sizeof(*psm));
962 /* FIXME: what if the same package is included in ts twice? */
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));
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));
973 ts->probs = rpmpsFree(ts->probs);
974 ts->probs = rpmpsCreate();
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);
980 /* Open database RDWR for installing packages. */
981 if (rpmtsOpenDB(ts, dbmode))
982 return -1; /* XXX W2DO? */
985 ts->ignoreSet = ignoreSet;
986 { const char * currDir = currentDirectory();
987 rpmtsSetCurrDir(ts, currDir);
988 currDir = _free(currDir);
991 (void) rpmtsSetChrootDone(ts, 0);
993 { int_32 tid = (int_32) time(NULL);
994 (void) rpmtsSetTid(ts, tid);
997 memset(psm, 0, sizeof(*psm));
998 psm->ts = rpmtsLink(ts, "tsRun");
1000 /* Get available space on mounted file systems. */
1001 xx = rpmtsInitDSI(ts);
1003 /* ===============================================
1004 * For packages being installed:
1005 * - verify package arch/os.
1006 * - verify package epoch:version-release is newer.
1008 * For packages being removed:
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;
1020 if ((fi = rpmtsiFi(pi)) == NULL)
1021 continue; /* XXX can't happen */
1024 if (!(rpmtsFilterFlags(ts) & RPMPROB_FILTER_IGNOREARCH))
1025 if (!archOkay(rpmteA(p)))
1026 rpmpsAppend(ps, RPMPROB_BADARCH,
1027 rpmteNEVR(p), rpmteKey(p),
1031 if (!(rpmtsFilterFlags(ts) & RPMPROB_FILTER_IGNOREOS))
1032 if (!osOkay(rpmteO(p)))
1033 rpmpsAppend(ps, RPMPROB_BADOS,
1034 rpmteNEVR(p), rpmteKey(p),
1038 if (!(rpmtsFilterFlags(ts) & RPMPROB_FILTER_OLDPACKAGE)) {
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);
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,
1051 xx = rpmdbSetIteratorRE(mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT,
1054 while (rpmdbNextIterator(mi) != NULL) {
1055 rpmpsAppend(ps, RPMPROB_PKG_INSTALLED,
1056 rpmteNEVR(p), rpmteKey(p),
1059 /*@innerbreak@*/ break;
1061 mi = rpmdbFreeIterator(mi);
1064 /* Count no. of files (if any). */
1065 totalFileCount += fc;
1068 pi = rpmtsiFree(pi);
1071 /* The ordering doesn't matter here */
1072 pi = rpmtsiInit(ts);
1073 while ((p = rpmtsiNext(pi, TR_REMOVED)) != NULL) {
1076 if ((fi = rpmtsiFi(pi)) == NULL)
1077 continue; /* XXX can't happen */
1080 totalFileCount += fc;
1082 pi = rpmtsiFree(pi);
1084 /* ===============================================
1085 * Initialize transaction element file info for package:
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.
1093 rpmMessage(RPMMESS_DEBUG, _("computing %d file fingerprints\n"), totalFileCount);
1094 pi = rpmtsiInit(ts);
1095 while ((p = rpmtsiNext(pi, 0)) != NULL) {
1098 if ((fi = rpmtsiFi(pi)) == NULL)
1099 continue; /* XXX can't happen */
1103 switch (rpmteType(p)) {
1106 /* Skip netshared paths, not our i18n files, and excluded docs */
1109 /*@switchbreak@*/ break;
1111 fi->record = rpmteDBOffset(p);
1112 /*@switchbreak@*/ break;
1116 fi->fps = (fc > 0 ? xmalloc(fc * sizeof(*fi->fps)) : NULL);
1118 pi = rpmtsiFree(pi);
1120 if (!rpmtsChrootDone(ts)) {
1121 const char * rootDir = rpmtsRootDir(ts);
1123 /*@-superuser -noeffect @*/
1124 if (rootDir != NULL)
1125 xx = chroot(rootDir);
1126 /*@=superuser =noeffect @*/
1127 (void) rpmtsSetChrootDone(ts, 1);
1130 ts->ht = htCreate(totalFileCount * 2, 0, 0, fpHashFunction, fpEqual);
1131 fpc = fpCacheCreate(totalFileCount);
1133 /* ===============================================
1134 * Add fingerprint for each file not skipped.
1136 pi = rpmtsiInit(ts);
1137 while ((p = rpmtsiNext(pi, 0)) != NULL) {
1140 if ((fi = rpmtsiFi(pi)) == NULL)
1141 continue; /* XXX can't happen */
1144 fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fc, fi->fps);
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@*/
1157 pi = rpmtsiFree(pi);
1159 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_START, 6, ts->orderCount,
1160 NULL, ts->notifyData));
1162 /* ===============================================
1163 * Compute file disposition for each package in transaction set.
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;
1173 if ((fi = rpmtsiFi(pi)) == NULL)
1174 continue; /* XXX can't happen */
1177 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_PROGRESS, rpmtsiOc(pi),
1178 ts->orderCount, NULL, ts->notifyData));
1180 if (fc == 0) continue;
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)");
1187 return 1; /* XXX WTFO? */
1191 fi = rpmfiInit(fi, 0);
1192 while ((i = rpmfiNext(fi)) >= 0)
1193 numShared += dbiIndexSetCount(matches[i]);
1195 /* Build sorted file info list for this package. */
1196 shared = sharedList = xcalloc((numShared + 1), sizeof(*sharedList));
1198 fi = rpmfiInit(fi, 0);
1199 while ((i = rpmfiNext(fi)) >= 0) {
1201 * Take care not to mark files as replaced in packages that will
1202 * have been removed before we will get here.
1204 for (j = 0; j < dbiIndexSetCount(matches[i]); j++) {
1206 ro = dbiIndexRecordOffset(matches[i], j);
1208 qi = rpmtsiInit(ts);
1209 while ((q = rpmtsiNext(qi, TR_REMOVED)) != NULL) {
1211 /*@innerbreak@*/ break;
1212 if (rpmteDBOffset(q) == ro)
1215 qi = rpmtsiFree(qi);
1217 shared->pkgFileNum = i;
1218 shared->otherPkg = dbiIndexRecordOffset(matches[i], j);
1219 shared->otherFileNum = dbiIndexRecordFileNumber(matches[i], j);
1220 shared->isRemoved = (knownBad == ro);
1223 matches[i] = dbiFreeIndexSet(matches[i]);
1225 numShared = shared - sharedList;
1226 shared->otherPkg = -1;
1227 matches = _free(matches);
1229 /* Sort file info by other package index (otherPkg) */
1230 qsort(sharedList, numShared, sizeof(*shared), sharedCmp);
1232 /* For all files from this package that are in the database ... */
1234 for (i = 0; i < numShared; i = nexti) {
1237 shared = sharedList + i;
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;
1245 /* Is this file from a package being removed? */
1247 if (ts->removedPackages != NULL)
1248 for (j = 0; j < ts->numRemovedPackages; j++) {
1249 if (ts->removedPackages[j] != shared->otherPkg)
1250 /*@innercontinue@*/ continue;
1252 /*@innerbreak@*/ break;
1255 /* Determine the fate of each file. */
1256 switch (rpmteType(p)) {
1258 xx = handleInstInstalledFiles(ts, p, fi, shared, nexti - i,
1259 !(beingRemoved || (rpmtsFilterFlags(ts) & RPMPROB_FILTER_REPLACEOLDFILES)));
1260 /*@switchbreak@*/ break;
1263 xx = handleRmvdInstalledFiles(ts, fi, shared, nexti - i);
1264 /*@switchbreak@*/ break;
1271 /* Update disk space needs on each partition for this package. */
1272 handleOverlappedFiles(ts, p, fi);
1274 /* Check added package has sufficient space on each partition used. */
1275 switch (rpmteType(p)) {
1277 rpmtsCheckDSIProblems(ts, p);
1278 /*@switchbreak@*/ break;
1280 /*@switchbreak@*/ break;
1283 pi = rpmtsiFree(pi);
1286 if (rpmtsChrootDone(ts)) {
1287 const char * currDir = rpmtsCurrDir(ts);
1288 /*@-superuser -noeffect @*/
1290 /*@=superuser =noeffect @*/
1291 (void) rpmtsSetChrootDone(ts, 0);
1292 if (currDir != NULL)
1293 xx = chdir(currDir);
1296 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_STOP, 6, ts->orderCount,
1297 NULL, ts->notifyData));
1299 /* ===============================================
1300 * Free unused memory as soon as possible.
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)
1308 fi->fps = _free(fi->fps);
1310 pi = rpmtsiFree(pi);
1312 fpc = fpCacheFree(fpc);
1313 ts->ht = htFree(ts->ht);
1315 /* ===============================================
1316 * If unfiltered problems exist, free memory and return.
1318 if ((rpmtsFlags(ts) & RPMTRANS_FLAG_BUILD_PROBS)
1319 || (ts->probs->numProblems &&
1320 (okProbs != NULL || rpmpsTrim(ts->probs, okProbs)))
1323 if (psm->ts != NULL)
1324 psm->ts = rpmtsUnlink(psm->ts, "tsRun (problems)");
1325 return ts->orderCount;
1328 /* ===============================================
1329 * Save removed files before erasing.
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) {
1336 switch (rpmteType(p)) {
1338 /*@switchbreak@*/ break;
1340 if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_REPACKAGE))
1341 /*@switchbreak@*/ break;
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");
1352 /*@switchbreak@*/ break;
1355 pi = rpmtsiFree(pi);
1358 /* ===============================================
1359 * Install and remove packages.
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) {
1370 if ((fi = rpmtsiFi(pi)) == NULL)
1371 continue; /* XXX can't happen */
1374 psm->fi = rpmfiLink(fi, "tsInstall");
1375 switch (rpmteType(p)) {
1378 pkgKey = rpmteAddedKey(p);
1380 rpmMessage(RPMMESS_DEBUG, "========== +++ %s\n", rpmteNEVR(p));
1382 /*@-type@*/ /* FIX: rpmte not opaque */
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) {
1391 rpmrc = rpmReadPackageFile(ts, rpmteFd(p),
1394 if (!(rpmrc == RPMRC_OK || rpmrc == RPMRC_BADSIZE)) {
1395 /*@-noeffectuncon@*/ /* FIX: notify annotations */
1396 p->fd = ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE,
1398 rpmteKey(p), ts->notifyData);
1399 /*@=noeffectuncon@*/
1403 if (rpmteFd(p) != NULL) gotfd = 1;
1408 if (rpmteFd(p) != NULL) {
1410 char * fstates = fi->fstates;
1411 fileAction * actions = fi->actions;
1415 psm->fi = rpmfiFree(psm->fi, 1);
1416 (void) rpmfiFree(fi, 0);
1418 fi->magic = RPMFIMAGIC;
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;
1430 if (rpmteMultiLib(p))
1431 (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) | RPMTRANS_FLAG_MULTILIB));
1433 (void) rpmtsSetFlags(ts, (rpmtsFlags(ts) & ~RPMTRANS_FLAG_MULTILIB));
1435 if (psmStage(psm, PSM_PKGINSTALL)) {
1439 fi->h = headerFree(fi->h);
1445 p->h = headerFree(p->h);
1448 /*@-noeffectuncon @*/ /* FIX: check rc */
1449 (void) ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE, 0, 0,
1450 rpmteKey(p), ts->notifyData);
1451 /*@=noeffectuncon @*/
1456 /*@switchbreak@*/ break;
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))
1464 /*@switchbreak@*/ break;
1466 xx = rpmdbSync(rpmtsGetRdb(ts));
1467 (void) rpmfiUnlink(psm->fi, "tsInstall");
1470 /*@-type@*/ /* FIX: p is almost opaque */
1471 p->fi = rpmfiFree(fi, 1);
1475 pi = rpmtsiFree(pi);
1477 psm->ts = rpmtsUnlink(psm->ts, "tsRun");
1479 /*@-nullstate@*/ /* FIX: ts->flList may be NULL */