2 * \file lib/transaction.c
7 #include <rpmmacro.h> /* XXX for rpmExpand */
10 #define _NEED_TEITERATOR 1
16 #include "legacy.h" /* XXX mdfile */
17 #include "misc.h" /* XXX stripTrailingChar, splitString, currentDirectory */
20 /*@-redecl -exportheadervar@*/
22 extern const char * chroot_prefix;
23 /*@=redecl =exportheadervar@*/
25 /* XXX FIXME: merge with existing (broken?) tests in system.h */
26 /* portability fiddles */
27 #if STATFS_IN_SYS_STATVFS
29 # include <sys/statvfs.h>
30 #if defined(__LCLINT__)
31 /*@-declundef -exportheader -protoparammatch @*/ /* LCL: missing annotation */
32 extern int statvfs (const char * file, /*@out@*/ struct statvfs * buf)
33 /*@globals fileSystem @*/
34 /*@modifies *buf, fileSystem @*/;
35 /*@=declundef =exportheader =protoparammatch @*/
39 # if STATFS_IN_SYS_VFS
42 # if STATFS_IN_SYS_MOUNT
43 # include <sys/mount.h>
45 # if STATFS_IN_SYS_STATFS
46 # include <sys/statfs.h>
54 /*@access FD_t @*/ /* XXX compared with NULL */
55 /*@access Header @*/ /* XXX compared with NULL */
56 /*@access rpmProblemSet @*/ /* XXX need rpmProblemSetOK() */
57 /*@access dbiIndexSet @*/
65 /*@access rpmFNSet @*/
68 /*@access teIterator @*/
69 /*@access transactionElement @*/
70 /*@access rpmTransactionSet @*/
74 struct diskspaceInfo {
75 dev_t dev; /*!< File system device number. */
76 signed long bneeded; /*!< No. of blocks needed. */
77 signed long ineeded; /*!< No. of inodes needed. */
78 int bsize; /*!< File system block size. */
79 signed long bavail; /*!< No. of blocks available. */
80 signed long iavail; /*!< No. of inodes available. */
84 * Adjust for root only reserved space. On linux e2fs, this is 5%.
86 #define adj_fs_blocks(_nb) (((_nb) * 21) / 20)
88 /* argon thought a shift optimization here was a waste of time... he's
90 #define BLOCK_ROUND(size, block) (((size) + (block) - 1) / (block))
94 static /*@null@*/ void * freeFl(rpmTransactionSet ts,
95 /*@only@*/ /*@null@*/ TFI_t flList)
102 /*@-usereleased -onlytrans @*/ /* FIX: fi needs to be only */
103 for (oc = 0, fi = flList; oc < ts->orderCount; oc++, fi++)
105 flList = _free(flList);
106 /*@=usereleased =onlytrans @*/
111 void rpmtransSetScriptFd(rpmTransactionSet ts, FD_t fd)
113 /*@-type@*/ /* FIX: cast? */
114 ts->scriptFd = (fd ? fdLink(fd, "rpmtransSetScriptFd") : NULL);
118 int rpmtransGetKeys(const rpmTransactionSet ts, fnpyKey ** ep, int * nep)
122 if (nep) *nep = ts->orderCount;
127 *ep = e = xmalloc(ts->orderCount * sizeof(*e));
128 for (oc = 0; oc < ts->orderCount; oc++, e++) {
129 switch (ts->order[oc].type) {
131 *e = ts->order[oc].key;
132 /*@switchbreak@*/ break;
135 /*@-mods@*/ /* FIX: double indirection. */
138 /*@switchbreak@*/ break;
147 static int archOkay(/*@null@*/ const char * pkgArch)
150 if (pkgArch == NULL) return 0;
151 return (rpmMachineScore(RPM_MACHTABLE_INSTARCH, pkgArch) ? 1 : 0);
156 static int osOkay(/*@null@*/ const char * pkgOs)
159 if (pkgOs == NULL) return 0;
160 return (rpmMachineScore(RPM_MACHTABLE_INSTOS, pkgOs) ? 1 : 0);
165 static int sharedCmp(const void * one, const void * two)
168 const struct sharedFileInfo * a = one;
169 const struct sharedFileInfo * b = two;
171 if (a->otherPkg < b->otherPkg)
173 else if (a->otherPkg > b->otherPkg)
181 static fileAction decideFileFate(const char * dirName,
182 const char * baseName, short dbMode,
183 const char * dbMd5, const char * dbLink, short newMode,
184 const char * newMd5, const char * newLink, int newFlags,
185 rpmtransFlags transFlags)
186 /*@globals fileSystem @*/
187 /*@modifies fileSystem @*/
190 const char * dbAttr, * newAttr;
191 fileTypes dbWhat, newWhat, diskWhat;
194 int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
195 char * filespec = alloca(strlen(dirName) + strlen(baseName) + 1);
197 (void) stpcpy( stpcpy(filespec, dirName), baseName);
199 if (lstat(filespec, &sb)) {
201 * The file doesn't exist on the disk. Create it unless the new
202 * package has marked it as missingok, or allfiles is requested.
204 if (!(transFlags & RPMTRANS_FLAG_ALLFILES) &&
205 (newFlags & RPMFILE_MISSINGOK)) {
206 rpmMessage(RPMMESS_DEBUG, _("%s skipped due to missingok flag\n"),
214 diskWhat = whatis(sb.st_mode);
215 dbWhat = whatis(dbMode);
216 newWhat = whatis(newMode);
218 /* RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
219 them in older packages as well */
220 if (newWhat == XDIR) {
224 if (diskWhat != newWhat) {
226 } else if (newWhat != dbWhat && diskWhat != dbWhat) {
228 } else if (dbWhat != newWhat) {
230 } else if (dbWhat != LINK && dbWhat != REG) {
235 rc = mdfile(filespec, buffer);
238 /* assume the file has been removed, don't freak */
243 } else /* dbWhat == LINK */ {
244 memset(buffer, 0, sizeof(buffer));
245 i = readlink(filespec, buffer, sizeof(buffer) - 1);
247 /* assume the file has been removed, don't freak */
254 /* this order matters - we'd prefer to CREATE the file if at all
255 possible in case something else (like the timestamp) has changed */
257 if (!strcmp(dbAttr, buffer)) {
258 /* this config file has never been modified, so just replace it */
262 if (!strcmp(dbAttr, newAttr)) {
263 /* this file is the same in all versions of this package */
268 * The config file on the disk has been modified, but
269 * the ones in the two packages are different. It would
270 * be nice if RPM was smart enough to at least try and
271 * merge the difference ala CVS, but...
278 static int filecmp(short mode1, const char * md51, const char * link1,
279 short mode2, const char * md52, const char * link2)
282 fileTypes what1 = whatis(mode1);
283 fileTypes what2 = whatis(mode2);
285 if (what1 != what2) return 1;
288 return strcmp(link1, link2);
289 else if (what1 == REG)
290 return strcmp(md51, md52);
297 /* XXX only ts->{probs,rpmdb} modified */
298 static int handleInstInstalledFiles(const rpmTransactionSet ts, TFI_t fi,
299 struct sharedFileInfo * shared,
300 int sharedCount, int reportConflicts)
301 /*@globals fileSystem @*/
302 /*@modifies ts, fi, fileSystem @*/
305 HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
306 rpmtransFlags transFlags = ts->transFlags;
307 rpmTagType oltype, omtype;
310 const char ** otherMd5s;
311 const char ** otherLinks;
312 const char * otherStates;
313 uint_32 * otherFlags;
314 uint_32 * otherSizes;
315 uint_16 * otherModes;
319 rpmdbMatchIterator mi;
321 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
322 &shared->otherPkg, sizeof(shared->otherPkg));
323 h = rpmdbNextIterator(mi);
325 mi = rpmdbFreeIterator(mi);
329 xx = hge(h, RPMTAG_FILEMD5S, &omtype, (void **) &otherMd5s, NULL);
330 xx = hge(h, RPMTAG_FILELINKTOS, &oltype, (void **) &otherLinks, NULL);
331 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
332 xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &otherModes, NULL);
333 xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &otherFlags, NULL);
334 xx = hge(h, RPMTAG_FILESIZES, NULL, (void **) &otherSizes, NULL);
336 fi->replaced = xmalloc(sharedCount * sizeof(*fi->replaced));
338 for (i = 0; i < sharedCount; i++, shared++) {
339 int otherFileNum, fileNum;
340 otherFileNum = shared->otherFileNum;
341 fileNum = shared->pkgFileNum;
343 /* XXX another tedious segfault, assume file state normal. */
344 if (otherStates && otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
347 if (XFA_SKIPPING(fi->actions[fileNum]))
350 if (filecmp(otherModes[otherFileNum],
351 otherMd5s[otherFileNum],
352 otherLinks[otherFileNum],
355 fi->flinks[fileNum])) {
356 /*@-compdef@*/ /* FIX: *fi->replaced undefined */
357 if (reportConflicts) {
358 const char * pkgNEVR = fiGetNEVR(fi);
359 const char * altNEVR = hGetNEVR(h, NULL);
360 rpmProblemSetAppend(ts->probs, RPMPROB_FILE_CONFLICT,
362 fi->dnl[fi->dil[fileNum]], fi->bnl[fileNum],
365 pkgNEVR = _free(pkgNEVR);
366 altNEVR = _free(altNEVR);
369 if (!(otherFlags[otherFileNum] | fi->fflags[fileNum])
372 if (!shared->isRemoved)
373 fi->replaced[numReplaced++] = *shared;
378 if ((otherFlags[otherFileNum] | fi->fflags[fileNum]) & RPMFILE_CONFIG) {
379 fi->actions[fileNum] = decideFileFate(
380 fi->dnl[fi->dil[fileNum]],
382 otherModes[otherFileNum],
383 otherMd5s[otherFileNum],
384 otherLinks[otherFileNum],
392 fi->replacedSizes[fileNum] = otherSizes[otherFileNum];
395 otherMd5s = hfd(otherMd5s, omtype);
396 otherLinks = hfd(otherLinks, oltype);
397 mi = rpmdbFreeIterator(mi);
399 fi->replaced = xrealloc(fi->replaced, /* XXX memory leak */
400 sizeof(*fi->replaced) * (numReplaced + 1));
401 fi->replaced[numReplaced].otherPkg = 0;
408 /* XXX only ts->rpmdb modified */
409 static int handleRmvdInstalledFiles(const rpmTransactionSet ts, TFI_t fi,
410 struct sharedFileInfo * shared, int sharedCount)
411 /*@globals fileSystem @*/
412 /*@modifies fi, fileSystem @*/
416 const char * otherStates;
419 rpmdbMatchIterator mi;
421 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
422 &shared->otherPkg, sizeof(shared->otherPkg));
423 h = rpmdbNextIterator(mi);
425 mi = rpmdbFreeIterator(mi);
429 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
431 for (i = 0; i < sharedCount; i++, shared++) {
432 int otherFileNum, fileNum;
433 otherFileNum = shared->otherFileNum;
434 fileNum = shared->pkgFileNum;
436 if (otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
439 fi->actions[fileNum] = FA_SKIP;
442 mi = rpmdbFreeIterator(mi);
448 * Update disk space needs on each partition for this package.
450 /* XXX only ts->{probs,di} modified */
451 static void handleOverlappedFiles(const rpmTransactionSet ts, TFI_t fi)
452 /*@globals fileSystem @*/
453 /*@modifies ts, fi, fileSystem @*/
455 struct diskspaceInfo * ds = NULL;
456 uint_32 fixupSize = 0;
457 char * filespec = NULL;
458 int fileSpecAlloced = 0;
461 for (i = 0; i < fi->fc; i++) {
462 int otherPkgNum, otherFileNum;
466 if (XFA_SKIPPING(fi->actions[i]))
469 j = strlen(fi->dnl[fi->dil[i]]) + strlen(fi->bnl[i]) + 1;
471 if (j > fileSpecAlloced) {
472 fileSpecAlloced = j * 2;
473 filespec = xrealloc(filespec, fileSpecAlloced);
477 (void) stpcpy( stpcpy( filespec, fi->dnl[fi->dil[i]]), fi->bnl[i]);
481 while (ds->bsize && ds->dev != fi->fps[i].entry->dev) ds++;
482 if (!ds->bsize) ds = NULL;
487 * Retrieve all records that apply to this file. Note that the
488 * file info records were built in the same order as the packages
489 * will be installed and removed so the records for an overlapped
490 * files will be sorted in exactly the same order.
492 (void) htGetEntry(ts->ht, &fi->fps[i],
493 (const void ***) &recs, &numRecs, NULL);
496 * If this package is being added, look only at other packages
497 * being added -- removed packages dance to a different tune.
498 * If both this and the other package are being added, overlapped
499 * files must be identical (or marked as a conflict). The
500 * disposition of already installed config files leads to
501 * a small amount of extra complexity.
503 * If this package is being removed, then there are two cases that
504 * need to be worried about:
505 * If the other package is being added, then skip any overlapped files
506 * so that this package removal doesn't nuke the overlapped files
507 * that were just installed.
508 * If both this and the other package are being removed, then each
509 * file removal from preceding packages needs to be skipped so that
510 * the file removal occurs only on the last occurence of an overlapped
511 * file in the transaction set.
515 /* Locate this overlapped file in the set of added/removed packages. */
516 for (j = 0; j < numRecs && recs[j] != fi; j++)
519 /* Find what the previous disposition of this file was. */
520 otherFileNum = -1; /* keep gcc quiet */
521 for (otherPkgNum = j - 1; otherPkgNum >= 0; otherPkgNum--) {
522 /* Added packages need only look at other added packages. */
523 if (fi->type == TR_ADDED && recs[otherPkgNum]->type != TR_ADDED)
524 /*@innercontinue@*/ continue;
526 /* TESTME: there are more efficient searches in the world... */
527 for (otherFileNum = 0; otherFileNum < recs[otherPkgNum]->fc;
530 /* If the addresses are the same, so are the values. */
531 if ((fi->fps + i) == (recs[otherPkgNum]->fps + otherFileNum))
532 /*@innerbreak@*/ break;
534 /* Otherwise, compare fingerprints by value. */
535 /*@-nullpass@*/ /* LCL: looks good to me */
536 if (FP_EQUAL(fi->fps[i], recs[otherPkgNum]->fps[otherFileNum]))
537 /*@innerbreak@*/ break;
541 /* XXX is this test still necessary? */
542 if (recs[otherPkgNum]->actions[otherFileNum] != FA_UNKNOWN)
543 /*@innerbreak@*/ break;
549 if (otherPkgNum < 0) {
550 /* XXX is this test still necessary? */
551 if (fi->actions[i] != FA_UNKNOWN)
552 /*@switchbreak@*/ break;
553 if ((fi->fflags[i] & RPMFILE_CONFIG) &&
554 !lstat(filespec, &sb)) {
555 /* Here is a non-overlapped pre-existing config file. */
556 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
557 ? FA_ALTNAME : FA_BACKUP;
559 fi->actions[i] = FA_CREATE;
561 /*@switchbreak@*/ break;
564 /* Mark added overlapped non-identical files as a conflict. */
565 if ((ts->ignoreSet & RPMPROB_FILTER_REPLACENEWFILES)
566 && filecmp(recs[otherPkgNum]->fmodes[otherFileNum],
567 recs[otherPkgNum]->fmd5s[otherFileNum],
568 recs[otherPkgNum]->flinks[otherFileNum],
573 const char * pkgNEVR = fiGetNEVR(fi);
574 const char * altNEVR = fiGetNEVR(recs[otherPkgNum]);
575 rpmProblemSetAppend(ts->probs, RPMPROB_NEW_FILE_CONFLICT,
580 pkgNEVR = _free(pkgNEVR);
581 altNEVR = _free(altNEVR);
584 /* Try to get the disk accounting correct even if a conflict. */
585 fixupSize = recs[otherPkgNum]->fsizes[otherFileNum];
587 if ((fi->fflags[i] & RPMFILE_CONFIG) && !lstat(filespec, &sb)) {
588 /* Here is an overlapped pre-existing config file. */
589 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
590 ? FA_ALTNAME : FA_SKIP;
592 fi->actions[i] = FA_CREATE;
594 } /*@switchbreak@*/ break;
596 if (otherPkgNum >= 0) {
597 /* Here is an overlapped added file we don't want to nuke. */
598 if (recs[otherPkgNum]->actions[otherFileNum] != FA_ERASE) {
599 /* On updates, don't remove files. */
600 fi->actions[i] = FA_SKIP;
601 /*@switchbreak@*/ break;
603 /* Here is an overlapped removed file: skip in previous. */
604 recs[otherPkgNum]->actions[otherFileNum] = FA_SKIP;
606 if (XFA_SKIPPING(fi->actions[i]))
607 /*@switchbreak@*/ break;
608 if (fi->fstates && fi->fstates[i] != RPMFILE_STATE_NORMAL)
609 /*@switchbreak@*/ break;
610 if (!(S_ISREG(fi->fmodes[i]) && (fi->fflags[i] & RPMFILE_CONFIG))) {
611 fi->actions[i] = FA_ERASE;
612 /*@switchbreak@*/ break;
615 /* Here is a pre-existing modified config file that needs saving. */
617 if (!mdfile(filespec, mdsum) && strcmp(fi->fmd5s[i], mdsum)) {
618 fi->actions[i] = FA_BACKUP;
619 /*@switchbreak@*/ break;
622 fi->actions[i] = FA_ERASE;
623 /*@switchbreak@*/ break;
627 uint_32 s = BLOCK_ROUND(fi->fsizes[i], ds->bsize);
629 switch (fi->actions[i]) {
635 /*@switchbreak@*/ break;
638 * FIXME: If two packages share a file (same md5sum), and
639 * that file is being replaced on disk, will ds->bneeded get
640 * decremented twice? Quite probably!
644 ds->bneeded -= BLOCK_ROUND(fi->replacedSizes[i], ds->bsize);
645 /*@switchbreak@*/ break;
650 /*@switchbreak@*/ break;
653 /*@switchbreak@*/ break;
656 ds->bneeded -= BLOCK_ROUND(fixupSize, ds->bsize);
659 filespec = _free(filespec);
663 * Ensure that current package is newer than installed package.
664 * @param ts transaction set
665 * @param p current transaction element
666 * @param h installed header
667 * @return 0 if not newer, 1 if okay
669 static int ensureOlder(rpmTransactionSet ts,
670 const transactionElement p, const Header h)
673 int_32 reqFlags = (RPMSENSE_LESS | RPMSENSE_EQUAL);
679 if (p == NULL || h == NULL)
682 t = alloca(strlen(p->NEVR) + (p->epoch != NULL ? strlen(p->epoch) : 0) + 1);
685 if (p->epoch != NULL) t = stpcpy( stpcpy(t, p->epoch), ":");
686 if (p->version != NULL) t = stpcpy(t, p->version);
688 if (p->release != NULL) t = stpcpy(t, p->release);
690 req = dsSingle(RPMTAG_REQUIRENAME, p->name, reqEVR, reqFlags);
691 rc = headerMatchesDepFlags(h, req);
694 /*@-branchstate@*/ /* FIX: p->key ??? */
696 const char * altNEVR = hGetNEVR(h, NULL);
697 rpmProblemSetAppend(ts->probs, RPMPROB_OLDPACKAGE,
702 altNEVR = _free(altNEVR);
713 static void skipFiles(const rpmTransactionSet ts, TFI_t fi)
714 /*@globals rpmGlobalMacroContext @*/
715 /*@modifies fi, rpmGlobalMacroContext @*/
717 int noDocs = (ts->transFlags & RPMTRANS_FLAG_NODOCS);
718 char ** netsharedPaths = NULL;
719 const char ** languages;
720 const char * dn, * bn;
721 int dnlen, bnlen, ix;
728 noDocs = rpmExpandNumeric("%{_excludedocs}");
730 { const char *tmpPath = rpmExpand("%{_netsharedpath}", NULL);
732 if (tmpPath && *tmpPath != '%')
733 netsharedPaths = splitString(tmpPath, strlen(tmpPath), ':');
735 tmpPath = _free(tmpPath);
738 s = rpmExpand("%{_install_langs}", NULL);
740 if (!(s && *s != '%'))
743 languages = (const char **) splitString(s, strlen(s), ':');
749 /* Compute directory refcount, skip directory if now empty. */
750 drc = alloca(fi->dc * sizeof(*drc));
751 memset(drc, 0, fi->dc * sizeof(*drc));
752 dff = alloca(fi->dc * sizeof(*dff));
753 memset(dff, 0, fi->dc * sizeof(*dff));
755 for (i = 0; i < fi->fc; i++) {
766 /* Don't bother with skipped files */
767 if (XFA_SKIPPING(fi->actions[i])) {
773 * Skip net shared paths.
774 * Net shared paths are not relative to the current root (though
775 * they do need to take package relocations into account).
777 for (nsp = netsharedPaths; nsp && *nsp; nsp++) {
782 if (strncmp(dn, *nsp, len))
783 /*@innercontinue@*/ continue;
784 /* Only directories or complete file paths can be net shared */
785 if (!(dn[len] == '/' || dn[len] == '\0'))
786 /*@innercontinue@*/ continue;
788 if (len < (dnlen + bnlen))
789 /*@innercontinue@*/ continue;
790 if (strncmp(dn, *nsp, dnlen))
791 /*@innercontinue@*/ continue;
792 if (strncmp(bn, (*nsp) + dnlen, bnlen))
793 /*@innercontinue@*/ continue;
795 /* Only directories or complete file paths can be net shared */
796 if (!((*nsp)[len] == '/' || (*nsp)[len] == '\0'))
797 /*@innercontinue@*/ continue;
800 /*@innerbreak@*/ break;
804 drc[ix]--; dff[ix] = 1;
805 fi->actions[i] = FA_SKIPNETSHARED;
810 * Skip i18n language specific files.
812 if (fi->flangs && languages && *fi->flangs[i]) {
813 const char **lang, *l, *le;
814 for (lang = languages; *lang != NULL; lang++) {
815 if (!strcmp(*lang, "all"))
816 /*@innerbreak@*/ break;
817 for (l = fi->flangs[i]; *l != '\0'; l = le) {
818 for (le = l; *le != '\0' && *le != '|'; le++)
820 if ((le-l) > 0 && !strncmp(*lang, l, (le-l)))
821 /*@innerbreak@*/ break;
822 if (*le == '|') le++; /* skip over | */
825 /*@innerbreak@*/ break;
828 drc[ix]--; dff[ix] = 1;
829 fi->actions[i] = FA_SKIPNSTATE;
835 * Skip documentation if requested.
837 if (noDocs && (fi->fflags[i] & RPMFILE_DOC)) {
838 drc[ix]--; dff[ix] = 1;
839 fi->actions[i] = FA_SKIPNSTATE;
844 /* Skip (now empty) directories that had skipped files. */
845 for (j = 0; j < fi->dc; j++) {
847 if (drc[j]) continue; /* dir still has files. */
848 if (!dff[j]) continue; /* dir was not emptied here. */
850 /* Find parent directory and basename. */
851 dn = fi->dnl[j]; dnlen = strlen(dn) - 1;
852 bn = dn + dnlen; bnlen = 0;
853 while (bn > dn && bn[-1] != '/') {
859 /* If explicitly included in the package, skip the directory. */
860 for (i = 0; i < fi->fc; i++) {
863 if (XFA_SKIPPING(fi->actions[i]))
864 /*@innercontinue@*/ continue;
865 if (whatis(fi->fmodes[i]) != XDIR)
866 /*@innercontinue@*/ continue;
867 dir = fi->dnl[fi->dil[i]];
868 if (strlen(dir) != dnlen)
869 /*@innercontinue@*/ continue;
870 if (strncmp(dir, dn, dnlen))
871 /*@innercontinue@*/ continue;
872 if (strlen(fi->bnl[i]) != bnlen)
873 /*@innercontinue@*/ continue;
874 if (strncmp(fi->bnl[i], bn, bnlen))
875 /*@innercontinue@*/ continue;
876 rpmMessage(RPMMESS_DEBUG, _("excluding directory %s\n"), dn);
877 fi->actions[i] = FA_SKIPNSTATE;
878 /*@innerbreak@*/ break;
882 if (netsharedPaths) freeSplitString(netsharedPaths);
883 #ifdef DYING /* XXX freeFi will deal with this later. */
884 fi->flangs = _free(fi->flangs);
886 if (languages) freeSplitString((char **)languages);
890 * Return next transaction element's file info.
891 * @param tei transaction element iterator
892 * @return nest transaction element file info, NULL on termination
894 /*@unused@*/ static inline
895 TFI_t teNextFi(teIterator tei)
900 if (teNextIterator(tei) != NULL && tei->ocsave != -1)
901 fi = tei->ts->flList + tei->ocsave;
902 /*@-compdef -onlytrans -usereleased@*/ /* FIX: ts->flList may be released */
904 /*@=compdef =onlytrans =usereleased@*/
907 #define NOTIFY(_ts, _al) if ((_ts)->notify) (void) (_ts)->notify _al
909 int rpmRunTransactions( rpmTransactionSet ts,
910 rpmCallbackFunction notify, rpmCallbackData notifyData,
911 rpmProblemSet okProbs, rpmProblemSet * newProbs,
912 rpmtransFlags transFlags, rpmprobFilterFlags ignoreSet)
916 int totalFileCount = 0;
918 struct diskspaceInfo * dip;
919 struct sharedFileInfo * shared, * sharedList;
922 alKey pkgKey, lastKey;
924 transactionElement p;
925 fingerPrintCache fpc;
933 /* FIXME: what if the same package is included in ts twice? */
935 ts->transFlags = transFlags;
936 if (ts->transFlags & RPMTRANS_FLAG_NOSCRIPTS)
937 ts->transFlags |= (_noTransScripts | _noTransTriggers);
938 if (ts->transFlags & RPMTRANS_FLAG_NOTRIGGERS)
939 ts->transFlags |= _noTransTriggers;
941 /* XXX MULTILIB is broken, as packages can and do execute /sbin/ldconfig. */
942 if (ts->transFlags & (RPMTRANS_FLAG_JUSTDB | RPMTRANS_FLAG_MULTILIB))
943 ts->transFlags |= (_noTransScripts | _noTransTriggers);
946 ts->notifyData = notifyData;
948 ts->probs = *newProbs = rpmProblemSetCreate();
949 *newProbs = rpmpsLink(ts->probs, "RunTransactions");
951 ts->ignoreSet = ignoreSet;
952 ts->currDir = _free(ts->currDir);
953 ts->currDir = currentDirectory();
955 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
956 ts->id = (int_32) time(NULL);
958 memset(psm, 0, sizeof(*psm));
960 psm->ts = rpmtsLink(ts, "tsRun");
963 /* Get available space on mounted file systems. */
964 if (!(ts->ignoreSet & RPMPROB_FILTER_DISKSPACE) &&
965 !rpmGetFilesystemList(&ts->filesystems, &ts->filesystemCount)) {
968 ts->di = _free(ts->di);
969 dip = ts->di = xcalloc((ts->filesystemCount + 1), sizeof(*ts->di));
971 for (i = 0; (i < ts->filesystemCount) && dip; i++) {
972 #if STATFS_IN_SYS_STATVFS
974 memset(&sfb, 0, sizeof(sfb));
975 if (statvfs(ts->filesystems[i], &sfb))
979 /* This platform has the 4-argument version of the statfs call. The last two
980 * should be the size of struct statfs and 0, respectively. The 0 is the
981 * filesystem type, and is always 0 when statfs is called on a mounted
982 * filesystem, as we're doing.
984 memset(&sfb, 0, sizeof(sfb));
985 if (statfs(ts->filesystems[i], &sfb, sizeof(sfb), 0))
987 memset(&sfb, 0, sizeof(sfb));
988 if (statfs(ts->filesystems[i], &sfb))
994 ts->di[i].bsize = sfb.f_bsize;
995 ts->di[i].bneeded = 0;
996 ts->di[i].ineeded = 0;
997 #ifdef STATFS_HAS_F_BAVAIL
998 ts->di[i].bavail = sfb.f_bavail;
1000 /* FIXME: the statfs struct doesn't have a member to tell how many blocks are
1001 * available for non-superusers. f_blocks - f_bfree is probably too big, but
1002 * it's about all we can do.
1004 ts->di[i].bavail = sfb.f_blocks - sfb.f_bfree;
1006 /* XXX Avoid FAT and other file systems that have not inodes. */
1007 ts->di[i].iavail = !(sfb.f_ffree == 0 && sfb.f_files == 0)
1010 xx = stat(ts->filesystems[i], &sb);
1011 ts->di[i].dev = sb.st_dev;
1015 if (dip) ts->di[i].bsize = 0;
1018 /* ===============================================
1019 * For packages being installed:
1020 * - verify package arch/os.
1021 * - verify package epoch:version-release is newer.
1023 * For packages being removed:
1026 /* The ordering doesn't matter here */
1027 tei = teInitIterator(ts);
1028 while ((p = teNext(tei, TR_ADDED)) != NULL) {
1029 rpmdbMatchIterator mi;
1031 /*@-branchstate@*/ /* FIX: p->key ??? */
1032 if (!archOkay(p->arch) && !(ts->ignoreSet & RPMPROB_FILTER_IGNOREARCH))
1033 rpmProblemSetAppend(ts->probs, RPMPROB_BADARCH,
1038 if (!osOkay(p->os) && !(ts->ignoreSet & RPMPROB_FILTER_IGNOREOS))
1039 rpmProblemSetAppend(ts->probs, RPMPROB_BADOS,
1045 if (!(ts->ignoreSet & RPMPROB_FILTER_OLDPACKAGE)) {
1047 mi = rpmtsInitIterator(ts, RPMTAG_NAME, p->name, 0);
1048 while ((h = rpmdbNextIterator(mi)) != NULL)
1049 xx = ensureOlder(ts, p, h);
1050 mi = rpmdbFreeIterator(mi);
1053 /* XXX multilib should not display "already installed" problems */
1054 if (!(ts->ignoreSet & RPMPROB_FILTER_REPLACEPKG)
1055 #ifdef DYING /* XXX MULTILIB multiLib from transactionElement */
1056 && !alGetMultiLib(ts->addedPackages, i)
1059 mi = rpmtsInitIterator(ts, RPMTAG_NAME, p->name, 0);
1060 xx = rpmdbSetIteratorRE(mi, RPMTAG_VERSION, RPMMIRE_DEFAULT,
1062 xx = rpmdbSetIteratorRE(mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT,
1065 while (rpmdbNextIterator(mi) != NULL) {
1066 rpmProblemSetAppend(ts->probs, RPMPROB_PKG_INSTALLED,
1070 /*@innerbreak@*/ break;
1072 mi = rpmdbFreeIterator(mi);
1075 /* Count no. of files (if any). */
1077 totalFileCount += p->fns->fc;
1080 tei = teFreeIterator(tei);
1082 /* FIXME: it seems a bit silly to read in all of these headers twice */
1083 /* The ordering doesn't matter here */
1084 if (ts->numRemovedPackages > 0) {
1085 rpmdbMatchIterator mi;
1089 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES, NULL, 0);
1090 xx = rpmdbAppendIterator(mi, ts->removedPackages, ts->numRemovedPackages);
1091 while ((h = rpmdbNextIterator(mi)) != NULL) {
1092 if (headerGetEntry(h, RPMTAG_BASENAMES, NULL, NULL, &fileCount))
1093 totalFileCount += fileCount;
1095 mi = rpmdbFreeIterator(mi);
1098 /* ===============================================
1099 * Initialize transaction element file info for package:
1101 ts->flEntries = ts->numAddedPackages + ts->numRemovedPackages;
1102 ts->flList = xcalloc(ts->flEntries, sizeof(*ts->flList));
1105 * FIXME?: we'd be better off assembling one very large file list and
1106 * calling fpLookupList only once. I'm not sure that the speedup is
1107 * worth the trouble though.
1109 tei = teInitIterator(ts);
1110 while ((fi = teNextFi(tei)) != NULL) {
1113 fi->magic = TFIMAGIC;
1115 fi->type = ts->order[oc].type;
1122 /*@i@*/ fi->h = headerLink(ts->order[oc].h, "xfer to fi->h");
1123 ts->order[oc].h = headerFree(ts->order[oc].h, "xfer to fi->h");
1125 #ifdef DYING /* XXX MULTILIB multiLib from transactionElement */
1126 fi->multiLib = alGetMultiLib(ts->addedPackages, i);
1128 fi->multiLib = ts->order[oc].multiLib;
1131 /*@i@*/ fi->key = ts->order[oc].key;
1132 fi->relocs = ts->order[oc].relocs;
1133 /*@i@*/ fi->fd = ts->order[oc].fd;
1135 /* XXX availablePackage can be dumped here XXX */
1137 /* XXX header arg unused. */
1138 loadFi(ts, fi, fi->h, keep_header);
1143 /* Skip netshared paths, not our i18n files, and excluded docs */
1145 /*@switchbreak@*/ break;
1147 fi->record = ts->order[oc].u.removed.dboffset;
1148 /* Retrieve erased package header from the database. */
1149 { rpmdbMatchIterator mi;
1151 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
1152 &fi->record, sizeof(fi->record));
1153 if ((fi->h = rpmdbNextIterator(mi)) != NULL)
1154 fi->h = headerLink(fi->h, "TR_REMOVED loadFi");
1155 mi = rpmdbFreeIterator(mi);
1157 if (fi->h == NULL) {
1161 /* XXX header arg unused. */
1162 loadFi(ts, fi, fi->h, 0);
1163 /*@switchbreak@*/ break;
1168 fi->fps = xmalloc(fi->fc * sizeof(*fi->fps));
1170 tei = teFreeIterator(tei);
1172 if (!ts->chrootDone) {
1174 /*@-superuser -noeffect @*/
1175 xx = chroot(ts->rootDir);
1176 /*@=superuser =noeffect @*/
1178 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 1;
1181 chroot_prefix = ts->rootDir;
1186 ts->ht = htCreate(totalFileCount * 2, 0, 0, fpHashFunction, fpEqual);
1187 fpc = fpCacheCreate(totalFileCount);
1189 /* ===============================================
1190 * Add fingerprint for each file not skipped.
1192 tei = teInitIterator(ts);
1193 while ((fi = teNextFi(tei)) != NULL) {
1194 fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fi->fc, fi->fps);
1195 for (i = 0; i < fi->fc; i++) {
1196 if (XFA_SKIPPING(fi->actions[i]))
1197 /*@innercontinue@*/ continue;
1198 /*@-dependenttrans@*/
1199 htAddEntry(ts->ht, fi->fps + i, fi);
1200 /*@=dependenttrans@*/
1203 tei = teFreeIterator(tei);
1205 /*@-noeffectuncon @*/ /* FIX: check rc */
1206 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_START, 6, ts->flEntries,
1207 NULL, ts->notifyData));
1208 /*@=noeffectuncon@*/
1210 /* ===============================================
1211 * Compute file disposition for each package in transaction set.
1213 tei = teInitIterator(ts);
1214 while ((fi = teNextFi(tei)) != NULL) {
1215 dbiIndexSet * matches;
1218 /*@-noeffectuncon @*/ /* FIX: check rc */
1219 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_PROGRESS, (fi - ts->flList),
1220 ts->flEntries, NULL, ts->notifyData));
1221 /*@=noeffectuncon@*/
1223 if (fi->fc == 0) continue;
1225 /* Extract file info for all files in this package from the database. */
1226 matches = xcalloc(fi->fc, sizeof(*matches));
1227 if (rpmdbFindFpList(ts->rpmdb, fi->fps, matches, fi->fc)) {
1228 psm->ts = rpmtsUnlink(ts, "tsRun (rpmFindFpList fail)");
1229 return 1; /* XXX WTFO? */
1233 for (i = 0; i < fi->fc; i++)
1234 numShared += dbiIndexSetCount(matches[i]);
1236 /* Build sorted file info list for this package. */
1237 shared = sharedList = xcalloc((numShared + 1), sizeof(*sharedList));
1238 for (i = 0; i < fi->fc; i++) {
1240 * Take care not to mark files as replaced in packages that will
1241 * have been removed before we will get here.
1243 for (j = 0; j < dbiIndexSetCount(matches[i]); j++) {
1245 ro = dbiIndexRecordOffset(matches[i], j);
1247 for (k = 0; ro != knownBad && k < ts->orderCount; k++) {
1248 switch (ts->order[k].type) {
1250 if (ts->order[k].u.removed.dboffset == ro)
1252 /*@switchbreak@*/ break;
1254 /*@switchbreak@*/ break;
1258 shared->pkgFileNum = i;
1259 shared->otherPkg = dbiIndexRecordOffset(matches[i], j);
1260 shared->otherFileNum = dbiIndexRecordFileNumber(matches[i], j);
1261 shared->isRemoved = (knownBad == ro);
1264 matches[i] = dbiFreeIndexSet(matches[i]);
1266 numShared = shared - sharedList;
1267 shared->otherPkg = -1;
1268 matches = _free(matches);
1270 /* Sort file info by other package index (otherPkg) */
1271 qsort(sharedList, numShared, sizeof(*shared), sharedCmp);
1273 /* For all files from this package that are in the database ... */
1274 for (i = 0; i < numShared; i = nexti) {
1277 shared = sharedList + i;
1279 /* Find the end of the files in the other package. */
1280 for (nexti = i + 1; nexti < numShared; nexti++) {
1281 if (sharedList[nexti].otherPkg != shared->otherPkg)
1282 /*@innerbreak@*/ break;
1285 /* Is this file from a package being removed? */
1287 for (j = 0; j < ts->numRemovedPackages; j++) {
1288 if (ts->removedPackages[j] != shared->otherPkg)
1289 /*@innercontinue@*/ continue;
1291 /*@innerbreak@*/ break;
1294 /* Determine the fate of each file. */
1297 xx = handleInstInstalledFiles(ts, fi, shared, nexti - i,
1298 !(beingRemoved || (ts->ignoreSet & RPMPROB_FILTER_REPLACEOLDFILES)));
1299 /*@switchbreak@*/ break;
1302 xx = handleRmvdInstalledFiles(ts, fi, shared, nexti - i);
1303 /*@switchbreak@*/ break;
1309 /* Update disk space needs on each partition for this package. */
1310 handleOverlappedFiles(ts, fi);
1312 /* Check added package has sufficient space on each partition used. */
1315 if (!(ts->di && fi->fc))
1316 /*@switchbreak@*/ break;
1317 for (i = 0; i < ts->filesystemCount; i++) {
1321 /* XXX Avoid FAT and other file systems that have not inodes. */
1322 if (dip->iavail <= 0)
1323 /*@innercontinue@*/ continue;
1325 if (adj_fs_blocks(dip->bneeded) > dip->bavail) {
1326 const char * pkgNEVR = fiGetNEVR(fi);
1327 rpmProblemSetAppend(ts->probs, RPMPROB_DISKSPACE,
1329 ts->filesystems[i], NULL, NULL,
1330 (adj_fs_blocks(dip->bneeded) - dip->bavail) * dip->bsize);
1331 pkgNEVR = _free(pkgNEVR);
1334 if (adj_fs_blocks(dip->ineeded) > dip->iavail) {
1335 const char * pkgNEVR = fiGetNEVR(fi);
1336 rpmProblemSetAppend(ts->probs, RPMPROB_DISKNODES,
1338 ts->filesystems[i], NULL, NULL,
1339 (adj_fs_blocks(dip->ineeded) - dip->iavail));
1340 pkgNEVR = _free(pkgNEVR);
1343 /*@switchbreak@*/ break;
1345 /*@switchbreak@*/ break;
1348 tei = teFreeIterator(tei);
1350 if (ts->chrootDone) {
1351 /*@-superuser -noeffect @*/
1353 /*@=superuser =noeffect @*/
1355 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
1357 chroot_prefix = NULL;
1359 xx = chdir(ts->currDir);
1362 /*@-noeffectuncon @*/ /* FIX: check rc */
1363 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_STOP, 6, ts->flEntries,
1364 NULL, ts->notifyData));
1365 /*@=noeffectuncon @*/
1367 /* ===============================================
1368 * Free unused memory as soon as possible.
1371 tei = teInitIterator(ts);
1372 while ((fi = teNextFi(tei)) != NULL) {
1375 fi->fps = _free(fi->fps);
1377 tei = teFreeIterator(tei);
1383 /* ===============================================
1384 * If unfiltered problems exist, free memory and return.
1386 if ((ts->transFlags & RPMTRANS_FLAG_BUILD_PROBS)
1387 || (ts->probs->numProblems &&
1388 (okProbs != NULL || rpmProblemSetTrim(ts->probs, okProbs)))
1391 ts->flList = freeFl(ts, ts->flList);
1393 if (psm->ts != NULL)
1394 psm->ts = rpmtsUnlink(psm->ts, "tsRun (problems)");
1395 /*@-nullstate@*/ /* FIX: ts->flList may be NULL */
1396 return ts->orderCount;
1400 /* ===============================================
1401 * Save removed files before erasing.
1403 if (ts->transFlags & (RPMTRANS_FLAG_DIRSTASH | RPMTRANS_FLAG_REPACKAGE)) {
1404 tei = teInitIterator(ts);
1405 while ((fi = teNextFi(tei)) != NULL) {
1408 /*@switchbreak@*/ break;
1410 if (ts->transFlags & RPMTRANS_FLAG_REPACKAGE) {
1411 psm->fi = rpmfiLink(fi, "tsRepackage");
1412 xx = psmStage(psm, PSM_PKGSAVE);
1413 (void) rpmfiUnlink(fi, "tsRepackage");
1416 /*@switchbreak@*/ break;
1419 tei = teFreeIterator(tei);
1422 /* ===============================================
1423 * Install and remove packages.
1426 lastKey = (alKey)-2; /* erased packages have -1 */
1427 tei = teInitIterator(ts);
1428 /*@-branchstate@*/ /* FIX: fi reload needs work */
1429 while ((fi = teNextFi(tei)) != NULL) {
1435 psm->fi = rpmfiLink(fi, "tsInstall");
1439 pkgKey = ts->order[oc].u.addedKey;
1441 rpmMessage(RPMMESS_DEBUG, "========== +++ %s-%s-%s\n",
1442 fi->name, fi->version, fi->release);
1443 h = (fi->h ? headerLink(fi->h, "TR_ADDED install") : NULL);
1445 if (fi->fd == NULL) {
1446 /*@-noeffectuncon @*/ /* FIX: ??? */
1447 fi->fd = ts->notify(fi->h, RPMCALLBACK_INST_OPEN_FILE, 0, 0,
1448 fi->key, ts->notifyData);
1449 /*@=noeffectuncon @*/
1450 if (fi->fd != NULL) {
1453 h = headerFree(h, "TR_ADDED install");
1455 /*@-mustmod@*/ /* LCL: segfault */
1456 rpmrc = rpmReadPackageFile(ts, fi->fd,
1457 "rpmRunTransactions", &h);
1460 if (!(rpmrc == RPMRC_OK || rpmrc == RPMRC_BADSIZE)) {
1461 /*@-noeffectuncon @*/ /* FIX: check rc */
1462 (void) ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE,
1464 fi->key, ts->notifyData);
1465 /*@=noeffectuncon @*/
1468 } else if (fi->h != NULL) {
1469 Header foo = relocateFileList(ts, fi, h, NULL);
1470 h = headerFree(h, "TR_ADDED read free");
1471 h = headerLink(foo, "TR_ADDED relocate xfer");
1472 foo = headerFree(foo, "TR_ADDED relocate");
1474 if (fi->fd != NULL) gotfd = 1;
1479 if (fi->fd != NULL) {
1480 Header hsave = NULL;
1483 hsave = headerLink(fi->h, "TR_ADDED fi->h hsave");
1484 fi->h = headerFree(fi->h, "TR_ADDED fi->h free");
1485 fi->h = headerLink(h, "TR_ADDED fi->h link");
1487 char * fstates = fi->fstates;
1488 fileAction * actions = fi->actions;
1489 uint_32 multiLib = fi->multiLib;
1490 const void * key = fi->key;
1491 rpmRelocation * relocs = fi->relocs;
1501 fi->magic = TFIMAGIC;
1502 fi->type = ts->order[oc].type;
1504 loadFi(ts, fi, h, 1);
1505 fi->fstates = _free(fi->fstates);
1506 fi->fstates = fstates;
1507 fi->actions = _free(fi->actions);
1508 fi->actions = actions;
1509 fi->multiLib = multiLib;
1511 fi->relocs = relocs;
1513 /*@i@*/ fi->fd = fd;
1518 ts->transFlags |= RPMTRANS_FLAG_MULTILIB;
1520 if (psmStage(psm, PSM_PKGINSTALL)) {
1524 fi->h = headerFree(fi->h, "TR_ADDED fi->h free");
1526 fi->h = headerLink(hsave, "TR_ADDED fi->h restore");
1527 hsave = headerFree(hsave, "TR_ADDED hsave free");
1534 h = headerFree(h, "TR_ADDED h free");
1537 /*@-noeffectuncon @*/ /* FIX: check rc */
1538 (void)ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE, 0, 0,
1539 fi->key, ts->notifyData);
1540 /*@=noeffectuncon @*/
1543 fi->h = headerFree(fi->h, "TR_ADDED fini");
1545 /*@switchbreak@*/ break;
1547 rpmMessage(RPMMESS_DEBUG, "========== --- %s-%s-%s\n",
1548 fi->name, fi->version, fi->release);
1550 /* If install failed, then we shouldn't erase. */
1551 if (ts->order[oc].u.removed.dependsOnKey != lastKey) {
1552 if (psmStage(psm, PSM_PKGERASE))
1555 fi->h = headerFree(fi->h, "TR_REMOVED fini");
1557 /*@switchbreak@*/ break;
1559 xx = rpmdbSync(ts->rpmdb);
1560 (void) rpmfiUnlink(fi, "tsInstall");
1564 tei = teFreeIterator(tei);
1566 ts->flList = freeFl(ts, ts->flList);
1569 psm->ts = rpmtsUnlink(psm->ts, "tsRun");
1571 /*@-nullstate@*/ /* FIX: ts->flList may be NULL */