2 * \file lib/transaction.c
8 #include <rpmmacro.h> /* XXX for rpmExpand */
11 #include "misc.h" /* XXX stripTrailingChar, splitString, currentDirectory */
14 /*@-redecl -exportheadervar@*/
16 extern const char * chroot_prefix;
17 /*@=redecl =exportheadervar@*/
19 /* XXX FIXME: merge with existing (broken?) tests in system.h */
20 /* portability fiddles */
21 #if STATFS_IN_SYS_STATVFS
23 # include <sys/statvfs.h>
24 #if defined(__LCLINT__)
25 /*@-declundef -exportheader -protoparammatch @*/ /* LCL: missing annotation */
26 extern int statvfs (const char * file, /*@out@*/ struct statvfs * buf)
27 /*@globals fileSystem @*/
28 /*@modifies *buf, fileSystem @*/;
29 /*@=declundef =exportheader =protoparammatch @*/
33 # if STATFS_IN_SYS_VFS
36 # if STATFS_IN_SYS_MOUNT
37 # include <sys/mount.h>
39 # if STATFS_IN_SYS_STATFS
40 # include <sys/statfs.h>
48 /*@access FD_t@*/ /* XXX compared with NULL */
49 /*@access Header@*/ /* XXX compared with NULL */
50 /*@access dbiIndexSet@*/
52 /*@access rpmTransactionSet@*/
55 /*@access rpmProblemSet@*/
56 /*@access rpmProblem@*/
60 struct diskspaceInfo {
61 dev_t dev; /*!< file system device number. */
62 signed long bneeded; /*!< no. of blocks needed. */
63 signed long ineeded; /*!< no. of inodes needed. */
64 int bsize; /*!< file system block size. */
65 signed long bavail; /*!< no. of blocks available. */
66 signed long iavail; /*!< no. of inodes available. */
70 * Adjust for root only reserved space. On linux e2fs, this is 5%.
72 #define adj_fs_blocks(_nb) (((_nb) * 21) / 20)
74 /* argon thought a shift optimization here was a waste of time... he's
76 #define BLOCK_ROUND(size, block) (((size) + (block) - 1) / (block))
78 #define XSTRCMP(a, b) ((!(a) && !(b)) || ((a) && (b) && !strcmp((a), (b))))
82 static /*@null@*/ void * freeFl(rpmTransactionSet ts,
83 /*@only@*/ /*@null@*/ TFI_t flList)
91 for (oc = 0, fi = flList; oc < ts->orderCount; oc++, fi++)
93 flList = _free(flList);
99 void rpmtransSetScriptFd(rpmTransactionSet ts, FD_t fd)
101 /*@-type@*/ /* FIX: cast? */
102 ts->scriptFd = (fd ? fdLink(fd, "rpmtransSetScriptFd") : NULL);
106 int rpmtransGetKeys(const rpmTransactionSet ts, const void *** ep, int * nep)
110 if (nep) *nep = ts->orderCount;
115 *ep = e = xmalloc(ts->orderCount * sizeof(*e));
116 for (oc = 0; oc < ts->orderCount; oc++, e++) {
117 switch (ts->order[oc].type) {
119 if (ts->addedPackages.list) {
120 struct availablePackage * alp;
121 alp = ts->addedPackages.list + ts->order[oc].u.addedIndex;
123 /*@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)) {
198 void rpmProblemSetFree(rpmProblemSet probs)
202 for (i = 0; i < probs->numProblems; i++) {
203 rpmProblem p = probs->probs + i;
204 p->h = headerFree(p->h);
205 p->pkgNEVR = _free(p->pkgNEVR);
206 p->altNEVR = _free(p->altNEVR);
207 p->str1 = _free(p->str1);
213 * Filter a problem set.
214 * As the problem sets are generated in an order solely dependent
215 * on the ordering of the packages in the transaction, and that
216 * ordering can't be changed, the problem sets must be parallel to
217 * one another. Additionally, the filter set must be a subset of the
218 * target set, given the operations available on transaction set.
219 * This is good, as it lets us perform this trim in linear time, rather
220 * then logarithmic or quadratic.
222 * @param filter filter
223 * @param target problem set
224 * @return 0 no problems, 1 if problems remain
226 static int psTrim(rpmProblemSet filter, rpmProblemSet target)
227 /*@modifies target @*/
229 rpmProblem f = filter->probs;
230 rpmProblem t = target->probs;
234 while ((f - filter->probs) < filter->numProblems) {
235 if (!f->ignoreProblem) {
239 while ((t - target->probs) < target->numProblems) {
240 /*@-nullpass@*/ /* LCL: looks good to me */
241 if (f->h == t->h && f->type == t->type && t->key == f->key &&
242 XSTRCMP(f->str1, t->str1))
243 /*@innerbreak@*/ break;
249 if ((t - target->probs) == target->numProblems) {
250 /* this can't happen ;-) let's be sane if it doesn though */
254 t->ignoreProblem = f->ignoreProblem;
259 if ((t - target->probs) < target->numProblems)
267 static int sharedCmp(const void * one, const void * two)
270 const struct sharedFileInfo * a = one;
271 const struct sharedFileInfo * b = two;
273 if (a->otherPkg < b->otherPkg)
275 else if (a->otherPkg > b->otherPkg)
283 static fileAction decideFileFate(const char * dirName,
284 const char * baseName, short dbMode,
285 const char * dbMd5, const char * dbLink, short newMode,
286 const char * newMd5, const char * newLink, int newFlags,
287 rpmtransFlags transFlags)
288 /*@globals fileSystem @*/
289 /*@modifies fileSystem @*/
292 const char * dbAttr, * newAttr;
293 fileTypes dbWhat, newWhat, diskWhat;
296 int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
297 char * filespec = alloca(strlen(dirName) + strlen(baseName) + 1);
299 (void) stpcpy( stpcpy(filespec, dirName), baseName);
301 if (lstat(filespec, &sb)) {
303 * The file doesn't exist on the disk. Create it unless the new
304 * package has marked it as missingok, or allfiles is requested.
306 if (!(transFlags & RPMTRANS_FLAG_ALLFILES) &&
307 (newFlags & RPMFILE_MISSINGOK)) {
308 rpmMessage(RPMMESS_DEBUG, _("%s skipped due to missingok flag\n"),
316 diskWhat = whatis(sb.st_mode);
317 dbWhat = whatis(dbMode);
318 newWhat = whatis(newMode);
320 /* RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
321 them in older packages as well */
322 if (newWhat == XDIR) {
326 if (diskWhat != newWhat) {
328 } else if (newWhat != dbWhat && diskWhat != dbWhat) {
330 } else if (dbWhat != newWhat) {
332 } else if (dbWhat != LINK && dbWhat != REG) {
337 rc = mdfile(filespec, buffer);
340 /* assume the file has been removed, don't freak */
345 } else /* dbWhat == LINK */ {
346 memset(buffer, 0, sizeof(buffer));
347 i = readlink(filespec, buffer, sizeof(buffer) - 1);
349 /* assume the file has been removed, don't freak */
356 /* this order matters - we'd prefer to CREATE the file if at all
357 possible in case something else (like the timestamp) has changed */
359 if (!strcmp(dbAttr, buffer)) {
360 /* this config file has never been modified, so just replace it */
364 if (!strcmp(dbAttr, newAttr)) {
365 /* this file is the same in all versions of this package */
370 * The config file on the disk has been modified, but
371 * the ones in the two packages are different. It would
372 * be nice if RPM was smart enough to at least try and
373 * merge the difference ala CVS, but...
380 static int filecmp(short mode1, const char * md51, const char * link1,
381 short mode2, const char * md52, const char * link2)
384 fileTypes what1 = whatis(mode1);
385 fileTypes what2 = whatis(mode2);
387 if (what1 != what2) return 1;
390 return strcmp(link1, link2);
391 else if (what1 == REG)
392 return strcmp(md51, md52);
399 /* XXX only ts->{probs,rpmdb} modified */
400 static int handleInstInstalledFiles(const rpmTransactionSet ts, TFI_t fi,
401 struct sharedFileInfo * shared,
402 int sharedCount, int reportConflicts)
403 /*@globals fileSystem @*/
404 /*@modifies ts, fi, fileSystem @*/
407 HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
408 rpmdb db = ts->rpmdb;
409 rpmProblemSet probs = ts->probs;
410 rpmtransFlags transFlags = ts->transFlags;
411 rpmTagType oltype, omtype;
414 const char ** otherMd5s;
415 const char ** otherLinks;
416 const char * otherStates;
417 uint_32 * otherFlags;
418 uint_32 * otherSizes;
419 uint_16 * otherModes;
423 rpmdbMatchIterator mi;
425 mi = rpmdbInitIterator(db, RPMDBI_PACKAGES, &shared->otherPkg, sizeof(shared->otherPkg));
426 h = rpmdbNextIterator(mi);
428 mi = rpmdbFreeIterator(mi);
432 xx = hge(h, RPMTAG_FILEMD5S, &omtype, (void **) &otherMd5s, NULL);
433 xx = hge(h, RPMTAG_FILELINKTOS, &oltype, (void **) &otherLinks, NULL);
434 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
435 xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &otherModes, NULL);
436 xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &otherFlags, NULL);
437 xx = hge(h, RPMTAG_FILESIZES, NULL, (void **) &otherSizes, NULL);
439 fi->replaced = xmalloc(sharedCount * sizeof(*fi->replaced));
441 for (i = 0; i < sharedCount; i++, shared++) {
442 int otherFileNum, fileNum;
443 otherFileNum = shared->otherFileNum;
444 fileNum = shared->pkgFileNum;
446 /* XXX another tedious segfault, assume file state normal. */
447 if (otherStates && otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
450 if (XFA_SKIPPING(fi->actions[fileNum]))
453 if (filecmp(otherModes[otherFileNum],
454 otherMd5s[otherFileNum],
455 otherLinks[otherFileNum],
458 fi->flinks[fileNum])) {
460 psAppend(probs, RPMPROB_FILE_CONFLICT, fi->ap,
461 fi->dnl[fi->dil[fileNum]], fi->bnl[fileNum], h, 0);
462 if (!(otherFlags[otherFileNum] | fi->fflags[fileNum])
465 if (!shared->isRemoved)
466 fi->replaced[numReplaced++] = *shared;
471 if ((otherFlags[otherFileNum] | fi->fflags[fileNum]) & RPMFILE_CONFIG) {
472 fi->actions[fileNum] = decideFileFate(
473 fi->dnl[fi->dil[fileNum]],
475 otherModes[otherFileNum],
476 otherMd5s[otherFileNum],
477 otherLinks[otherFileNum],
485 fi->replacedSizes[fileNum] = otherSizes[otherFileNum];
488 otherMd5s = hfd(otherMd5s, omtype);
489 otherLinks = hfd(otherLinks, oltype);
490 mi = rpmdbFreeIterator(mi);
492 fi->replaced = xrealloc(fi->replaced, /* XXX memory leak */
493 sizeof(*fi->replaced) * (numReplaced + 1));
494 fi->replaced[numReplaced].otherPkg = 0;
501 /* XXX only ts->rpmdb modified */
502 static int handleRmvdInstalledFiles(const rpmTransactionSet ts, TFI_t fi,
503 struct sharedFileInfo * shared, int sharedCount)
504 /*@globals fileSystem @*/
505 /*@modifies ts, fi, fileSystem @*/
508 rpmdb db = ts->rpmdb;
510 const char * otherStates;
513 rpmdbMatchIterator mi;
515 mi = rpmdbInitIterator(db, RPMDBI_PACKAGES,
516 &shared->otherPkg, sizeof(shared->otherPkg));
517 h = rpmdbNextIterator(mi);
519 mi = rpmdbFreeIterator(mi);
523 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
525 for (i = 0; i < sharedCount; i++, shared++) {
526 int otherFileNum, fileNum;
527 otherFileNum = shared->otherFileNum;
528 fileNum = shared->pkgFileNum;
530 if (otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
533 fi->actions[fileNum] = FA_SKIP;
536 mi = rpmdbFreeIterator(mi);
542 * Update disk space needs on each partition for this package.
544 /* XXX only ts->{probs,di} modified */
545 static void handleOverlappedFiles(const rpmTransactionSet ts, TFI_t fi)
546 /*@globals fileSystem @*/
547 /*@modifies ts, fi, fileSystem @*/
549 struct diskspaceInfo * dsl = ts->di;
550 rpmProblemSet probs = (ts->ignoreSet & RPMPROB_FILTER_REPLACENEWFILES)
552 hashTable ht = ts->ht;
553 struct diskspaceInfo * ds = NULL;
554 uint_32 fixupSize = 0;
555 char * filespec = NULL;
556 int fileSpecAlloced = 0;
559 for (i = 0; i < fi->fc; i++) {
560 int otherPkgNum, otherFileNum;
564 if (XFA_SKIPPING(fi->actions[i]))
567 j = strlen(fi->dnl[fi->dil[i]]) + strlen(fi->bnl[i]) + 1;
569 if (j > fileSpecAlloced) {
570 fileSpecAlloced = j * 2;
571 filespec = xrealloc(filespec, fileSpecAlloced);
575 (void) stpcpy( stpcpy( filespec, fi->dnl[fi->dil[i]]), fi->bnl[i]);
579 while (ds->bsize && ds->dev != fi->fps[i].entry->dev) ds++;
580 if (!ds->bsize) ds = NULL;
585 * Retrieve all records that apply to this file. Note that the
586 * file info records were built in the same order as the packages
587 * will be installed and removed so the records for an overlapped
588 * files will be sorted in exactly the same order.
590 (void) htGetEntry(ht, &fi->fps[i], (const void ***) &recs, &numRecs, NULL);
593 * If this package is being added, look only at other packages
594 * being added -- removed packages dance to a different tune.
595 * If both this and the other package are being added, overlapped
596 * files must be identical (or marked as a conflict). The
597 * disposition of already installed config files leads to
598 * a small amount of extra complexity.
600 * If this package is being removed, then there are two cases that
601 * need to be worried about:
602 * If the other package is being added, then skip any overlapped files
603 * so that this package removal doesn't nuke the overlapped files
604 * that were just installed.
605 * If both this and the other package are being removed, then each
606 * file removal from preceding packages needs to be skipped so that
607 * the file removal occurs only on the last occurence of an overlapped
608 * file in the transaction set.
612 /* Locate this overlapped file in the set of added/removed packages. */
613 for (j = 0; j < numRecs && recs[j] != fi; j++)
616 /* Find what the previous disposition of this file was. */
617 otherFileNum = -1; /* keep gcc quiet */
618 for (otherPkgNum = j - 1; otherPkgNum >= 0; otherPkgNum--) {
619 /* Added packages need only look at other added packages. */
620 if (fi->type == TR_ADDED && recs[otherPkgNum]->type != TR_ADDED)
621 /*@innercontinue@*/ continue;
623 /* TESTME: there are more efficient searches in the world... */
624 for (otherFileNum = 0; otherFileNum < recs[otherPkgNum]->fc;
627 /* If the addresses are the same, so are the values. */
628 if ((fi->fps + i) == (recs[otherPkgNum]->fps + otherFileNum))
629 /*@innerbreak@*/ break;
631 /* Otherwise, compare fingerprints by value. */
632 /*@-nullpass@*/ /* LCL: looks good to me */
633 if (FP_EQUAL(fi->fps[i], recs[otherPkgNum]->fps[otherFileNum]))
634 /*@innerbreak@*/ break;
638 /* XXX is this test still necessary? */
639 if (recs[otherPkgNum]->actions[otherFileNum] != FA_UNKNOWN)
640 /*@innerbreak@*/ break;
646 if (otherPkgNum < 0) {
647 /* XXX is this test still necessary? */
648 if (fi->actions[i] != FA_UNKNOWN)
649 /*@switchbreak@*/ break;
650 if ((fi->fflags[i] & RPMFILE_CONFIG) &&
651 !lstat(filespec, &sb)) {
652 /* Here is a non-overlapped pre-existing config file. */
653 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
654 ? FA_ALTNAME : FA_BACKUP;
656 fi->actions[i] = FA_CREATE;
658 /*@switchbreak@*/ break;
661 /* Mark added overlapped non-identical files as a conflict. */
662 if (probs && filecmp(recs[otherPkgNum]->fmodes[otherFileNum],
663 recs[otherPkgNum]->fmd5s[otherFileNum],
664 recs[otherPkgNum]->flinks[otherFileNum],
668 psAppend(probs, RPMPROB_NEW_FILE_CONFLICT, fi->ap,
669 filespec, NULL, recs[otherPkgNum]->ap->h, 0);
672 /* Try to get the disk accounting correct even if a conflict. */
673 fixupSize = recs[otherPkgNum]->fsizes[otherFileNum];
675 if ((fi->fflags[i] & RPMFILE_CONFIG) && !lstat(filespec, &sb)) {
676 /* Here is an overlapped pre-existing config file. */
677 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
678 ? FA_ALTNAME : FA_SKIP;
680 fi->actions[i] = FA_CREATE;
682 } /*@switchbreak@*/ break;
684 if (otherPkgNum >= 0) {
685 /* Here is an overlapped added file we don't want to nuke. */
686 if (recs[otherPkgNum]->actions[otherFileNum] != FA_ERASE) {
687 /* On updates, don't remove files. */
688 fi->actions[i] = FA_SKIP;
689 /*@switchbreak@*/ break;
691 /* Here is an overlapped removed file: skip in previous. */
692 recs[otherPkgNum]->actions[otherFileNum] = FA_SKIP;
694 if (XFA_SKIPPING(fi->actions[i]))
695 /*@switchbreak@*/ break;
696 if (fi->fstates && fi->fstates[i] != RPMFILE_STATE_NORMAL)
697 /*@switchbreak@*/ break;
698 if (!(S_ISREG(fi->fmodes[i]) && (fi->fflags[i] & RPMFILE_CONFIG))) {
699 fi->actions[i] = FA_ERASE;
700 /*@switchbreak@*/ break;
703 /* Here is a pre-existing modified config file that needs saving. */
705 if (!mdfile(filespec, mdsum) && strcmp(fi->fmd5s[i], mdsum)) {
706 fi->actions[i] = FA_BACKUP;
707 /*@switchbreak@*/ break;
710 fi->actions[i] = FA_ERASE;
711 /*@switchbreak@*/ break;
715 uint_32 s = BLOCK_ROUND(fi->fsizes[i], ds->bsize);
717 switch (fi->actions[i]) {
723 /*@switchbreak@*/ break;
726 * FIXME: If two packages share a file (same md5sum), and
727 * that file is being replaced on disk, will ds->bneeded get
728 * decremented twice? Quite probably!
732 ds->bneeded -= BLOCK_ROUND(fi->replacedSizes[i], ds->bsize);
733 /*@switchbreak@*/ break;
738 /*@switchbreak@*/ break;
741 /*@switchbreak@*/ break;
744 ds->bneeded -= BLOCK_ROUND(fixupSize, ds->bsize);
747 if (filespec) free(filespec);
752 static int ensureOlder(struct availablePackage * alp, Header old,
754 /*@modifies alp, probs @*/
758 if (old == NULL) return 1;
760 result = rpmVersionCompare(old, alp->h);
763 else if (result > 0) {
765 psAppend(probs, RPMPROB_OLDPACKAGE, alp, NULL, NULL, old, 0);
773 static void skipFiles(const rpmTransactionSet ts, TFI_t fi)
774 /*@globals rpmGlobalMacroContext @*/
775 /*@modifies fi, rpmGlobalMacroContext @*/
777 int noDocs = (ts->transFlags & RPMTRANS_FLAG_NODOCS);
778 char ** netsharedPaths = NULL;
779 const char ** languages;
780 const char * dn, * bn;
781 int dnlen, bnlen, ix;
788 noDocs = rpmExpandNumeric("%{_excludedocs}");
790 { const char *tmpPath = rpmExpand("%{_netsharedpath}", NULL);
792 if (tmpPath && *tmpPath != '%')
793 netsharedPaths = splitString(tmpPath, strlen(tmpPath), ':');
795 tmpPath = _free(tmpPath);
798 s = rpmExpand("%{_install_langs}", NULL);
800 if (!(s && *s != '%'))
803 languages = (const char **) splitString(s, strlen(s), ':');
809 /* Compute directory refcount, skip directory if now empty. */
810 drc = alloca(fi->dc * sizeof(*drc));
811 memset(drc, 0, fi->dc * sizeof(*drc));
812 dff = alloca(fi->dc * sizeof(*dff));
813 memset(dff, 0, fi->dc * sizeof(*dff));
815 for (i = 0; i < fi->fc; i++) {
826 /* Don't bother with skipped files */
827 if (XFA_SKIPPING(fi->actions[i])) {
833 * Skip net shared paths.
834 * Net shared paths are not relative to the current root (though
835 * they do need to take package relocations into account).
837 for (nsp = netsharedPaths; nsp && *nsp; nsp++) {
842 if (strncmp(dn, *nsp, len))
843 /*@innercontinue@*/ continue;
844 /* Only directories or complete file paths can be net shared */
845 if (!(dn[len] == '/' || dn[len] == '\0'))
846 /*@innercontinue@*/ continue;
848 if (len < (dnlen + bnlen))
849 /*@innercontinue@*/ continue;
850 if (strncmp(dn, *nsp, dnlen))
851 /*@innercontinue@*/ continue;
852 if (strncmp(bn, (*nsp) + dnlen, bnlen))
853 /*@innercontinue@*/ continue;
855 /* Only directories or complete file paths can be net shared */
856 if (!((*nsp)[len] == '/' || (*nsp)[len] == '\0'))
857 /*@innercontinue@*/ continue;
860 /*@innerbreak@*/ break;
864 drc[ix]--; dff[ix] = 1;
865 fi->actions[i] = FA_SKIPNETSHARED;
870 * Skip i18n language specific files.
872 if (fi->flangs && languages && *fi->flangs[i]) {
873 const char **lang, *l, *le;
874 for (lang = languages; *lang != NULL; lang++) {
875 if (!strcmp(*lang, "all"))
876 /*@innerbreak@*/ break;
877 for (l = fi->flangs[i]; *l != '\0'; l = le) {
878 for (le = l; *le != '\0' && *le != '|'; le++)
880 if ((le-l) > 0 && !strncmp(*lang, l, (le-l)))
881 /*@innerbreak@*/ break;
882 if (*le == '|') le++; /* skip over | */
885 /*@innerbreak@*/ break;
888 drc[ix]--; dff[ix] = 1;
889 fi->actions[i] = FA_SKIPNSTATE;
895 * Skip documentation if requested.
897 if (noDocs && (fi->fflags[i] & RPMFILE_DOC)) {
898 drc[ix]--; dff[ix] = 1;
899 fi->actions[i] = FA_SKIPNSTATE;
904 /* Skip (now empty) directories that had skipped files. */
905 for (j = 0; j < fi->dc; j++) {
907 if (drc[j]) continue; /* dir still has files. */
908 if (!dff[j]) continue; /* dir was not emptied here. */
910 /* Find parent directory and basename. */
911 dn = fi->dnl[j]; dnlen = strlen(dn) - 1;
912 bn = dn + dnlen; bnlen = 0;
913 while (bn > dn && bn[-1] != '/') {
919 /* If explicitly included in the package, skip the directory. */
920 for (i = 0; i < fi->fc; i++) {
923 if (XFA_SKIPPING(fi->actions[i]))
924 /*@innercontinue@*/ continue;
925 if (whatis(fi->fmodes[i]) != XDIR)
926 /*@innercontinue@*/ continue;
927 dir = fi->dnl[fi->dil[i]];
928 if (strlen(dir) != dnlen)
929 /*@innercontinue@*/ continue;
930 if (strncmp(dir, dn, dnlen))
931 /*@innercontinue@*/ continue;
932 if (strlen(fi->bnl[i]) != bnlen)
933 /*@innercontinue@*/ continue;
934 if (strncmp(fi->bnl[i], bn, bnlen))
935 /*@innercontinue@*/ continue;
936 rpmMessage(RPMMESS_DEBUG, _("excluding directory %s\n"), dn);
937 fi->actions[i] = FA_SKIPNSTATE;
938 /*@innerbreak@*/ break;
942 if (netsharedPaths) freeSplitString(netsharedPaths);
943 #ifdef DYING /* XXX freeFi will deal with this later. */
944 fi->flangs = _free(fi->flangs);
946 if (languages) freeSplitString((char **)languages);
950 * Iterator across transaction elements, forward on install, backward on erase.
952 struct tsIterator_s {
953 /*@kept@*/ rpmTransactionSet ts; /*!< transaction set. */
954 int reverse; /*!< reversed traversal? */
955 int ocsave; /*!< last returned iterator index. */
956 int oc; /*!< iterator index. */
960 * Return transaction element order count.
961 * @param a transaction element iterator
962 * @return element order count
964 static int tsGetOc(void * a)
967 struct tsIterator_s * iter = a;
968 int oc = iter->ocsave;
973 * Return transaction element available package pointer.
974 * @param a transaction element iterator
975 * @return available package pointer
977 static /*@dependent@*/ struct availablePackage * tsGetAlp(void * a)
980 struct tsIterator_s * iter = a;
981 struct availablePackage * alp = NULL;
982 int oc = iter->ocsave;
986 rpmTransactionSet ts = iter->ts;
987 TFI_t fi = ts->flList + oc;
988 if (ts->addedPackages.list && fi->type == TR_ADDED)
989 alp = ts->addedPackages.list + ts->order[oc].u.addedIndex;
996 * Destroy transaction element iterator.
997 * @param a transaction element iterator
998 * @return NULL always
1000 static /*@null@*/ void * tsFreeIterator(/*@only@*//*@null@*/ const void * a)
1007 * Create transaction element iterator.
1008 * @param a transaction set
1009 * @return transaction element iterator
1011 static void * tsInitIterator(/*@kept@*/ const void * a)
1014 rpmTransactionSet ts = (void *)a;
1015 struct tsIterator_s * iter = NULL;
1017 iter = xcalloc(1, sizeof(*iter));
1019 iter->reverse = ((ts->transFlags & RPMTRANS_FLAG_REVERSE) ? 1 : 0);
1020 iter->oc = (iter->reverse ? (ts->orderCount - 1) : 0);
1021 iter->ocsave = iter->oc;
1026 * Return next transaction element's file info.
1027 * @param a file info iterator
1028 * @return next index, -1 on termination
1030 static /*@dependent@*/ TFI_t tsNextIterator(void * a)
1033 struct tsIterator_s * iter = a;
1034 rpmTransactionSet ts = iter->ts;
1038 if (iter->reverse) {
1039 if (iter->oc >= 0) oc = iter->oc--;
1041 if (iter->oc < ts->orderCount) oc = iter->oc++;
1045 fi = ts->flList + oc;
1049 #define NOTIFY(_ts, _al) if ((_ts)->notify) (void) (_ts)->notify _al
1051 int rpmRunTransactions( rpmTransactionSet ts,
1052 rpmCallbackFunction notify, rpmCallbackData notifyData,
1053 rpmProblemSet okProbs, rpmProblemSet * newProbs,
1054 rpmtransFlags transFlags, rpmprobFilterFlags ignoreSet)
1058 struct availablePackage * alp;
1059 int totalFileCount = 0;
1061 struct diskspaceInfo * dip;
1062 struct sharedFileInfo * shared, * sharedList;
1067 fingerPrintCache fpc;
1068 struct psm_s psmbuf;
1069 PSM_t psm = &psmbuf;
1073 /* FIXME: what if the same package is included in ts twice? */
1075 ts->transFlags = transFlags;
1076 if (ts->transFlags & RPMTRANS_FLAG_NOSCRIPTS)
1077 ts->transFlags |= (_noTransScripts | _noTransTriggers);
1078 if (ts->transFlags & RPMTRANS_FLAG_NOTRIGGERS)
1079 ts->transFlags |= _noTransTriggers;
1081 /* XXX MULTILIB is broken, as packages can and do execute /sbin/ldconfig. */
1082 if (ts->transFlags & (RPMTRANS_FLAG_JUSTDB | RPMTRANS_FLAG_MULTILIB))
1083 ts->transFlags |= (_noTransScripts | _noTransTriggers);
1085 ts->notify = notify;
1086 ts->notifyData = notifyData;
1088 ts->probs = *newProbs = psCreate();
1090 ts->ignoreSet = ignoreSet;
1091 ts->currDir = _free(ts->currDir);
1092 ts->currDir = currentDirectory();
1094 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
1095 ts->id = (int_32) time(NULL);
1097 memset(psm, 0, sizeof(*psm));
1102 /* Get available space on mounted file systems. */
1103 if (!(ts->ignoreSet & RPMPROB_FILTER_DISKSPACE) &&
1104 !rpmGetFilesystemList(&ts->filesystems, &ts->filesystemCount)) {
1107 ts->di = _free(ts->di);
1108 dip = ts->di = xcalloc((ts->filesystemCount + 1), sizeof(*ts->di));
1110 for (i = 0; (i < ts->filesystemCount) && dip; i++) {
1111 #if STATFS_IN_SYS_STATVFS
1113 memset(&sfb, 0, sizeof(sfb));
1114 if (statvfs(ts->filesystems[i], &sfb))
1118 /* This platform has the 4-argument version of the statfs call. The last two
1119 * should be the size of struct statfs and 0, respectively. The 0 is the
1120 * filesystem type, and is always 0 when statfs is called on a mounted
1121 * filesystem, as we're doing.
1123 memset(&sfb, 0, sizeof(sfb));
1124 if (statfs(ts->filesystems[i], &sfb, sizeof(sfb), 0))
1126 memset(&sfb, 0, sizeof(sfb));
1127 if (statfs(ts->filesystems[i], &sfb))
1133 ts->di[i].bsize = sfb.f_bsize;
1134 ts->di[i].bneeded = 0;
1135 ts->di[i].ineeded = 0;
1136 #ifdef STATFS_HAS_F_BAVAIL
1137 ts->di[i].bavail = sfb.f_bavail;
1139 /* FIXME: the statfs struct doesn't have a member to tell how many blocks are
1140 * available for non-superusers. f_blocks - f_bfree is probably too big, but
1141 * it's about all we can do.
1143 ts->di[i].bavail = sfb.f_blocks - sfb.f_bfree;
1145 /* XXX Avoid FAT and other file systems that have not inodes. */
1146 ts->di[i].iavail = !(sfb.f_ffree == 0 && sfb.f_files == 0)
1149 xx = stat(ts->filesystems[i], &sb);
1150 ts->di[i].dev = sb.st_dev;
1154 if (dip) ts->di[i].bsize = 0;
1157 /* ===============================================
1158 * For packages being installed:
1159 * - verify package arch/os.
1160 * - verify package epoch:version-release is newer.
1162 * For packages being removed:
1165 /* The ordering doesn't matter here */
1166 if (ts->addedPackages.list != NULL)
1167 for (alp = ts->addedPackages.list;
1168 (alp - ts->addedPackages.list) < ts->addedPackages.size;
1171 if (!archOkay(alp->h) && !(ts->ignoreSet & RPMPROB_FILTER_IGNOREARCH))
1172 psAppend(ts->probs, RPMPROB_BADARCH, alp, NULL, NULL, NULL, 0);
1174 if (!osOkay(alp->h) && !(ts->ignoreSet & RPMPROB_FILTER_IGNOREOS))
1175 psAppend(ts->probs, RPMPROB_BADOS, alp, NULL, NULL, NULL, 0);
1177 if (!(ts->ignoreSet & RPMPROB_FILTER_OLDPACKAGE)) {
1178 rpmdbMatchIterator mi;
1180 mi = rpmdbInitIterator(ts->rpmdb, RPMTAG_NAME, alp->name, 0);
1181 while ((oldH = rpmdbNextIterator(mi)) != NULL)
1182 xx = ensureOlder(alp, oldH, ts->probs);
1183 mi = rpmdbFreeIterator(mi);
1186 /* XXX multilib should not display "already installed" problems */
1187 if (!(ts->ignoreSet & RPMPROB_FILTER_REPLACEPKG) && !alp->multiLib) {
1188 rpmdbMatchIterator mi;
1189 mi = rpmdbInitIterator(ts->rpmdb, RPMTAG_NAME, alp->name, 0);
1190 xx = rpmdbSetIteratorRE(mi, RPMTAG_VERSION,
1191 RPMMIRE_DEFAULT, alp->version);
1192 xx = rpmdbSetIteratorRE(mi, RPMTAG_RELEASE,
1193 RPMMIRE_DEFAULT, alp->release);
1195 while (rpmdbNextIterator(mi) != NULL) {
1196 psAppend(ts->probs, RPMPROB_PKG_INSTALLED, alp,
1197 NULL, NULL, NULL, 0);
1198 /*@innerbreak@*/ break;
1200 mi = rpmdbFreeIterator(mi);
1203 totalFileCount += alp->filesCount;
1207 /* FIXME: it seems a bit silly to read in all of these headers twice */
1208 /* The ordering doesn't matter here */
1209 if (ts->numRemovedPackages > 0) {
1210 rpmdbMatchIterator mi;
1214 mi = rpmdbInitIterator(ts->rpmdb, RPMDBI_PACKAGES, NULL, 0);
1215 xx = rpmdbAppendIterator(mi, ts->removedPackages, ts->numRemovedPackages);
1216 while ((h = rpmdbNextIterator(mi)) != NULL) {
1217 if (headerGetEntry(h, RPMTAG_BASENAMES, NULL, NULL, &fileCount))
1218 totalFileCount += fileCount;
1220 mi = rpmdbFreeIterator(mi);
1223 /* ===============================================
1224 * Initialize file list:
1226 ts->flEntries = ts->addedPackages.size + ts->numRemovedPackages;
1227 ts->flList = xcalloc(ts->flEntries, sizeof(*ts->flList));
1230 * FIXME?: we'd be better off assembling one very large file list and
1231 * calling fpLookupList only once. I'm not sure that the speedup is
1232 * worth the trouble though.
1234 tsi = tsInitIterator(ts);
1235 while ((fi = tsNextIterator(tsi)) != NULL) {
1237 fi->magic = TFIMAGIC;
1239 /* XXX watchout: fi->type must be set for tsGetAlp() to "work" */
1240 fi->type = ts->order[oc].type;
1244 /* XXX watchout: fi->type must be set for tsGetAlp() to "work" */
1245 fi->ap = tsGetAlp(tsi);
1247 loadFi(ts, fi, fi->ap->h, 1);
1248 /* XXX free fi->ap->h here if/when possible */
1252 /* Skip netshared paths, not our i18n files, and excluded docs */
1254 /*@switchbreak@*/ break;
1257 fi->record = ts->order[oc].u.removed.dboffset;
1258 /* Retrieve erased package header from the database. */
1259 { rpmdbMatchIterator mi;
1261 mi = rpmdbInitIterator(ts->rpmdb, RPMDBI_PACKAGES,
1262 &fi->record, sizeof(fi->record));
1263 if ((fi->h = rpmdbNextIterator(mi)) != NULL)
1264 fi->h = headerLink(fi->h);
1265 mi = rpmdbFreeIterator(mi);
1267 if (fi->h == NULL) {
1271 /* XXX header arg unused. */
1272 loadFi(ts, fi, fi->h, 0);
1273 /*@switchbreak@*/ break;
1278 fi->fps = xmalloc(fi->fc * sizeof(*fi->fps));
1280 tsi = tsFreeIterator(tsi);
1282 if (!ts->chrootDone) {
1284 /*@-superuser -noeffect @*/
1285 xx = chroot(ts->rootDir);
1286 /*@=superuser =noeffect @*/
1288 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 1;
1291 chroot_prefix = ts->rootDir;
1296 ts->ht = htCreate(totalFileCount * 2, 0, 0, fpHashFunction, fpEqual);
1297 fpc = fpCacheCreate(totalFileCount);
1299 /* ===============================================
1300 * Add fingerprint for each file not skipped.
1302 tsi = tsInitIterator(ts);
1303 while ((fi = tsNextIterator(tsi)) != NULL) {
1304 fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fi->fc, fi->fps);
1305 for (i = 0; i < fi->fc; i++) {
1306 if (XFA_SKIPPING(fi->actions[i]))
1307 /*@innercontinue@*/ continue;
1308 /*@-dependenttrans@*/
1309 htAddEntry(ts->ht, fi->fps + i, fi);
1310 /*@=dependenttrans@*/
1313 tsi = tsFreeIterator(tsi);
1315 /*@-noeffectuncon @*/ /* FIX: check rc */
1316 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_START, 6, ts->flEntries,
1317 NULL, ts->notifyData));
1318 /*@=noeffectuncon@*/
1320 /* ===============================================
1321 * Compute file disposition for each package in transaction set.
1323 tsi = tsInitIterator(ts);
1324 while ((fi = tsNextIterator(tsi)) != NULL) {
1325 dbiIndexSet * matches;
1328 /*@-noeffectuncon @*/ /* FIX: check rc */
1329 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_PROGRESS, (fi - ts->flList),
1330 ts->flEntries, NULL, ts->notifyData));
1331 /*@=noeffectuncon@*/
1333 if (fi->fc == 0) continue;
1335 /* Extract file info for all files in this package from the database. */
1336 matches = xcalloc(fi->fc, sizeof(*matches));
1337 if (rpmdbFindFpList(ts->rpmdb, fi->fps, matches, fi->fc))
1338 return 1; /* XXX WTFO? */
1341 for (i = 0; i < fi->fc; i++)
1342 numShared += dbiIndexSetCount(matches[i]);
1344 /* Build sorted file info list for this package. */
1345 shared = sharedList = xcalloc((numShared + 1), sizeof(*sharedList));
1346 for (i = 0; i < fi->fc; i++) {
1348 * Take care not to mark files as replaced in packages that will
1349 * have been removed before we will get here.
1351 for (j = 0; j < dbiIndexSetCount(matches[i]); j++) {
1353 ro = dbiIndexRecordOffset(matches[i], j);
1355 for (k = 0; ro != knownBad && k < ts->orderCount; k++) {
1356 switch (ts->order[k].type) {
1358 if (ts->order[k].u.removed.dboffset == ro)
1360 /*@switchbreak@*/ break;
1362 /*@switchbreak@*/ break;
1366 shared->pkgFileNum = i;
1367 shared->otherPkg = dbiIndexRecordOffset(matches[i], j);
1368 shared->otherFileNum = dbiIndexRecordFileNumber(matches[i], j);
1369 shared->isRemoved = (knownBad == ro);
1372 matches[i] = dbiFreeIndexSet(matches[i]);
1374 numShared = shared - sharedList;
1375 shared->otherPkg = -1;
1376 matches = _free(matches);
1378 /* Sort file info by other package index (otherPkg) */
1379 qsort(sharedList, numShared, sizeof(*shared), sharedCmp);
1381 /* For all files from this package that are in the database ... */
1382 for (i = 0; i < numShared; i = nexti) {
1385 shared = sharedList + i;
1387 /* Find the end of the files in the other package. */
1388 for (nexti = i + 1; nexti < numShared; nexti++) {
1389 if (sharedList[nexti].otherPkg != shared->otherPkg)
1390 /*@innerbreak@*/ break;
1393 /* Is this file from a package being removed? */
1395 for (j = 0; j < ts->numRemovedPackages; j++) {
1396 if (ts->removedPackages[j] != shared->otherPkg)
1397 /*@innercontinue@*/ continue;
1399 /*@innerbreak@*/ break;
1402 /* Determine the fate of each file. */
1405 xx = handleInstInstalledFiles(ts, fi, shared, nexti - i,
1406 !(beingRemoved || (ts->ignoreSet & RPMPROB_FILTER_REPLACEOLDFILES)));
1407 /*@switchbreak@*/ break;
1410 xx = handleRmvdInstalledFiles(ts, fi, shared, nexti - i);
1411 /*@switchbreak@*/ break;
1417 /* Update disk space needs on each partition for this package. */
1418 handleOverlappedFiles(ts, fi);
1420 /* Check added package has sufficient space on each partition used. */
1423 if (!(ts->di && fi->fc))
1424 /*@switchbreak@*/ break;
1425 for (i = 0; i < ts->filesystemCount; i++) {
1429 /* XXX Avoid FAT and other file systems that have not inodes. */
1430 if (dip->iavail <= 0)
1431 /*@innercontinue@*/ continue;
1433 if (adj_fs_blocks(dip->bneeded) > dip->bavail)
1434 psAppend(ts->probs, RPMPROB_DISKSPACE, fi->ap,
1435 ts->filesystems[i], NULL, NULL,
1436 (adj_fs_blocks(dip->bneeded) - dip->bavail) * dip->bsize);
1438 if (adj_fs_blocks(dip->ineeded) > dip->iavail)
1439 psAppend(ts->probs, RPMPROB_DISKNODES, fi->ap,
1440 ts->filesystems[i], NULL, NULL,
1441 (adj_fs_blocks(dip->ineeded) - dip->iavail));
1443 /*@switchbreak@*/ break;
1445 /*@switchbreak@*/ break;
1448 tsi = tsFreeIterator(tsi);
1450 if (ts->chrootDone) {
1451 /*@-superuser -noeffect @*/
1453 /*@=superuser =noeffect @*/
1455 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
1457 chroot_prefix = NULL;
1459 xx = chdir(ts->currDir);
1462 /*@-noeffectuncon @*/ /* FIX: check rc */
1463 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_STOP, 6, ts->flEntries,
1464 NULL, ts->notifyData));
1465 /*@=noeffectuncon @*/
1467 /* ===============================================
1468 * Free unused memory as soon as possible.
1471 tsi = tsInitIterator(ts);
1472 while ((fi = tsNextIterator(tsi)) != NULL) {
1476 fi->fps = _free(fi->fps);
1478 tsi = tsFreeIterator(tsi);
1484 /* ===============================================
1485 * If unfiltered problems exist, free memory and return.
1487 if ((ts->transFlags & RPMTRANS_FLAG_BUILD_PROBS) ||
1488 (ts->probs->numProblems && (!okProbs || psTrim(okProbs, ts->probs))))
1490 *newProbs = ts->probs;
1492 ts->flList = freeFl(ts, ts->flList);
1495 return ts->orderCount;
1499 /* ===============================================
1500 * Save removed files before erasing.
1502 if (ts->transFlags & (RPMTRANS_FLAG_DIRSTASH | RPMTRANS_FLAG_REPACKAGE)) {
1503 tsi = tsInitIterator(ts);
1504 while ((fi = tsNextIterator(tsi)) != NULL) {
1508 /*@switchbreak@*/ break;
1510 if (ts->transFlags & RPMTRANS_FLAG_REPACKAGE)
1511 xx = psmStage(psm, PSM_PKGSAVE);
1512 /*@switchbreak@*/ break;
1515 tsi = tsFreeIterator(tsi);
1518 /* ===============================================
1519 * Install and remove packages.
1522 lastFailed = -2; /* erased packages have -1 */
1523 tsi = tsInitIterator(ts);
1524 while ((fi = tsNextIterator(tsi)) != NULL) {
1532 alp = tsGetAlp(tsi);
1533 assert(alp == fi->ap);
1534 i = alp - ts->addedPackages.list;
1536 rpmMessage(RPMMESS_DEBUG, "========== +++ %s-%s-%s\n",
1537 fi->name, fi->version, fi->release);
1538 h = (fi->h ? headerLink(fi->h) : NULL);
1540 if (alp->fd == NULL) {
1541 alp->fd = ts->notify(fi->h, RPMCALLBACK_INST_OPEN_FILE, 0, 0,
1542 alp->key, ts->notifyData);
1548 /*@-mustmod@*/ /* LCL: segfault */
1549 rpmrc = rpmReadPackageHeader(alp->fd, &h, NULL, NULL, NULL);
1551 if (!(rpmrc == RPMRC_OK || rpmrc == RPMRC_BADSIZE)) {
1552 /*@-noeffectuncon @*/ /* FIX: check rc */
1553 (void)ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE,
1554 0, 0, alp->key, ts->notifyData);
1555 /*@=noeffectuncon @*/
1558 } else if (fi->h != NULL) {
1559 Header foo = relocateFileList(ts, fi, alp, h, NULL);
1561 h = headerLink(foo);
1562 foo = headerFree(foo);
1564 if (alp->fd) gotfd = 1;
1570 Header hsave = NULL;
1573 hsave = headerLink(fi->h);
1574 fi->h = headerFree(fi->h);
1575 fi->h = headerLink(h);
1577 char * fstates = fi->fstates;
1578 fileAction * actions = fi->actions;
1583 fi->magic = TFIMAGIC;
1584 fi->type = ts->order[oc].type;
1585 fi->ap = tsGetAlp(tsi);
1587 loadFi(ts, fi, h, 1);
1588 fi->fstates = _free(fi->fstates);
1589 fi->fstates = fstates;
1590 fi->actions = _free(fi->actions);
1591 fi->actions = actions;
1594 ts->transFlags |= RPMTRANS_FLAG_MULTILIB;
1596 assert(alp == fi->ap);
1597 if (psmStage(psm, PSM_PKGINSTALL)) {
1601 fi->h = headerFree(fi->h);
1603 fi->h = headerLink(hsave);
1604 hsave = headerFree(hsave);
1614 /*@-noeffectuncon @*/ /* FIX: check rc */
1615 (void)ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE, 0, 0,
1616 alp->key, ts->notifyData);
1617 /*@=noeffectuncon @*/
1621 /*@switchbreak@*/ break;
1623 rpmMessage(RPMMESS_DEBUG, "========== --- %s-%s-%s\n",
1624 fi->name, fi->version, fi->release);
1626 /* If install failed, then we shouldn't erase. */
1627 if (ts->order[oc].u.removed.dependsOnIndex != lastFailed) {
1628 if (psmStage(psm, PSM_PKGERASE))
1632 /*@switchbreak@*/ break;
1634 xx = rpmdbSync(ts->rpmdb);
1636 tsi = tsFreeIterator(tsi);
1638 ts->flList = freeFl(ts, ts->flList);