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@*/
64 /*@access teIterator@*/
65 /*@access transactionElement@*/
66 /*@access rpmTransactionSet@*/
70 struct diskspaceInfo {
71 dev_t dev; /*!< file system device number. */
72 signed long bneeded; /*!< no. of blocks needed. */
73 signed long ineeded; /*!< no. of inodes needed. */
74 int bsize; /*!< file system block size. */
75 signed long bavail; /*!< no. of blocks available. */
76 signed long iavail; /*!< no. of inodes available. */
80 * Adjust for root only reserved space. On linux e2fs, this is 5%.
82 #define adj_fs_blocks(_nb) (((_nb) * 21) / 20)
84 /* argon thought a shift optimization here was a waste of time... he's
86 #define BLOCK_ROUND(size, block) (((size) + (block) - 1) / (block))
90 static /*@null@*/ void * freeFl(rpmTransactionSet ts,
91 /*@only@*/ /*@null@*/ TFI_t flList)
98 /*@-usereleased -onlytrans @*/ /* FIX: fi needs to be only */
99 for (oc = 0, fi = flList; oc < ts->orderCount; oc++, fi++)
101 flList = _free(flList);
102 /*@=usereleased =onlytrans @*/
107 void rpmtransSetScriptFd(rpmTransactionSet ts, FD_t fd)
109 /*@-type@*/ /* FIX: cast? */
110 ts->scriptFd = (fd ? fdLink(fd, "rpmtransSetScriptFd") : NULL);
114 int rpmtransGetKeys(const rpmTransactionSet ts, fnpyKey ** ep, int * nep)
118 if (nep) *nep = ts->orderCount;
123 *ep = e = xmalloc(ts->orderCount * sizeof(*e));
124 for (oc = 0; oc < ts->orderCount; oc++, e++) {
125 switch (ts->order[oc].type) {
127 *e = ts->order[oc].key;
128 /*@switchbreak@*/ break;
131 /*@-mods@*/ /* FIX: double indirection. */
134 /*@switchbreak@*/ break;
143 static int archOkay(Header h, /*@out@*/ const char ** pkgArchPtr)
144 /*@modifies *pkgArchPtr @*/
146 const char * pkgArch;
148 int rc = 1; /* assume AOK */
150 if (pkgArchPtr != NULL) *pkgArchPtr = pkgArch = NULL;
152 /* make sure we're trying to install this on the proper architecture */
153 (void) headerGetEntry(h, RPMTAG_ARCH, &type, (void **) &pkgArch, &count);
156 if (type == RPM_INT8_TYPE) {
160 /* old arch handling */
161 rpmGetArchInfo(NULL, &archNum);
162 pkgArchNum = pkgArch;
163 if (archNum != *pkgArchNum)
168 if (!rpmMachineScore(RPM_MACHTABLE_INSTARCH, pkgArch)) {
170 if (pkgArchPtr != NULL) *pkgArchPtr = pkgArch;
178 static int osOkay(Header h, /*@out@*/ const char ** pkgOsPtr)
179 /*@modifies *pkgOsPtr @*/
183 int rc = 1; /* assume AOK */
185 if (pkgOsPtr != NULL) *pkgOsPtr = pkgOs = NULL;
187 /* make sure we're trying to install this on the proper os */
188 (void) headerGetEntry(h, RPMTAG_OS, &type, (void **) &pkgOs, &count);
191 if (type == RPM_INT8_TYPE) {
192 /* v1 packages and v2 packages both used improper OS numbers, so just
193 deal with it hope things work */
198 if (!rpmMachineScore(RPM_MACHTABLE_INSTOS, pkgOs)) {
200 if (pkgOsPtr != NULL) *pkgOsPtr = pkgOs;
208 static int sharedCmp(const void * one, const void * two)
211 const struct sharedFileInfo * a = one;
212 const struct sharedFileInfo * b = two;
214 if (a->otherPkg < b->otherPkg)
216 else if (a->otherPkg > b->otherPkg)
224 static fileAction decideFileFate(const char * dirName,
225 const char * baseName, short dbMode,
226 const char * dbMd5, const char * dbLink, short newMode,
227 const char * newMd5, const char * newLink, int newFlags,
228 rpmtransFlags transFlags)
229 /*@globals fileSystem @*/
230 /*@modifies fileSystem @*/
233 const char * dbAttr, * newAttr;
234 fileTypes dbWhat, newWhat, diskWhat;
237 int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
238 char * filespec = alloca(strlen(dirName) + strlen(baseName) + 1);
240 (void) stpcpy( stpcpy(filespec, dirName), baseName);
242 if (lstat(filespec, &sb)) {
244 * The file doesn't exist on the disk. Create it unless the new
245 * package has marked it as missingok, or allfiles is requested.
247 if (!(transFlags & RPMTRANS_FLAG_ALLFILES) &&
248 (newFlags & RPMFILE_MISSINGOK)) {
249 rpmMessage(RPMMESS_DEBUG, _("%s skipped due to missingok flag\n"),
257 diskWhat = whatis(sb.st_mode);
258 dbWhat = whatis(dbMode);
259 newWhat = whatis(newMode);
261 /* RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
262 them in older packages as well */
263 if (newWhat == XDIR) {
267 if (diskWhat != newWhat) {
269 } else if (newWhat != dbWhat && diskWhat != dbWhat) {
271 } else if (dbWhat != newWhat) {
273 } else if (dbWhat != LINK && dbWhat != REG) {
278 rc = mdfile(filespec, buffer);
281 /* assume the file has been removed, don't freak */
286 } else /* dbWhat == LINK */ {
287 memset(buffer, 0, sizeof(buffer));
288 i = readlink(filespec, buffer, sizeof(buffer) - 1);
290 /* assume the file has been removed, don't freak */
297 /* this order matters - we'd prefer to CREATE the file if at all
298 possible in case something else (like the timestamp) has changed */
300 if (!strcmp(dbAttr, buffer)) {
301 /* this config file has never been modified, so just replace it */
305 if (!strcmp(dbAttr, newAttr)) {
306 /* this file is the same in all versions of this package */
311 * The config file on the disk has been modified, but
312 * the ones in the two packages are different. It would
313 * be nice if RPM was smart enough to at least try and
314 * merge the difference ala CVS, but...
321 static int filecmp(short mode1, const char * md51, const char * link1,
322 short mode2, const char * md52, const char * link2)
325 fileTypes what1 = whatis(mode1);
326 fileTypes what2 = whatis(mode2);
328 if (what1 != what2) return 1;
331 return strcmp(link1, link2);
332 else if (what1 == REG)
333 return strcmp(md51, md52);
340 /* XXX only ts->{probs,rpmdb} modified */
341 static int handleInstInstalledFiles(const rpmTransactionSet ts, TFI_t fi,
342 struct sharedFileInfo * shared,
343 int sharedCount, int reportConflicts)
344 /*@globals fileSystem @*/
345 /*@modifies ts, fi, fileSystem @*/
348 HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
349 rpmtransFlags transFlags = ts->transFlags;
350 rpmTagType oltype, omtype;
353 const char ** otherMd5s;
354 const char ** otherLinks;
355 const char * otherStates;
356 uint_32 * otherFlags;
357 uint_32 * otherSizes;
358 uint_16 * otherModes;
362 rpmdbMatchIterator mi;
364 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
365 &shared->otherPkg, sizeof(shared->otherPkg));
366 h = rpmdbNextIterator(mi);
368 mi = rpmdbFreeIterator(mi);
372 xx = hge(h, RPMTAG_FILEMD5S, &omtype, (void **) &otherMd5s, NULL);
373 xx = hge(h, RPMTAG_FILELINKTOS, &oltype, (void **) &otherLinks, NULL);
374 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
375 xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &otherModes, NULL);
376 xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &otherFlags, NULL);
377 xx = hge(h, RPMTAG_FILESIZES, NULL, (void **) &otherSizes, NULL);
379 fi->replaced = xmalloc(sharedCount * sizeof(*fi->replaced));
381 for (i = 0; i < sharedCount; i++, shared++) {
382 int otherFileNum, fileNum;
383 otherFileNum = shared->otherFileNum;
384 fileNum = shared->pkgFileNum;
386 /* XXX another tedious segfault, assume file state normal. */
387 if (otherStates && otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
390 if (XFA_SKIPPING(fi->actions[fileNum]))
393 if (filecmp(otherModes[otherFileNum],
394 otherMd5s[otherFileNum],
395 otherLinks[otherFileNum],
398 fi->flinks[fileNum])) {
399 /*@-compdef@*/ /* FIX: *fi->replaced undefined */
400 if (reportConflicts) {
401 const char * pkgNEVR = fiGetNEVR(fi);
402 const char * altNEVR = hGetNEVR(h, NULL);
403 rpmProblemSetAppend(ts->probs, RPMPROB_FILE_CONFLICT,
405 fi->dnl[fi->dil[fileNum]], fi->bnl[fileNum],
408 pkgNEVR = _free(pkgNEVR);
409 altNEVR = _free(altNEVR);
412 if (!(otherFlags[otherFileNum] | fi->fflags[fileNum])
415 if (!shared->isRemoved)
416 fi->replaced[numReplaced++] = *shared;
421 if ((otherFlags[otherFileNum] | fi->fflags[fileNum]) & RPMFILE_CONFIG) {
422 fi->actions[fileNum] = decideFileFate(
423 fi->dnl[fi->dil[fileNum]],
425 otherModes[otherFileNum],
426 otherMd5s[otherFileNum],
427 otherLinks[otherFileNum],
435 fi->replacedSizes[fileNum] = otherSizes[otherFileNum];
438 otherMd5s = hfd(otherMd5s, omtype);
439 otherLinks = hfd(otherLinks, oltype);
440 mi = rpmdbFreeIterator(mi);
442 fi->replaced = xrealloc(fi->replaced, /* XXX memory leak */
443 sizeof(*fi->replaced) * (numReplaced + 1));
444 fi->replaced[numReplaced].otherPkg = 0;
451 /* XXX only ts->rpmdb modified */
452 static int handleRmvdInstalledFiles(const rpmTransactionSet ts, TFI_t fi,
453 struct sharedFileInfo * shared, int sharedCount)
454 /*@globals fileSystem @*/
455 /*@modifies fi, fileSystem @*/
459 const char * otherStates;
462 rpmdbMatchIterator mi;
464 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
465 &shared->otherPkg, sizeof(shared->otherPkg));
466 h = rpmdbNextIterator(mi);
468 mi = rpmdbFreeIterator(mi);
472 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
474 for (i = 0; i < sharedCount; i++, shared++) {
475 int otherFileNum, fileNum;
476 otherFileNum = shared->otherFileNum;
477 fileNum = shared->pkgFileNum;
479 if (otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
482 fi->actions[fileNum] = FA_SKIP;
485 mi = rpmdbFreeIterator(mi);
491 * Update disk space needs on each partition for this package.
493 /* XXX only ts->{probs,di} modified */
494 static void handleOverlappedFiles(const rpmTransactionSet ts, TFI_t fi)
495 /*@globals fileSystem @*/
496 /*@modifies ts, fi, fileSystem @*/
498 struct diskspaceInfo * ds = NULL;
499 uint_32 fixupSize = 0;
500 char * filespec = NULL;
501 int fileSpecAlloced = 0;
504 for (i = 0; i < fi->fc; i++) {
505 int otherPkgNum, otherFileNum;
509 if (XFA_SKIPPING(fi->actions[i]))
512 j = strlen(fi->dnl[fi->dil[i]]) + strlen(fi->bnl[i]) + 1;
514 if (j > fileSpecAlloced) {
515 fileSpecAlloced = j * 2;
516 filespec = xrealloc(filespec, fileSpecAlloced);
520 (void) stpcpy( stpcpy( filespec, fi->dnl[fi->dil[i]]), fi->bnl[i]);
524 while (ds->bsize && ds->dev != fi->fps[i].entry->dev) ds++;
525 if (!ds->bsize) ds = NULL;
530 * Retrieve all records that apply to this file. Note that the
531 * file info records were built in the same order as the packages
532 * will be installed and removed so the records for an overlapped
533 * files will be sorted in exactly the same order.
535 (void) htGetEntry(ts->ht, &fi->fps[i],
536 (const void ***) &recs, &numRecs, NULL);
539 * If this package is being added, look only at other packages
540 * being added -- removed packages dance to a different tune.
541 * If both this and the other package are being added, overlapped
542 * files must be identical (or marked as a conflict). The
543 * disposition of already installed config files leads to
544 * a small amount of extra complexity.
546 * If this package is being removed, then there are two cases that
547 * need to be worried about:
548 * If the other package is being added, then skip any overlapped files
549 * so that this package removal doesn't nuke the overlapped files
550 * that were just installed.
551 * If both this and the other package are being removed, then each
552 * file removal from preceding packages needs to be skipped so that
553 * the file removal occurs only on the last occurence of an overlapped
554 * file in the transaction set.
558 /* Locate this overlapped file in the set of added/removed packages. */
559 for (j = 0; j < numRecs && recs[j] != fi; j++)
562 /* Find what the previous disposition of this file was. */
563 otherFileNum = -1; /* keep gcc quiet */
564 for (otherPkgNum = j - 1; otherPkgNum >= 0; otherPkgNum--) {
565 /* Added packages need only look at other added packages. */
566 if (fi->type == TR_ADDED && recs[otherPkgNum]->type != TR_ADDED)
567 /*@innercontinue@*/ continue;
569 /* TESTME: there are more efficient searches in the world... */
570 for (otherFileNum = 0; otherFileNum < recs[otherPkgNum]->fc;
573 /* If the addresses are the same, so are the values. */
574 if ((fi->fps + i) == (recs[otherPkgNum]->fps + otherFileNum))
575 /*@innerbreak@*/ break;
577 /* Otherwise, compare fingerprints by value. */
578 /*@-nullpass@*/ /* LCL: looks good to me */
579 if (FP_EQUAL(fi->fps[i], recs[otherPkgNum]->fps[otherFileNum]))
580 /*@innerbreak@*/ break;
584 /* XXX is this test still necessary? */
585 if (recs[otherPkgNum]->actions[otherFileNum] != FA_UNKNOWN)
586 /*@innerbreak@*/ break;
592 if (otherPkgNum < 0) {
593 /* XXX is this test still necessary? */
594 if (fi->actions[i] != FA_UNKNOWN)
595 /*@switchbreak@*/ break;
596 if ((fi->fflags[i] & RPMFILE_CONFIG) &&
597 !lstat(filespec, &sb)) {
598 /* Here is a non-overlapped pre-existing config file. */
599 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
600 ? FA_ALTNAME : FA_BACKUP;
602 fi->actions[i] = FA_CREATE;
604 /*@switchbreak@*/ break;
607 /* Mark added overlapped non-identical files as a conflict. */
608 if ((ts->ignoreSet & RPMPROB_FILTER_REPLACENEWFILES)
609 && filecmp(recs[otherPkgNum]->fmodes[otherFileNum],
610 recs[otherPkgNum]->fmd5s[otherFileNum],
611 recs[otherPkgNum]->flinks[otherFileNum],
616 const char * pkgNEVR = fiGetNEVR(fi);
617 const char * altNEVR = fiGetNEVR(recs[otherPkgNum]);
618 rpmProblemSetAppend(ts->probs, RPMPROB_NEW_FILE_CONFLICT,
623 pkgNEVR = _free(pkgNEVR);
624 altNEVR = _free(altNEVR);
627 /* Try to get the disk accounting correct even if a conflict. */
628 fixupSize = recs[otherPkgNum]->fsizes[otherFileNum];
630 if ((fi->fflags[i] & RPMFILE_CONFIG) && !lstat(filespec, &sb)) {
631 /* Here is an overlapped pre-existing config file. */
632 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
633 ? FA_ALTNAME : FA_SKIP;
635 fi->actions[i] = FA_CREATE;
637 } /*@switchbreak@*/ break;
639 if (otherPkgNum >= 0) {
640 /* Here is an overlapped added file we don't want to nuke. */
641 if (recs[otherPkgNum]->actions[otherFileNum] != FA_ERASE) {
642 /* On updates, don't remove files. */
643 fi->actions[i] = FA_SKIP;
644 /*@switchbreak@*/ break;
646 /* Here is an overlapped removed file: skip in previous. */
647 recs[otherPkgNum]->actions[otherFileNum] = FA_SKIP;
649 if (XFA_SKIPPING(fi->actions[i]))
650 /*@switchbreak@*/ break;
651 if (fi->fstates && fi->fstates[i] != RPMFILE_STATE_NORMAL)
652 /*@switchbreak@*/ break;
653 if (!(S_ISREG(fi->fmodes[i]) && (fi->fflags[i] & RPMFILE_CONFIG))) {
654 fi->actions[i] = FA_ERASE;
655 /*@switchbreak@*/ break;
658 /* Here is a pre-existing modified config file that needs saving. */
660 if (!mdfile(filespec, mdsum) && strcmp(fi->fmd5s[i], mdsum)) {
661 fi->actions[i] = FA_BACKUP;
662 /*@switchbreak@*/ break;
665 fi->actions[i] = FA_ERASE;
666 /*@switchbreak@*/ break;
670 uint_32 s = BLOCK_ROUND(fi->fsizes[i], ds->bsize);
672 switch (fi->actions[i]) {
678 /*@switchbreak@*/ break;
681 * FIXME: If two packages share a file (same md5sum), and
682 * that file is being replaced on disk, will ds->bneeded get
683 * decremented twice? Quite probably!
687 ds->bneeded -= BLOCK_ROUND(fi->replacedSizes[i], ds->bsize);
688 /*@switchbreak@*/ break;
693 /*@switchbreak@*/ break;
696 /*@switchbreak@*/ break;
699 ds->bneeded -= BLOCK_ROUND(fixupSize, ds->bsize);
702 filespec = _free(filespec);
707 static int ensureOlder(rpmTransactionSet ts,
708 const Header h, /*@null@*/ const Header old,
709 /*@dependent@*/ /*@null@*/ const void * key)
714 if (old == NULL) return 1;
716 result = rpmVersionCompare(old, h);
719 else if (result > 0) {
720 const char * pkgNEVR = hGetNEVR(h, NULL);
721 const char * altNEVR = hGetNEVR(old, NULL);
722 /*@-evalorder@*/ /* LCL: is confused */
723 rpmProblemSetAppend(ts->probs, RPMPROB_OLDPACKAGE,
729 pkgNEVR = _free(pkgNEVR);
730 altNEVR = _free(altNEVR);
739 static void skipFiles(const rpmTransactionSet ts, TFI_t fi)
740 /*@globals rpmGlobalMacroContext @*/
741 /*@modifies fi, rpmGlobalMacroContext @*/
743 int noDocs = (ts->transFlags & RPMTRANS_FLAG_NODOCS);
744 char ** netsharedPaths = NULL;
745 const char ** languages;
746 const char * dn, * bn;
747 int dnlen, bnlen, ix;
754 noDocs = rpmExpandNumeric("%{_excludedocs}");
756 { const char *tmpPath = rpmExpand("%{_netsharedpath}", NULL);
758 if (tmpPath && *tmpPath != '%')
759 netsharedPaths = splitString(tmpPath, strlen(tmpPath), ':');
761 tmpPath = _free(tmpPath);
764 s = rpmExpand("%{_install_langs}", NULL);
766 if (!(s && *s != '%'))
769 languages = (const char **) splitString(s, strlen(s), ':');
775 /* Compute directory refcount, skip directory if now empty. */
776 drc = alloca(fi->dc * sizeof(*drc));
777 memset(drc, 0, fi->dc * sizeof(*drc));
778 dff = alloca(fi->dc * sizeof(*dff));
779 memset(dff, 0, fi->dc * sizeof(*dff));
781 for (i = 0; i < fi->fc; i++) {
792 /* Don't bother with skipped files */
793 if (XFA_SKIPPING(fi->actions[i])) {
799 * Skip net shared paths.
800 * Net shared paths are not relative to the current root (though
801 * they do need to take package relocations into account).
803 for (nsp = netsharedPaths; nsp && *nsp; nsp++) {
808 if (strncmp(dn, *nsp, len))
809 /*@innercontinue@*/ continue;
810 /* Only directories or complete file paths can be net shared */
811 if (!(dn[len] == '/' || dn[len] == '\0'))
812 /*@innercontinue@*/ continue;
814 if (len < (dnlen + bnlen))
815 /*@innercontinue@*/ continue;
816 if (strncmp(dn, *nsp, dnlen))
817 /*@innercontinue@*/ continue;
818 if (strncmp(bn, (*nsp) + dnlen, bnlen))
819 /*@innercontinue@*/ continue;
821 /* Only directories or complete file paths can be net shared */
822 if (!((*nsp)[len] == '/' || (*nsp)[len] == '\0'))
823 /*@innercontinue@*/ continue;
826 /*@innerbreak@*/ break;
830 drc[ix]--; dff[ix] = 1;
831 fi->actions[i] = FA_SKIPNETSHARED;
836 * Skip i18n language specific files.
838 if (fi->flangs && languages && *fi->flangs[i]) {
839 const char **lang, *l, *le;
840 for (lang = languages; *lang != NULL; lang++) {
841 if (!strcmp(*lang, "all"))
842 /*@innerbreak@*/ break;
843 for (l = fi->flangs[i]; *l != '\0'; l = le) {
844 for (le = l; *le != '\0' && *le != '|'; le++)
846 if ((le-l) > 0 && !strncmp(*lang, l, (le-l)))
847 /*@innerbreak@*/ break;
848 if (*le == '|') le++; /* skip over | */
851 /*@innerbreak@*/ break;
854 drc[ix]--; dff[ix] = 1;
855 fi->actions[i] = FA_SKIPNSTATE;
861 * Skip documentation if requested.
863 if (noDocs && (fi->fflags[i] & RPMFILE_DOC)) {
864 drc[ix]--; dff[ix] = 1;
865 fi->actions[i] = FA_SKIPNSTATE;
870 /* Skip (now empty) directories that had skipped files. */
871 for (j = 0; j < fi->dc; j++) {
873 if (drc[j]) continue; /* dir still has files. */
874 if (!dff[j]) continue; /* dir was not emptied here. */
876 /* Find parent directory and basename. */
877 dn = fi->dnl[j]; dnlen = strlen(dn) - 1;
878 bn = dn + dnlen; bnlen = 0;
879 while (bn > dn && bn[-1] != '/') {
885 /* If explicitly included in the package, skip the directory. */
886 for (i = 0; i < fi->fc; i++) {
889 if (XFA_SKIPPING(fi->actions[i]))
890 /*@innercontinue@*/ continue;
891 if (whatis(fi->fmodes[i]) != XDIR)
892 /*@innercontinue@*/ continue;
893 dir = fi->dnl[fi->dil[i]];
894 if (strlen(dir) != dnlen)
895 /*@innercontinue@*/ continue;
896 if (strncmp(dir, dn, dnlen))
897 /*@innercontinue@*/ continue;
898 if (strlen(fi->bnl[i]) != bnlen)
899 /*@innercontinue@*/ continue;
900 if (strncmp(fi->bnl[i], 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);
916 * Return next transaction element's file info.
917 * @param tei transaction element iterator
918 * @return nest transaction element file info, NULL on termination
920 /*@unused@*/ static inline
921 TFI_t teNextFi(teIterator tei)
926 if (teNextIterator(tei) != NULL && tei->ocsave != -1)
927 fi = tei->ts->flList + tei->ocsave;
928 /*@-compdef -onlytrans -usereleased@*/ /* FIX: ts->flList may be released */
930 /*@=compdef =onlytrans =usereleased@*/
933 #define NOTIFY(_ts, _al) if ((_ts)->notify) (void) (_ts)->notify _al
935 int rpmRunTransactions( rpmTransactionSet ts,
936 rpmCallbackFunction notify, rpmCallbackData notifyData,
937 rpmProblemSet okProbs, rpmProblemSet * newProbs,
938 rpmtransFlags transFlags, rpmprobFilterFlags ignoreSet)
942 int totalFileCount = 0;
944 struct diskspaceInfo * dip;
945 struct sharedFileInfo * shared, * sharedList;
948 alKey pkgKey, lastKey;
950 transactionElement p;
951 fingerPrintCache fpc;
957 int keep_header = 1; /* XXX rpmProblemSetAppend prevents dumping headers. */
959 /* FIXME: what if the same package is included in ts twice? */
961 ts->transFlags = transFlags;
962 if (ts->transFlags & RPMTRANS_FLAG_NOSCRIPTS)
963 ts->transFlags |= (_noTransScripts | _noTransTriggers);
964 if (ts->transFlags & RPMTRANS_FLAG_NOTRIGGERS)
965 ts->transFlags |= _noTransTriggers;
967 /* XXX MULTILIB is broken, as packages can and do execute /sbin/ldconfig. */
968 if (ts->transFlags & (RPMTRANS_FLAG_JUSTDB | RPMTRANS_FLAG_MULTILIB))
969 ts->transFlags |= (_noTransScripts | _noTransTriggers);
972 ts->notifyData = notifyData;
974 ts->probs = *newProbs = rpmProblemSetCreate();
975 *newProbs = rpmpsLink(ts->probs, "RunTransactions");
977 ts->ignoreSet = ignoreSet;
978 ts->currDir = _free(ts->currDir);
979 ts->currDir = currentDirectory();
981 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
982 ts->id = (int_32) time(NULL);
984 memset(psm, 0, sizeof(*psm));
986 psm->ts = rpmtsLink(ts, "tsRun");
989 /* Get available space on mounted file systems. */
990 if (!(ts->ignoreSet & RPMPROB_FILTER_DISKSPACE) &&
991 !rpmGetFilesystemList(&ts->filesystems, &ts->filesystemCount)) {
994 ts->di = _free(ts->di);
995 dip = ts->di = xcalloc((ts->filesystemCount + 1), sizeof(*ts->di));
997 for (i = 0; (i < ts->filesystemCount) && dip; i++) {
998 #if STATFS_IN_SYS_STATVFS
1000 memset(&sfb, 0, sizeof(sfb));
1001 if (statvfs(ts->filesystems[i], &sfb))
1005 /* This platform has the 4-argument version of the statfs call. The last two
1006 * should be the size of struct statfs and 0, respectively. The 0 is the
1007 * filesystem type, and is always 0 when statfs is called on a mounted
1008 * filesystem, as we're doing.
1010 memset(&sfb, 0, sizeof(sfb));
1011 if (statfs(ts->filesystems[i], &sfb, sizeof(sfb), 0))
1013 memset(&sfb, 0, sizeof(sfb));
1014 if (statfs(ts->filesystems[i], &sfb))
1020 ts->di[i].bsize = sfb.f_bsize;
1021 ts->di[i].bneeded = 0;
1022 ts->di[i].ineeded = 0;
1023 #ifdef STATFS_HAS_F_BAVAIL
1024 ts->di[i].bavail = sfb.f_bavail;
1026 /* FIXME: the statfs struct doesn't have a member to tell how many blocks are
1027 * available for non-superusers. f_blocks - f_bfree is probably too big, but
1028 * it's about all we can do.
1030 ts->di[i].bavail = sfb.f_blocks - sfb.f_bfree;
1032 /* XXX Avoid FAT and other file systems that have not inodes. */
1033 ts->di[i].iavail = !(sfb.f_ffree == 0 && sfb.f_files == 0)
1036 xx = stat(ts->filesystems[i], &sb);
1037 ts->di[i].dev = sb.st_dev;
1041 if (dip) ts->di[i].bsize = 0;
1044 /* ===============================================
1045 * For packages being installed:
1046 * - verify package arch/os.
1047 * - verify package epoch:version-release is newer.
1049 * For packages being removed:
1052 /* The ordering doesn't matter here */
1053 tei = teInitIterator(ts);
1054 while ((p = teNext(tei, TR_ADDED)) != NULL) {
1055 const char * n, * v, * r;
1057 rpmdbMatchIterator mi;
1061 pkgKey = p->u.addedKey;
1063 h = alGetHeader(ts->addedPackages, pkgKey, 0);
1064 if (h == NULL) /* XXX can't happen */
1067 (void) headerNVR(h, &n, &v, &r);
1071 if (!archOkay(h, &str1) && !(ts->ignoreSet & RPMPROB_FILTER_IGNOREARCH)) {
1072 const char * pkgNEVR = hGetNEVR(h, NULL);
1073 rpmProblemSetAppend(ts->probs, RPMPROB_BADARCH,
1077 pkgNEVR = _free(pkgNEVR);
1081 if (!osOkay(h, &str1) && !(ts->ignoreSet & RPMPROB_FILTER_IGNOREOS)) {
1082 const char * pkgNEVR = hGetNEVR(h, NULL);
1083 rpmProblemSetAppend(ts->probs, RPMPROB_BADOS,
1087 pkgNEVR = _free(pkgNEVR);
1090 if (!(ts->ignoreSet & RPMPROB_FILTER_OLDPACKAGE)) {
1092 mi = rpmtsInitIterator(ts, RPMTAG_NAME, n, 0);
1093 while ((oldH = rpmdbNextIterator(mi)) != NULL)
1094 xx = ensureOlder(ts, h, oldH, key);
1095 mi = rpmdbFreeIterator(mi);
1098 /* XXX multilib should not display "already installed" problems */
1099 if (!(ts->ignoreSet & RPMPROB_FILTER_REPLACEPKG)
1100 #ifdef DYING /* XXX MULTILIB multiLib from transactionElement */
1101 && !alGetMultiLib(ts->addedPackages, i)
1104 mi = rpmtsInitIterator(ts, RPMTAG_NAME, n, 0);
1105 xx = rpmdbSetIteratorRE(mi, RPMTAG_VERSION, RPMMIRE_DEFAULT, v);
1106 xx = rpmdbSetIteratorRE(mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT, r);
1108 while (rpmdbNextIterator(mi) != NULL) {
1109 const char * pkgNEVR = hGetNEVR(h, NULL);
1110 rpmProblemSetAppend(ts->probs, RPMPROB_PKG_INSTALLED,
1114 pkgNEVR = _free(pkgNEVR);
1115 /*@innerbreak@*/ break;
1117 mi = rpmdbFreeIterator(mi);
1120 totalFileCount += alGetFilesCount(ts->addedPackages, pkgKey);
1122 h = headerFree(h, "alGetHeader (rpmtsRun sanity)");
1125 tei = teFreeIterator(tei);
1127 /* FIXME: it seems a bit silly to read in all of these headers twice */
1128 /* The ordering doesn't matter here */
1129 if (ts->numRemovedPackages > 0) {
1130 rpmdbMatchIterator mi;
1134 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES, NULL, 0);
1135 xx = rpmdbAppendIterator(mi, ts->removedPackages, ts->numRemovedPackages);
1136 while ((h = rpmdbNextIterator(mi)) != NULL) {
1137 if (headerGetEntry(h, RPMTAG_BASENAMES, NULL, NULL, &fileCount))
1138 totalFileCount += fileCount;
1140 mi = rpmdbFreeIterator(mi);
1143 /* ===============================================
1144 * Initialize transaction element file info for package:
1146 ts->flEntries = alGetSize(ts->addedPackages) + ts->numRemovedPackages;
1147 ts->flList = xcalloc(ts->flEntries, sizeof(*ts->flList));
1150 * FIXME?: we'd be better off assembling one very large file list and
1151 * calling fpLookupList only once. I'm not sure that the speedup is
1152 * worth the trouble though.
1154 tei = teInitIterator(ts);
1155 while ((fi = teNextFi(tei)) != NULL) {
1158 fi->magic = TFIMAGIC;
1160 fi->type = ts->order[oc].type;
1167 pkgKey = ts->order[oc].u.addedKey;
1169 fi->h = alGetHeader(ts->addedPackages, pkgKey, 1);
1170 #ifdef DYING /* XXX MULTILIB multiLib from transactionElement */
1171 fi->multiLib = alGetMultiLib(ts->addedPackages, i);
1173 fi->multiLib = ts->order[oc].multiLib;
1176 /*@i@*/ fi->key = ts->order[oc].key;
1177 fi->relocs = ts->order[oc].relocs;
1178 /*@i@*/ fi->fd = ts->order[oc].fd;
1180 /* XXX availablePackage can be dumped here XXX */
1182 /* XXX header arg unused. */
1183 loadFi(ts, fi, fi->h, keep_header);
1188 /* Skip netshared paths, not our i18n files, and excluded docs */
1190 /*@switchbreak@*/ break;
1192 fi->record = ts->order[oc].u.removed.dboffset;
1193 /* Retrieve erased package header from the database. */
1194 { rpmdbMatchIterator mi;
1196 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
1197 &fi->record, sizeof(fi->record));
1198 if ((fi->h = rpmdbNextIterator(mi)) != NULL)
1199 fi->h = headerLink(fi->h, "TR_REMOVED loadFi");
1200 mi = rpmdbFreeIterator(mi);
1202 if (fi->h == NULL) {
1206 /* XXX header arg unused. */
1207 loadFi(ts, fi, fi->h, 0);
1208 /*@switchbreak@*/ break;
1213 fi->fps = xmalloc(fi->fc * sizeof(*fi->fps));
1215 tei = teFreeIterator(tei);
1217 if (!ts->chrootDone) {
1219 /*@-superuser -noeffect @*/
1220 xx = chroot(ts->rootDir);
1221 /*@=superuser =noeffect @*/
1223 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 1;
1226 chroot_prefix = ts->rootDir;
1231 ts->ht = htCreate(totalFileCount * 2, 0, 0, fpHashFunction, fpEqual);
1232 fpc = fpCacheCreate(totalFileCount);
1234 /* ===============================================
1235 * Add fingerprint for each file not skipped.
1237 tei = teInitIterator(ts);
1238 while ((fi = teNextFi(tei)) != NULL) {
1239 fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fi->fc, fi->fps);
1240 for (i = 0; i < fi->fc; i++) {
1241 if (XFA_SKIPPING(fi->actions[i]))
1242 /*@innercontinue@*/ continue;
1243 /*@-dependenttrans@*/
1244 htAddEntry(ts->ht, fi->fps + i, fi);
1245 /*@=dependenttrans@*/
1248 tei = teFreeIterator(tei);
1250 /*@-noeffectuncon @*/ /* FIX: check rc */
1251 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_START, 6, ts->flEntries,
1252 NULL, ts->notifyData));
1253 /*@=noeffectuncon@*/
1255 /* ===============================================
1256 * Compute file disposition for each package in transaction set.
1258 tei = teInitIterator(ts);
1259 while ((fi = teNextFi(tei)) != NULL) {
1260 dbiIndexSet * matches;
1263 /*@-noeffectuncon @*/ /* FIX: check rc */
1264 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_PROGRESS, (fi - ts->flList),
1265 ts->flEntries, NULL, ts->notifyData));
1266 /*@=noeffectuncon@*/
1268 if (fi->fc == 0) continue;
1270 /* Extract file info for all files in this package from the database. */
1271 matches = xcalloc(fi->fc, sizeof(*matches));
1272 if (rpmdbFindFpList(ts->rpmdb, fi->fps, matches, fi->fc)) {
1273 psm->ts = rpmtsUnlink(ts, "tsRun (rpmFindFpList fail)");
1274 return 1; /* XXX WTFO? */
1278 for (i = 0; i < fi->fc; i++)
1279 numShared += dbiIndexSetCount(matches[i]);
1281 /* Build sorted file info list for this package. */
1282 shared = sharedList = xcalloc((numShared + 1), sizeof(*sharedList));
1283 for (i = 0; i < fi->fc; i++) {
1285 * Take care not to mark files as replaced in packages that will
1286 * have been removed before we will get here.
1288 for (j = 0; j < dbiIndexSetCount(matches[i]); j++) {
1290 ro = dbiIndexRecordOffset(matches[i], j);
1292 for (k = 0; ro != knownBad && k < ts->orderCount; k++) {
1293 switch (ts->order[k].type) {
1295 if (ts->order[k].u.removed.dboffset == ro)
1297 /*@switchbreak@*/ break;
1299 /*@switchbreak@*/ break;
1303 shared->pkgFileNum = i;
1304 shared->otherPkg = dbiIndexRecordOffset(matches[i], j);
1305 shared->otherFileNum = dbiIndexRecordFileNumber(matches[i], j);
1306 shared->isRemoved = (knownBad == ro);
1309 matches[i] = dbiFreeIndexSet(matches[i]);
1311 numShared = shared - sharedList;
1312 shared->otherPkg = -1;
1313 matches = _free(matches);
1315 /* Sort file info by other package index (otherPkg) */
1316 qsort(sharedList, numShared, sizeof(*shared), sharedCmp);
1318 /* For all files from this package that are in the database ... */
1319 for (i = 0; i < numShared; i = nexti) {
1322 shared = sharedList + i;
1324 /* Find the end of the files in the other package. */
1325 for (nexti = i + 1; nexti < numShared; nexti++) {
1326 if (sharedList[nexti].otherPkg != shared->otherPkg)
1327 /*@innerbreak@*/ break;
1330 /* Is this file from a package being removed? */
1332 for (j = 0; j < ts->numRemovedPackages; j++) {
1333 if (ts->removedPackages[j] != shared->otherPkg)
1334 /*@innercontinue@*/ continue;
1336 /*@innerbreak@*/ break;
1339 /* Determine the fate of each file. */
1342 xx = handleInstInstalledFiles(ts, fi, shared, nexti - i,
1343 !(beingRemoved || (ts->ignoreSet & RPMPROB_FILTER_REPLACEOLDFILES)));
1344 /*@switchbreak@*/ break;
1347 xx = handleRmvdInstalledFiles(ts, fi, shared, nexti - i);
1348 /*@switchbreak@*/ break;
1354 /* Update disk space needs on each partition for this package. */
1355 handleOverlappedFiles(ts, fi);
1357 /* Check added package has sufficient space on each partition used. */
1360 if (!(ts->di && fi->fc))
1361 /*@switchbreak@*/ break;
1362 for (i = 0; i < ts->filesystemCount; i++) {
1366 /* XXX Avoid FAT and other file systems that have not inodes. */
1367 if (dip->iavail <= 0)
1368 /*@innercontinue@*/ continue;
1370 if (adj_fs_blocks(dip->bneeded) > dip->bavail) {
1371 const char * pkgNEVR = fiGetNEVR(fi);
1372 rpmProblemSetAppend(ts->probs, RPMPROB_DISKSPACE,
1374 ts->filesystems[i], NULL, NULL,
1375 (adj_fs_blocks(dip->bneeded) - dip->bavail) * dip->bsize);
1376 pkgNEVR = _free(pkgNEVR);
1379 if (adj_fs_blocks(dip->ineeded) > dip->iavail) {
1380 const char * pkgNEVR = fiGetNEVR(fi);
1381 rpmProblemSetAppend(ts->probs, RPMPROB_DISKNODES,
1383 ts->filesystems[i], NULL, NULL,
1384 (adj_fs_blocks(dip->ineeded) - dip->iavail));
1385 pkgNEVR = _free(pkgNEVR);
1388 /*@switchbreak@*/ break;
1390 /*@switchbreak@*/ break;
1393 tei = teFreeIterator(tei);
1395 if (ts->chrootDone) {
1396 /*@-superuser -noeffect @*/
1398 /*@=superuser =noeffect @*/
1400 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
1402 chroot_prefix = NULL;
1404 xx = chdir(ts->currDir);
1407 /*@-noeffectuncon @*/ /* FIX: check rc */
1408 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_STOP, 6, ts->flEntries,
1409 NULL, ts->notifyData));
1410 /*@=noeffectuncon @*/
1412 /* ===============================================
1413 * Free unused memory as soon as possible.
1416 tei = teInitIterator(ts);
1417 while ((fi = teNextFi(tei)) != NULL) {
1420 fi->fps = _free(fi->fps);
1422 tei = teFreeIterator(tei);
1428 /* ===============================================
1429 * If unfiltered problems exist, free memory and return.
1431 if ((ts->transFlags & RPMTRANS_FLAG_BUILD_PROBS)
1432 || (ts->probs->numProblems &&
1433 (okProbs != NULL || rpmProblemSetTrim(ts->probs, okProbs)))
1436 ts->flList = freeFl(ts, ts->flList);
1438 if (psm->ts != NULL)
1439 psm->ts = rpmtsUnlink(psm->ts, "tsRun (problems)");
1440 /*@-nullstate@*/ /* FIX: ts->flList may be NULL */
1441 return ts->orderCount;
1445 /* ===============================================
1446 * Save removed files before erasing.
1448 if (ts->transFlags & (RPMTRANS_FLAG_DIRSTASH | RPMTRANS_FLAG_REPACKAGE)) {
1449 tei = teInitIterator(ts);
1450 while ((fi = teNextFi(tei)) != NULL) {
1453 /*@switchbreak@*/ break;
1455 if (ts->transFlags & RPMTRANS_FLAG_REPACKAGE) {
1456 psm->fi = rpmfiLink(fi, "tsRepackage");
1457 xx = psmStage(psm, PSM_PKGSAVE);
1458 (void) rpmfiUnlink(fi, "tsRepackage");
1461 /*@switchbreak@*/ break;
1464 tei = teFreeIterator(tei);
1467 /* ===============================================
1468 * Install and remove packages.
1471 lastKey = (alKey)-2; /* erased packages have -1 */
1472 tei = teInitIterator(ts);
1473 /*@-branchstate@*/ /* FIX: fi reload needs work */
1474 while ((fi = teNextFi(tei)) != NULL) {
1480 psm->fi = rpmfiLink(fi, "tsInstall");
1484 pkgKey = ts->order[oc].u.addedKey;
1486 rpmMessage(RPMMESS_DEBUG, "========== +++ %s-%s-%s\n",
1487 fi->name, fi->version, fi->release);
1488 h = (fi->h ? headerLink(fi->h, "TR_ADDED install") : NULL);
1490 if (fi->fd == NULL) {
1491 /*@-noeffectuncon @*/ /* FIX: ??? */
1492 fi->fd = ts->notify(fi->h, RPMCALLBACK_INST_OPEN_FILE, 0, 0,
1493 fi->key, ts->notifyData);
1494 /*@=noeffectuncon @*/
1495 if (fi->fd != NULL) {
1498 h = headerFree(h, "TR_ADDED install");
1500 /*@-mustmod@*/ /* LCL: segfault */
1501 rpmrc = rpmReadPackageFile(ts, fi->fd,
1502 "rpmRunTransactions", &h);
1505 if (!(rpmrc == RPMRC_OK || rpmrc == RPMRC_BADSIZE)) {
1506 /*@-noeffectuncon @*/ /* FIX: check rc */
1507 (void) ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE,
1509 fi->key, ts->notifyData);
1510 /*@=noeffectuncon @*/
1513 } else if (fi->h != NULL) {
1514 Header foo = relocateFileList(ts, fi, h, NULL);
1515 h = headerFree(h, "TR_ADDED read free");
1516 h = headerLink(foo, "TR_ADDED relocate xfer");
1517 foo = headerFree(foo, "TR_ADDED relocate");
1519 if (fi->fd != NULL) gotfd = 1;
1524 if (fi->fd != NULL) {
1525 Header hsave = NULL;
1528 hsave = headerLink(fi->h, "TR_ADDED fi->h hsave");
1529 fi->h = headerFree(fi->h, "TR_ADDED fi->h free");
1530 fi->h = headerLink(h, "TR_ADDED fi->h link");
1532 char * fstates = fi->fstates;
1533 fileAction * actions = fi->actions;
1534 uint_32 multiLib = fi->multiLib;
1535 const void * key = fi->key;
1536 rpmRelocation * relocs = fi->relocs;
1546 fi->magic = TFIMAGIC;
1547 fi->type = ts->order[oc].type;
1549 loadFi(ts, fi, h, 1);
1550 fi->fstates = _free(fi->fstates);
1551 fi->fstates = fstates;
1552 fi->actions = _free(fi->actions);
1553 fi->actions = actions;
1554 fi->multiLib = multiLib;
1556 fi->relocs = relocs;
1558 /*@i@*/ fi->fd = fd;
1563 ts->transFlags |= RPMTRANS_FLAG_MULTILIB;
1565 if (psmStage(psm, PSM_PKGINSTALL)) {
1569 fi->h = headerFree(fi->h, "TR_ADDED fi->h free");
1571 fi->h = headerLink(hsave, "TR_ADDED fi->h restore");
1572 hsave = headerFree(hsave, "TR_ADDED hsave free");
1579 h = headerFree(h, "TR_ADDED h free");
1582 /*@-noeffectuncon @*/ /* FIX: check rc */
1583 (void)ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE, 0, 0,
1584 fi->key, ts->notifyData);
1585 /*@=noeffectuncon @*/
1589 /*@switchbreak@*/ break;
1591 rpmMessage(RPMMESS_DEBUG, "========== --- %s-%s-%s\n",
1592 fi->name, fi->version, fi->release);
1594 /* If install failed, then we shouldn't erase. */
1595 if (ts->order[oc].u.removed.dependsOnKey != lastKey) {
1596 if (psmStage(psm, PSM_PKGERASE))
1600 /*@switchbreak@*/ break;
1602 xx = rpmdbSync(ts->rpmdb);
1603 (void) rpmfiUnlink(fi, "tsInstall");
1607 tei = teFreeIterator(tei);
1609 ts->flList = freeFl(ts, ts->flList);
1612 psm->ts = rpmtsUnlink(psm->ts, "tsRun");
1614 /*@-nullstate@*/ /* FIX: ts->flList may be NULL */