2 * \file lib/transaction.c
8 #include <rpmmacro.h> /* XXX for rpmExpand */
11 #include "legacy.h" /* XXX mdfile */
12 #include "misc.h" /* XXX stripTrailingChar, splitString, currentDirectory */
15 /*@-redecl -exportheadervar@*/
17 extern const char * chroot_prefix;
18 /*@=redecl =exportheadervar@*/
20 /* XXX FIXME: merge with existing (broken?) tests in system.h */
21 /* portability fiddles */
22 #if STATFS_IN_SYS_STATVFS
24 # include <sys/statvfs.h>
25 #if defined(__LCLINT__)
26 /*@-declundef -exportheader -protoparammatch @*/ /* LCL: missing annotation */
27 extern int statvfs (const char * file, /*@out@*/ struct statvfs * buf)
28 /*@globals fileSystem @*/
29 /*@modifies *buf, fileSystem @*/;
30 /*@=declundef =exportheader =protoparammatch @*/
34 # if STATFS_IN_SYS_VFS
37 # if STATFS_IN_SYS_MOUNT
38 # include <sys/mount.h>
40 # if STATFS_IN_SYS_STATFS
41 # include <sys/statfs.h>
49 /*@access FD_t@*/ /* XXX compared with NULL */
50 /*@access Header@*/ /* XXX compared with NULL */
51 /*@access dbiIndexSet@*/
53 /*@access rpmTransactionSet@*/
56 /*@access rpmProblemSet@*/
57 /*@access rpmProblem@*/
61 struct diskspaceInfo {
62 dev_t dev; /*!< file system device number. */
63 signed long bneeded; /*!< no. of blocks needed. */
64 signed long ineeded; /*!< no. of inodes needed. */
65 int bsize; /*!< file system block size. */
66 signed long bavail; /*!< no. of blocks available. */
67 signed long iavail; /*!< no. of inodes available. */
71 * Adjust for root only reserved space. On linux e2fs, this is 5%.
73 #define adj_fs_blocks(_nb) (((_nb) * 21) / 20)
75 /* argon thought a shift optimization here was a waste of time... he's
77 #define BLOCK_ROUND(size, block) (((size) + (block) - 1) / (block))
79 #define XSTRCMP(a, b) ((!(a) && !(b)) || ((a) && (b) && !strcmp((a), (b))))
83 static /*@null@*/ void * freeFl(rpmTransactionSet ts,
84 /*@only@*/ /*@null@*/ TFI_t flList)
92 for (oc = 0, fi = flList; oc < ts->orderCount; oc++, fi++)
94 flList = _free(flList);
100 void rpmtransSetScriptFd(rpmTransactionSet ts, FD_t fd)
102 /*@-type@*/ /* FIX: cast? */
103 ts->scriptFd = (fd ? fdLink(fd, "rpmtransSetScriptFd") : NULL);
107 int rpmtransGetKeys(const rpmTransactionSet ts, const void *** ep, int * nep)
111 if (nep) *nep = ts->orderCount;
116 *ep = e = xmalloc(ts->orderCount * sizeof(*e));
117 for (oc = 0; oc < ts->orderCount; oc++, e++) {
118 switch (ts->order[oc].type) {
120 if (ts->addedPackages.list) {
121 struct availablePackage * alp;
122 alp = ts->addedPackages.list + ts->order[oc].u.addedIndex;
124 /*@switchbreak@*/ break;
129 /*@-mods@*/ /* FIX: double indirection. */
132 /*@switchbreak@*/ break;
141 static int archOkay(Header h)
147 /* make sure we're trying to install this on the proper architecture */
148 (void) headerGetEntry(h, RPMTAG_ARCH, &type, (void **) &pkgArch, &count);
150 if (type == RPM_INT8_TYPE) {
154 /* old arch handling */
155 rpmGetArchInfo(NULL, &archNum);
156 pkgArchNum = pkgArch;
157 if (archNum != *pkgArchNum) {
163 /* new arch handling */
164 if (!rpmMachineScore(RPM_MACHTABLE_INSTARCH, pkgArch)) {
174 static int osOkay(Header h)
180 /* make sure we're trying to install this on the proper os */
181 (void) headerGetEntry(h, RPMTAG_OS, &type, (void **) &pkgOs, &count);
183 if (type == RPM_INT8_TYPE) {
184 /* v1 packages and v2 packages both used improper OS numbers, so just
185 deal with it hope things work */
190 /* new os handling */
191 if (!rpmMachineScore(RPM_MACHTABLE_INSTOS, pkgOs)) {
199 void rpmProblemSetFree(rpmProblemSet probs)
203 for (i = 0; i < probs->numProblems; i++) {
204 rpmProblem p = probs->probs + i;
205 p->h = headerFree(p->h);
206 p->pkgNEVR = _free(p->pkgNEVR);
207 p->altNEVR = _free(p->altNEVR);
208 p->str1 = _free(p->str1);
214 * Filter a problem set.
215 * As the problem sets are generated in an order solely dependent
216 * on the ordering of the packages in the transaction, and that
217 * ordering can't be changed, the problem sets must be parallel to
218 * one another. Additionally, the filter set must be a subset of the
219 * target set, given the operations available on transaction set.
220 * This is good, as it lets us perform this trim in linear time, rather
221 * then logarithmic or quadratic.
223 * @param filter filter
224 * @param target problem set
225 * @return 0 no problems, 1 if problems remain
227 static int psTrim(rpmProblemSet filter, rpmProblemSet target)
228 /*@modifies target @*/
230 rpmProblem f = filter->probs;
231 rpmProblem t = target->probs;
235 while ((f - filter->probs) < filter->numProblems) {
236 if (!f->ignoreProblem) {
240 while ((t - target->probs) < target->numProblems) {
241 /*@-nullpass@*/ /* LCL: looks good to me */
242 if (f->h == t->h && f->type == t->type && t->key == f->key &&
243 XSTRCMP(f->str1, t->str1))
244 /*@innerbreak@*/ break;
250 if ((t - target->probs) == target->numProblems) {
251 /* this can't happen ;-) let's be sane if it doesn though */
255 t->ignoreProblem = f->ignoreProblem;
260 if ((t - target->probs) < target->numProblems)
268 static int sharedCmp(const void * one, const void * two)
271 const struct sharedFileInfo * a = one;
272 const struct sharedFileInfo * b = two;
274 if (a->otherPkg < b->otherPkg)
276 else if (a->otherPkg > b->otherPkg)
284 static fileAction decideFileFate(const char * dirName,
285 const char * baseName, short dbMode,
286 const char * dbMd5, const char * dbLink, short newMode,
287 const char * newMd5, const char * newLink, int newFlags,
288 rpmtransFlags transFlags)
289 /*@globals fileSystem @*/
290 /*@modifies fileSystem @*/
293 const char * dbAttr, * newAttr;
294 fileTypes dbWhat, newWhat, diskWhat;
297 int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
298 char * filespec = alloca(strlen(dirName) + strlen(baseName) + 1);
300 (void) stpcpy( stpcpy(filespec, dirName), baseName);
302 if (lstat(filespec, &sb)) {
304 * The file doesn't exist on the disk. Create it unless the new
305 * package has marked it as missingok, or allfiles is requested.
307 if (!(transFlags & RPMTRANS_FLAG_ALLFILES) &&
308 (newFlags & RPMFILE_MISSINGOK)) {
309 rpmMessage(RPMMESS_DEBUG, _("%s skipped due to missingok flag\n"),
317 diskWhat = whatis(sb.st_mode);
318 dbWhat = whatis(dbMode);
319 newWhat = whatis(newMode);
321 /* RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
322 them in older packages as well */
323 if (newWhat == XDIR) {
327 if (diskWhat != newWhat) {
329 } else if (newWhat != dbWhat && diskWhat != dbWhat) {
331 } else if (dbWhat != newWhat) {
333 } else if (dbWhat != LINK && dbWhat != REG) {
338 rc = mdfile(filespec, buffer);
341 /* assume the file has been removed, don't freak */
346 } else /* dbWhat == LINK */ {
347 memset(buffer, 0, sizeof(buffer));
348 i = readlink(filespec, buffer, sizeof(buffer) - 1);
350 /* assume the file has been removed, don't freak */
357 /* this order matters - we'd prefer to CREATE the file if at all
358 possible in case something else (like the timestamp) has changed */
360 if (!strcmp(dbAttr, buffer)) {
361 /* this config file has never been modified, so just replace it */
365 if (!strcmp(dbAttr, newAttr)) {
366 /* this file is the same in all versions of this package */
371 * The config file on the disk has been modified, but
372 * the ones in the two packages are different. It would
373 * be nice if RPM was smart enough to at least try and
374 * merge the difference ala CVS, but...
381 static int filecmp(short mode1, const char * md51, const char * link1,
382 short mode2, const char * md52, const char * link2)
385 fileTypes what1 = whatis(mode1);
386 fileTypes what2 = whatis(mode2);
388 if (what1 != what2) return 1;
391 return strcmp(link1, link2);
392 else if (what1 == REG)
393 return strcmp(md51, md52);
400 /* XXX only ts->{probs,rpmdb} modified */
401 static int handleInstInstalledFiles(const rpmTransactionSet ts, TFI_t fi,
402 struct sharedFileInfo * shared,
403 int sharedCount, int reportConflicts)
404 /*@globals fileSystem @*/
405 /*@modifies ts, fi, fileSystem @*/
408 HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
409 rpmdb db = ts->rpmdb;
410 rpmProblemSet probs = ts->probs;
411 rpmtransFlags transFlags = ts->transFlags;
412 rpmTagType oltype, omtype;
415 const char ** otherMd5s;
416 const char ** otherLinks;
417 const char * otherStates;
418 uint_32 * otherFlags;
419 uint_32 * otherSizes;
420 uint_16 * otherModes;
424 rpmdbMatchIterator mi;
426 mi = rpmdbInitIterator(db, RPMDBI_PACKAGES, &shared->otherPkg, sizeof(shared->otherPkg));
427 h = rpmdbNextIterator(mi);
429 mi = rpmdbFreeIterator(mi);
433 xx = hge(h, RPMTAG_FILEMD5S, &omtype, (void **) &otherMd5s, NULL);
434 xx = hge(h, RPMTAG_FILELINKTOS, &oltype, (void **) &otherLinks, NULL);
435 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
436 xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &otherModes, NULL);
437 xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &otherFlags, NULL);
438 xx = hge(h, RPMTAG_FILESIZES, NULL, (void **) &otherSizes, NULL);
440 fi->replaced = xmalloc(sharedCount * sizeof(*fi->replaced));
442 for (i = 0; i < sharedCount; i++, shared++) {
443 int otherFileNum, fileNum;
444 otherFileNum = shared->otherFileNum;
445 fileNum = shared->pkgFileNum;
447 /* XXX another tedious segfault, assume file state normal. */
448 if (otherStates && otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
451 if (XFA_SKIPPING(fi->actions[fileNum]))
454 if (filecmp(otherModes[otherFileNum],
455 otherMd5s[otherFileNum],
456 otherLinks[otherFileNum],
459 fi->flinks[fileNum])) {
461 psAppend(probs, RPMPROB_FILE_CONFLICT, fi->ap,
462 fi->dnl[fi->dil[fileNum]], fi->bnl[fileNum], h, 0);
463 if (!(otherFlags[otherFileNum] | fi->fflags[fileNum])
466 if (!shared->isRemoved)
467 fi->replaced[numReplaced++] = *shared;
472 if ((otherFlags[otherFileNum] | fi->fflags[fileNum]) & RPMFILE_CONFIG) {
473 fi->actions[fileNum] = decideFileFate(
474 fi->dnl[fi->dil[fileNum]],
476 otherModes[otherFileNum],
477 otherMd5s[otherFileNum],
478 otherLinks[otherFileNum],
486 fi->replacedSizes[fileNum] = otherSizes[otherFileNum];
489 otherMd5s = hfd(otherMd5s, omtype);
490 otherLinks = hfd(otherLinks, oltype);
491 mi = rpmdbFreeIterator(mi);
493 fi->replaced = xrealloc(fi->replaced, /* XXX memory leak */
494 sizeof(*fi->replaced) * (numReplaced + 1));
495 fi->replaced[numReplaced].otherPkg = 0;
502 /* XXX only ts->rpmdb modified */
503 static int handleRmvdInstalledFiles(const rpmTransactionSet ts, TFI_t fi,
504 struct sharedFileInfo * shared, int sharedCount)
505 /*@globals fileSystem @*/
506 /*@modifies ts, fi, fileSystem @*/
509 rpmdb db = ts->rpmdb;
511 const char * otherStates;
514 rpmdbMatchIterator mi;
516 mi = rpmdbInitIterator(db, RPMDBI_PACKAGES,
517 &shared->otherPkg, sizeof(shared->otherPkg));
518 h = rpmdbNextIterator(mi);
520 mi = rpmdbFreeIterator(mi);
524 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
526 for (i = 0; i < sharedCount; i++, shared++) {
527 int otherFileNum, fileNum;
528 otherFileNum = shared->otherFileNum;
529 fileNum = shared->pkgFileNum;
531 if (otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
534 fi->actions[fileNum] = FA_SKIP;
537 mi = rpmdbFreeIterator(mi);
543 * Update disk space needs on each partition for this package.
545 /* XXX only ts->{probs,di} modified */
546 static void handleOverlappedFiles(const rpmTransactionSet ts, TFI_t fi)
547 /*@globals fileSystem @*/
548 /*@modifies ts, fi, fileSystem @*/
550 struct diskspaceInfo * dsl = ts->di;
551 rpmProblemSet probs = (ts->ignoreSet & RPMPROB_FILTER_REPLACENEWFILES)
553 hashTable ht = ts->ht;
554 struct diskspaceInfo * ds = NULL;
555 uint_32 fixupSize = 0;
556 char * filespec = NULL;
557 int fileSpecAlloced = 0;
560 for (i = 0; i < fi->fc; i++) {
561 int otherPkgNum, otherFileNum;
565 if (XFA_SKIPPING(fi->actions[i]))
568 j = strlen(fi->dnl[fi->dil[i]]) + strlen(fi->bnl[i]) + 1;
570 if (j > fileSpecAlloced) {
571 fileSpecAlloced = j * 2;
572 filespec = xrealloc(filespec, fileSpecAlloced);
576 (void) stpcpy( stpcpy( filespec, fi->dnl[fi->dil[i]]), fi->bnl[i]);
580 while (ds->bsize && ds->dev != fi->fps[i].entry->dev) ds++;
581 if (!ds->bsize) ds = NULL;
586 * Retrieve all records that apply to this file. Note that the
587 * file info records were built in the same order as the packages
588 * will be installed and removed so the records for an overlapped
589 * files will be sorted in exactly the same order.
591 (void) htGetEntry(ht, &fi->fps[i], (const void ***) &recs, &numRecs, NULL);
594 * If this package is being added, look only at other packages
595 * being added -- removed packages dance to a different tune.
596 * If both this and the other package are being added, overlapped
597 * files must be identical (or marked as a conflict). The
598 * disposition of already installed config files leads to
599 * a small amount of extra complexity.
601 * If this package is being removed, then there are two cases that
602 * need to be worried about:
603 * If the other package is being added, then skip any overlapped files
604 * so that this package removal doesn't nuke the overlapped files
605 * that were just installed.
606 * If both this and the other package are being removed, then each
607 * file removal from preceding packages needs to be skipped so that
608 * the file removal occurs only on the last occurence of an overlapped
609 * file in the transaction set.
613 /* Locate this overlapped file in the set of added/removed packages. */
614 for (j = 0; j < numRecs && recs[j] != fi; j++)
617 /* Find what the previous disposition of this file was. */
618 otherFileNum = -1; /* keep gcc quiet */
619 for (otherPkgNum = j - 1; otherPkgNum >= 0; otherPkgNum--) {
620 /* Added packages need only look at other added packages. */
621 if (fi->type == TR_ADDED && recs[otherPkgNum]->type != TR_ADDED)
622 /*@innercontinue@*/ continue;
624 /* TESTME: there are more efficient searches in the world... */
625 for (otherFileNum = 0; otherFileNum < recs[otherPkgNum]->fc;
628 /* If the addresses are the same, so are the values. */
629 if ((fi->fps + i) == (recs[otherPkgNum]->fps + otherFileNum))
630 /*@innerbreak@*/ break;
632 /* Otherwise, compare fingerprints by value. */
633 /*@-nullpass@*/ /* LCL: looks good to me */
634 if (FP_EQUAL(fi->fps[i], recs[otherPkgNum]->fps[otherFileNum]))
635 /*@innerbreak@*/ break;
639 /* XXX is this test still necessary? */
640 if (recs[otherPkgNum]->actions[otherFileNum] != FA_UNKNOWN)
641 /*@innerbreak@*/ break;
647 if (otherPkgNum < 0) {
648 /* XXX is this test still necessary? */
649 if (fi->actions[i] != FA_UNKNOWN)
650 /*@switchbreak@*/ break;
651 if ((fi->fflags[i] & RPMFILE_CONFIG) &&
652 !lstat(filespec, &sb)) {
653 /* Here is a non-overlapped pre-existing config file. */
654 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
655 ? FA_ALTNAME : FA_BACKUP;
657 fi->actions[i] = FA_CREATE;
659 /*@switchbreak@*/ break;
662 /* Mark added overlapped non-identical files as a conflict. */
663 if (probs && filecmp(recs[otherPkgNum]->fmodes[otherFileNum],
664 recs[otherPkgNum]->fmd5s[otherFileNum],
665 recs[otherPkgNum]->flinks[otherFileNum],
669 psAppend(probs, RPMPROB_NEW_FILE_CONFLICT, fi->ap,
670 filespec, NULL, recs[otherPkgNum]->ap->h, 0);
673 /* Try to get the disk accounting correct even if a conflict. */
674 fixupSize = recs[otherPkgNum]->fsizes[otherFileNum];
676 if ((fi->fflags[i] & RPMFILE_CONFIG) && !lstat(filespec, &sb)) {
677 /* Here is an overlapped pre-existing config file. */
678 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
679 ? FA_ALTNAME : FA_SKIP;
681 fi->actions[i] = FA_CREATE;
683 } /*@switchbreak@*/ break;
685 if (otherPkgNum >= 0) {
686 /* Here is an overlapped added file we don't want to nuke. */
687 if (recs[otherPkgNum]->actions[otherFileNum] != FA_ERASE) {
688 /* On updates, don't remove files. */
689 fi->actions[i] = FA_SKIP;
690 /*@switchbreak@*/ break;
692 /* Here is an overlapped removed file: skip in previous. */
693 recs[otherPkgNum]->actions[otherFileNum] = FA_SKIP;
695 if (XFA_SKIPPING(fi->actions[i]))
696 /*@switchbreak@*/ break;
697 if (fi->fstates && fi->fstates[i] != RPMFILE_STATE_NORMAL)
698 /*@switchbreak@*/ break;
699 if (!(S_ISREG(fi->fmodes[i]) && (fi->fflags[i] & RPMFILE_CONFIG))) {
700 fi->actions[i] = FA_ERASE;
701 /*@switchbreak@*/ break;
704 /* Here is a pre-existing modified config file that needs saving. */
706 if (!mdfile(filespec, mdsum) && strcmp(fi->fmd5s[i], mdsum)) {
707 fi->actions[i] = FA_BACKUP;
708 /*@switchbreak@*/ break;
711 fi->actions[i] = FA_ERASE;
712 /*@switchbreak@*/ break;
716 uint_32 s = BLOCK_ROUND(fi->fsizes[i], ds->bsize);
718 switch (fi->actions[i]) {
724 /*@switchbreak@*/ break;
727 * FIXME: If two packages share a file (same md5sum), and
728 * that file is being replaced on disk, will ds->bneeded get
729 * decremented twice? Quite probably!
733 ds->bneeded -= BLOCK_ROUND(fi->replacedSizes[i], ds->bsize);
734 /*@switchbreak@*/ break;
739 /*@switchbreak@*/ break;
742 /*@switchbreak@*/ break;
745 ds->bneeded -= BLOCK_ROUND(fixupSize, ds->bsize);
748 if (filespec) free(filespec);
753 static int ensureOlder(struct availablePackage * alp, Header old,
755 /*@modifies alp, probs @*/
759 if (old == NULL) return 1;
761 result = rpmVersionCompare(old, alp->h);
764 else if (result > 0) {
766 psAppend(probs, RPMPROB_OLDPACKAGE, alp, NULL, NULL, old, 0);
774 static void skipFiles(const rpmTransactionSet ts, TFI_t fi)
775 /*@globals rpmGlobalMacroContext @*/
776 /*@modifies fi, rpmGlobalMacroContext @*/
778 int noDocs = (ts->transFlags & RPMTRANS_FLAG_NODOCS);
779 char ** netsharedPaths = NULL;
780 const char ** languages;
781 const char * dn, * bn;
782 int dnlen, bnlen, ix;
789 noDocs = rpmExpandNumeric("%{_excludedocs}");
791 { const char *tmpPath = rpmExpand("%{_netsharedpath}", NULL);
793 if (tmpPath && *tmpPath != '%')
794 netsharedPaths = splitString(tmpPath, strlen(tmpPath), ':');
796 tmpPath = _free(tmpPath);
799 s = rpmExpand("%{_install_langs}", NULL);
801 if (!(s && *s != '%'))
804 languages = (const char **) splitString(s, strlen(s), ':');
810 /* Compute directory refcount, skip directory if now empty. */
811 drc = alloca(fi->dc * sizeof(*drc));
812 memset(drc, 0, fi->dc * sizeof(*drc));
813 dff = alloca(fi->dc * sizeof(*dff));
814 memset(dff, 0, fi->dc * sizeof(*dff));
816 for (i = 0; i < fi->fc; i++) {
827 /* Don't bother with skipped files */
828 if (XFA_SKIPPING(fi->actions[i])) {
834 * Skip net shared paths.
835 * Net shared paths are not relative to the current root (though
836 * they do need to take package relocations into account).
838 for (nsp = netsharedPaths; nsp && *nsp; nsp++) {
843 if (strncmp(dn, *nsp, len))
844 /*@innercontinue@*/ continue;
845 /* Only directories or complete file paths can be net shared */
846 if (!(dn[len] == '/' || dn[len] == '\0'))
847 /*@innercontinue@*/ continue;
849 if (len < (dnlen + bnlen))
850 /*@innercontinue@*/ continue;
851 if (strncmp(dn, *nsp, dnlen))
852 /*@innercontinue@*/ continue;
853 if (strncmp(bn, (*nsp) + dnlen, bnlen))
854 /*@innercontinue@*/ continue;
856 /* Only directories or complete file paths can be net shared */
857 if (!((*nsp)[len] == '/' || (*nsp)[len] == '\0'))
858 /*@innercontinue@*/ continue;
861 /*@innerbreak@*/ break;
865 drc[ix]--; dff[ix] = 1;
866 fi->actions[i] = FA_SKIPNETSHARED;
871 * Skip i18n language specific files.
873 if (fi->flangs && languages && *fi->flangs[i]) {
874 const char **lang, *l, *le;
875 for (lang = languages; *lang != NULL; lang++) {
876 if (!strcmp(*lang, "all"))
877 /*@innerbreak@*/ break;
878 for (l = fi->flangs[i]; *l != '\0'; l = le) {
879 for (le = l; *le != '\0' && *le != '|'; le++)
881 if ((le-l) > 0 && !strncmp(*lang, l, (le-l)))
882 /*@innerbreak@*/ break;
883 if (*le == '|') le++; /* skip over | */
886 /*@innerbreak@*/ break;
889 drc[ix]--; dff[ix] = 1;
890 fi->actions[i] = FA_SKIPNSTATE;
896 * Skip documentation if requested.
898 if (noDocs && (fi->fflags[i] & RPMFILE_DOC)) {
899 drc[ix]--; dff[ix] = 1;
900 fi->actions[i] = FA_SKIPNSTATE;
905 /* Skip (now empty) directories that had skipped files. */
906 for (j = 0; j < fi->dc; j++) {
908 if (drc[j]) continue; /* dir still has files. */
909 if (!dff[j]) continue; /* dir was not emptied here. */
911 /* Find parent directory and basename. */
912 dn = fi->dnl[j]; dnlen = strlen(dn) - 1;
913 bn = dn + dnlen; bnlen = 0;
914 while (bn > dn && bn[-1] != '/') {
920 /* If explicitly included in the package, skip the directory. */
921 for (i = 0; i < fi->fc; i++) {
924 if (XFA_SKIPPING(fi->actions[i]))
925 /*@innercontinue@*/ continue;
926 if (whatis(fi->fmodes[i]) != XDIR)
927 /*@innercontinue@*/ continue;
928 dir = fi->dnl[fi->dil[i]];
929 if (strlen(dir) != dnlen)
930 /*@innercontinue@*/ continue;
931 if (strncmp(dir, dn, dnlen))
932 /*@innercontinue@*/ continue;
933 if (strlen(fi->bnl[i]) != bnlen)
934 /*@innercontinue@*/ continue;
935 if (strncmp(fi->bnl[i], bn, bnlen))
936 /*@innercontinue@*/ continue;
937 rpmMessage(RPMMESS_DEBUG, _("excluding directory %s\n"), dn);
938 fi->actions[i] = FA_SKIPNSTATE;
939 /*@innerbreak@*/ break;
943 if (netsharedPaths) freeSplitString(netsharedPaths);
944 #ifdef DYING /* XXX freeFi will deal with this later. */
945 fi->flangs = _free(fi->flangs);
947 if (languages) freeSplitString((char **)languages);
951 * Iterator across transaction elements, forward on install, backward on erase.
953 struct tsIterator_s {
954 /*@kept@*/ rpmTransactionSet ts; /*!< transaction set. */
955 int reverse; /*!< reversed traversal? */
956 int ocsave; /*!< last returned iterator index. */
957 int oc; /*!< iterator index. */
961 * Return transaction element order count.
962 * @param a transaction element iterator
963 * @return element order count
965 static int tsGetOc(void * a)
968 struct tsIterator_s * iter = a;
969 int oc = iter->ocsave;
974 * Return transaction element available package pointer.
975 * @param a transaction element iterator
976 * @return available package pointer
978 static /*@dependent@*/ struct availablePackage * tsGetAlp(void * a)
981 struct tsIterator_s * iter = a;
982 struct availablePackage * alp = NULL;
983 int oc = iter->ocsave;
987 rpmTransactionSet ts = iter->ts;
988 TFI_t fi = ts->flList + oc;
989 if (ts->addedPackages.list && fi->type == TR_ADDED)
990 alp = ts->addedPackages.list + ts->order[oc].u.addedIndex;
997 * Destroy transaction element iterator.
998 * @param a transaction element iterator
999 * @return NULL always
1001 static /*@null@*/ void * tsFreeIterator(/*@only@*//*@null@*/ const void * a)
1008 * Create transaction element iterator.
1009 * @param a transaction set
1010 * @return transaction element iterator
1012 static void * tsInitIterator(/*@kept@*/ const void * a)
1015 rpmTransactionSet ts = (void *)a;
1016 struct tsIterator_s * iter = NULL;
1018 iter = xcalloc(1, sizeof(*iter));
1020 iter->reverse = ((ts->transFlags & RPMTRANS_FLAG_REVERSE) ? 1 : 0);
1021 iter->oc = (iter->reverse ? (ts->orderCount - 1) : 0);
1022 iter->ocsave = iter->oc;
1027 * Return next transaction element's file info.
1028 * @param a file info iterator
1029 * @return next index, -1 on termination
1031 static /*@dependent@*/ TFI_t tsNextIterator(void * a)
1034 struct tsIterator_s * iter = a;
1035 rpmTransactionSet ts = iter->ts;
1039 if (iter->reverse) {
1040 if (iter->oc >= 0) oc = iter->oc--;
1042 if (iter->oc < ts->orderCount) oc = iter->oc++;
1046 fi = ts->flList + oc;
1050 #define NOTIFY(_ts, _al) if ((_ts)->notify) (void) (_ts)->notify _al
1052 int rpmRunTransactions( rpmTransactionSet ts,
1053 rpmCallbackFunction notify, rpmCallbackData notifyData,
1054 rpmProblemSet okProbs, rpmProblemSet * newProbs,
1055 rpmtransFlags transFlags, rpmprobFilterFlags ignoreSet)
1059 struct availablePackage * alp;
1060 int totalFileCount = 0;
1062 struct diskspaceInfo * dip;
1063 struct sharedFileInfo * shared, * sharedList;
1068 fingerPrintCache fpc;
1069 struct psm_s psmbuf;
1070 PSM_t psm = &psmbuf;
1074 /* FIXME: what if the same package is included in ts twice? */
1076 ts->transFlags = transFlags;
1077 if (ts->transFlags & RPMTRANS_FLAG_NOSCRIPTS)
1078 ts->transFlags |= (_noTransScripts | _noTransTriggers);
1079 if (ts->transFlags & RPMTRANS_FLAG_NOTRIGGERS)
1080 ts->transFlags |= _noTransTriggers;
1082 /* XXX MULTILIB is broken, as packages can and do execute /sbin/ldconfig. */
1083 if (ts->transFlags & (RPMTRANS_FLAG_JUSTDB | RPMTRANS_FLAG_MULTILIB))
1084 ts->transFlags |= (_noTransScripts | _noTransTriggers);
1086 ts->notify = notify;
1087 ts->notifyData = notifyData;
1089 ts->probs = *newProbs = psCreate();
1091 ts->ignoreSet = ignoreSet;
1092 ts->currDir = _free(ts->currDir);
1093 ts->currDir = currentDirectory();
1095 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
1096 ts->id = (int_32) time(NULL);
1098 memset(psm, 0, sizeof(*psm));
1103 /* Get available space on mounted file systems. */
1104 if (!(ts->ignoreSet & RPMPROB_FILTER_DISKSPACE) &&
1105 !rpmGetFilesystemList(&ts->filesystems, &ts->filesystemCount)) {
1108 ts->di = _free(ts->di);
1109 dip = ts->di = xcalloc((ts->filesystemCount + 1), sizeof(*ts->di));
1111 for (i = 0; (i < ts->filesystemCount) && dip; i++) {
1112 #if STATFS_IN_SYS_STATVFS
1114 memset(&sfb, 0, sizeof(sfb));
1115 if (statvfs(ts->filesystems[i], &sfb))
1119 /* This platform has the 4-argument version of the statfs call. The last two
1120 * should be the size of struct statfs and 0, respectively. The 0 is the
1121 * filesystem type, and is always 0 when statfs is called on a mounted
1122 * filesystem, as we're doing.
1124 memset(&sfb, 0, sizeof(sfb));
1125 if (statfs(ts->filesystems[i], &sfb, sizeof(sfb), 0))
1127 memset(&sfb, 0, sizeof(sfb));
1128 if (statfs(ts->filesystems[i], &sfb))
1134 ts->di[i].bsize = sfb.f_bsize;
1135 ts->di[i].bneeded = 0;
1136 ts->di[i].ineeded = 0;
1137 #ifdef STATFS_HAS_F_BAVAIL
1138 ts->di[i].bavail = sfb.f_bavail;
1140 /* FIXME: the statfs struct doesn't have a member to tell how many blocks are
1141 * available for non-superusers. f_blocks - f_bfree is probably too big, but
1142 * it's about all we can do.
1144 ts->di[i].bavail = sfb.f_blocks - sfb.f_bfree;
1146 /* XXX Avoid FAT and other file systems that have not inodes. */
1147 ts->di[i].iavail = !(sfb.f_ffree == 0 && sfb.f_files == 0)
1150 xx = stat(ts->filesystems[i], &sb);
1151 ts->di[i].dev = sb.st_dev;
1155 if (dip) ts->di[i].bsize = 0;
1158 /* ===============================================
1159 * For packages being installed:
1160 * - verify package arch/os.
1161 * - verify package epoch:version-release is newer.
1163 * For packages being removed:
1166 /* The ordering doesn't matter here */
1167 if (ts->addedPackages.list != NULL)
1168 for (alp = ts->addedPackages.list;
1169 (alp - ts->addedPackages.list) < ts->addedPackages.size;
1172 if (!archOkay(alp->h) && !(ts->ignoreSet & RPMPROB_FILTER_IGNOREARCH))
1173 psAppend(ts->probs, RPMPROB_BADARCH, alp, NULL, NULL, NULL, 0);
1175 if (!osOkay(alp->h) && !(ts->ignoreSet & RPMPROB_FILTER_IGNOREOS))
1176 psAppend(ts->probs, RPMPROB_BADOS, alp, NULL, NULL, NULL, 0);
1178 if (!(ts->ignoreSet & RPMPROB_FILTER_OLDPACKAGE)) {
1179 rpmdbMatchIterator mi;
1181 mi = rpmdbInitIterator(ts->rpmdb, RPMTAG_NAME, alp->name, 0);
1182 while ((oldH = rpmdbNextIterator(mi)) != NULL)
1183 xx = ensureOlder(alp, oldH, ts->probs);
1184 mi = rpmdbFreeIterator(mi);
1187 /* XXX multilib should not display "already installed" problems */
1188 if (!(ts->ignoreSet & RPMPROB_FILTER_REPLACEPKG) && !alp->multiLib) {
1189 rpmdbMatchIterator mi;
1190 mi = rpmdbInitIterator(ts->rpmdb, RPMTAG_NAME, alp->name, 0);
1191 xx = rpmdbSetIteratorRE(mi, RPMTAG_VERSION,
1192 RPMMIRE_DEFAULT, alp->version);
1193 xx = rpmdbSetIteratorRE(mi, RPMTAG_RELEASE,
1194 RPMMIRE_DEFAULT, alp->release);
1196 while (rpmdbNextIterator(mi) != NULL) {
1197 psAppend(ts->probs, RPMPROB_PKG_INSTALLED, alp,
1198 NULL, NULL, NULL, 0);
1199 /*@innerbreak@*/ break;
1201 mi = rpmdbFreeIterator(mi);
1204 totalFileCount += alp->filesCount;
1208 /* FIXME: it seems a bit silly to read in all of these headers twice */
1209 /* The ordering doesn't matter here */
1210 if (ts->numRemovedPackages > 0) {
1211 rpmdbMatchIterator mi;
1215 mi = rpmdbInitIterator(ts->rpmdb, RPMDBI_PACKAGES, NULL, 0);
1216 xx = rpmdbAppendIterator(mi, ts->removedPackages, ts->numRemovedPackages);
1217 while ((h = rpmdbNextIterator(mi)) != NULL) {
1218 if (headerGetEntry(h, RPMTAG_BASENAMES, NULL, NULL, &fileCount))
1219 totalFileCount += fileCount;
1221 mi = rpmdbFreeIterator(mi);
1224 /* ===============================================
1225 * Initialize file list:
1227 ts->flEntries = ts->addedPackages.size + ts->numRemovedPackages;
1228 ts->flList = xcalloc(ts->flEntries, sizeof(*ts->flList));
1231 * FIXME?: we'd be better off assembling one very large file list and
1232 * calling fpLookupList only once. I'm not sure that the speedup is
1233 * worth the trouble though.
1235 tsi = tsInitIterator(ts);
1236 while ((fi = tsNextIterator(tsi)) != NULL) {
1238 fi->magic = TFIMAGIC;
1240 /* XXX watchout: fi->type must be set for tsGetAlp() to "work" */
1241 fi->type = ts->order[oc].type;
1245 /* XXX watchout: fi->type must be set for tsGetAlp() to "work" */
1246 fi->ap = tsGetAlp(tsi);
1248 loadFi(ts, fi, fi->ap->h, 1);
1249 /* XXX free fi->ap->h here if/when possible */
1253 /* Skip netshared paths, not our i18n files, and excluded docs */
1255 /*@switchbreak@*/ break;
1258 fi->record = ts->order[oc].u.removed.dboffset;
1259 /* Retrieve erased package header from the database. */
1260 { rpmdbMatchIterator mi;
1262 mi = rpmdbInitIterator(ts->rpmdb, RPMDBI_PACKAGES,
1263 &fi->record, sizeof(fi->record));
1264 if ((fi->h = rpmdbNextIterator(mi)) != NULL)
1265 fi->h = headerLink(fi->h);
1266 mi = rpmdbFreeIterator(mi);
1268 if (fi->h == NULL) {
1272 /* XXX header arg unused. */
1273 loadFi(ts, fi, fi->h, 0);
1274 /*@switchbreak@*/ break;
1279 fi->fps = xmalloc(fi->fc * sizeof(*fi->fps));
1281 tsi = tsFreeIterator(tsi);
1283 if (!ts->chrootDone) {
1285 /*@-superuser -noeffect @*/
1286 xx = chroot(ts->rootDir);
1287 /*@=superuser =noeffect @*/
1289 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 1;
1292 chroot_prefix = ts->rootDir;
1297 ts->ht = htCreate(totalFileCount * 2, 0, 0, fpHashFunction, fpEqual);
1298 fpc = fpCacheCreate(totalFileCount);
1300 /* ===============================================
1301 * Add fingerprint for each file not skipped.
1303 tsi = tsInitIterator(ts);
1304 while ((fi = tsNextIterator(tsi)) != NULL) {
1305 fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fi->fc, fi->fps);
1306 for (i = 0; i < fi->fc; i++) {
1307 if (XFA_SKIPPING(fi->actions[i]))
1308 /*@innercontinue@*/ continue;
1309 /*@-dependenttrans@*/
1310 htAddEntry(ts->ht, fi->fps + i, fi);
1311 /*@=dependenttrans@*/
1314 tsi = tsFreeIterator(tsi);
1316 /*@-noeffectuncon @*/ /* FIX: check rc */
1317 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_START, 6, ts->flEntries,
1318 NULL, ts->notifyData));
1319 /*@=noeffectuncon@*/
1321 /* ===============================================
1322 * Compute file disposition for each package in transaction set.
1324 tsi = tsInitIterator(ts);
1325 while ((fi = tsNextIterator(tsi)) != NULL) {
1326 dbiIndexSet * matches;
1329 /*@-noeffectuncon @*/ /* FIX: check rc */
1330 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_PROGRESS, (fi - ts->flList),
1331 ts->flEntries, NULL, ts->notifyData));
1332 /*@=noeffectuncon@*/
1334 if (fi->fc == 0) continue;
1336 /* Extract file info for all files in this package from the database. */
1337 matches = xcalloc(fi->fc, sizeof(*matches));
1338 if (rpmdbFindFpList(ts->rpmdb, fi->fps, matches, fi->fc))
1339 return 1; /* XXX WTFO? */
1342 for (i = 0; i < fi->fc; i++)
1343 numShared += dbiIndexSetCount(matches[i]);
1345 /* Build sorted file info list for this package. */
1346 shared = sharedList = xcalloc((numShared + 1), sizeof(*sharedList));
1347 for (i = 0; i < fi->fc; i++) {
1349 * Take care not to mark files as replaced in packages that will
1350 * have been removed before we will get here.
1352 for (j = 0; j < dbiIndexSetCount(matches[i]); j++) {
1354 ro = dbiIndexRecordOffset(matches[i], j);
1356 for (k = 0; ro != knownBad && k < ts->orderCount; k++) {
1357 switch (ts->order[k].type) {
1359 if (ts->order[k].u.removed.dboffset == ro)
1361 /*@switchbreak@*/ break;
1363 /*@switchbreak@*/ break;
1367 shared->pkgFileNum = i;
1368 shared->otherPkg = dbiIndexRecordOffset(matches[i], j);
1369 shared->otherFileNum = dbiIndexRecordFileNumber(matches[i], j);
1370 shared->isRemoved = (knownBad == ro);
1373 matches[i] = dbiFreeIndexSet(matches[i]);
1375 numShared = shared - sharedList;
1376 shared->otherPkg = -1;
1377 matches = _free(matches);
1379 /* Sort file info by other package index (otherPkg) */
1380 qsort(sharedList, numShared, sizeof(*shared), sharedCmp);
1382 /* For all files from this package that are in the database ... */
1383 for (i = 0; i < numShared; i = nexti) {
1386 shared = sharedList + i;
1388 /* Find the end of the files in the other package. */
1389 for (nexti = i + 1; nexti < numShared; nexti++) {
1390 if (sharedList[nexti].otherPkg != shared->otherPkg)
1391 /*@innerbreak@*/ break;
1394 /* Is this file from a package being removed? */
1396 for (j = 0; j < ts->numRemovedPackages; j++) {
1397 if (ts->removedPackages[j] != shared->otherPkg)
1398 /*@innercontinue@*/ continue;
1400 /*@innerbreak@*/ break;
1403 /* Determine the fate of each file. */
1406 xx = handleInstInstalledFiles(ts, fi, shared, nexti - i,
1407 !(beingRemoved || (ts->ignoreSet & RPMPROB_FILTER_REPLACEOLDFILES)));
1408 /*@switchbreak@*/ break;
1411 xx = handleRmvdInstalledFiles(ts, fi, shared, nexti - i);
1412 /*@switchbreak@*/ break;
1418 /* Update disk space needs on each partition for this package. */
1419 handleOverlappedFiles(ts, fi);
1421 /* Check added package has sufficient space on each partition used. */
1424 if (!(ts->di && fi->fc))
1425 /*@switchbreak@*/ break;
1426 for (i = 0; i < ts->filesystemCount; i++) {
1430 /* XXX Avoid FAT and other file systems that have not inodes. */
1431 if (dip->iavail <= 0)
1432 /*@innercontinue@*/ continue;
1434 if (adj_fs_blocks(dip->bneeded) > dip->bavail)
1435 psAppend(ts->probs, RPMPROB_DISKSPACE, fi->ap,
1436 ts->filesystems[i], NULL, NULL,
1437 (adj_fs_blocks(dip->bneeded) - dip->bavail) * dip->bsize);
1439 if (adj_fs_blocks(dip->ineeded) > dip->iavail)
1440 psAppend(ts->probs, RPMPROB_DISKNODES, fi->ap,
1441 ts->filesystems[i], NULL, NULL,
1442 (adj_fs_blocks(dip->ineeded) - dip->iavail));
1444 /*@switchbreak@*/ break;
1446 /*@switchbreak@*/ break;
1449 tsi = tsFreeIterator(tsi);
1451 if (ts->chrootDone) {
1452 /*@-superuser -noeffect @*/
1454 /*@=superuser =noeffect @*/
1456 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
1458 chroot_prefix = NULL;
1460 xx = chdir(ts->currDir);
1463 /*@-noeffectuncon @*/ /* FIX: check rc */
1464 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_STOP, 6, ts->flEntries,
1465 NULL, ts->notifyData));
1466 /*@=noeffectuncon @*/
1468 /* ===============================================
1469 * Free unused memory as soon as possible.
1472 tsi = tsInitIterator(ts);
1473 while ((fi = tsNextIterator(tsi)) != NULL) {
1477 fi->fps = _free(fi->fps);
1479 tsi = tsFreeIterator(tsi);
1485 /* ===============================================
1486 * If unfiltered problems exist, free memory and return.
1488 if ((ts->transFlags & RPMTRANS_FLAG_BUILD_PROBS) ||
1489 (ts->probs->numProblems && (!okProbs || psTrim(okProbs, ts->probs))))
1491 *newProbs = ts->probs;
1493 ts->flList = freeFl(ts, ts->flList);
1496 return ts->orderCount;
1500 /* ===============================================
1501 * Save removed files before erasing.
1503 if (ts->transFlags & (RPMTRANS_FLAG_DIRSTASH | RPMTRANS_FLAG_REPACKAGE)) {
1504 tsi = tsInitIterator(ts);
1505 while ((fi = tsNextIterator(tsi)) != NULL) {
1509 /*@switchbreak@*/ break;
1511 if (ts->transFlags & RPMTRANS_FLAG_REPACKAGE)
1512 xx = psmStage(psm, PSM_PKGSAVE);
1513 /*@switchbreak@*/ break;
1516 tsi = tsFreeIterator(tsi);
1519 /* ===============================================
1520 * Install and remove packages.
1523 lastFailed = -2; /* erased packages have -1 */
1524 tsi = tsInitIterator(ts);
1525 while ((fi = tsNextIterator(tsi)) != NULL) {
1533 alp = tsGetAlp(tsi);
1534 assert(alp == fi->ap);
1535 i = alp - ts->addedPackages.list;
1537 rpmMessage(RPMMESS_DEBUG, "========== +++ %s-%s-%s\n",
1538 fi->name, fi->version, fi->release);
1539 h = (fi->h ? headerLink(fi->h) : NULL);
1541 if (alp->fd == NULL) {
1542 alp->fd = ts->notify(fi->h, RPMCALLBACK_INST_OPEN_FILE, 0, 0,
1543 alp->key, ts->notifyData);
1549 /*@-mustmod@*/ /* LCL: segfault */
1550 rpmrc = rpmReadPackageHeader(alp->fd, &h, NULL, NULL, NULL);
1552 if (!(rpmrc == RPMRC_OK || rpmrc == RPMRC_BADSIZE)) {
1553 /*@-noeffectuncon @*/ /* FIX: check rc */
1554 (void)ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE,
1555 0, 0, alp->key, ts->notifyData);
1556 /*@=noeffectuncon @*/
1559 } else if (fi->h != NULL) {
1560 Header foo = relocateFileList(ts, fi, alp, h, NULL);
1562 h = headerLink(foo);
1563 foo = headerFree(foo);
1565 if (alp->fd) gotfd = 1;
1571 Header hsave = NULL;
1574 hsave = headerLink(fi->h);
1575 fi->h = headerFree(fi->h);
1576 fi->h = headerLink(h);
1578 char * fstates = fi->fstates;
1579 fileAction * actions = fi->actions;
1584 fi->magic = TFIMAGIC;
1585 fi->type = ts->order[oc].type;
1586 fi->ap = tsGetAlp(tsi);
1588 loadFi(ts, fi, h, 1);
1589 fi->fstates = _free(fi->fstates);
1590 fi->fstates = fstates;
1591 fi->actions = _free(fi->actions);
1592 fi->actions = actions;
1595 ts->transFlags |= RPMTRANS_FLAG_MULTILIB;
1597 assert(alp == fi->ap);
1598 if (psmStage(psm, PSM_PKGINSTALL)) {
1602 fi->h = headerFree(fi->h);
1604 fi->h = headerLink(hsave);
1605 hsave = headerFree(hsave);
1615 /*@-noeffectuncon @*/ /* FIX: check rc */
1616 (void)ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE, 0, 0,
1617 alp->key, ts->notifyData);
1618 /*@=noeffectuncon @*/
1622 /*@switchbreak@*/ break;
1624 rpmMessage(RPMMESS_DEBUG, "========== --- %s-%s-%s\n",
1625 fi->name, fi->version, fi->release);
1627 /* If install failed, then we shouldn't erase. */
1628 if (ts->order[oc].u.removed.dependsOnIndex != lastFailed) {
1629 if (psmStage(psm, PSM_PKGERASE))
1633 /*@switchbreak@*/ break;
1635 xx = rpmdbSync(ts->rpmdb);
1637 tsi = tsFreeIterator(tsi);
1639 ts->flList = freeFl(ts, ts->flList);