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@*/
63 /*@access teIterator@*/
64 /*@access transactionElement@*/
65 /*@access rpmTransactionSet@*/
69 struct diskspaceInfo {
70 dev_t dev; /*!< file system device number. */
71 signed long bneeded; /*!< no. of blocks needed. */
72 signed long ineeded; /*!< no. of inodes needed. */
73 int bsize; /*!< file system block size. */
74 signed long bavail; /*!< no. of blocks available. */
75 signed long iavail; /*!< no. of inodes available. */
79 * Adjust for root only reserved space. On linux e2fs, this is 5%.
81 #define adj_fs_blocks(_nb) (((_nb) * 21) / 20)
83 /* argon thought a shift optimization here was a waste of time... he's
85 #define BLOCK_ROUND(size, block) (((size) + (block) - 1) / (block))
89 static /*@null@*/ void * freeFl(rpmTransactionSet ts,
90 /*@only@*/ /*@null@*/ TFI_t flList)
97 /*@-usereleased -onlytrans @*/ /* FIX: fi needs to be only */
98 for (oc = 0, fi = flList; oc < ts->orderCount; oc++, fi++)
100 flList = _free(flList);
101 /*@=usereleased =onlytrans @*/
106 void rpmtransSetScriptFd(rpmTransactionSet ts, FD_t fd)
108 /*@-type@*/ /* FIX: cast? */
109 ts->scriptFd = (fd ? fdLink(fd, "rpmtransSetScriptFd") : NULL);
113 int rpmtransGetKeys(const rpmTransactionSet ts, const void *** ep, int * nep)
117 if (nep) *nep = ts->orderCount;
122 *ep = e = xmalloc(ts->orderCount * sizeof(*e));
123 for (oc = 0; oc < ts->orderCount; oc++, e++) {
124 switch (ts->order[oc].type) {
126 *e = alGetKey(ts->addedPackages, ts->order[oc].u.addedKey);
127 /*@switchbreak@*/ break;
130 /*@-mods@*/ /* FIX: double indirection. */
133 /*@switchbreak@*/ break;
142 static int archOkay(Header h)
148 /* make sure we're trying to install this on the proper architecture */
149 (void) headerGetEntry(h, RPMTAG_ARCH, &type, (void **) &pkgArch, &count);
151 if (type == RPM_INT8_TYPE) {
155 /* old arch handling */
156 rpmGetArchInfo(NULL, &archNum);
157 pkgArchNum = pkgArch;
158 if (archNum != *pkgArchNum) {
164 /* new arch handling */
165 if (!rpmMachineScore(RPM_MACHTABLE_INSTARCH, pkgArch)) {
175 static int osOkay(Header h)
181 /* make sure we're trying to install this on the proper os */
182 (void) headerGetEntry(h, RPMTAG_OS, &type, (void **) &pkgOs, &count);
184 if (type == RPM_INT8_TYPE) {
185 /* v1 packages and v2 packages both used improper OS numbers, so just
186 deal with it hope things work */
191 /* new os handling */
192 if (!rpmMachineScore(RPM_MACHTABLE_INSTOS, pkgOs)) {
202 static int sharedCmp(const void * one, const void * two)
205 const struct sharedFileInfo * a = one;
206 const struct sharedFileInfo * b = two;
208 if (a->otherPkg < b->otherPkg)
210 else if (a->otherPkg > b->otherPkg)
218 static fileAction decideFileFate(const char * dirName,
219 const char * baseName, short dbMode,
220 const char * dbMd5, const char * dbLink, short newMode,
221 const char * newMd5, const char * newLink, int newFlags,
222 rpmtransFlags transFlags)
223 /*@globals fileSystem @*/
224 /*@modifies fileSystem @*/
227 const char * dbAttr, * newAttr;
228 fileTypes dbWhat, newWhat, diskWhat;
231 int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
232 char * filespec = alloca(strlen(dirName) + strlen(baseName) + 1);
234 (void) stpcpy( stpcpy(filespec, dirName), baseName);
236 if (lstat(filespec, &sb)) {
238 * The file doesn't exist on the disk. Create it unless the new
239 * package has marked it as missingok, or allfiles is requested.
241 if (!(transFlags & RPMTRANS_FLAG_ALLFILES) &&
242 (newFlags & RPMFILE_MISSINGOK)) {
243 rpmMessage(RPMMESS_DEBUG, _("%s skipped due to missingok flag\n"),
251 diskWhat = whatis(sb.st_mode);
252 dbWhat = whatis(dbMode);
253 newWhat = whatis(newMode);
255 /* RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
256 them in older packages as well */
257 if (newWhat == XDIR) {
261 if (diskWhat != newWhat) {
263 } else if (newWhat != dbWhat && diskWhat != dbWhat) {
265 } else if (dbWhat != newWhat) {
267 } else if (dbWhat != LINK && dbWhat != REG) {
272 rc = mdfile(filespec, buffer);
275 /* assume the file has been removed, don't freak */
280 } else /* dbWhat == LINK */ {
281 memset(buffer, 0, sizeof(buffer));
282 i = readlink(filespec, buffer, sizeof(buffer) - 1);
284 /* assume the file has been removed, don't freak */
291 /* this order matters - we'd prefer to CREATE the file if at all
292 possible in case something else (like the timestamp) has changed */
294 if (!strcmp(dbAttr, buffer)) {
295 /* this config file has never been modified, so just replace it */
299 if (!strcmp(dbAttr, newAttr)) {
300 /* this file is the same in all versions of this package */
305 * The config file on the disk has been modified, but
306 * the ones in the two packages are different. It would
307 * be nice if RPM was smart enough to at least try and
308 * merge the difference ala CVS, but...
315 static int filecmp(short mode1, const char * md51, const char * link1,
316 short mode2, const char * md52, const char * link2)
319 fileTypes what1 = whatis(mode1);
320 fileTypes what2 = whatis(mode2);
322 if (what1 != what2) return 1;
325 return strcmp(link1, link2);
326 else if (what1 == REG)
327 return strcmp(md51, md52);
334 /* XXX only ts->{probs,rpmdb} modified */
335 static int handleInstInstalledFiles(const rpmTransactionSet ts, TFI_t fi,
336 struct sharedFileInfo * shared,
337 int sharedCount, int reportConflicts)
338 /*@globals fileSystem @*/
339 /*@modifies ts, fi, fileSystem @*/
342 HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
343 rpmtransFlags transFlags = ts->transFlags;
344 rpmTagType oltype, omtype;
347 const char ** otherMd5s;
348 const char ** otherLinks;
349 const char * otherStates;
350 uint_32 * otherFlags;
351 uint_32 * otherSizes;
352 uint_16 * otherModes;
356 rpmdbMatchIterator mi;
358 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES, &shared->otherPkg, sizeof(shared->otherPkg));
359 h = rpmdbNextIterator(mi);
361 mi = rpmdbFreeIterator(mi);
365 xx = hge(h, RPMTAG_FILEMD5S, &omtype, (void **) &otherMd5s, NULL);
366 xx = hge(h, RPMTAG_FILELINKTOS, &oltype, (void **) &otherLinks, NULL);
367 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
368 xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &otherModes, NULL);
369 xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &otherFlags, NULL);
370 xx = hge(h, RPMTAG_FILESIZES, NULL, (void **) &otherSizes, NULL);
372 fi->replaced = xmalloc(sharedCount * sizeof(*fi->replaced));
374 for (i = 0; i < sharedCount; i++, shared++) {
375 int otherFileNum, fileNum;
376 otherFileNum = shared->otherFileNum;
377 fileNum = shared->pkgFileNum;
379 /* XXX another tedious segfault, assume file state normal. */
380 if (otherStates && otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
383 if (XFA_SKIPPING(fi->actions[fileNum]))
386 if (filecmp(otherModes[otherFileNum],
387 otherMd5s[otherFileNum],
388 otherLinks[otherFileNum],
391 fi->flinks[fileNum])) {
392 /*@-compdef@*/ /* FIX: *fi->replaced undefined */
394 rpmProblemSetAppend(ts->probs, RPMPROB_FILE_CONFLICT,
395 fiGetNVR(fi), fi->key,
396 fi->dnl[fi->dil[fileNum]], fi->bnl[fileNum],
400 if (!(otherFlags[otherFileNum] | fi->fflags[fileNum])
403 if (!shared->isRemoved)
404 fi->replaced[numReplaced++] = *shared;
409 if ((otherFlags[otherFileNum] | fi->fflags[fileNum]) & RPMFILE_CONFIG) {
410 fi->actions[fileNum] = decideFileFate(
411 fi->dnl[fi->dil[fileNum]],
413 otherModes[otherFileNum],
414 otherMd5s[otherFileNum],
415 otherLinks[otherFileNum],
423 fi->replacedSizes[fileNum] = otherSizes[otherFileNum];
426 otherMd5s = hfd(otherMd5s, omtype);
427 otherLinks = hfd(otherLinks, oltype);
428 mi = rpmdbFreeIterator(mi);
430 fi->replaced = xrealloc(fi->replaced, /* XXX memory leak */
431 sizeof(*fi->replaced) * (numReplaced + 1));
432 fi->replaced[numReplaced].otherPkg = 0;
439 /* XXX only ts->rpmdb modified */
440 static int handleRmvdInstalledFiles(const rpmTransactionSet ts, TFI_t fi,
441 struct sharedFileInfo * shared, int sharedCount)
442 /*@globals fileSystem @*/
443 /*@modifies fi, fileSystem @*/
447 const char * otherStates;
450 rpmdbMatchIterator mi;
452 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
453 &shared->otherPkg, sizeof(shared->otherPkg));
454 h = rpmdbNextIterator(mi);
456 mi = rpmdbFreeIterator(mi);
460 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
462 for (i = 0; i < sharedCount; i++, shared++) {
463 int otherFileNum, fileNum;
464 otherFileNum = shared->otherFileNum;
465 fileNum = shared->pkgFileNum;
467 if (otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
470 fi->actions[fileNum] = FA_SKIP;
473 mi = rpmdbFreeIterator(mi);
479 * Update disk space needs on each partition for this package.
481 /* XXX only ts->{probs,di} modified */
482 static void handleOverlappedFiles(const rpmTransactionSet ts, TFI_t fi)
483 /*@globals fileSystem @*/
484 /*@modifies ts, fi, fileSystem @*/
486 struct diskspaceInfo * ds = NULL;
487 uint_32 fixupSize = 0;
488 char * filespec = NULL;
489 int fileSpecAlloced = 0;
492 for (i = 0; i < fi->fc; i++) {
493 int otherPkgNum, otherFileNum;
497 if (XFA_SKIPPING(fi->actions[i]))
500 j = strlen(fi->dnl[fi->dil[i]]) + strlen(fi->bnl[i]) + 1;
502 if (j > fileSpecAlloced) {
503 fileSpecAlloced = j * 2;
504 filespec = xrealloc(filespec, fileSpecAlloced);
508 (void) stpcpy( stpcpy( filespec, fi->dnl[fi->dil[i]]), fi->bnl[i]);
512 while (ds->bsize && ds->dev != fi->fps[i].entry->dev) ds++;
513 if (!ds->bsize) ds = NULL;
518 * Retrieve all records that apply to this file. Note that the
519 * file info records were built in the same order as the packages
520 * will be installed and removed so the records for an overlapped
521 * files will be sorted in exactly the same order.
523 (void) htGetEntry(ts->ht, &fi->fps[i],
524 (const void ***) &recs, &numRecs, NULL);
527 * If this package is being added, look only at other packages
528 * being added -- removed packages dance to a different tune.
529 * If both this and the other package are being added, overlapped
530 * files must be identical (or marked as a conflict). The
531 * disposition of already installed config files leads to
532 * a small amount of extra complexity.
534 * If this package is being removed, then there are two cases that
535 * need to be worried about:
536 * If the other package is being added, then skip any overlapped files
537 * so that this package removal doesn't nuke the overlapped files
538 * that were just installed.
539 * If both this and the other package are being removed, then each
540 * file removal from preceding packages needs to be skipped so that
541 * the file removal occurs only on the last occurence of an overlapped
542 * file in the transaction set.
546 /* Locate this overlapped file in the set of added/removed packages. */
547 for (j = 0; j < numRecs && recs[j] != fi; j++)
550 /* Find what the previous disposition of this file was. */
551 otherFileNum = -1; /* keep gcc quiet */
552 for (otherPkgNum = j - 1; otherPkgNum >= 0; otherPkgNum--) {
553 /* Added packages need only look at other added packages. */
554 if (fi->type == TR_ADDED && recs[otherPkgNum]->type != TR_ADDED)
555 /*@innercontinue@*/ continue;
557 /* TESTME: there are more efficient searches in the world... */
558 for (otherFileNum = 0; otherFileNum < recs[otherPkgNum]->fc;
561 /* If the addresses are the same, so are the values. */
562 if ((fi->fps + i) == (recs[otherPkgNum]->fps + otherFileNum))
563 /*@innerbreak@*/ break;
565 /* Otherwise, compare fingerprints by value. */
566 /*@-nullpass@*/ /* LCL: looks good to me */
567 if (FP_EQUAL(fi->fps[i], recs[otherPkgNum]->fps[otherFileNum]))
568 /*@innerbreak@*/ break;
572 /* XXX is this test still necessary? */
573 if (recs[otherPkgNum]->actions[otherFileNum] != FA_UNKNOWN)
574 /*@innerbreak@*/ break;
580 if (otherPkgNum < 0) {
581 /* XXX is this test still necessary? */
582 if (fi->actions[i] != FA_UNKNOWN)
583 /*@switchbreak@*/ break;
584 if ((fi->fflags[i] & RPMFILE_CONFIG) &&
585 !lstat(filespec, &sb)) {
586 /* Here is a non-overlapped pre-existing config file. */
587 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
588 ? FA_ALTNAME : FA_BACKUP;
590 fi->actions[i] = FA_CREATE;
592 /*@switchbreak@*/ break;
595 /* Mark added overlapped non-identical files as a conflict. */
596 if ((ts->ignoreSet & RPMPROB_FILTER_REPLACENEWFILES)
597 && filecmp(recs[otherPkgNum]->fmodes[otherFileNum],
598 recs[otherPkgNum]->fmd5s[otherFileNum],
599 recs[otherPkgNum]->flinks[otherFileNum],
604 rpmProblemSetAppend(ts->probs, RPMPROB_NEW_FILE_CONFLICT,
605 fiGetNVR(fi), fi->key,
607 fiGetNVR(recs[otherPkgNum]),
611 /* Try to get the disk accounting correct even if a conflict. */
612 fixupSize = recs[otherPkgNum]->fsizes[otherFileNum];
614 if ((fi->fflags[i] & RPMFILE_CONFIG) && !lstat(filespec, &sb)) {
615 /* Here is an overlapped pre-existing config file. */
616 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
617 ? FA_ALTNAME : FA_SKIP;
619 fi->actions[i] = FA_CREATE;
621 } /*@switchbreak@*/ break;
623 if (otherPkgNum >= 0) {
624 /* Here is an overlapped added file we don't want to nuke. */
625 if (recs[otherPkgNum]->actions[otherFileNum] != FA_ERASE) {
626 /* On updates, don't remove files. */
627 fi->actions[i] = FA_SKIP;
628 /*@switchbreak@*/ break;
630 /* Here is an overlapped removed file: skip in previous. */
631 recs[otherPkgNum]->actions[otherFileNum] = FA_SKIP;
633 if (XFA_SKIPPING(fi->actions[i]))
634 /*@switchbreak@*/ break;
635 if (fi->fstates && fi->fstates[i] != RPMFILE_STATE_NORMAL)
636 /*@switchbreak@*/ break;
637 if (!(S_ISREG(fi->fmodes[i]) && (fi->fflags[i] & RPMFILE_CONFIG))) {
638 fi->actions[i] = FA_ERASE;
639 /*@switchbreak@*/ break;
642 /* Here is a pre-existing modified config file that needs saving. */
644 if (!mdfile(filespec, mdsum) && strcmp(fi->fmd5s[i], mdsum)) {
645 fi->actions[i] = FA_BACKUP;
646 /*@switchbreak@*/ break;
649 fi->actions[i] = FA_ERASE;
650 /*@switchbreak@*/ break;
654 uint_32 s = BLOCK_ROUND(fi->fsizes[i], ds->bsize);
656 switch (fi->actions[i]) {
662 /*@switchbreak@*/ break;
665 * FIXME: If two packages share a file (same md5sum), and
666 * that file is being replaced on disk, will ds->bneeded get
667 * decremented twice? Quite probably!
671 ds->bneeded -= BLOCK_ROUND(fi->replacedSizes[i], ds->bsize);
672 /*@switchbreak@*/ break;
677 /*@switchbreak@*/ break;
680 /*@switchbreak@*/ break;
683 ds->bneeded -= BLOCK_ROUND(fixupSize, ds->bsize);
686 filespec = _free(filespec);
691 static int ensureOlder(rpmTransactionSet ts,
692 const Header h, /*@null@*/ const Header old,
693 /*@dependent@*/ /*@null@*/ const void * key)
698 if (old == NULL) return 1;
700 result = rpmVersionCompare(old, h);
703 else if (result > 0) {
705 /*@-evalorder@*/ /* LCL: is confused */
706 rpmProblemSetAppend(ts->probs, RPMPROB_OLDPACKAGE,
707 hGetNVR(h, NULL), key,
719 static void skipFiles(const rpmTransactionSet ts, TFI_t fi)
720 /*@globals rpmGlobalMacroContext @*/
721 /*@modifies fi, rpmGlobalMacroContext @*/
723 int noDocs = (ts->transFlags & RPMTRANS_FLAG_NODOCS);
724 char ** netsharedPaths = NULL;
725 const char ** languages;
726 const char * dn, * bn;
727 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. */
756 drc = alloca(fi->dc * sizeof(*drc));
757 memset(drc, 0, fi->dc * sizeof(*drc));
758 dff = alloca(fi->dc * sizeof(*dff));
759 memset(dff, 0, fi->dc * sizeof(*dff));
761 for (i = 0; i < fi->fc; i++) {
772 /* Don't bother with skipped files */
773 if (XFA_SKIPPING(fi->actions[i])) {
779 * Skip net shared paths.
780 * Net shared paths are not relative to the current root (though
781 * they do need to take package relocations into account).
783 for (nsp = netsharedPaths; nsp && *nsp; nsp++) {
788 if (strncmp(dn, *nsp, len))
789 /*@innercontinue@*/ continue;
790 /* Only directories or complete file paths can be net shared */
791 if (!(dn[len] == '/' || dn[len] == '\0'))
792 /*@innercontinue@*/ continue;
794 if (len < (dnlen + bnlen))
795 /*@innercontinue@*/ continue;
796 if (strncmp(dn, *nsp, dnlen))
797 /*@innercontinue@*/ continue;
798 if (strncmp(bn, (*nsp) + dnlen, bnlen))
799 /*@innercontinue@*/ continue;
801 /* Only directories or complete file paths can be net shared */
802 if (!((*nsp)[len] == '/' || (*nsp)[len] == '\0'))
803 /*@innercontinue@*/ continue;
806 /*@innerbreak@*/ break;
810 drc[ix]--; dff[ix] = 1;
811 fi->actions[i] = FA_SKIPNETSHARED;
816 * Skip i18n language specific files.
818 if (fi->flangs && languages && *fi->flangs[i]) {
819 const char **lang, *l, *le;
820 for (lang = languages; *lang != NULL; lang++) {
821 if (!strcmp(*lang, "all"))
822 /*@innerbreak@*/ break;
823 for (l = fi->flangs[i]; *l != '\0'; l = le) {
824 for (le = l; *le != '\0' && *le != '|'; le++)
826 if ((le-l) > 0 && !strncmp(*lang, l, (le-l)))
827 /*@innerbreak@*/ break;
828 if (*le == '|') le++; /* skip over | */
831 /*@innerbreak@*/ break;
834 drc[ix]--; dff[ix] = 1;
835 fi->actions[i] = FA_SKIPNSTATE;
841 * Skip documentation if requested.
843 if (noDocs && (fi->fflags[i] & RPMFILE_DOC)) {
844 drc[ix]--; dff[ix] = 1;
845 fi->actions[i] = FA_SKIPNSTATE;
850 /* Skip (now empty) directories that had skipped files. */
851 for (j = 0; j < fi->dc; j++) {
853 if (drc[j]) continue; /* dir still has files. */
854 if (!dff[j]) continue; /* dir was not emptied here. */
856 /* Find parent directory and basename. */
857 dn = fi->dnl[j]; dnlen = strlen(dn) - 1;
858 bn = dn + dnlen; bnlen = 0;
859 while (bn > dn && bn[-1] != '/') {
865 /* If explicitly included in the package, skip the directory. */
866 for (i = 0; i < fi->fc; i++) {
869 if (XFA_SKIPPING(fi->actions[i]))
870 /*@innercontinue@*/ continue;
871 if (whatis(fi->fmodes[i]) != XDIR)
872 /*@innercontinue@*/ continue;
873 dir = fi->dnl[fi->dil[i]];
874 if (strlen(dir) != dnlen)
875 /*@innercontinue@*/ continue;
876 if (strncmp(dir, dn, dnlen))
877 /*@innercontinue@*/ continue;
878 if (strlen(fi->bnl[i]) != bnlen)
879 /*@innercontinue@*/ continue;
880 if (strncmp(fi->bnl[i], bn, bnlen))
881 /*@innercontinue@*/ continue;
882 rpmMessage(RPMMESS_DEBUG, _("excluding directory %s\n"), dn);
883 fi->actions[i] = FA_SKIPNSTATE;
884 /*@innerbreak@*/ break;
888 if (netsharedPaths) freeSplitString(netsharedPaths);
889 #ifdef DYING /* XXX freeFi will deal with this later. */
890 fi->flangs = _free(fi->flangs);
892 if (languages) freeSplitString((char **)languages);
896 * Return next transaction element's file info.
897 * @param tei transaction element iterator
898 * @return nest transaction element file info, NULL on termination
900 /*@unused@*/ static inline
901 TFI_t teNextFi(teIterator tei)
906 if (teNextIterator(tei) != NULL && tei->ocsave != -1)
907 fi = tei->ts->flList + tei->ocsave;
908 /*@-compdef -onlytrans -usereleased@*/ /* FIX: ts->flList may be released */
910 /*@=compdef =onlytrans =usereleased@*/
913 #define NOTIFY(_ts, _al) if ((_ts)->notify) (void) (_ts)->notify _al
915 int rpmRunTransactions( rpmTransactionSet ts,
916 rpmCallbackFunction notify, rpmCallbackData notifyData,
917 rpmProblemSet okProbs, rpmProblemSet * newProbs,
918 rpmtransFlags transFlags, rpmprobFilterFlags ignoreSet)
922 int totalFileCount = 0;
924 struct diskspaceInfo * dip;
925 struct sharedFileInfo * shared, * sharedList;
928 alKey pkgKey, lastKey;
930 fingerPrintCache fpc;
935 int keep_header = 1; /* XXX rpmProblemSetAppend prevents dumping headers. */
937 /* FIXME: what if the same package is included in ts twice? */
939 ts->transFlags = transFlags;
940 if (ts->transFlags & RPMTRANS_FLAG_NOSCRIPTS)
941 ts->transFlags |= (_noTransScripts | _noTransTriggers);
942 if (ts->transFlags & RPMTRANS_FLAG_NOTRIGGERS)
943 ts->transFlags |= _noTransTriggers;
945 /* XXX MULTILIB is broken, as packages can and do execute /sbin/ldconfig. */
946 if (ts->transFlags & (RPMTRANS_FLAG_JUSTDB | RPMTRANS_FLAG_MULTILIB))
947 ts->transFlags |= (_noTransScripts | _noTransTriggers);
950 ts->notifyData = notifyData;
952 ts->probs = *newProbs = rpmProblemSetCreate();
954 ts->ignoreSet = ignoreSet;
955 ts->currDir = _free(ts->currDir);
956 ts->currDir = currentDirectory();
958 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
959 ts->id = (int_32) time(NULL);
961 memset(psm, 0, sizeof(*psm));
963 psm->ts = rpmtsLink(ts, "tsRun");
966 /* Get available space on mounted file systems. */
967 if (!(ts->ignoreSet & RPMPROB_FILTER_DISKSPACE) &&
968 !rpmGetFilesystemList(&ts->filesystems, &ts->filesystemCount)) {
971 ts->di = _free(ts->di);
972 dip = ts->di = xcalloc((ts->filesystemCount + 1), sizeof(*ts->di));
974 for (i = 0; (i < ts->filesystemCount) && dip; i++) {
975 #if STATFS_IN_SYS_STATVFS
977 memset(&sfb, 0, sizeof(sfb));
978 if (statvfs(ts->filesystems[i], &sfb))
982 /* This platform has the 4-argument version of the statfs call. The last two
983 * should be the size of struct statfs and 0, respectively. The 0 is the
984 * filesystem type, and is always 0 when statfs is called on a mounted
985 * filesystem, as we're doing.
987 memset(&sfb, 0, sizeof(sfb));
988 if (statfs(ts->filesystems[i], &sfb, sizeof(sfb), 0))
990 memset(&sfb, 0, sizeof(sfb));
991 if (statfs(ts->filesystems[i], &sfb))
997 ts->di[i].bsize = sfb.f_bsize;
998 ts->di[i].bneeded = 0;
999 ts->di[i].ineeded = 0;
1000 #ifdef STATFS_HAS_F_BAVAIL
1001 ts->di[i].bavail = sfb.f_bavail;
1003 /* FIXME: the statfs struct doesn't have a member to tell how many blocks are
1004 * available for non-superusers. f_blocks - f_bfree is probably too big, but
1005 * it's about all we can do.
1007 ts->di[i].bavail = sfb.f_blocks - sfb.f_bfree;
1009 /* XXX Avoid FAT and other file systems that have not inodes. */
1010 ts->di[i].iavail = !(sfb.f_ffree == 0 && sfb.f_files == 0)
1013 xx = stat(ts->filesystems[i], &sb);
1014 ts->di[i].dev = sb.st_dev;
1018 if (dip) ts->di[i].bsize = 0;
1021 /* ===============================================
1022 * For packages being installed:
1023 * - verify package arch/os.
1024 * - verify package epoch:version-release is newer.
1026 * For packages being removed:
1029 /* The ordering doesn't matter here */
1030 for (i = 0; i < alGetSize(ts->addedPackages); i++) {
1031 const char * n, * v, * r;
1033 rpmdbMatchIterator mi;
1036 /* XXX cast assumes that available keys are indices, not pointers */
1039 h = alGetHeader(ts->addedPackages, pkgKey, 0);
1040 if (h == NULL) /* XXX can't happen */
1043 (void) headerNVR(h, &n, &v, &r);
1044 key = alGetKey(ts->addedPackages, pkgKey);
1046 if (!archOkay(h) && !(ts->ignoreSet & RPMPROB_FILTER_IGNOREARCH))
1047 rpmProblemSetAppend(ts->probs, RPMPROB_BADARCH,
1048 hGetNVR(h, NULL), key,
1049 NULL, NULL, NULL, 0);
1051 if (!osOkay(h) && !(ts->ignoreSet & RPMPROB_FILTER_IGNOREOS))
1052 rpmProblemSetAppend(ts->probs, RPMPROB_BADOS,
1053 hGetNVR(h, NULL), key,
1054 NULL, NULL, NULL, 0);
1056 if (!(ts->ignoreSet & RPMPROB_FILTER_OLDPACKAGE)) {
1058 mi = rpmtsInitIterator(ts, RPMTAG_NAME, n, 0);
1059 while ((oldH = rpmdbNextIterator(mi)) != NULL)
1060 xx = ensureOlder(ts, h, oldH, key);
1061 mi = rpmdbFreeIterator(mi);
1064 /* XXX multilib should not display "already installed" problems */
1065 if (!(ts->ignoreSet & RPMPROB_FILTER_REPLACEPKG)
1066 #ifdef DYING /* XXX MULTILIB multiLib from transactionElement */
1067 && !alGetMultiLib(ts->addedPackages, i)
1070 mi = rpmtsInitIterator(ts, RPMTAG_NAME, n, 0);
1071 xx = rpmdbSetIteratorRE(mi, RPMTAG_VERSION, RPMMIRE_DEFAULT, v);
1072 xx = rpmdbSetIteratorRE(mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT, r);
1074 while (rpmdbNextIterator(mi) != NULL) {
1075 rpmProblemSetAppend(ts->probs, RPMPROB_PKG_INSTALLED,
1076 hGetNVR(h, NULL), key,
1077 NULL, NULL, NULL, 0);
1078 /*@innerbreak@*/ break;
1080 mi = rpmdbFreeIterator(mi);
1083 totalFileCount += alGetFilesCount(ts->addedPackages, pkgKey);
1085 h = headerFree(h, "alGetHeader (rpmtsRun sanity)");
1089 /* FIXME: it seems a bit silly to read in all of these headers twice */
1090 /* The ordering doesn't matter here */
1091 if (ts->numRemovedPackages > 0) {
1092 rpmdbMatchIterator mi;
1096 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES, NULL, 0);
1097 xx = rpmdbAppendIterator(mi, ts->removedPackages, ts->numRemovedPackages);
1098 while ((h = rpmdbNextIterator(mi)) != NULL) {
1099 if (headerGetEntry(h, RPMTAG_BASENAMES, NULL, NULL, &fileCount))
1100 totalFileCount += fileCount;
1102 mi = rpmdbFreeIterator(mi);
1105 /* ===============================================
1106 * Initialize transaction element file info for package:
1108 ts->flEntries = alGetSize(ts->addedPackages) + ts->numRemovedPackages;
1109 ts->flList = xcalloc(ts->flEntries, sizeof(*ts->flList));
1112 * FIXME?: we'd be better off assembling one very large file list and
1113 * calling fpLookupList only once. I'm not sure that the speedup is
1114 * worth the trouble though.
1116 tei = teInitIterator(ts);
1117 while ((fi = teNextFi(tei)) != NULL) {
1120 fi->magic = TFIMAGIC;
1122 fi->type = ts->order[oc].type;
1129 pkgKey = ts->order[oc].u.addedKey;
1131 fi->h = alGetHeader(ts->addedPackages, pkgKey, 1);
1132 #ifdef DYING /* XXX MULTILIB multiLib from transactionElement */
1133 fi->multiLib = alGetMultiLib(ts->addedPackages, i);
1135 fi->multiLib = ts->order[oc].multiLib;
1138 fi->key = alGetKey(ts->addedPackages, pkgKey);
1140 fi->relocs = alGetRelocs(ts->addedPackages, pkgKey);
1141 fi->fd = alGetFd(ts->addedPackages, pkgKey);
1143 /* XXX availablePackage can be dumped here XXX */
1145 /* XXX header arg unused. */
1146 loadFi(ts, fi, fi->h, keep_header);
1151 /* Skip netshared paths, not our i18n files, and excluded docs */
1153 /*@switchbreak@*/ break;
1155 fi->record = ts->order[oc].u.removed.dboffset;
1156 /* Retrieve erased package header from the database. */
1157 { rpmdbMatchIterator mi;
1159 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
1160 &fi->record, sizeof(fi->record));
1161 if ((fi->h = rpmdbNextIterator(mi)) != NULL)
1162 fi->h = headerLink(fi->h, "TR_REMOVED loadFi");
1163 mi = rpmdbFreeIterator(mi);
1165 if (fi->h == NULL) {
1169 /* XXX header arg unused. */
1170 loadFi(ts, fi, fi->h, 0);
1171 /*@switchbreak@*/ break;
1176 fi->fps = xmalloc(fi->fc * sizeof(*fi->fps));
1178 tei = teFreeIterator(tei);
1180 if (!ts->chrootDone) {
1182 /*@-superuser -noeffect @*/
1183 xx = chroot(ts->rootDir);
1184 /*@=superuser =noeffect @*/
1186 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 1;
1189 chroot_prefix = ts->rootDir;
1194 ts->ht = htCreate(totalFileCount * 2, 0, 0, fpHashFunction, fpEqual);
1195 fpc = fpCacheCreate(totalFileCount);
1197 /* ===============================================
1198 * Add fingerprint for each file not skipped.
1200 tei = teInitIterator(ts);
1201 while ((fi = teNextFi(tei)) != NULL) {
1202 fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fi->fc, fi->fps);
1203 for (i = 0; i < fi->fc; i++) {
1204 if (XFA_SKIPPING(fi->actions[i]))
1205 /*@innercontinue@*/ continue;
1206 /*@-dependenttrans@*/
1207 htAddEntry(ts->ht, fi->fps + i, fi);
1208 /*@=dependenttrans@*/
1211 tei = teFreeIterator(tei);
1213 /*@-noeffectuncon @*/ /* FIX: check rc */
1214 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_START, 6, ts->flEntries,
1215 NULL, ts->notifyData));
1216 /*@=noeffectuncon@*/
1218 /* ===============================================
1219 * Compute file disposition for each package in transaction set.
1221 tei = teInitIterator(ts);
1222 while ((fi = teNextFi(tei)) != NULL) {
1223 dbiIndexSet * matches;
1226 /*@-noeffectuncon @*/ /* FIX: check rc */
1227 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_PROGRESS, (fi - ts->flList),
1228 ts->flEntries, NULL, ts->notifyData));
1229 /*@=noeffectuncon@*/
1231 if (fi->fc == 0) continue;
1233 /* Extract file info for all files in this package from the database. */
1234 matches = xcalloc(fi->fc, sizeof(*matches));
1235 if (rpmdbFindFpList(ts->rpmdb, fi->fps, matches, fi->fc)) {
1236 psm->ts = rpmtsUnlink(ts, "tsRun (rpmFindFpList fail)");
1237 return 1; /* XXX WTFO? */
1241 for (i = 0; i < fi->fc; i++)
1242 numShared += dbiIndexSetCount(matches[i]);
1244 /* Build sorted file info list for this package. */
1245 shared = sharedList = xcalloc((numShared + 1), sizeof(*sharedList));
1246 for (i = 0; i < fi->fc; i++) {
1248 * Take care not to mark files as replaced in packages that will
1249 * have been removed before we will get here.
1251 for (j = 0; j < dbiIndexSetCount(matches[i]); j++) {
1253 ro = dbiIndexRecordOffset(matches[i], j);
1255 for (k = 0; ro != knownBad && k < ts->orderCount; k++) {
1256 switch (ts->order[k].type) {
1258 if (ts->order[k].u.removed.dboffset == ro)
1260 /*@switchbreak@*/ break;
1262 /*@switchbreak@*/ break;
1266 shared->pkgFileNum = i;
1267 shared->otherPkg = dbiIndexRecordOffset(matches[i], j);
1268 shared->otherFileNum = dbiIndexRecordFileNumber(matches[i], j);
1269 shared->isRemoved = (knownBad == ro);
1272 matches[i] = dbiFreeIndexSet(matches[i]);
1274 numShared = shared - sharedList;
1275 shared->otherPkg = -1;
1276 matches = _free(matches);
1278 /* Sort file info by other package index (otherPkg) */
1279 qsort(sharedList, numShared, sizeof(*shared), sharedCmp);
1281 /* For all files from this package that are in the database ... */
1282 for (i = 0; i < numShared; i = nexti) {
1285 shared = sharedList + i;
1287 /* Find the end of the files in the other package. */
1288 for (nexti = i + 1; nexti < numShared; nexti++) {
1289 if (sharedList[nexti].otherPkg != shared->otherPkg)
1290 /*@innerbreak@*/ break;
1293 /* Is this file from a package being removed? */
1295 for (j = 0; j < ts->numRemovedPackages; j++) {
1296 if (ts->removedPackages[j] != shared->otherPkg)
1297 /*@innercontinue@*/ continue;
1299 /*@innerbreak@*/ break;
1302 /* Determine the fate of each file. */
1305 xx = handleInstInstalledFiles(ts, fi, shared, nexti - i,
1306 !(beingRemoved || (ts->ignoreSet & RPMPROB_FILTER_REPLACEOLDFILES)));
1307 /*@switchbreak@*/ break;
1310 xx = handleRmvdInstalledFiles(ts, fi, shared, nexti - i);
1311 /*@switchbreak@*/ break;
1317 /* Update disk space needs on each partition for this package. */
1318 handleOverlappedFiles(ts, fi);
1320 /* Check added package has sufficient space on each partition used. */
1323 if (!(ts->di && fi->fc))
1324 /*@switchbreak@*/ break;
1325 for (i = 0; i < ts->filesystemCount; i++) {
1329 /* XXX Avoid FAT and other file systems that have not inodes. */
1330 if (dip->iavail <= 0)
1331 /*@innercontinue@*/ continue;
1333 if (adj_fs_blocks(dip->bneeded) > dip->bavail)
1334 rpmProblemSetAppend(ts->probs, RPMPROB_DISKSPACE,
1335 fiGetNVR(fi), fi->key,
1336 ts->filesystems[i], NULL, NULL,
1337 (adj_fs_blocks(dip->bneeded) - dip->bavail) * dip->bsize);
1339 if (adj_fs_blocks(dip->ineeded) > dip->iavail)
1340 rpmProblemSetAppend(ts->probs, RPMPROB_DISKNODES,
1341 fiGetNVR(fi), fi->key,
1342 ts->filesystems[i], NULL, NULL,
1343 (adj_fs_blocks(dip->ineeded) - dip->iavail));
1345 /*@switchbreak@*/ break;
1347 /*@switchbreak@*/ break;
1350 tei = teFreeIterator(tei);
1352 if (ts->chrootDone) {
1353 /*@-superuser -noeffect @*/
1355 /*@=superuser =noeffect @*/
1357 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
1359 chroot_prefix = NULL;
1361 xx = chdir(ts->currDir);
1364 /*@-noeffectuncon @*/ /* FIX: check rc */
1365 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_STOP, 6, ts->flEntries,
1366 NULL, ts->notifyData));
1367 /*@=noeffectuncon @*/
1369 /* ===============================================
1370 * Free unused memory as soon as possible.
1373 tei = teInitIterator(ts);
1374 while ((fi = teNextFi(tei)) != NULL) {
1377 fi->fps = _free(fi->fps);
1379 tei = teFreeIterator(tei);
1385 /* ===============================================
1386 * If unfiltered problems exist, free memory and return.
1388 if ((ts->transFlags & RPMTRANS_FLAG_BUILD_PROBS)
1389 || (ts->probs->numProblems &&
1390 (okProbs != NULL || rpmProblemSetTrim(ts->probs, okProbs)))
1393 *newProbs = ts->probs;
1395 ts->flList = freeFl(ts, ts->flList);
1397 if (psm->ts != NULL)
1398 psm->ts = rpmtsUnlink(psm->ts, "tsRun (problems)");
1399 /*@-nullstate@*/ /* FIX: ts->flList may be NULL */
1400 return ts->orderCount;
1404 /* ===============================================
1405 * Save removed files before erasing.
1407 if (ts->transFlags & (RPMTRANS_FLAG_DIRSTASH | RPMTRANS_FLAG_REPACKAGE)) {
1408 tei = teInitIterator(ts);
1409 while ((fi = teNextFi(tei)) != NULL) {
1412 /*@switchbreak@*/ break;
1414 if (ts->transFlags & RPMTRANS_FLAG_REPACKAGE) {
1415 psm->fi = rpmfiLink(fi, "tsRepackage");
1416 xx = psmStage(psm, PSM_PKGSAVE);
1417 (void) rpmfiUnlink(fi, "tsRepackage");
1420 /*@switchbreak@*/ break;
1423 tei = teFreeIterator(tei);
1426 /* ===============================================
1427 * Install and remove packages.
1430 lastKey = (alKey)-2; /* erased packages have -1 */
1431 tei = teInitIterator(ts);
1432 /*@-branchstate@*/ /* FIX: fi reload needs work */
1433 while ((fi = teNextFi(tei)) != NULL) {
1439 psm->fi = rpmfiLink(fi, "tsInstall");
1443 pkgKey = ts->order[oc].u.addedKey;
1445 rpmMessage(RPMMESS_DEBUG, "========== +++ %s-%s-%s\n",
1446 fi->name, fi->version, fi->release);
1447 h = (fi->h ? headerLink(fi->h, "TR_ADDED install") : NULL);
1449 if (fi->fd == NULL) {
1450 /*@-noeffectuncon @*/ /* FIX: ??? */
1451 fi->fd = ts->notify(fi->h, RPMCALLBACK_INST_OPEN_FILE, 0, 0,
1452 fi->key, ts->notifyData);
1453 /*@=noeffectuncon @*/
1454 if (fi->fd != NULL) {
1457 h = headerFree(h, "TR_ADDED install");
1459 /*@-mustmod@*/ /* LCL: segfault */
1460 rpmrc = rpmReadPackageFile(ts, fi->fd,
1461 "rpmRunTransactions", &h);
1464 if (!(rpmrc == RPMRC_OK || rpmrc == RPMRC_BADSIZE)) {
1465 /*@-noeffectuncon @*/ /* FIX: check rc */
1466 (void) ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE,
1468 fi->key, ts->notifyData);
1469 /*@=noeffectuncon @*/
1472 } else if (fi->h != NULL) {
1473 Header foo = relocateFileList(ts, fi, h, NULL);
1474 h = headerFree(h, "TR_ADDED read free");
1475 h = headerLink(foo, "TR_ADDED relocate xfer");
1476 foo = headerFree(foo, "TR_ADDED relocate");
1478 if (fi->fd != NULL) gotfd = 1;
1483 if (fi->fd != NULL) {
1484 Header hsave = NULL;
1487 hsave = headerLink(fi->h, "TR_ADDED fi->h hsave");
1488 fi->h = headerFree(fi->h, "TR_ADDED fi->h free");
1489 fi->h = headerLink(h, "TR_ADDED fi->h link");
1491 char * fstates = fi->fstates;
1492 fileAction * actions = fi->actions;
1493 uint_32 multiLib = fi->multiLib;
1494 const void * key = fi->key;
1495 rpmRelocation * relocs = fi->relocs;
1505 fi->magic = TFIMAGIC;
1506 fi->type = ts->order[oc].type;
1508 loadFi(ts, fi, h, 1);
1509 fi->fstates = _free(fi->fstates);
1510 fi->fstates = fstates;
1511 fi->actions = _free(fi->actions);
1512 fi->actions = actions;
1513 fi->multiLib = multiLib;
1515 fi->relocs = relocs;
1517 /*@i@*/ fi->fd = fd;
1522 ts->transFlags |= RPMTRANS_FLAG_MULTILIB;
1524 if (psmStage(psm, PSM_PKGINSTALL)) {
1528 fi->h = headerFree(fi->h, "TR_ADDED fi->h free");
1530 fi->h = headerLink(hsave, "TR_ADDED fi->h restore");
1531 hsave = headerFree(hsave, "TR_ADDED hsave free");
1538 h = headerFree(h, "TR_ADDED h free");
1541 /*@-noeffectuncon @*/ /* FIX: check rc */
1542 (void)ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE, 0, 0,
1543 fi->key, ts->notifyData);
1544 /*@=noeffectuncon @*/
1548 /*@switchbreak@*/ break;
1550 rpmMessage(RPMMESS_DEBUG, "========== --- %s-%s-%s\n",
1551 fi->name, fi->version, fi->release);
1553 /* If install failed, then we shouldn't erase. */
1554 if (ts->order[oc].u.removed.dependsOnKey != lastKey) {
1555 if (psmStage(psm, PSM_PKGERASE))
1559 /*@switchbreak@*/ break;
1561 xx = rpmdbSync(ts->rpmdb);
1562 (void) rpmfiUnlink(fi, "tsInstall");
1566 tei = teFreeIterator(tei);
1568 ts->flList = freeFl(ts, ts->flList);
1571 psm->ts = rpmtsUnlink(psm->ts, "tsRun");
1573 /*@-nullstate@*/ /* FIX: ts->flList may be NULL */