2 * \file lib/transaction.c
7 #define _NEED_TEITERATOR 1
11 #include <rpmmacro.h> /* XXX for rpmExpand */
14 #include "legacy.h" /* XXX mdfile */
15 #include "misc.h" /* XXX stripTrailingChar, splitString, currentDirectory */
18 /*@-redecl -exportheadervar@*/
20 extern const char * chroot_prefix;
21 /*@=redecl =exportheadervar@*/
23 /* XXX FIXME: merge with existing (broken?) tests in system.h */
24 /* portability fiddles */
25 #if STATFS_IN_SYS_STATVFS
27 # include <sys/statvfs.h>
28 #if defined(__LCLINT__)
29 /*@-declundef -exportheader -protoparammatch @*/ /* LCL: missing annotation */
30 extern int statvfs (const char * file, /*@out@*/ struct statvfs * buf)
31 /*@globals fileSystem @*/
32 /*@modifies *buf, fileSystem @*/;
33 /*@=declundef =exportheader =protoparammatch @*/
37 # if STATFS_IN_SYS_VFS
40 # if STATFS_IN_SYS_MOUNT
41 # include <sys/mount.h>
43 # if STATFS_IN_SYS_STATFS
44 # include <sys/statfs.h>
52 /*@access FD_t@*/ /* XXX compared with NULL */
53 /*@access Header@*/ /* XXX compared with NULL */
54 /*@access rpmProblemSet@*/ /* XXX need rpmProblemSetOK() */
55 /*@access dbiIndexSet@*/
61 /*@access teIterator@*/
62 /*@access transactionElement@*/
63 /*@access rpmTransactionSet@*/
67 struct diskspaceInfo {
68 dev_t dev; /*!< file system device number. */
69 signed long bneeded; /*!< no. of blocks needed. */
70 signed long ineeded; /*!< no. of inodes needed. */
71 int bsize; /*!< file system block size. */
72 signed long bavail; /*!< no. of blocks available. */
73 signed long iavail; /*!< no. of inodes available. */
77 * Adjust for root only reserved space. On linux e2fs, this is 5%.
79 #define adj_fs_blocks(_nb) (((_nb) * 21) / 20)
81 /* argon thought a shift optimization here was a waste of time... he's
83 #define BLOCK_ROUND(size, block) (((size) + (block) - 1) / (block))
87 static /*@null@*/ void * freeFl(rpmTransactionSet ts,
88 /*@only@*/ /*@null@*/ TFI_t flList)
95 /*@-usereleased -onlytrans @*/ /* FIX: fi needs to be only */
96 for (oc = 0, fi = flList; oc < ts->orderCount; oc++, fi++)
98 flList = _free(flList);
99 /*@=usereleased =onlytrans @*/
104 void rpmtransSetScriptFd(rpmTransactionSet ts, FD_t fd)
106 /*@-type@*/ /* FIX: cast? */
107 ts->scriptFd = (fd ? fdLink(fd, "rpmtransSetScriptFd") : NULL);
111 int rpmtransGetKeys(const rpmTransactionSet ts, const void *** ep, int * nep)
115 if (nep) *nep = ts->orderCount;
120 *ep = e = xmalloc(ts->orderCount * sizeof(*e));
121 for (oc = 0; oc < ts->orderCount; oc++, e++) {
122 switch (ts->order[oc].type) {
124 *e = alGetKey(ts->addedPackages, ts->order[oc].u.addedIndex);
125 /*@switchbreak@*/ break;
128 /*@-mods@*/ /* FIX: double indirection. */
131 /*@switchbreak@*/ break;
140 static int archOkay(Header h)
146 /* make sure we're trying to install this on the proper architecture */
147 (void) headerGetEntry(h, RPMTAG_ARCH, &type, (void **) &pkgArch, &count);
149 if (type == RPM_INT8_TYPE) {
153 /* old arch handling */
154 rpmGetArchInfo(NULL, &archNum);
155 pkgArchNum = pkgArch;
156 if (archNum != *pkgArchNum) {
162 /* new arch handling */
163 if (!rpmMachineScore(RPM_MACHTABLE_INSTARCH, pkgArch)) {
173 static int osOkay(Header h)
179 /* make sure we're trying to install this on the proper os */
180 (void) headerGetEntry(h, RPMTAG_OS, &type, (void **) &pkgOs, &count);
182 if (type == RPM_INT8_TYPE) {
183 /* v1 packages and v2 packages both used improper OS numbers, so just
184 deal with it hope things work */
189 /* new os handling */
190 if (!rpmMachineScore(RPM_MACHTABLE_INSTOS, pkgOs)) {
200 static int sharedCmp(const void * one, const void * two)
203 const struct sharedFileInfo * a = one;
204 const struct sharedFileInfo * b = two;
206 if (a->otherPkg < b->otherPkg)
208 else if (a->otherPkg > b->otherPkg)
216 static fileAction decideFileFate(const char * dirName,
217 const char * baseName, short dbMode,
218 const char * dbMd5, const char * dbLink, short newMode,
219 const char * newMd5, const char * newLink, int newFlags,
220 rpmtransFlags transFlags)
221 /*@globals fileSystem @*/
222 /*@modifies fileSystem @*/
225 const char * dbAttr, * newAttr;
226 fileTypes dbWhat, newWhat, diskWhat;
229 int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
230 char * filespec = alloca(strlen(dirName) + strlen(baseName) + 1);
232 (void) stpcpy( stpcpy(filespec, dirName), baseName);
234 if (lstat(filespec, &sb)) {
236 * The file doesn't exist on the disk. Create it unless the new
237 * package has marked it as missingok, or allfiles is requested.
239 if (!(transFlags & RPMTRANS_FLAG_ALLFILES) &&
240 (newFlags & RPMFILE_MISSINGOK)) {
241 rpmMessage(RPMMESS_DEBUG, _("%s skipped due to missingok flag\n"),
249 diskWhat = whatis(sb.st_mode);
250 dbWhat = whatis(dbMode);
251 newWhat = whatis(newMode);
253 /* RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
254 them in older packages as well */
255 if (newWhat == XDIR) {
259 if (diskWhat != newWhat) {
261 } else if (newWhat != dbWhat && diskWhat != dbWhat) {
263 } else if (dbWhat != newWhat) {
265 } else if (dbWhat != LINK && dbWhat != REG) {
270 rc = mdfile(filespec, buffer);
273 /* assume the file has been removed, don't freak */
278 } else /* dbWhat == LINK */ {
279 memset(buffer, 0, sizeof(buffer));
280 i = readlink(filespec, buffer, sizeof(buffer) - 1);
282 /* assume the file has been removed, don't freak */
289 /* this order matters - we'd prefer to CREATE the file if at all
290 possible in case something else (like the timestamp) has changed */
292 if (!strcmp(dbAttr, buffer)) {
293 /* this config file has never been modified, so just replace it */
297 if (!strcmp(dbAttr, newAttr)) {
298 /* this file is the same in all versions of this package */
303 * The config file on the disk has been modified, but
304 * the ones in the two packages are different. It would
305 * be nice if RPM was smart enough to at least try and
306 * merge the difference ala CVS, but...
313 static int filecmp(short mode1, const char * md51, const char * link1,
314 short mode2, const char * md52, const char * link2)
317 fileTypes what1 = whatis(mode1);
318 fileTypes what2 = whatis(mode2);
320 if (what1 != what2) return 1;
323 return strcmp(link1, link2);
324 else if (what1 == REG)
325 return strcmp(md51, md52);
332 /* XXX only ts->{probs,rpmdb} modified */
333 static int handleInstInstalledFiles(const rpmTransactionSet ts, TFI_t fi,
334 struct sharedFileInfo * shared,
335 int sharedCount, int reportConflicts)
336 /*@globals fileSystem @*/
337 /*@modifies ts, fi, fileSystem @*/
340 HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
341 rpmtransFlags transFlags = ts->transFlags;
342 rpmTagType oltype, omtype;
345 const char ** otherMd5s;
346 const char ** otherLinks;
347 const char * otherStates;
348 uint_32 * otherFlags;
349 uint_32 * otherSizes;
350 uint_16 * otherModes;
354 rpmdbMatchIterator mi;
356 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES, &shared->otherPkg, sizeof(shared->otherPkg));
357 h = rpmdbNextIterator(mi);
359 mi = rpmdbFreeIterator(mi);
363 xx = hge(h, RPMTAG_FILEMD5S, &omtype, (void **) &otherMd5s, NULL);
364 xx = hge(h, RPMTAG_FILELINKTOS, &oltype, (void **) &otherLinks, NULL);
365 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
366 xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &otherModes, NULL);
367 xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &otherFlags, NULL);
368 xx = hge(h, RPMTAG_FILESIZES, NULL, (void **) &otherSizes, NULL);
370 fi->replaced = xmalloc(sharedCount * sizeof(*fi->replaced));
372 for (i = 0; i < sharedCount; i++, shared++) {
373 int otherFileNum, fileNum;
374 otherFileNum = shared->otherFileNum;
375 fileNum = shared->pkgFileNum;
377 /* XXX another tedious segfault, assume file state normal. */
378 if (otherStates && otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
381 if (XFA_SKIPPING(fi->actions[fileNum]))
384 if (filecmp(otherModes[otherFileNum],
385 otherMd5s[otherFileNum],
386 otherLinks[otherFileNum],
389 fi->flinks[fileNum])) {
390 /*@-compdef@*/ /* FIX: *fi->replaced undefined */
392 rpmProblemSetAppend(ts->probs, RPMPROB_FILE_CONFLICT,
393 fiGetNVR(fi), fi->key,
394 fi->dnl[fi->dil[fileNum]], fi->bnl[fileNum],
398 if (!(otherFlags[otherFileNum] | fi->fflags[fileNum])
401 if (!shared->isRemoved)
402 fi->replaced[numReplaced++] = *shared;
407 if ((otherFlags[otherFileNum] | fi->fflags[fileNum]) & RPMFILE_CONFIG) {
408 fi->actions[fileNum] = decideFileFate(
409 fi->dnl[fi->dil[fileNum]],
411 otherModes[otherFileNum],
412 otherMd5s[otherFileNum],
413 otherLinks[otherFileNum],
421 fi->replacedSizes[fileNum] = otherSizes[otherFileNum];
424 otherMd5s = hfd(otherMd5s, omtype);
425 otherLinks = hfd(otherLinks, oltype);
426 mi = rpmdbFreeIterator(mi);
428 fi->replaced = xrealloc(fi->replaced, /* XXX memory leak */
429 sizeof(*fi->replaced) * (numReplaced + 1));
430 fi->replaced[numReplaced].otherPkg = 0;
437 /* XXX only ts->rpmdb modified */
438 static int handleRmvdInstalledFiles(const rpmTransactionSet ts, TFI_t fi,
439 struct sharedFileInfo * shared, int sharedCount)
440 /*@globals fileSystem @*/
441 /*@modifies fi, fileSystem @*/
445 const char * otherStates;
448 rpmdbMatchIterator mi;
450 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
451 &shared->otherPkg, sizeof(shared->otherPkg));
452 h = rpmdbNextIterator(mi);
454 mi = rpmdbFreeIterator(mi);
458 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
460 for (i = 0; i < sharedCount; i++, shared++) {
461 int otherFileNum, fileNum;
462 otherFileNum = shared->otherFileNum;
463 fileNum = shared->pkgFileNum;
465 if (otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
468 fi->actions[fileNum] = FA_SKIP;
471 mi = rpmdbFreeIterator(mi);
477 * Update disk space needs on each partition for this package.
479 /* XXX only ts->{probs,di} modified */
480 static void handleOverlappedFiles(const rpmTransactionSet ts, TFI_t fi)
481 /*@globals fileSystem @*/
482 /*@modifies ts, fi, fileSystem @*/
484 struct diskspaceInfo * ds = NULL;
485 uint_32 fixupSize = 0;
486 char * filespec = NULL;
487 int fileSpecAlloced = 0;
490 for (i = 0; i < fi->fc; i++) {
491 int otherPkgNum, otherFileNum;
495 if (XFA_SKIPPING(fi->actions[i]))
498 j = strlen(fi->dnl[fi->dil[i]]) + strlen(fi->bnl[i]) + 1;
500 if (j > fileSpecAlloced) {
501 fileSpecAlloced = j * 2;
502 filespec = xrealloc(filespec, fileSpecAlloced);
506 (void) stpcpy( stpcpy( filespec, fi->dnl[fi->dil[i]]), fi->bnl[i]);
510 while (ds->bsize && ds->dev != fi->fps[i].entry->dev) ds++;
511 if (!ds->bsize) ds = NULL;
516 * Retrieve all records that apply to this file. Note that the
517 * file info records were built in the same order as the packages
518 * will be installed and removed so the records for an overlapped
519 * files will be sorted in exactly the same order.
521 (void) htGetEntry(ts->ht, &fi->fps[i],
522 (const void ***) &recs, &numRecs, NULL);
525 * If this package is being added, look only at other packages
526 * being added -- removed packages dance to a different tune.
527 * If both this and the other package are being added, overlapped
528 * files must be identical (or marked as a conflict). The
529 * disposition of already installed config files leads to
530 * a small amount of extra complexity.
532 * If this package is being removed, then there are two cases that
533 * need to be worried about:
534 * If the other package is being added, then skip any overlapped files
535 * so that this package removal doesn't nuke the overlapped files
536 * that were just installed.
537 * If both this and the other package are being removed, then each
538 * file removal from preceding packages needs to be skipped so that
539 * the file removal occurs only on the last occurence of an overlapped
540 * file in the transaction set.
544 /* Locate this overlapped file in the set of added/removed packages. */
545 for (j = 0; j < numRecs && recs[j] != fi; j++)
548 /* Find what the previous disposition of this file was. */
549 otherFileNum = -1; /* keep gcc quiet */
550 for (otherPkgNum = j - 1; otherPkgNum >= 0; otherPkgNum--) {
551 /* Added packages need only look at other added packages. */
552 if (fi->type == TR_ADDED && recs[otherPkgNum]->type != TR_ADDED)
553 /*@innercontinue@*/ continue;
555 /* TESTME: there are more efficient searches in the world... */
556 for (otherFileNum = 0; otherFileNum < recs[otherPkgNum]->fc;
559 /* If the addresses are the same, so are the values. */
560 if ((fi->fps + i) == (recs[otherPkgNum]->fps + otherFileNum))
561 /*@innerbreak@*/ break;
563 /* Otherwise, compare fingerprints by value. */
564 /*@-nullpass@*/ /* LCL: looks good to me */
565 if (FP_EQUAL(fi->fps[i], recs[otherPkgNum]->fps[otherFileNum]))
566 /*@innerbreak@*/ break;
570 /* XXX is this test still necessary? */
571 if (recs[otherPkgNum]->actions[otherFileNum] != FA_UNKNOWN)
572 /*@innerbreak@*/ break;
578 if (otherPkgNum < 0) {
579 /* XXX is this test still necessary? */
580 if (fi->actions[i] != FA_UNKNOWN)
581 /*@switchbreak@*/ break;
582 if ((fi->fflags[i] & RPMFILE_CONFIG) &&
583 !lstat(filespec, &sb)) {
584 /* Here is a non-overlapped pre-existing config file. */
585 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
586 ? FA_ALTNAME : FA_BACKUP;
588 fi->actions[i] = FA_CREATE;
590 /*@switchbreak@*/ break;
593 /* Mark added overlapped non-identical files as a conflict. */
594 if ((ts->ignoreSet & RPMPROB_FILTER_REPLACENEWFILES)
595 && filecmp(recs[otherPkgNum]->fmodes[otherFileNum],
596 recs[otherPkgNum]->fmd5s[otherFileNum],
597 recs[otherPkgNum]->flinks[otherFileNum],
602 rpmProblemSetAppend(ts->probs, RPMPROB_NEW_FILE_CONFLICT,
603 fiGetNVR(fi), fi->key,
605 fiGetNVR(recs[otherPkgNum]),
609 /* Try to get the disk accounting correct even if a conflict. */
610 fixupSize = recs[otherPkgNum]->fsizes[otherFileNum];
612 if ((fi->fflags[i] & RPMFILE_CONFIG) && !lstat(filespec, &sb)) {
613 /* Here is an overlapped pre-existing config file. */
614 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
615 ? FA_ALTNAME : FA_SKIP;
617 fi->actions[i] = FA_CREATE;
619 } /*@switchbreak@*/ break;
621 if (otherPkgNum >= 0) {
622 /* Here is an overlapped added file we don't want to nuke. */
623 if (recs[otherPkgNum]->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 recs[otherPkgNum]->actions[otherFileNum] = FA_SKIP;
631 if (XFA_SKIPPING(fi->actions[i]))
632 /*@switchbreak@*/ break;
633 if (fi->fstates && fi->fstates[i] != RPMFILE_STATE_NORMAL)
634 /*@switchbreak@*/ break;
635 if (!(S_ISREG(fi->fmodes[i]) && (fi->fflags[i] & RPMFILE_CONFIG))) {
636 fi->actions[i] = FA_ERASE;
637 /*@switchbreak@*/ break;
640 /* Here is a pre-existing modified config file that needs saving. */
642 if (!mdfile(filespec, mdsum) && strcmp(fi->fmd5s[i], mdsum)) {
643 fi->actions[i] = FA_BACKUP;
644 /*@switchbreak@*/ break;
647 fi->actions[i] = FA_ERASE;
648 /*@switchbreak@*/ break;
652 uint_32 s = BLOCK_ROUND(fi->fsizes[i], ds->bsize);
654 switch (fi->actions[i]) {
660 /*@switchbreak@*/ break;
663 * FIXME: If two packages share a file (same md5sum), and
664 * that file is being replaced on disk, will ds->bneeded get
665 * decremented twice? Quite probably!
669 ds->bneeded -= BLOCK_ROUND(fi->replacedSizes[i], ds->bsize);
670 /*@switchbreak@*/ break;
675 /*@switchbreak@*/ break;
678 /*@switchbreak@*/ break;
681 ds->bneeded -= BLOCK_ROUND(fixupSize, ds->bsize);
684 filespec = _free(filespec);
689 static int ensureOlder(rpmTransactionSet ts,
690 const Header h, /*@null@*/ const Header old,
691 /*@dependent@*/ /*@null@*/ const void * key)
696 if (old == NULL) return 1;
698 result = rpmVersionCompare(old, h);
701 else if (result > 0) {
703 /*@-evalorder@*/ /* LCL: is confused */
704 rpmProblemSetAppend(ts->probs, RPMPROB_OLDPACKAGE,
705 hGetNVR(h, NULL), key,
717 static void skipFiles(const rpmTransactionSet ts, TFI_t fi)
718 /*@globals rpmGlobalMacroContext @*/
719 /*@modifies fi, rpmGlobalMacroContext @*/
721 int noDocs = (ts->transFlags & RPMTRANS_FLAG_NODOCS);
722 char ** netsharedPaths = NULL;
723 const char ** languages;
724 const char * dn, * bn;
725 int dnlen, bnlen, ix;
732 noDocs = rpmExpandNumeric("%{_excludedocs}");
734 { const char *tmpPath = rpmExpand("%{_netsharedpath}", NULL);
736 if (tmpPath && *tmpPath != '%')
737 netsharedPaths = splitString(tmpPath, strlen(tmpPath), ':');
739 tmpPath = _free(tmpPath);
742 s = rpmExpand("%{_install_langs}", NULL);
744 if (!(s && *s != '%'))
747 languages = (const char **) splitString(s, strlen(s), ':');
753 /* Compute directory refcount, skip directory if now empty. */
754 drc = alloca(fi->dc * sizeof(*drc));
755 memset(drc, 0, fi->dc * sizeof(*drc));
756 dff = alloca(fi->dc * sizeof(*dff));
757 memset(dff, 0, fi->dc * sizeof(*dff));
759 for (i = 0; i < fi->fc; i++) {
770 /* Don't bother with skipped files */
771 if (XFA_SKIPPING(fi->actions[i])) {
777 * Skip net shared paths.
778 * Net shared paths are not relative to the current root (though
779 * they do need to take package relocations into account).
781 for (nsp = netsharedPaths; nsp && *nsp; nsp++) {
786 if (strncmp(dn, *nsp, len))
787 /*@innercontinue@*/ continue;
788 /* Only directories or complete file paths can be net shared */
789 if (!(dn[len] == '/' || dn[len] == '\0'))
790 /*@innercontinue@*/ continue;
792 if (len < (dnlen + bnlen))
793 /*@innercontinue@*/ continue;
794 if (strncmp(dn, *nsp, dnlen))
795 /*@innercontinue@*/ continue;
796 if (strncmp(bn, (*nsp) + dnlen, bnlen))
797 /*@innercontinue@*/ continue;
799 /* Only directories or complete file paths can be net shared */
800 if (!((*nsp)[len] == '/' || (*nsp)[len] == '\0'))
801 /*@innercontinue@*/ continue;
804 /*@innerbreak@*/ break;
808 drc[ix]--; dff[ix] = 1;
809 fi->actions[i] = FA_SKIPNETSHARED;
814 * Skip i18n language specific files.
816 if (fi->flangs && languages && *fi->flangs[i]) {
817 const char **lang, *l, *le;
818 for (lang = languages; *lang != NULL; lang++) {
819 if (!strcmp(*lang, "all"))
820 /*@innerbreak@*/ break;
821 for (l = fi->flangs[i]; *l != '\0'; l = le) {
822 for (le = l; *le != '\0' && *le != '|'; le++)
824 if ((le-l) > 0 && !strncmp(*lang, l, (le-l)))
825 /*@innerbreak@*/ break;
826 if (*le == '|') le++; /* skip over | */
829 /*@innerbreak@*/ break;
832 drc[ix]--; dff[ix] = 1;
833 fi->actions[i] = FA_SKIPNSTATE;
839 * Skip documentation if requested.
841 if (noDocs && (fi->fflags[i] & RPMFILE_DOC)) {
842 drc[ix]--; dff[ix] = 1;
843 fi->actions[i] = FA_SKIPNSTATE;
848 /* Skip (now empty) directories that had skipped files. */
849 for (j = 0; j < fi->dc; j++) {
851 if (drc[j]) continue; /* dir still has files. */
852 if (!dff[j]) continue; /* dir was not emptied here. */
854 /* Find parent directory and basename. */
855 dn = fi->dnl[j]; dnlen = strlen(dn) - 1;
856 bn = dn + dnlen; bnlen = 0;
857 while (bn > dn && bn[-1] != '/') {
863 /* If explicitly included in the package, skip the directory. */
864 for (i = 0; i < fi->fc; i++) {
867 if (XFA_SKIPPING(fi->actions[i]))
868 /*@innercontinue@*/ continue;
869 if (whatis(fi->fmodes[i]) != XDIR)
870 /*@innercontinue@*/ continue;
871 dir = fi->dnl[fi->dil[i]];
872 if (strlen(dir) != dnlen)
873 /*@innercontinue@*/ continue;
874 if (strncmp(dir, dn, dnlen))
875 /*@innercontinue@*/ continue;
876 if (strlen(fi->bnl[i]) != bnlen)
877 /*@innercontinue@*/ continue;
878 if (strncmp(fi->bnl[i], bn, bnlen))
879 /*@innercontinue@*/ continue;
880 rpmMessage(RPMMESS_DEBUG, _("excluding directory %s\n"), dn);
881 fi->actions[i] = FA_SKIPNSTATE;
882 /*@innerbreak@*/ break;
886 if (netsharedPaths) freeSplitString(netsharedPaths);
887 #ifdef DYING /* XXX freeFi will deal with this later. */
888 fi->flangs = _free(fi->flangs);
890 if (languages) freeSplitString((char **)languages);
894 * Return next transaction element's file info.
895 * @param tei transaction element iterator
896 * @return nest transaction element file info, NULL on termination
898 /*@unused@*/ static inline
899 TFI_t teNextFi(teIterator tei)
904 if (teNextIterator(tei) != NULL && tei->ocsave != -1)
905 fi = tei->ts->flList + tei->ocsave;
906 /*@-compdef -onlytrans -usereleased@*/ /* FIX: ts->flList may be released */
908 /*@=compdef =onlytrans =usereleased@*/
911 #define NOTIFY(_ts, _al) if ((_ts)->notify) (void) (_ts)->notify _al
913 int rpmRunTransactions( rpmTransactionSet ts,
914 rpmCallbackFunction notify, rpmCallbackData notifyData,
915 rpmProblemSet okProbs, rpmProblemSet * newProbs,
916 rpmtransFlags transFlags, rpmprobFilterFlags ignoreSet)
920 int totalFileCount = 0;
922 struct diskspaceInfo * dip;
923 struct sharedFileInfo * shared, * sharedList;
928 fingerPrintCache fpc;
933 int keep_header = 1; /* XXX rpmProblemSetAppend prevents dumping headers. */
935 /* FIXME: what if the same package is included in ts twice? */
937 ts->transFlags = transFlags;
938 if (ts->transFlags & RPMTRANS_FLAG_NOSCRIPTS)
939 ts->transFlags |= (_noTransScripts | _noTransTriggers);
940 if (ts->transFlags & RPMTRANS_FLAG_NOTRIGGERS)
941 ts->transFlags |= _noTransTriggers;
943 /* XXX MULTILIB is broken, as packages can and do execute /sbin/ldconfig. */
944 if (ts->transFlags & (RPMTRANS_FLAG_JUSTDB | RPMTRANS_FLAG_MULTILIB))
945 ts->transFlags |= (_noTransScripts | _noTransTriggers);
948 ts->notifyData = notifyData;
950 ts->probs = *newProbs = rpmProblemSetCreate();
952 ts->ignoreSet = ignoreSet;
953 ts->currDir = _free(ts->currDir);
954 ts->currDir = currentDirectory();
956 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
957 ts->id = (int_32) time(NULL);
959 memset(psm, 0, sizeof(*psm));
961 psm->ts = rpmtsLink(ts, "tsRun");
964 /* Get available space on mounted file systems. */
965 if (!(ts->ignoreSet & RPMPROB_FILTER_DISKSPACE) &&
966 !rpmGetFilesystemList(&ts->filesystems, &ts->filesystemCount)) {
969 ts->di = _free(ts->di);
970 dip = ts->di = xcalloc((ts->filesystemCount + 1), sizeof(*ts->di));
972 for (i = 0; (i < ts->filesystemCount) && dip; i++) {
973 #if STATFS_IN_SYS_STATVFS
975 memset(&sfb, 0, sizeof(sfb));
976 if (statvfs(ts->filesystems[i], &sfb))
980 /* This platform has the 4-argument version of the statfs call. The last two
981 * should be the size of struct statfs and 0, respectively. The 0 is the
982 * filesystem type, and is always 0 when statfs is called on a mounted
983 * filesystem, as we're doing.
985 memset(&sfb, 0, sizeof(sfb));
986 if (statfs(ts->filesystems[i], &sfb, sizeof(sfb), 0))
988 memset(&sfb, 0, sizeof(sfb));
989 if (statfs(ts->filesystems[i], &sfb))
995 ts->di[i].bsize = sfb.f_bsize;
996 ts->di[i].bneeded = 0;
997 ts->di[i].ineeded = 0;
998 #ifdef STATFS_HAS_F_BAVAIL
999 ts->di[i].bavail = sfb.f_bavail;
1001 /* FIXME: the statfs struct doesn't have a member to tell how many blocks are
1002 * available for non-superusers. f_blocks - f_bfree is probably too big, but
1003 * it's about all we can do.
1005 ts->di[i].bavail = sfb.f_blocks - sfb.f_bfree;
1007 /* XXX Avoid FAT and other file systems that have not inodes. */
1008 ts->di[i].iavail = !(sfb.f_ffree == 0 && sfb.f_files == 0)
1011 xx = stat(ts->filesystems[i], &sb);
1012 ts->di[i].dev = sb.st_dev;
1016 if (dip) ts->di[i].bsize = 0;
1019 /* ===============================================
1020 * For packages being installed:
1021 * - verify package arch/os.
1022 * - verify package epoch:version-release is newer.
1024 * For packages being removed:
1027 /* The ordering doesn't matter here */
1028 for (i = 0; i < alGetSize(ts->addedPackages); i++) {
1029 const char * n, * v, * r;
1031 rpmdbMatchIterator mi;
1034 h = alGetHeader(ts->addedPackages, i, 0);
1035 if (h == NULL) /* XXX can't happen */
1038 (void) headerNVR(h, &n, &v, &r);
1039 key = alGetKey(ts->addedPackages, i);
1041 if (!archOkay(h) && !(ts->ignoreSet & RPMPROB_FILTER_IGNOREARCH))
1042 rpmProblemSetAppend(ts->probs, RPMPROB_BADARCH,
1043 hGetNVR(h, NULL), key,
1044 NULL, NULL, NULL, 0);
1046 if (!osOkay(h) && !(ts->ignoreSet & RPMPROB_FILTER_IGNOREOS))
1047 rpmProblemSetAppend(ts->probs, RPMPROB_BADOS,
1048 hGetNVR(h, NULL), key,
1049 NULL, NULL, NULL, 0);
1051 if (!(ts->ignoreSet & RPMPROB_FILTER_OLDPACKAGE)) {
1053 mi = rpmtsInitIterator(ts, RPMTAG_NAME, n, 0);
1054 while ((oldH = rpmdbNextIterator(mi)) != NULL)
1055 xx = ensureOlder(ts, h, oldH, key);
1056 mi = rpmdbFreeIterator(mi);
1059 /* XXX multilib should not display "already installed" problems */
1060 if (!(ts->ignoreSet & RPMPROB_FILTER_REPLACEPKG)
1061 #ifdef DYING /* XXX MULTILIB multiLib from transactionElement */
1062 && !alGetMultiLib(ts->addedPackages, i)
1065 mi = rpmtsInitIterator(ts, RPMTAG_NAME, n, 0);
1066 xx = rpmdbSetIteratorRE(mi, RPMTAG_VERSION, RPMMIRE_DEFAULT, v);
1067 xx = rpmdbSetIteratorRE(mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT, r);
1069 while (rpmdbNextIterator(mi) != NULL) {
1070 rpmProblemSetAppend(ts->probs, RPMPROB_PKG_INSTALLED,
1071 hGetNVR(h, NULL), key,
1072 NULL, NULL, NULL, 0);
1073 /*@innerbreak@*/ break;
1075 mi = rpmdbFreeIterator(mi);
1078 totalFileCount += alGetFilesCount(ts->addedPackages, i);
1080 h = headerFree(h, "alGetHeader (rpmtsRun sanity)");
1084 /* FIXME: it seems a bit silly to read in all of these headers twice */
1085 /* The ordering doesn't matter here */
1086 if (ts->numRemovedPackages > 0) {
1087 rpmdbMatchIterator mi;
1091 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES, NULL, 0);
1092 xx = rpmdbAppendIterator(mi, ts->removedPackages, ts->numRemovedPackages);
1093 while ((h = rpmdbNextIterator(mi)) != NULL) {
1094 if (headerGetEntry(h, RPMTAG_BASENAMES, NULL, NULL, &fileCount))
1095 totalFileCount += fileCount;
1097 mi = rpmdbFreeIterator(mi);
1100 /* ===============================================
1101 * Initialize transaction element file info for package:
1103 ts->flEntries = alGetSize(ts->addedPackages) + ts->numRemovedPackages;
1104 ts->flList = xcalloc(ts->flEntries, sizeof(*ts->flList));
1107 * FIXME?: we'd be better off assembling one very large file list and
1108 * calling fpLookupList only once. I'm not sure that the speedup is
1109 * worth the trouble though.
1111 tei = teInitIterator(ts);
1112 while ((fi = teNextFi(tei)) != NULL) {
1114 fi->magic = TFIMAGIC;
1116 fi->type = ts->order[oc].type;
1123 i = ts->order[oc].u.addedIndex;
1125 fi->h = alGetHeader(ts->addedPackages, i, 1);
1126 #ifdef DYING /* XXX MULTILIB multiLib from transactionElement */
1127 fi->multiLib = alGetMultiLib(ts->addedPackages, i);
1129 fi->multiLib = ts->order[oc].multiLib;
1132 fi->key = alGetKey(ts->addedPackages, i);
1134 fi->relocs = alGetRelocs(ts->addedPackages, i);
1135 fi->fd = alGetFd(ts->addedPackages, i);
1137 /* XXX availablePackage can be dumped here XXX */
1139 /* XXX header arg unused. */
1140 loadFi(ts, fi, fi->h, keep_header);
1145 /* Skip netshared paths, not our i18n files, and excluded docs */
1147 /*@switchbreak@*/ break;
1149 fi->record = ts->order[oc].u.removed.dboffset;
1150 /* Retrieve erased package header from the database. */
1151 { rpmdbMatchIterator mi;
1153 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
1154 &fi->record, sizeof(fi->record));
1155 if ((fi->h = rpmdbNextIterator(mi)) != NULL)
1156 fi->h = headerLink(fi->h, "TR_REMOVED loadFi");
1157 mi = rpmdbFreeIterator(mi);
1159 if (fi->h == NULL) {
1163 /* XXX header arg unused. */
1164 loadFi(ts, fi, fi->h, 0);
1165 /*@switchbreak@*/ break;
1170 fi->fps = xmalloc(fi->fc * sizeof(*fi->fps));
1172 tei = teFreeIterator(tei);
1174 if (!ts->chrootDone) {
1176 /*@-superuser -noeffect @*/
1177 xx = chroot(ts->rootDir);
1178 /*@=superuser =noeffect @*/
1180 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 1;
1183 chroot_prefix = ts->rootDir;
1188 ts->ht = htCreate(totalFileCount * 2, 0, 0, fpHashFunction, fpEqual);
1189 fpc = fpCacheCreate(totalFileCount);
1191 /* ===============================================
1192 * Add fingerprint for each file not skipped.
1194 tei = teInitIterator(ts);
1195 while ((fi = teNextFi(tei)) != NULL) {
1196 fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fi->fc, fi->fps);
1197 for (i = 0; i < fi->fc; i++) {
1198 if (XFA_SKIPPING(fi->actions[i]))
1199 /*@innercontinue@*/ continue;
1200 /*@-dependenttrans@*/
1201 htAddEntry(ts->ht, fi->fps + i, fi);
1202 /*@=dependenttrans@*/
1205 tei = teFreeIterator(tei);
1207 /*@-noeffectuncon @*/ /* FIX: check rc */
1208 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_START, 6, ts->flEntries,
1209 NULL, ts->notifyData));
1210 /*@=noeffectuncon@*/
1212 /* ===============================================
1213 * Compute file disposition for each package in transaction set.
1215 tei = teInitIterator(ts);
1216 while ((fi = teNextFi(tei)) != NULL) {
1217 dbiIndexSet * matches;
1220 /*@-noeffectuncon @*/ /* FIX: check rc */
1221 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_PROGRESS, (fi - ts->flList),
1222 ts->flEntries, NULL, ts->notifyData));
1223 /*@=noeffectuncon@*/
1225 if (fi->fc == 0) continue;
1227 /* Extract file info for all files in this package from the database. */
1228 matches = xcalloc(fi->fc, sizeof(*matches));
1229 if (rpmdbFindFpList(ts->rpmdb, fi->fps, matches, fi->fc)) {
1230 psm->ts = rpmtsUnlink(ts, "tsRun (rpmFindFpList fail)");
1231 return 1; /* XXX WTFO? */
1235 for (i = 0; i < fi->fc; i++)
1236 numShared += dbiIndexSetCount(matches[i]);
1238 /* Build sorted file info list for this package. */
1239 shared = sharedList = xcalloc((numShared + 1), sizeof(*sharedList));
1240 for (i = 0; i < fi->fc; i++) {
1242 * Take care not to mark files as replaced in packages that will
1243 * have been removed before we will get here.
1245 for (j = 0; j < dbiIndexSetCount(matches[i]); j++) {
1247 ro = dbiIndexRecordOffset(matches[i], j);
1249 for (k = 0; ro != knownBad && k < ts->orderCount; k++) {
1250 switch (ts->order[k].type) {
1252 if (ts->order[k].u.removed.dboffset == ro)
1254 /*@switchbreak@*/ break;
1256 /*@switchbreak@*/ break;
1260 shared->pkgFileNum = i;
1261 shared->otherPkg = dbiIndexRecordOffset(matches[i], j);
1262 shared->otherFileNum = dbiIndexRecordFileNumber(matches[i], j);
1263 shared->isRemoved = (knownBad == ro);
1266 matches[i] = dbiFreeIndexSet(matches[i]);
1268 numShared = shared - sharedList;
1269 shared->otherPkg = -1;
1270 matches = _free(matches);
1272 /* Sort file info by other package index (otherPkg) */
1273 qsort(sharedList, numShared, sizeof(*shared), sharedCmp);
1275 /* For all files from this package that are in the database ... */
1276 for (i = 0; i < numShared; i = nexti) {
1279 shared = sharedList + i;
1281 /* Find the end of the files in the other package. */
1282 for (nexti = i + 1; nexti < numShared; nexti++) {
1283 if (sharedList[nexti].otherPkg != shared->otherPkg)
1284 /*@innerbreak@*/ break;
1287 /* Is this file from a package being removed? */
1289 for (j = 0; j < ts->numRemovedPackages; j++) {
1290 if (ts->removedPackages[j] != shared->otherPkg)
1291 /*@innercontinue@*/ continue;
1293 /*@innerbreak@*/ break;
1296 /* Determine the fate of each file. */
1299 xx = handleInstInstalledFiles(ts, fi, shared, nexti - i,
1300 !(beingRemoved || (ts->ignoreSet & RPMPROB_FILTER_REPLACEOLDFILES)));
1301 /*@switchbreak@*/ break;
1304 xx = handleRmvdInstalledFiles(ts, fi, shared, nexti - i);
1305 /*@switchbreak@*/ break;
1311 /* Update disk space needs on each partition for this package. */
1312 handleOverlappedFiles(ts, fi);
1314 /* Check added package has sufficient space on each partition used. */
1317 if (!(ts->di && fi->fc))
1318 /*@switchbreak@*/ break;
1319 for (i = 0; i < ts->filesystemCount; i++) {
1323 /* XXX Avoid FAT and other file systems that have not inodes. */
1324 if (dip->iavail <= 0)
1325 /*@innercontinue@*/ continue;
1327 if (adj_fs_blocks(dip->bneeded) > dip->bavail)
1328 rpmProblemSetAppend(ts->probs, RPMPROB_DISKSPACE,
1329 fiGetNVR(fi), fi->key,
1330 ts->filesystems[i], NULL, NULL,
1331 (adj_fs_blocks(dip->bneeded) - dip->bavail) * dip->bsize);
1333 if (adj_fs_blocks(dip->ineeded) > dip->iavail)
1334 rpmProblemSetAppend(ts->probs, RPMPROB_DISKNODES,
1335 fiGetNVR(fi), fi->key,
1336 ts->filesystems[i], NULL, NULL,
1337 (adj_fs_blocks(dip->ineeded) - dip->iavail));
1339 /*@switchbreak@*/ break;
1341 /*@switchbreak@*/ break;
1344 tei = teFreeIterator(tei);
1346 if (ts->chrootDone) {
1347 /*@-superuser -noeffect @*/
1349 /*@=superuser =noeffect @*/
1351 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
1353 chroot_prefix = NULL;
1355 xx = chdir(ts->currDir);
1358 /*@-noeffectuncon @*/ /* FIX: check rc */
1359 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_STOP, 6, ts->flEntries,
1360 NULL, ts->notifyData));
1361 /*@=noeffectuncon @*/
1363 /* ===============================================
1364 * Free unused memory as soon as possible.
1367 tei = teInitIterator(ts);
1368 while ((fi = teNextFi(tei)) != NULL) {
1371 fi->fps = _free(fi->fps);
1373 tei = teFreeIterator(tei);
1379 /* ===============================================
1380 * If unfiltered problems exist, free memory and return.
1382 if ((ts->transFlags & RPMTRANS_FLAG_BUILD_PROBS)
1383 || (ts->probs->numProblems &&
1384 (okProbs != NULL || rpmProblemSetTrim(ts->probs, okProbs)))
1387 *newProbs = ts->probs;
1389 ts->flList = freeFl(ts, ts->flList);
1391 if (psm->ts != NULL)
1392 psm->ts = rpmtsUnlink(psm->ts, "tsRun (problems)");
1393 /*@-nullstate@*/ /* FIX: ts->flList may be NULL */
1394 return ts->orderCount;
1398 /* ===============================================
1399 * Save removed files before erasing.
1401 if (ts->transFlags & (RPMTRANS_FLAG_DIRSTASH | RPMTRANS_FLAG_REPACKAGE)) {
1402 tei = teInitIterator(ts);
1403 while ((fi = teNextFi(tei)) != NULL) {
1406 /*@switchbreak@*/ break;
1408 if (ts->transFlags & RPMTRANS_FLAG_REPACKAGE) {
1409 psm->fi = rpmfiLink(fi, "tsRepackage");
1410 xx = psmStage(psm, PSM_PKGSAVE);
1411 (void) rpmfiUnlink(fi, "tsRepackage");
1414 /*@switchbreak@*/ break;
1417 tei = teFreeIterator(tei);
1420 /* ===============================================
1421 * Install and remove packages.
1424 lastFailed = -2; /* erased packages have -1 */
1425 tei = teInitIterator(ts);
1426 /*@-branchstate@*/ /* FIX: fi reload needs work */
1427 while ((fi = teNextFi(tei)) != NULL) {
1433 psm->fi = rpmfiLink(fi, "tsInstall");
1437 i = ts->order[oc].u.addedIndex;
1439 rpmMessage(RPMMESS_DEBUG, "========== +++ %s-%s-%s\n",
1440 fi->name, fi->version, fi->release);
1441 h = (fi->h ? headerLink(fi->h, "TR_ADDED install") : NULL);
1443 if (fi->fd == NULL) {
1444 /*@-noeffectuncon @*/ /* FIX: ??? */
1445 fi->fd = ts->notify(fi->h, RPMCALLBACK_INST_OPEN_FILE, 0, 0,
1446 fi->key, ts->notifyData);
1447 /*@=noeffectuncon @*/
1448 if (fi->fd != NULL) {
1451 h = headerFree(h, "TR_ADDED install");
1453 /*@-mustmod@*/ /* LCL: segfault */
1454 rpmrc = rpmReadPackageFile(ts, fi->fd,
1455 "rpmRunTransactions", &h);
1458 if (!(rpmrc == RPMRC_OK || rpmrc == RPMRC_BADSIZE)) {
1459 /*@-noeffectuncon @*/ /* FIX: check rc */
1460 (void) ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE,
1462 fi->key, ts->notifyData);
1463 /*@=noeffectuncon @*/
1466 } else if (fi->h != NULL) {
1467 Header foo = relocateFileList(ts, fi, h, NULL);
1468 h = headerFree(h, "TR_ADDED read free");
1469 h = headerLink(foo, "TR_ADDED relocate xfer");
1470 foo = headerFree(foo, "TR_ADDED relocate");
1472 if (fi->fd != NULL) gotfd = 1;
1477 if (fi->fd != NULL) {
1478 Header hsave = NULL;
1481 hsave = headerLink(fi->h, "TR_ADDED fi->h hsave");
1482 fi->h = headerFree(fi->h, "TR_ADDED fi->h free");
1483 fi->h = headerLink(h, "TR_ADDED fi->h link");
1485 char * fstates = fi->fstates;
1486 fileAction * actions = fi->actions;
1487 uint_32 multiLib = fi->multiLib;
1488 const void * key = fi->key;
1489 rpmRelocation * relocs = fi->relocs;
1499 fi->magic = TFIMAGIC;
1500 fi->type = ts->order[oc].type;
1502 loadFi(ts, fi, h, 1);
1503 fi->fstates = _free(fi->fstates);
1504 fi->fstates = fstates;
1505 fi->actions = _free(fi->actions);
1506 fi->actions = actions;
1507 fi->multiLib = multiLib;
1509 fi->relocs = relocs;
1511 /*@i@*/ fi->fd = fd;
1516 ts->transFlags |= RPMTRANS_FLAG_MULTILIB;
1518 if (psmStage(psm, PSM_PKGINSTALL)) {
1522 fi->h = headerFree(fi->h, "TR_ADDED fi->h free");
1524 fi->h = headerLink(hsave, "TR_ADDED fi->h restore");
1525 hsave = headerFree(hsave, "TR_ADDED hsave free");
1532 h = headerFree(h, "TR_ADDED h free");
1535 /*@-noeffectuncon @*/ /* FIX: check rc */
1536 (void)ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE, 0, 0,
1537 fi->key, ts->notifyData);
1538 /*@=noeffectuncon @*/
1542 /*@switchbreak@*/ break;
1544 rpmMessage(RPMMESS_DEBUG, "========== --- %s-%s-%s\n",
1545 fi->name, fi->version, fi->release);
1547 /* If install failed, then we shouldn't erase. */
1548 if (ts->order[oc].u.removed.dependsOnIndex != lastFailed) {
1549 if (psmStage(psm, PSM_PKGERASE))
1553 /*@switchbreak@*/ break;
1555 xx = rpmdbSync(ts->rpmdb);
1556 (void) rpmfiUnlink(fi, "tsInstall");
1560 tei = teFreeIterator(tei);
1562 ts->flList = freeFl(ts, ts->flList);
1565 psm->ts = rpmtsUnlink(psm->ts, "tsRun");
1567 /*@-nullstate@*/ /* FIX: ts->flList may be NULL */