2 * \file lib/transaction.c
7 #define _NEED_TEITERATOR 1
11 #include <rpmmacro.h> /* XXX for rpmExpand */
14 #include "legacy.h" /* XXX mdfile */
15 #include "misc.h" /* XXX stripTrailingChar, splitString, currentDirectory */
18 /*@-redecl -exportheadervar@*/
20 extern const char * chroot_prefix;
21 /*@=redecl =exportheadervar@*/
23 /* XXX FIXME: merge with existing (broken?) tests in system.h */
24 /* portability fiddles */
25 #if STATFS_IN_SYS_STATVFS
27 # include <sys/statvfs.h>
28 #if defined(__LCLINT__)
29 /*@-declundef -exportheader -protoparammatch @*/ /* LCL: missing annotation */
30 extern int statvfs (const char * file, /*@out@*/ struct statvfs * buf)
31 /*@globals fileSystem @*/
32 /*@modifies *buf, fileSystem @*/;
33 /*@=declundef =exportheader =protoparammatch @*/
37 # if STATFS_IN_SYS_VFS
40 # if STATFS_IN_SYS_MOUNT
41 # include <sys/mount.h>
43 # if STATFS_IN_SYS_STATFS
44 # include <sys/statfs.h>
52 /*@access FD_t@*/ /* XXX compared with NULL */
53 /*@access Header@*/ /* XXX compared with NULL */
54 /*@access rpmProblemSet@*/ /* XXX need rpmProblemSetOK() */
55 /*@access dbiIndexSet@*/
64 /*@access teIterator@*/
65 /*@access transactionElement@*/
66 /*@access rpmTransactionSet@*/
70 struct diskspaceInfo {
71 dev_t dev; /*!< file system device number. */
72 signed long bneeded; /*!< no. of blocks needed. */
73 signed long ineeded; /*!< no. of inodes needed. */
74 int bsize; /*!< file system block size. */
75 signed long bavail; /*!< no. of blocks available. */
76 signed long iavail; /*!< no. of inodes available. */
80 * Adjust for root only reserved space. On linux e2fs, this is 5%.
82 #define adj_fs_blocks(_nb) (((_nb) * 21) / 20)
84 /* argon thought a shift optimization here was a waste of time... he's
86 #define BLOCK_ROUND(size, block) (((size) + (block) - 1) / (block))
90 static /*@null@*/ void * freeFl(rpmTransactionSet ts,
91 /*@only@*/ /*@null@*/ TFI_t flList)
98 /*@-usereleased -onlytrans @*/ /* FIX: fi needs to be only */
99 for (oc = 0, fi = flList; oc < ts->orderCount; oc++, fi++)
101 flList = _free(flList);
102 /*@=usereleased =onlytrans @*/
107 void rpmtransSetScriptFd(rpmTransactionSet ts, FD_t fd)
109 /*@-type@*/ /* FIX: cast? */
110 ts->scriptFd = (fd ? fdLink(fd, "rpmtransSetScriptFd") : NULL);
114 int rpmtransGetKeys(const rpmTransactionSet ts, fnpyKey ** ep, int * nep)
118 if (nep) *nep = ts->orderCount;
123 *ep = e = xmalloc(ts->orderCount * sizeof(*e));
124 for (oc = 0; oc < ts->orderCount; oc++, e++) {
125 switch (ts->order[oc].type) {
127 *e = ts->order[oc].key;
128 /*@switchbreak@*/ break;
131 /*@-mods@*/ /* FIX: double indirection. */
134 /*@switchbreak@*/ break;
143 static int archOkay(Header h)
149 /* make sure we're trying to install this on the proper architecture */
150 (void) headerGetEntry(h, RPMTAG_ARCH, &type, (void **) &pkgArch, &count);
152 if (type == RPM_INT8_TYPE) {
156 /* old arch handling */
157 rpmGetArchInfo(NULL, &archNum);
158 pkgArchNum = pkgArch;
159 if (archNum != *pkgArchNum) {
165 /* new arch handling */
166 if (!rpmMachineScore(RPM_MACHTABLE_INSTARCH, pkgArch)) {
176 static int osOkay(Header h)
182 /* make sure we're trying to install this on the proper os */
183 (void) headerGetEntry(h, RPMTAG_OS, &type, (void **) &pkgOs, &count);
185 if (type == RPM_INT8_TYPE) {
186 /* v1 packages and v2 packages both used improper OS numbers, so just
187 deal with it hope things work */
192 /* new os handling */
193 if (!rpmMachineScore(RPM_MACHTABLE_INSTOS, pkgOs)) {
203 static int sharedCmp(const void * one, const void * two)
206 const struct sharedFileInfo * a = one;
207 const struct sharedFileInfo * b = two;
209 if (a->otherPkg < b->otherPkg)
211 else if (a->otherPkg > b->otherPkg)
219 static fileAction decideFileFate(const char * dirName,
220 const char * baseName, short dbMode,
221 const char * dbMd5, const char * dbLink, short newMode,
222 const char * newMd5, const char * newLink, int newFlags,
223 rpmtransFlags transFlags)
224 /*@globals fileSystem @*/
225 /*@modifies fileSystem @*/
228 const char * dbAttr, * newAttr;
229 fileTypes dbWhat, newWhat, diskWhat;
232 int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
233 char * filespec = alloca(strlen(dirName) + strlen(baseName) + 1);
235 (void) stpcpy( stpcpy(filespec, dirName), baseName);
237 if (lstat(filespec, &sb)) {
239 * The file doesn't exist on the disk. Create it unless the new
240 * package has marked it as missingok, or allfiles is requested.
242 if (!(transFlags & RPMTRANS_FLAG_ALLFILES) &&
243 (newFlags & RPMFILE_MISSINGOK)) {
244 rpmMessage(RPMMESS_DEBUG, _("%s skipped due to missingok flag\n"),
252 diskWhat = whatis(sb.st_mode);
253 dbWhat = whatis(dbMode);
254 newWhat = whatis(newMode);
256 /* RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
257 them in older packages as well */
258 if (newWhat == XDIR) {
262 if (diskWhat != newWhat) {
264 } else if (newWhat != dbWhat && diskWhat != dbWhat) {
266 } else if (dbWhat != newWhat) {
268 } else if (dbWhat != LINK && dbWhat != REG) {
273 rc = mdfile(filespec, buffer);
276 /* assume the file has been removed, don't freak */
281 } else /* dbWhat == LINK */ {
282 memset(buffer, 0, sizeof(buffer));
283 i = readlink(filespec, buffer, sizeof(buffer) - 1);
285 /* assume the file has been removed, don't freak */
292 /* this order matters - we'd prefer to CREATE the file if at all
293 possible in case something else (like the timestamp) has changed */
295 if (!strcmp(dbAttr, buffer)) {
296 /* this config file has never been modified, so just replace it */
300 if (!strcmp(dbAttr, newAttr)) {
301 /* this file is the same in all versions of this package */
306 * The config file on the disk has been modified, but
307 * the ones in the two packages are different. It would
308 * be nice if RPM was smart enough to at least try and
309 * merge the difference ala CVS, but...
316 static int filecmp(short mode1, const char * md51, const char * link1,
317 short mode2, const char * md52, const char * link2)
320 fileTypes what1 = whatis(mode1);
321 fileTypes what2 = whatis(mode2);
323 if (what1 != what2) return 1;
326 return strcmp(link1, link2);
327 else if (what1 == REG)
328 return strcmp(md51, md52);
335 /* XXX only ts->{probs,rpmdb} modified */
336 static int handleInstInstalledFiles(const rpmTransactionSet ts, TFI_t fi,
337 struct sharedFileInfo * shared,
338 int sharedCount, int reportConflicts)
339 /*@globals fileSystem @*/
340 /*@modifies ts, fi, fileSystem @*/
343 HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
344 rpmtransFlags transFlags = ts->transFlags;
345 rpmTagType oltype, omtype;
348 const char ** otherMd5s;
349 const char ** otherLinks;
350 const char * otherStates;
351 uint_32 * otherFlags;
352 uint_32 * otherSizes;
353 uint_16 * otherModes;
357 rpmdbMatchIterator mi;
359 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES, &shared->otherPkg, sizeof(shared->otherPkg));
360 h = rpmdbNextIterator(mi);
362 mi = rpmdbFreeIterator(mi);
366 xx = hge(h, RPMTAG_FILEMD5S, &omtype, (void **) &otherMd5s, NULL);
367 xx = hge(h, RPMTAG_FILELINKTOS, &oltype, (void **) &otherLinks, NULL);
368 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
369 xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &otherModes, NULL);
370 xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &otherFlags, NULL);
371 xx = hge(h, RPMTAG_FILESIZES, NULL, (void **) &otherSizes, NULL);
373 fi->replaced = xmalloc(sharedCount * sizeof(*fi->replaced));
375 for (i = 0; i < sharedCount; i++, shared++) {
376 int otherFileNum, fileNum;
377 otherFileNum = shared->otherFileNum;
378 fileNum = shared->pkgFileNum;
380 /* XXX another tedious segfault, assume file state normal. */
381 if (otherStates && otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
384 if (XFA_SKIPPING(fi->actions[fileNum]))
387 if (filecmp(otherModes[otherFileNum],
388 otherMd5s[otherFileNum],
389 otherLinks[otherFileNum],
392 fi->flinks[fileNum])) {
393 /*@-compdef@*/ /* FIX: *fi->replaced undefined */
395 rpmProblemSetAppend(ts->probs, RPMPROB_FILE_CONFLICT,
396 fiGetNVR(fi), fi->key,
397 fi->dnl[fi->dil[fileNum]], fi->bnl[fileNum],
401 if (!(otherFlags[otherFileNum] | fi->fflags[fileNum])
404 if (!shared->isRemoved)
405 fi->replaced[numReplaced++] = *shared;
410 if ((otherFlags[otherFileNum] | fi->fflags[fileNum]) & RPMFILE_CONFIG) {
411 fi->actions[fileNum] = decideFileFate(
412 fi->dnl[fi->dil[fileNum]],
414 otherModes[otherFileNum],
415 otherMd5s[otherFileNum],
416 otherLinks[otherFileNum],
424 fi->replacedSizes[fileNum] = otherSizes[otherFileNum];
427 otherMd5s = hfd(otherMd5s, omtype);
428 otherLinks = hfd(otherLinks, oltype);
429 mi = rpmdbFreeIterator(mi);
431 fi->replaced = xrealloc(fi->replaced, /* XXX memory leak */
432 sizeof(*fi->replaced) * (numReplaced + 1));
433 fi->replaced[numReplaced].otherPkg = 0;
440 /* XXX only ts->rpmdb modified */
441 static int handleRmvdInstalledFiles(const rpmTransactionSet ts, TFI_t fi,
442 struct sharedFileInfo * shared, int sharedCount)
443 /*@globals fileSystem @*/
444 /*@modifies fi, fileSystem @*/
448 const char * otherStates;
451 rpmdbMatchIterator mi;
453 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
454 &shared->otherPkg, sizeof(shared->otherPkg));
455 h = rpmdbNextIterator(mi);
457 mi = rpmdbFreeIterator(mi);
461 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
463 for (i = 0; i < sharedCount; i++, shared++) {
464 int otherFileNum, fileNum;
465 otherFileNum = shared->otherFileNum;
466 fileNum = shared->pkgFileNum;
468 if (otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
471 fi->actions[fileNum] = FA_SKIP;
474 mi = rpmdbFreeIterator(mi);
480 * Update disk space needs on each partition for this package.
482 /* XXX only ts->{probs,di} modified */
483 static void handleOverlappedFiles(const rpmTransactionSet ts, TFI_t fi)
484 /*@globals fileSystem @*/
485 /*@modifies ts, fi, fileSystem @*/
487 struct diskspaceInfo * ds = NULL;
488 uint_32 fixupSize = 0;
489 char * filespec = NULL;
490 int fileSpecAlloced = 0;
493 for (i = 0; i < fi->fc; i++) {
494 int otherPkgNum, otherFileNum;
498 if (XFA_SKIPPING(fi->actions[i]))
501 j = strlen(fi->dnl[fi->dil[i]]) + strlen(fi->bnl[i]) + 1;
503 if (j > fileSpecAlloced) {
504 fileSpecAlloced = j * 2;
505 filespec = xrealloc(filespec, fileSpecAlloced);
509 (void) stpcpy( stpcpy( filespec, fi->dnl[fi->dil[i]]), fi->bnl[i]);
513 while (ds->bsize && ds->dev != fi->fps[i].entry->dev) ds++;
514 if (!ds->bsize) ds = NULL;
519 * Retrieve all records that apply to this file. Note that the
520 * file info records were built in the same order as the packages
521 * will be installed and removed so the records for an overlapped
522 * files will be sorted in exactly the same order.
524 (void) htGetEntry(ts->ht, &fi->fps[i],
525 (const void ***) &recs, &numRecs, NULL);
528 * If this package is being added, look only at other packages
529 * being added -- removed packages dance to a different tune.
530 * If both this and the other package are being added, overlapped
531 * files must be identical (or marked as a conflict). The
532 * disposition of already installed config files leads to
533 * a small amount of extra complexity.
535 * If this package is being removed, then there are two cases that
536 * need to be worried about:
537 * If the other package is being added, then skip any overlapped files
538 * so that this package removal doesn't nuke the overlapped files
539 * that were just installed.
540 * If both this and the other package are being removed, then each
541 * file removal from preceding packages needs to be skipped so that
542 * the file removal occurs only on the last occurence of an overlapped
543 * file in the transaction set.
547 /* Locate this overlapped file in the set of added/removed packages. */
548 for (j = 0; j < numRecs && recs[j] != fi; j++)
551 /* Find what the previous disposition of this file was. */
552 otherFileNum = -1; /* keep gcc quiet */
553 for (otherPkgNum = j - 1; otherPkgNum >= 0; otherPkgNum--) {
554 /* Added packages need only look at other added packages. */
555 if (fi->type == TR_ADDED && recs[otherPkgNum]->type != TR_ADDED)
556 /*@innercontinue@*/ continue;
558 /* TESTME: there are more efficient searches in the world... */
559 for (otherFileNum = 0; otherFileNum < recs[otherPkgNum]->fc;
562 /* If the addresses are the same, so are the values. */
563 if ((fi->fps + i) == (recs[otherPkgNum]->fps + otherFileNum))
564 /*@innerbreak@*/ break;
566 /* Otherwise, compare fingerprints by value. */
567 /*@-nullpass@*/ /* LCL: looks good to me */
568 if (FP_EQUAL(fi->fps[i], recs[otherPkgNum]->fps[otherFileNum]))
569 /*@innerbreak@*/ break;
573 /* XXX is this test still necessary? */
574 if (recs[otherPkgNum]->actions[otherFileNum] != FA_UNKNOWN)
575 /*@innerbreak@*/ break;
581 if (otherPkgNum < 0) {
582 /* XXX is this test still necessary? */
583 if (fi->actions[i] != FA_UNKNOWN)
584 /*@switchbreak@*/ break;
585 if ((fi->fflags[i] & RPMFILE_CONFIG) &&
586 !lstat(filespec, &sb)) {
587 /* Here is a non-overlapped pre-existing config file. */
588 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
589 ? FA_ALTNAME : FA_BACKUP;
591 fi->actions[i] = FA_CREATE;
593 /*@switchbreak@*/ break;
596 /* Mark added overlapped non-identical files as a conflict. */
597 if ((ts->ignoreSet & RPMPROB_FILTER_REPLACENEWFILES)
598 && filecmp(recs[otherPkgNum]->fmodes[otherFileNum],
599 recs[otherPkgNum]->fmd5s[otherFileNum],
600 recs[otherPkgNum]->flinks[otherFileNum],
605 rpmProblemSetAppend(ts->probs, RPMPROB_NEW_FILE_CONFLICT,
606 fiGetNVR(fi), fi->key,
608 fiGetNVR(recs[otherPkgNum]),
612 /* Try to get the disk accounting correct even if a conflict. */
613 fixupSize = recs[otherPkgNum]->fsizes[otherFileNum];
615 if ((fi->fflags[i] & RPMFILE_CONFIG) && !lstat(filespec, &sb)) {
616 /* Here is an overlapped pre-existing config file. */
617 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
618 ? FA_ALTNAME : FA_SKIP;
620 fi->actions[i] = FA_CREATE;
622 } /*@switchbreak@*/ break;
624 if (otherPkgNum >= 0) {
625 /* Here is an overlapped added file we don't want to nuke. */
626 if (recs[otherPkgNum]->actions[otherFileNum] != FA_ERASE) {
627 /* On updates, don't remove files. */
628 fi->actions[i] = FA_SKIP;
629 /*@switchbreak@*/ break;
631 /* Here is an overlapped removed file: skip in previous. */
632 recs[otherPkgNum]->actions[otherFileNum] = FA_SKIP;
634 if (XFA_SKIPPING(fi->actions[i]))
635 /*@switchbreak@*/ break;
636 if (fi->fstates && fi->fstates[i] != RPMFILE_STATE_NORMAL)
637 /*@switchbreak@*/ break;
638 if (!(S_ISREG(fi->fmodes[i]) && (fi->fflags[i] & RPMFILE_CONFIG))) {
639 fi->actions[i] = FA_ERASE;
640 /*@switchbreak@*/ break;
643 /* Here is a pre-existing modified config file that needs saving. */
645 if (!mdfile(filespec, mdsum) && strcmp(fi->fmd5s[i], mdsum)) {
646 fi->actions[i] = FA_BACKUP;
647 /*@switchbreak@*/ break;
650 fi->actions[i] = FA_ERASE;
651 /*@switchbreak@*/ break;
655 uint_32 s = BLOCK_ROUND(fi->fsizes[i], ds->bsize);
657 switch (fi->actions[i]) {
663 /*@switchbreak@*/ break;
666 * FIXME: If two packages share a file (same md5sum), and
667 * that file is being replaced on disk, will ds->bneeded get
668 * decremented twice? Quite probably!
672 ds->bneeded -= BLOCK_ROUND(fi->replacedSizes[i], ds->bsize);
673 /*@switchbreak@*/ break;
678 /*@switchbreak@*/ break;
681 /*@switchbreak@*/ break;
684 ds->bneeded -= BLOCK_ROUND(fixupSize, ds->bsize);
687 filespec = _free(filespec);
692 static int ensureOlder(rpmTransactionSet ts,
693 const Header h, /*@null@*/ const Header old,
694 /*@dependent@*/ /*@null@*/ const void * key)
699 if (old == NULL) return 1;
701 result = rpmVersionCompare(old, h);
704 else if (result > 0) {
706 /*@-evalorder@*/ /* LCL: is confused */
707 rpmProblemSetAppend(ts->probs, RPMPROB_OLDPACKAGE,
708 hGetNVR(h, NULL), key,
720 static void skipFiles(const rpmTransactionSet ts, TFI_t fi)
721 /*@globals rpmGlobalMacroContext @*/
722 /*@modifies fi, rpmGlobalMacroContext @*/
724 int noDocs = (ts->transFlags & RPMTRANS_FLAG_NODOCS);
725 char ** netsharedPaths = NULL;
726 const char ** languages;
727 const char * dn, * bn;
728 int dnlen, bnlen, ix;
735 noDocs = rpmExpandNumeric("%{_excludedocs}");
737 { const char *tmpPath = rpmExpand("%{_netsharedpath}", NULL);
739 if (tmpPath && *tmpPath != '%')
740 netsharedPaths = splitString(tmpPath, strlen(tmpPath), ':');
742 tmpPath = _free(tmpPath);
745 s = rpmExpand("%{_install_langs}", NULL);
747 if (!(s && *s != '%'))
750 languages = (const char **) splitString(s, strlen(s), ':');
756 /* Compute directory refcount, skip directory if now empty. */
757 drc = alloca(fi->dc * sizeof(*drc));
758 memset(drc, 0, fi->dc * sizeof(*drc));
759 dff = alloca(fi->dc * sizeof(*dff));
760 memset(dff, 0, fi->dc * sizeof(*dff));
762 for (i = 0; i < fi->fc; i++) {
773 /* Don't bother with skipped files */
774 if (XFA_SKIPPING(fi->actions[i])) {
780 * Skip net shared paths.
781 * Net shared paths are not relative to the current root (though
782 * they do need to take package relocations into account).
784 for (nsp = netsharedPaths; nsp && *nsp; nsp++) {
789 if (strncmp(dn, *nsp, len))
790 /*@innercontinue@*/ continue;
791 /* Only directories or complete file paths can be net shared */
792 if (!(dn[len] == '/' || dn[len] == '\0'))
793 /*@innercontinue@*/ continue;
795 if (len < (dnlen + bnlen))
796 /*@innercontinue@*/ continue;
797 if (strncmp(dn, *nsp, dnlen))
798 /*@innercontinue@*/ continue;
799 if (strncmp(bn, (*nsp) + dnlen, bnlen))
800 /*@innercontinue@*/ continue;
802 /* Only directories or complete file paths can be net shared */
803 if (!((*nsp)[len] == '/' || (*nsp)[len] == '\0'))
804 /*@innercontinue@*/ continue;
807 /*@innerbreak@*/ break;
811 drc[ix]--; dff[ix] = 1;
812 fi->actions[i] = FA_SKIPNETSHARED;
817 * Skip i18n language specific files.
819 if (fi->flangs && languages && *fi->flangs[i]) {
820 const char **lang, *l, *le;
821 for (lang = languages; *lang != NULL; lang++) {
822 if (!strcmp(*lang, "all"))
823 /*@innerbreak@*/ break;
824 for (l = fi->flangs[i]; *l != '\0'; l = le) {
825 for (le = l; *le != '\0' && *le != '|'; le++)
827 if ((le-l) > 0 && !strncmp(*lang, l, (le-l)))
828 /*@innerbreak@*/ break;
829 if (*le == '|') le++; /* skip over | */
832 /*@innerbreak@*/ break;
835 drc[ix]--; dff[ix] = 1;
836 fi->actions[i] = FA_SKIPNSTATE;
842 * Skip documentation if requested.
844 if (noDocs && (fi->fflags[i] & RPMFILE_DOC)) {
845 drc[ix]--; dff[ix] = 1;
846 fi->actions[i] = FA_SKIPNSTATE;
851 /* Skip (now empty) directories that had skipped files. */
852 for (j = 0; j < fi->dc; j++) {
854 if (drc[j]) continue; /* dir still has files. */
855 if (!dff[j]) continue; /* dir was not emptied here. */
857 /* Find parent directory and basename. */
858 dn = fi->dnl[j]; dnlen = strlen(dn) - 1;
859 bn = dn + dnlen; bnlen = 0;
860 while (bn > dn && bn[-1] != '/') {
866 /* If explicitly included in the package, skip the directory. */
867 for (i = 0; i < fi->fc; i++) {
870 if (XFA_SKIPPING(fi->actions[i]))
871 /*@innercontinue@*/ continue;
872 if (whatis(fi->fmodes[i]) != XDIR)
873 /*@innercontinue@*/ continue;
874 dir = fi->dnl[fi->dil[i]];
875 if (strlen(dir) != dnlen)
876 /*@innercontinue@*/ continue;
877 if (strncmp(dir, dn, dnlen))
878 /*@innercontinue@*/ continue;
879 if (strlen(fi->bnl[i]) != bnlen)
880 /*@innercontinue@*/ continue;
881 if (strncmp(fi->bnl[i], bn, bnlen))
882 /*@innercontinue@*/ continue;
883 rpmMessage(RPMMESS_DEBUG, _("excluding directory %s\n"), dn);
884 fi->actions[i] = FA_SKIPNSTATE;
885 /*@innerbreak@*/ break;
889 if (netsharedPaths) freeSplitString(netsharedPaths);
890 #ifdef DYING /* XXX freeFi will deal with this later. */
891 fi->flangs = _free(fi->flangs);
893 if (languages) freeSplitString((char **)languages);
897 * Return next transaction element's file info.
898 * @param tei transaction element iterator
899 * @return nest transaction element file info, NULL on termination
901 /*@unused@*/ static inline
902 TFI_t teNextFi(teIterator tei)
907 if (teNextIterator(tei) != NULL && tei->ocsave != -1)
908 fi = tei->ts->flList + tei->ocsave;
909 /*@-compdef -onlytrans -usereleased@*/ /* FIX: ts->flList may be released */
911 /*@=compdef =onlytrans =usereleased@*/
914 #define NOTIFY(_ts, _al) if ((_ts)->notify) (void) (_ts)->notify _al
916 int rpmRunTransactions( rpmTransactionSet ts,
917 rpmCallbackFunction notify, rpmCallbackData notifyData,
918 rpmProblemSet okProbs, rpmProblemSet * newProbs,
919 rpmtransFlags transFlags, rpmprobFilterFlags ignoreSet)
923 int totalFileCount = 0;
925 struct diskspaceInfo * dip;
926 struct sharedFileInfo * shared, * sharedList;
929 alKey pkgKey, lastKey;
931 transactionElement p;
932 fingerPrintCache fpc;
938 int keep_header = 1; /* XXX rpmProblemSetAppend prevents dumping headers. */
940 /* FIXME: what if the same package is included in ts twice? */
942 ts->transFlags = transFlags;
943 if (ts->transFlags & RPMTRANS_FLAG_NOSCRIPTS)
944 ts->transFlags |= (_noTransScripts | _noTransTriggers);
945 if (ts->transFlags & RPMTRANS_FLAG_NOTRIGGERS)
946 ts->transFlags |= _noTransTriggers;
948 /* XXX MULTILIB is broken, as packages can and do execute /sbin/ldconfig. */
949 if (ts->transFlags & (RPMTRANS_FLAG_JUSTDB | RPMTRANS_FLAG_MULTILIB))
950 ts->transFlags |= (_noTransScripts | _noTransTriggers);
953 ts->notifyData = notifyData;
955 ts->probs = *newProbs = rpmProblemSetCreate();
957 ts->ignoreSet = ignoreSet;
958 ts->currDir = _free(ts->currDir);
959 ts->currDir = currentDirectory();
961 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
962 ts->id = (int_32) time(NULL);
964 memset(psm, 0, sizeof(*psm));
966 psm->ts = rpmtsLink(ts, "tsRun");
969 /* Get available space on mounted file systems. */
970 if (!(ts->ignoreSet & RPMPROB_FILTER_DISKSPACE) &&
971 !rpmGetFilesystemList(&ts->filesystems, &ts->filesystemCount)) {
974 ts->di = _free(ts->di);
975 dip = ts->di = xcalloc((ts->filesystemCount + 1), sizeof(*ts->di));
977 for (i = 0; (i < ts->filesystemCount) && dip; i++) {
978 #if STATFS_IN_SYS_STATVFS
980 memset(&sfb, 0, sizeof(sfb));
981 if (statvfs(ts->filesystems[i], &sfb))
985 /* This platform has the 4-argument version of the statfs call. The last two
986 * should be the size of struct statfs and 0, respectively. The 0 is the
987 * filesystem type, and is always 0 when statfs is called on a mounted
988 * filesystem, as we're doing.
990 memset(&sfb, 0, sizeof(sfb));
991 if (statfs(ts->filesystems[i], &sfb, sizeof(sfb), 0))
993 memset(&sfb, 0, sizeof(sfb));
994 if (statfs(ts->filesystems[i], &sfb))
1000 ts->di[i].bsize = sfb.f_bsize;
1001 ts->di[i].bneeded = 0;
1002 ts->di[i].ineeded = 0;
1003 #ifdef STATFS_HAS_F_BAVAIL
1004 ts->di[i].bavail = sfb.f_bavail;
1006 /* FIXME: the statfs struct doesn't have a member to tell how many blocks are
1007 * available for non-superusers. f_blocks - f_bfree is probably too big, but
1008 * it's about all we can do.
1010 ts->di[i].bavail = sfb.f_blocks - sfb.f_bfree;
1012 /* XXX Avoid FAT and other file systems that have not inodes. */
1013 ts->di[i].iavail = !(sfb.f_ffree == 0 && sfb.f_files == 0)
1016 xx = stat(ts->filesystems[i], &sb);
1017 ts->di[i].dev = sb.st_dev;
1021 if (dip) ts->di[i].bsize = 0;
1024 /* ===============================================
1025 * For packages being installed:
1026 * - verify package arch/os.
1027 * - verify package epoch:version-release is newer.
1029 * For packages being removed:
1032 /* The ordering doesn't matter here */
1033 tei = teInitIterator(ts);
1034 while ((p = teNext(tei, TR_ADDED)) != NULL) {
1035 const char * n, * v, * r;
1037 rpmdbMatchIterator mi;
1040 pkgKey = p->u.addedKey;
1042 h = alGetHeader(ts->addedPackages, pkgKey, 0);
1043 if (h == NULL) /* XXX can't happen */
1046 (void) headerNVR(h, &n, &v, &r);
1049 if (!archOkay(h) && !(ts->ignoreSet & RPMPROB_FILTER_IGNOREARCH))
1050 rpmProblemSetAppend(ts->probs, RPMPROB_BADARCH,
1051 hGetNVR(h, NULL), key,
1052 NULL, NULL, NULL, 0);
1054 if (!osOkay(h) && !(ts->ignoreSet & RPMPROB_FILTER_IGNOREOS))
1055 rpmProblemSetAppend(ts->probs, RPMPROB_BADOS,
1056 hGetNVR(h, NULL), key,
1057 NULL, NULL, NULL, 0);
1059 if (!(ts->ignoreSet & RPMPROB_FILTER_OLDPACKAGE)) {
1061 mi = rpmtsInitIterator(ts, RPMTAG_NAME, n, 0);
1062 while ((oldH = rpmdbNextIterator(mi)) != NULL)
1063 xx = ensureOlder(ts, h, oldH, key);
1064 mi = rpmdbFreeIterator(mi);
1067 /* XXX multilib should not display "already installed" problems */
1068 if (!(ts->ignoreSet & RPMPROB_FILTER_REPLACEPKG)
1069 #ifdef DYING /* XXX MULTILIB multiLib from transactionElement */
1070 && !alGetMultiLib(ts->addedPackages, i)
1073 mi = rpmtsInitIterator(ts, RPMTAG_NAME, n, 0);
1074 xx = rpmdbSetIteratorRE(mi, RPMTAG_VERSION, RPMMIRE_DEFAULT, v);
1075 xx = rpmdbSetIteratorRE(mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT, r);
1077 while (rpmdbNextIterator(mi) != NULL) {
1078 rpmProblemSetAppend(ts->probs, RPMPROB_PKG_INSTALLED,
1079 hGetNVR(h, NULL), key,
1080 NULL, NULL, NULL, 0);
1081 /*@innerbreak@*/ break;
1083 mi = rpmdbFreeIterator(mi);
1086 totalFileCount += alGetFilesCount(ts->addedPackages, pkgKey);
1088 h = headerFree(h, "alGetHeader (rpmtsRun sanity)");
1091 tei = teFreeIterator(tei);
1093 /* FIXME: it seems a bit silly to read in all of these headers twice */
1094 /* The ordering doesn't matter here */
1095 if (ts->numRemovedPackages > 0) {
1096 rpmdbMatchIterator mi;
1100 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES, NULL, 0);
1101 xx = rpmdbAppendIterator(mi, ts->removedPackages, ts->numRemovedPackages);
1102 while ((h = rpmdbNextIterator(mi)) != NULL) {
1103 if (headerGetEntry(h, RPMTAG_BASENAMES, NULL, NULL, &fileCount))
1104 totalFileCount += fileCount;
1106 mi = rpmdbFreeIterator(mi);
1109 /* ===============================================
1110 * Initialize transaction element file info for package:
1112 ts->flEntries = alGetSize(ts->addedPackages) + ts->numRemovedPackages;
1113 ts->flList = xcalloc(ts->flEntries, sizeof(*ts->flList));
1116 * FIXME?: we'd be better off assembling one very large file list and
1117 * calling fpLookupList only once. I'm not sure that the speedup is
1118 * worth the trouble though.
1120 tei = teInitIterator(ts);
1121 while ((fi = teNextFi(tei)) != NULL) {
1124 fi->magic = TFIMAGIC;
1126 fi->type = ts->order[oc].type;
1133 pkgKey = ts->order[oc].u.addedKey;
1135 fi->h = alGetHeader(ts->addedPackages, pkgKey, 1);
1136 #ifdef DYING /* XXX MULTILIB multiLib from transactionElement */
1137 fi->multiLib = alGetMultiLib(ts->addedPackages, i);
1139 fi->multiLib = ts->order[oc].multiLib;
1142 /*@i@*/ fi->key = ts->order[oc].key;
1143 fi->relocs = ts->order[oc].relocs;
1144 /*@i@*/ fi->fd = ts->order[oc].fd;
1146 /* XXX availablePackage can be dumped here XXX */
1148 /* XXX header arg unused. */
1149 loadFi(ts, fi, fi->h, keep_header);
1154 /* Skip netshared paths, not our i18n files, and excluded docs */
1156 /*@switchbreak@*/ break;
1158 fi->record = ts->order[oc].u.removed.dboffset;
1159 /* Retrieve erased package header from the database. */
1160 { rpmdbMatchIterator mi;
1162 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
1163 &fi->record, sizeof(fi->record));
1164 if ((fi->h = rpmdbNextIterator(mi)) != NULL)
1165 fi->h = headerLink(fi->h, "TR_REMOVED loadFi");
1166 mi = rpmdbFreeIterator(mi);
1168 if (fi->h == NULL) {
1172 /* XXX header arg unused. */
1173 loadFi(ts, fi, fi->h, 0);
1174 /*@switchbreak@*/ break;
1179 fi->fps = xmalloc(fi->fc * sizeof(*fi->fps));
1181 tei = teFreeIterator(tei);
1183 if (!ts->chrootDone) {
1185 /*@-superuser -noeffect @*/
1186 xx = chroot(ts->rootDir);
1187 /*@=superuser =noeffect @*/
1189 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 1;
1192 chroot_prefix = ts->rootDir;
1197 ts->ht = htCreate(totalFileCount * 2, 0, 0, fpHashFunction, fpEqual);
1198 fpc = fpCacheCreate(totalFileCount);
1200 /* ===============================================
1201 * Add fingerprint for each file not skipped.
1203 tei = teInitIterator(ts);
1204 while ((fi = teNextFi(tei)) != NULL) {
1205 fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fi->fc, fi->fps);
1206 for (i = 0; i < fi->fc; i++) {
1207 if (XFA_SKIPPING(fi->actions[i]))
1208 /*@innercontinue@*/ continue;
1209 /*@-dependenttrans@*/
1210 htAddEntry(ts->ht, fi->fps + i, fi);
1211 /*@=dependenttrans@*/
1214 tei = teFreeIterator(tei);
1216 /*@-noeffectuncon @*/ /* FIX: check rc */
1217 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_START, 6, ts->flEntries,
1218 NULL, ts->notifyData));
1219 /*@=noeffectuncon@*/
1221 /* ===============================================
1222 * Compute file disposition for each package in transaction set.
1224 tei = teInitIterator(ts);
1225 while ((fi = teNextFi(tei)) != NULL) {
1226 dbiIndexSet * matches;
1229 /*@-noeffectuncon @*/ /* FIX: check rc */
1230 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_PROGRESS, (fi - ts->flList),
1231 ts->flEntries, NULL, ts->notifyData));
1232 /*@=noeffectuncon@*/
1234 if (fi->fc == 0) continue;
1236 /* Extract file info for all files in this package from the database. */
1237 matches = xcalloc(fi->fc, sizeof(*matches));
1238 if (rpmdbFindFpList(ts->rpmdb, fi->fps, matches, fi->fc)) {
1239 psm->ts = rpmtsUnlink(ts, "tsRun (rpmFindFpList fail)");
1240 return 1; /* XXX WTFO? */
1244 for (i = 0; i < fi->fc; i++)
1245 numShared += dbiIndexSetCount(matches[i]);
1247 /* Build sorted file info list for this package. */
1248 shared = sharedList = xcalloc((numShared + 1), sizeof(*sharedList));
1249 for (i = 0; i < fi->fc; i++) {
1251 * Take care not to mark files as replaced in packages that will
1252 * have been removed before we will get here.
1254 for (j = 0; j < dbiIndexSetCount(matches[i]); j++) {
1256 ro = dbiIndexRecordOffset(matches[i], j);
1258 for (k = 0; ro != knownBad && k < ts->orderCount; k++) {
1259 switch (ts->order[k].type) {
1261 if (ts->order[k].u.removed.dboffset == ro)
1263 /*@switchbreak@*/ break;
1265 /*@switchbreak@*/ break;
1269 shared->pkgFileNum = i;
1270 shared->otherPkg = dbiIndexRecordOffset(matches[i], j);
1271 shared->otherFileNum = dbiIndexRecordFileNumber(matches[i], j);
1272 shared->isRemoved = (knownBad == ro);
1275 matches[i] = dbiFreeIndexSet(matches[i]);
1277 numShared = shared - sharedList;
1278 shared->otherPkg = -1;
1279 matches = _free(matches);
1281 /* Sort file info by other package index (otherPkg) */
1282 qsort(sharedList, numShared, sizeof(*shared), sharedCmp);
1284 /* For all files from this package that are in the database ... */
1285 for (i = 0; i < numShared; i = nexti) {
1288 shared = sharedList + i;
1290 /* Find the end of the files in the other package. */
1291 for (nexti = i + 1; nexti < numShared; nexti++) {
1292 if (sharedList[nexti].otherPkg != shared->otherPkg)
1293 /*@innerbreak@*/ break;
1296 /* Is this file from a package being removed? */
1298 for (j = 0; j < ts->numRemovedPackages; j++) {
1299 if (ts->removedPackages[j] != shared->otherPkg)
1300 /*@innercontinue@*/ continue;
1302 /*@innerbreak@*/ break;
1305 /* Determine the fate of each file. */
1308 xx = handleInstInstalledFiles(ts, fi, shared, nexti - i,
1309 !(beingRemoved || (ts->ignoreSet & RPMPROB_FILTER_REPLACEOLDFILES)));
1310 /*@switchbreak@*/ break;
1313 xx = handleRmvdInstalledFiles(ts, fi, shared, nexti - i);
1314 /*@switchbreak@*/ break;
1320 /* Update disk space needs on each partition for this package. */
1321 handleOverlappedFiles(ts, fi);
1323 /* Check added package has sufficient space on each partition used. */
1326 if (!(ts->di && fi->fc))
1327 /*@switchbreak@*/ break;
1328 for (i = 0; i < ts->filesystemCount; i++) {
1332 /* XXX Avoid FAT and other file systems that have not inodes. */
1333 if (dip->iavail <= 0)
1334 /*@innercontinue@*/ continue;
1336 if (adj_fs_blocks(dip->bneeded) > dip->bavail)
1337 rpmProblemSetAppend(ts->probs, RPMPROB_DISKSPACE,
1338 fiGetNVR(fi), fi->key,
1339 ts->filesystems[i], NULL, NULL,
1340 (adj_fs_blocks(dip->bneeded) - dip->bavail) * dip->bsize);
1342 if (adj_fs_blocks(dip->ineeded) > dip->iavail)
1343 rpmProblemSetAppend(ts->probs, RPMPROB_DISKNODES,
1344 fiGetNVR(fi), fi->key,
1345 ts->filesystems[i], NULL, NULL,
1346 (adj_fs_blocks(dip->ineeded) - dip->iavail));
1348 /*@switchbreak@*/ break;
1350 /*@switchbreak@*/ break;
1353 tei = teFreeIterator(tei);
1355 if (ts->chrootDone) {
1356 /*@-superuser -noeffect @*/
1358 /*@=superuser =noeffect @*/
1360 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
1362 chroot_prefix = NULL;
1364 xx = chdir(ts->currDir);
1367 /*@-noeffectuncon @*/ /* FIX: check rc */
1368 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_STOP, 6, ts->flEntries,
1369 NULL, ts->notifyData));
1370 /*@=noeffectuncon @*/
1372 /* ===============================================
1373 * Free unused memory as soon as possible.
1376 tei = teInitIterator(ts);
1377 while ((fi = teNextFi(tei)) != NULL) {
1380 fi->fps = _free(fi->fps);
1382 tei = teFreeIterator(tei);
1388 /* ===============================================
1389 * If unfiltered problems exist, free memory and return.
1391 if ((ts->transFlags & RPMTRANS_FLAG_BUILD_PROBS)
1392 || (ts->probs->numProblems &&
1393 (okProbs != NULL || rpmProblemSetTrim(ts->probs, okProbs)))
1396 *newProbs = ts->probs;
1398 ts->flList = freeFl(ts, ts->flList);
1400 if (psm->ts != NULL)
1401 psm->ts = rpmtsUnlink(psm->ts, "tsRun (problems)");
1402 /*@-nullstate@*/ /* FIX: ts->flList may be NULL */
1403 return ts->orderCount;
1407 /* ===============================================
1408 * Save removed files before erasing.
1410 if (ts->transFlags & (RPMTRANS_FLAG_DIRSTASH | RPMTRANS_FLAG_REPACKAGE)) {
1411 tei = teInitIterator(ts);
1412 while ((fi = teNextFi(tei)) != NULL) {
1415 /*@switchbreak@*/ break;
1417 if (ts->transFlags & RPMTRANS_FLAG_REPACKAGE) {
1418 psm->fi = rpmfiLink(fi, "tsRepackage");
1419 xx = psmStage(psm, PSM_PKGSAVE);
1420 (void) rpmfiUnlink(fi, "tsRepackage");
1423 /*@switchbreak@*/ break;
1426 tei = teFreeIterator(tei);
1429 /* ===============================================
1430 * Install and remove packages.
1433 lastKey = (alKey)-2; /* erased packages have -1 */
1434 tei = teInitIterator(ts);
1435 /*@-branchstate@*/ /* FIX: fi reload needs work */
1436 while ((fi = teNextFi(tei)) != NULL) {
1442 psm->fi = rpmfiLink(fi, "tsInstall");
1446 pkgKey = ts->order[oc].u.addedKey;
1448 rpmMessage(RPMMESS_DEBUG, "========== +++ %s-%s-%s\n",
1449 fi->name, fi->version, fi->release);
1450 h = (fi->h ? headerLink(fi->h, "TR_ADDED install") : NULL);
1452 if (fi->fd == NULL) {
1453 /*@-noeffectuncon @*/ /* FIX: ??? */
1454 fi->fd = ts->notify(fi->h, RPMCALLBACK_INST_OPEN_FILE, 0, 0,
1455 fi->key, ts->notifyData);
1456 /*@=noeffectuncon @*/
1457 if (fi->fd != NULL) {
1460 h = headerFree(h, "TR_ADDED install");
1462 /*@-mustmod@*/ /* LCL: segfault */
1463 rpmrc = rpmReadPackageFile(ts, fi->fd,
1464 "rpmRunTransactions", &h);
1467 if (!(rpmrc == RPMRC_OK || rpmrc == RPMRC_BADSIZE)) {
1468 /*@-noeffectuncon @*/ /* FIX: check rc */
1469 (void) ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE,
1471 fi->key, ts->notifyData);
1472 /*@=noeffectuncon @*/
1475 } else if (fi->h != NULL) {
1476 Header foo = relocateFileList(ts, fi, h, NULL);
1477 h = headerFree(h, "TR_ADDED read free");
1478 h = headerLink(foo, "TR_ADDED relocate xfer");
1479 foo = headerFree(foo, "TR_ADDED relocate");
1481 if (fi->fd != NULL) gotfd = 1;
1486 if (fi->fd != NULL) {
1487 Header hsave = NULL;
1490 hsave = headerLink(fi->h, "TR_ADDED fi->h hsave");
1491 fi->h = headerFree(fi->h, "TR_ADDED fi->h free");
1492 fi->h = headerLink(h, "TR_ADDED fi->h link");
1494 char * fstates = fi->fstates;
1495 fileAction * actions = fi->actions;
1496 uint_32 multiLib = fi->multiLib;
1497 const void * key = fi->key;
1498 rpmRelocation * relocs = fi->relocs;
1508 fi->magic = TFIMAGIC;
1509 fi->type = ts->order[oc].type;
1511 loadFi(ts, fi, h, 1);
1512 fi->fstates = _free(fi->fstates);
1513 fi->fstates = fstates;
1514 fi->actions = _free(fi->actions);
1515 fi->actions = actions;
1516 fi->multiLib = multiLib;
1518 fi->relocs = relocs;
1520 /*@i@*/ fi->fd = fd;
1525 ts->transFlags |= RPMTRANS_FLAG_MULTILIB;
1527 if (psmStage(psm, PSM_PKGINSTALL)) {
1531 fi->h = headerFree(fi->h, "TR_ADDED fi->h free");
1533 fi->h = headerLink(hsave, "TR_ADDED fi->h restore");
1534 hsave = headerFree(hsave, "TR_ADDED hsave free");
1541 h = headerFree(h, "TR_ADDED h free");
1544 /*@-noeffectuncon @*/ /* FIX: check rc */
1545 (void)ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE, 0, 0,
1546 fi->key, ts->notifyData);
1547 /*@=noeffectuncon @*/
1551 /*@switchbreak@*/ break;
1553 rpmMessage(RPMMESS_DEBUG, "========== --- %s-%s-%s\n",
1554 fi->name, fi->version, fi->release);
1556 /* If install failed, then we shouldn't erase. */
1557 if (ts->order[oc].u.removed.dependsOnKey != lastKey) {
1558 if (psmStage(psm, PSM_PKGERASE))
1562 /*@switchbreak@*/ break;
1564 xx = rpmdbSync(ts->rpmdb);
1565 (void) rpmfiUnlink(fi, "tsInstall");
1569 tei = teFreeIterator(tei);
1571 ts->flList = freeFl(ts, ts->flList);
1574 psm->ts = rpmtsUnlink(psm->ts, "tsRun");
1576 /*@-nullstate@*/ /* FIX: ts->flList may be NULL */