2 * \file lib/transaction.c
7 #include <rpmmacro.h> /* XXX for rpmExpand */
10 #define _NEED_TEITERATOR 1
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 @*/
65 /*@access teIterator @*/
66 /*@access transactionElement @*/
67 /*@access rpmTransactionSet @*/
71 struct diskspaceInfo {
72 dev_t dev; /*!< File system device number. */
73 signed long bneeded; /*!< No. of blocks needed. */
74 signed long ineeded; /*!< No. of inodes needed. */
75 int bsize; /*!< File system block size. */
76 signed long bavail; /*!< No. of blocks available. */
77 signed long iavail; /*!< No. of inodes available. */
81 * Adjust for root only reserved space. On linux e2fs, this is 5%.
83 #define adj_fs_blocks(_nb) (((_nb) * 21) / 20)
85 /* argon thought a shift optimization here was a waste of time... he's
87 #define BLOCK_ROUND(size, block) (((size) + (block) - 1) / (block))
89 void rpmtransSetScriptFd(rpmTransactionSet ts, FD_t fd)
91 ts->scriptFd = (fd ? fdLink(fd, "rpmtransSetScriptFd") : NULL);
94 int rpmtransGetKeys(const rpmTransactionSet ts, fnpyKey ** ep, int * nep)
98 if (nep) *nep = ts->orderCount;
100 teIterator pi; transactionElement p;
103 *ep = e = xmalloc(ts->orderCount * sizeof(*e));
104 pi = teInitIterator(ts);
105 while ((p = teNextIterator(pi)) != NULL) {
108 /*@-dependenttrans@*/
110 /*@=dependenttrans@*/
111 /*@switchbreak@*/ break;
115 /*@switchbreak@*/ break;
119 pi = teFreeIterator(pi);
126 static int archOkay(/*@null@*/ const char * pkgArch)
129 if (pkgArch == NULL) return 0;
130 return (rpmMachineScore(RPM_MACHTABLE_INSTARCH, pkgArch) ? 1 : 0);
135 static int osOkay(/*@null@*/ const char * pkgOs)
138 if (pkgOs == NULL) return 0;
139 return (rpmMachineScore(RPM_MACHTABLE_INSTOS, pkgOs) ? 1 : 0);
144 static int sharedCmp(const void * one, const void * two)
147 sharedFileInfo a = (sharedFileInfo) one;
148 sharedFileInfo b = (sharedFileInfo) two;
150 if (a->otherPkg < b->otherPkg)
152 else if (a->otherPkg > b->otherPkg)
160 static fileAction decideFileFate(const char * fn,
162 const char * dbMd5, const char * dbLink, short newMode,
163 const char * newMd5, const char * newLink, int newFlags,
164 rpmtransFlags transFlags)
165 /*@globals fileSystem @*/
166 /*@modifies fileSystem @*/
169 const char * dbAttr, * newAttr;
170 fileTypes dbWhat, newWhat, diskWhat;
173 int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
175 if (lstat(fn, &sb)) {
177 * The file doesn't exist on the disk. Create it unless the new
178 * package has marked it as missingok, or allfiles is requested.
180 if (!(transFlags & RPMTRANS_FLAG_ALLFILES) &&
181 (newFlags & RPMFILE_MISSINGOK)) {
182 rpmMessage(RPMMESS_DEBUG, _("%s skipped due to missingok flag\n"),
190 diskWhat = whatis(sb.st_mode);
191 dbWhat = whatis(dbMode);
192 newWhat = whatis(newMode);
194 /* RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
195 them in older packages as well */
196 if (newWhat == XDIR) {
200 if (diskWhat != newWhat) {
202 } else if (newWhat != dbWhat && diskWhat != dbWhat) {
204 } else if (dbWhat != newWhat) {
206 } else if (dbWhat != LINK && dbWhat != REG) {
211 rc = mdfile(fn, buffer);
214 /* assume the file has been removed, don't freak */
219 } else /* dbWhat == LINK */ {
220 memset(buffer, 0, sizeof(buffer));
221 i = readlink(fn, buffer, sizeof(buffer) - 1);
223 /* assume the file has been removed, don't freak */
230 /* this order matters - we'd prefer to CREATE the file if at all
231 possible in case something else (like the timestamp) has changed */
233 if (!strcmp(dbAttr, buffer)) {
234 /* this config file has never been modified, so just replace it */
238 if (!strcmp(dbAttr, newAttr)) {
239 /* this file is the same in all versions of this package */
244 * The config file on the disk has been modified, but
245 * the ones in the two packages are different. It would
246 * be nice if RPM was smart enough to at least try and
247 * merge the difference ala CVS, but...
254 static int filecmp(short mode1, const char * md51, const char * link1,
255 short mode2, const char * md52, const char * link2)
258 fileTypes what1 = whatis(mode1);
259 fileTypes what2 = whatis(mode2);
261 if (what1 != what2) return 1;
264 return strcmp(link1, link2);
265 else if (what1 == REG)
266 return strcmp(md51, md52);
273 /* XXX only ts->{probs,rpmdb} modified */
274 static int handleInstInstalledFiles(const rpmTransactionSet ts,
275 transactionElement p, TFI_t fi,
276 sharedFileInfo shared,
277 int sharedCount, int reportConflicts)
278 /*@globals fileSystem @*/
279 /*@modifies ts, fi, fileSystem @*/
282 HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
283 rpmtransFlags transFlags = ts->transFlags;
284 rpmTagType oltype, omtype;
287 const char ** otherMd5s;
288 const char ** otherLinks;
289 const char * otherStates;
290 uint_32 * otherFlags;
291 uint_32 * otherSizes;
292 uint_16 * otherModes;
296 rpmdbMatchIterator mi;
298 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
299 &shared->otherPkg, sizeof(shared->otherPkg));
300 h = rpmdbNextIterator(mi);
302 mi = rpmdbFreeIterator(mi);
306 xx = hge(h, RPMTAG_FILEMD5S, &omtype, (void **) &otherMd5s, NULL);
307 xx = hge(h, RPMTAG_FILELINKTOS, &oltype, (void **) &otherLinks, NULL);
308 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
309 xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &otherModes, NULL);
310 xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &otherFlags, NULL);
311 xx = hge(h, RPMTAG_FILESIZES, NULL, (void **) &otherSizes, NULL);
313 fi->replaced = xcalloc(sharedCount, sizeof(*fi->replaced));
315 for (i = 0; i < sharedCount; i++, shared++) {
316 int otherFileNum, fileNum;
319 otherFileNum = shared->otherFileNum;
320 fileNum = shared->pkgFileNum;
322 (void) tfiSetFX(fi, fileNum);
325 /* XXX another tedious segfault, assume file state normal. */
326 if (otherStates && otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
329 if (XFA_SKIPPING(fi->actions[fileNum]))
332 if (filecmp(otherModes[otherFileNum],
333 otherMd5s[otherFileNum],
334 otherLinks[otherFileNum],
337 fi->flinks[fileNum])) {
338 if (reportConflicts) {
339 const char * altNEVR = hGetNEVR(h, NULL);
340 rpmProblemSetAppend(ts->probs, RPMPROB_FILE_CONFLICT,
342 tfiGetDN(fi), tfiGetBN(fi),
345 altNEVR = _free(altNEVR);
347 if (!(otherFlags[otherFileNum] | fi->fflags[fileNum])
349 /*@-assignexpose@*/ /* FIX: p->replaced, not fi */
350 if (!shared->isRemoved)
351 fi->replaced[numReplaced++] = *shared;
356 if ((otherFlags[otherFileNum] | fi->fflags[fileNum]) & RPMFILE_CONFIG) {
357 fi->actions[fileNum] = decideFileFate(fn,
358 otherModes[otherFileNum],
359 otherMd5s[otherFileNum],
360 otherLinks[otherFileNum],
368 fi->replacedSizes[fileNum] = otherSizes[otherFileNum];
371 otherMd5s = hfd(otherMd5s, omtype);
372 otherLinks = hfd(otherLinks, oltype);
373 mi = rpmdbFreeIterator(mi);
375 fi->replaced = xrealloc(fi->replaced, /* XXX memory leak */
376 sizeof(*fi->replaced) * (numReplaced + 1));
377 fi->replaced[numReplaced].otherPkg = 0;
384 /* XXX only ts->rpmdb modified */
385 static int handleRmvdInstalledFiles(const rpmTransactionSet ts, TFI_t fi,
386 sharedFileInfo shared, int sharedCount)
387 /*@globals fileSystem @*/
388 /*@modifies ts, fi, fileSystem @*/
392 const char * otherStates;
395 rpmdbMatchIterator mi;
397 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
398 &shared->otherPkg, sizeof(shared->otherPkg));
399 h = rpmdbNextIterator(mi);
401 mi = rpmdbFreeIterator(mi);
405 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
407 for (i = 0; i < sharedCount; i++, shared++) {
408 int otherFileNum, fileNum;
409 otherFileNum = shared->otherFileNum;
410 fileNum = shared->pkgFileNum;
412 if (otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
415 fi->actions[fileNum] = FA_SKIP;
418 mi = rpmdbFreeIterator(mi);
424 * Update disk space needs on each partition for this package.
426 /* XXX only ts->{probs,di} modified */
427 static void handleOverlappedFiles(const rpmTransactionSet ts,
428 const transactionElement p, TFI_t fi)
429 /*@globals fileSystem @*/
430 /*@modifies ts, fi, fileSystem @*/
432 struct diskspaceInfo * ds = NULL;
433 uint_32 fixupSize = 0;
439 while ((i = tfiNext(fi)) >= 0) {
440 int otherPkgNum, otherFileNum;
444 if (XFA_SKIPPING(fi->actions[i]))
451 while (ds->bsize && ds->dev != fi->fps[i].entry->dev) ds++;
452 if (!ds->bsize) ds = NULL;
457 * Retrieve all records that apply to this file. Note that the
458 * file info records were built in the same order as the packages
459 * will be installed and removed so the records for an overlapped
460 * files will be sorted in exactly the same order.
462 (void) htGetEntry(ts->ht, &fi->fps[i],
463 (const void ***) &recs, &numRecs, NULL);
466 * If this package is being added, look only at other packages
467 * being added -- removed packages dance to a different tune.
468 * If both this and the other package are being added, overlapped
469 * files must be identical (or marked as a conflict). The
470 * disposition of already installed config files leads to
471 * a small amount of extra complexity.
473 * If this package is being removed, then there are two cases that
474 * need to be worried about:
475 * If the other package is being added, then skip any overlapped files
476 * so that this package removal doesn't nuke the overlapped files
477 * that were just installed.
478 * If both this and the other package are being removed, then each
479 * file removal from preceding packages needs to be skipped so that
480 * the file removal occurs only on the last occurence of an overlapped
481 * file in the transaction set.
485 /* Locate this overlapped file in the set of added/removed packages. */
486 for (j = 0; j < numRecs && recs[j] != fi; j++)
489 /* Find what the previous disposition of this file was. */
490 otherFileNum = -1; /* keep gcc quiet */
491 for (otherPkgNum = j - 1; otherPkgNum >= 0; otherPkgNum--) {
492 /* Added packages need only look at other added packages. */
493 if (p->type == TR_ADDED && recs[otherPkgNum]->te->type != TR_ADDED)
494 /*@innercontinue@*/ continue;
496 /* TESTME: there are more efficient searches in the world... */
497 for (otherFileNum = 0;
498 otherFileNum < recs[otherPkgNum]->fc;
502 /* If the addresses are the same, so are the values. */
503 if ((fi->fps + i) == (recs[otherPkgNum]->fps + otherFileNum))
504 /*@innerbreak@*/ break;
506 /* Otherwise, compare fingerprints by value. */
507 /*@-nullpass@*/ /* LCL: looks good to me */
508 if (FP_EQUAL(fi->fps[i], recs[otherPkgNum]->fps[otherFileNum]))
509 /*@innerbreak@*/ break;
513 /* XXX is this test still necessary? */
514 if (recs[otherPkgNum]->actions[otherFileNum] != FA_UNKNOWN)
515 /*@innerbreak@*/ break;
521 if (otherPkgNum < 0) {
522 /* XXX is this test still necessary? */
523 if (fi->actions[i] != FA_UNKNOWN)
524 /*@switchbreak@*/ break;
525 if ((fi->fflags[i] & RPMFILE_CONFIG) &&
527 /* Here is a non-overlapped pre-existing config file. */
528 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
529 ? FA_ALTNAME : FA_BACKUP;
531 fi->actions[i] = FA_CREATE;
533 /*@switchbreak@*/ break;
536 /* Mark added overlapped non-identical files as a conflict. */
537 if ((ts->ignoreSet & RPMPROB_FILTER_REPLACENEWFILES)
538 && filecmp(recs[otherPkgNum]->fmodes[otherFileNum],
539 recs[otherPkgNum]->fmd5s[otherFileNum],
540 recs[otherPkgNum]->flinks[otherFileNum],
545 const char * altNEVR = recs[otherPkgNum]->te->NEVR;
546 rpmProblemSetAppend(ts->probs, RPMPROB_NEW_FILE_CONFLICT,
553 /* Try to get the disk accounting correct even if a conflict. */
554 fixupSize = recs[otherPkgNum]->fsizes[otherFileNum];
556 if ((fi->fflags[i] & RPMFILE_CONFIG) && !lstat(fn, &sb)) {
557 /* Here is an overlapped pre-existing config file. */
558 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
559 ? FA_ALTNAME : FA_SKIP;
561 fi->actions[i] = FA_CREATE;
563 } /*@switchbreak@*/ break;
565 if (otherPkgNum >= 0) {
566 /* Here is an overlapped added file we don't want to nuke. */
567 if (recs[otherPkgNum]->actions[otherFileNum] != FA_ERASE) {
568 /* On updates, don't remove files. */
569 fi->actions[i] = FA_SKIP;
570 /*@switchbreak@*/ break;
572 /* Here is an overlapped removed file: skip in previous. */
573 recs[otherPkgNum]->actions[otherFileNum] = FA_SKIP;
575 if (XFA_SKIPPING(fi->actions[i]))
576 /*@switchbreak@*/ break;
577 if (fi->fstates && fi->fstates[i] != RPMFILE_STATE_NORMAL)
578 /*@switchbreak@*/ break;
579 if (!(S_ISREG(fi->fmodes[i]) && (fi->fflags[i] & RPMFILE_CONFIG))) {
580 fi->actions[i] = FA_ERASE;
581 /*@switchbreak@*/ break;
584 /* Here is a pre-existing modified config file that needs saving. */
586 if (!mdfile(fn, mdsum) && strcmp(fi->fmd5s[i], mdsum)) {
587 fi->actions[i] = FA_BACKUP;
588 /*@switchbreak@*/ break;
591 fi->actions[i] = FA_ERASE;
592 /*@switchbreak@*/ break;
596 uint_32 s = BLOCK_ROUND(fi->fsizes[i], ds->bsize);
598 switch (fi->actions[i]) {
604 /*@switchbreak@*/ break;
607 * FIXME: If two packages share a file (same md5sum), and
608 * that file is being replaced on disk, will ds->bneeded get
609 * decremented twice? Quite probably!
613 ds->bneeded -= BLOCK_ROUND(fi->replacedSizes[i], ds->bsize);
614 /*@switchbreak@*/ break;
619 /*@switchbreak@*/ break;
622 /*@switchbreak@*/ break;
625 ds->bneeded -= BLOCK_ROUND(fixupSize, ds->bsize);
631 * Ensure that current package is newer than installed package.
632 * @param ts transaction set
633 * @param p current transaction element
634 * @param h installed header
635 * @return 0 if not newer, 1 if okay
637 static int ensureOlder(rpmTransactionSet ts,
638 const transactionElement p, const Header h)
641 int_32 reqFlags = (RPMSENSE_LESS | RPMSENSE_EQUAL);
647 if (p == NULL || h == NULL)
650 t = alloca(strlen(p->NEVR) + (p->epoch != NULL ? strlen(p->epoch) : 0) + 1);
653 if (p->epoch != NULL) t = stpcpy( stpcpy(t, p->epoch), ":");
654 if (p->version != NULL) t = stpcpy(t, p->version);
656 if (p->release != NULL) t = stpcpy(t, p->release);
658 req = dsSingle(RPMTAG_REQUIRENAME, p->name, reqEVR, reqFlags);
659 rc = headerMatchesDepFlags(h, req);
663 const char * altNEVR = hGetNEVR(h, NULL);
664 rpmProblemSetAppend(ts->probs, RPMPROB_OLDPACKAGE,
669 altNEVR = _free(altNEVR);
679 static void skipFiles(const rpmTransactionSet ts, TFI_t fi)
680 /*@globals rpmGlobalMacroContext @*/
681 /*@modifies fi, rpmGlobalMacroContext @*/
683 int noDocs = (ts->transFlags & RPMTRANS_FLAG_NODOCS);
684 char ** netsharedPaths = NULL;
685 const char ** languages;
686 const char * dn, * bn;
687 int dnlen, bnlen, ix;
694 noDocs = rpmExpandNumeric("%{_excludedocs}");
696 { const char *tmpPath = rpmExpand("%{_netsharedpath}", NULL);
698 if (tmpPath && *tmpPath != '%')
699 netsharedPaths = splitString(tmpPath, strlen(tmpPath), ':');
701 tmpPath = _free(tmpPath);
704 s = rpmExpand("%{_install_langs}", NULL);
706 if (!(s && *s != '%'))
709 languages = (const char **) splitString(s, strlen(s), ':');
715 /* Compute directory refcount, skip directory if now empty. */
716 drc = alloca(fi->dc * sizeof(*drc));
717 memset(drc, 0, fi->dc * sizeof(*drc));
718 dff = alloca(fi->dc * sizeof(*dff));
719 memset(dff, 0, fi->dc * sizeof(*dff));
721 for (i = 0; i < fi->fc; i++) {
732 /* Don't bother with skipped files */
733 if (XFA_SKIPPING(fi->actions[i])) {
739 * Skip net shared paths.
740 * Net shared paths are not relative to the current root (though
741 * they do need to take package relocations into account).
743 for (nsp = netsharedPaths; nsp && *nsp; nsp++) {
748 if (strncmp(dn, *nsp, len))
749 /*@innercontinue@*/ continue;
750 /* Only directories or complete file paths can be net shared */
751 if (!(dn[len] == '/' || dn[len] == '\0'))
752 /*@innercontinue@*/ continue;
754 if (len < (dnlen + bnlen))
755 /*@innercontinue@*/ continue;
756 if (strncmp(dn, *nsp, dnlen))
757 /*@innercontinue@*/ continue;
758 if (strncmp(bn, (*nsp) + dnlen, bnlen))
759 /*@innercontinue@*/ continue;
761 /* Only directories or complete file paths can be net shared */
762 if (!((*nsp)[len] == '/' || (*nsp)[len] == '\0'))
763 /*@innercontinue@*/ continue;
766 /*@innerbreak@*/ break;
770 drc[ix]--; dff[ix] = 1;
771 fi->actions[i] = FA_SKIPNETSHARED;
776 * Skip i18n language specific files.
778 if (fi->flangs && languages && *fi->flangs[i]) {
779 const char **lang, *l, *le;
780 for (lang = languages; *lang != NULL; lang++) {
781 if (!strcmp(*lang, "all"))
782 /*@innerbreak@*/ break;
783 for (l = fi->flangs[i]; *l != '\0'; l = le) {
784 for (le = l; *le != '\0' && *le != '|'; le++)
786 if ((le-l) > 0 && !strncmp(*lang, l, (le-l)))
787 /*@innerbreak@*/ break;
788 if (*le == '|') le++; /* skip over | */
791 /*@innerbreak@*/ break;
794 drc[ix]--; dff[ix] = 1;
795 fi->actions[i] = FA_SKIPNSTATE;
801 * Skip documentation if requested.
803 if (noDocs && (fi->fflags[i] & RPMFILE_DOC)) {
804 drc[ix]--; dff[ix] = 1;
805 fi->actions[i] = FA_SKIPNSTATE;
810 /* Skip (now empty) directories that had skipped files. */
811 for (j = 0; j < fi->dc; j++) {
813 if (drc[j]) continue; /* dir still has files. */
814 if (!dff[j]) continue; /* dir was not emptied here. */
816 /* Find parent directory and basename. */
817 dn = fi->dnl[j]; dnlen = strlen(dn) - 1;
818 bn = dn + dnlen; bnlen = 0;
819 while (bn > dn && bn[-1] != '/') {
825 /* If explicitly included in the package, skip the directory. */
826 for (i = 0; i < fi->fc; i++) {
829 if (XFA_SKIPPING(fi->actions[i]))
830 /*@innercontinue@*/ continue;
831 if (whatis(fi->fmodes[i]) != XDIR)
832 /*@innercontinue@*/ continue;
833 dir = fi->dnl[fi->dil[i]];
834 if (strlen(dir) != dnlen)
835 /*@innercontinue@*/ continue;
836 if (strncmp(dir, dn, dnlen))
837 /*@innercontinue@*/ continue;
838 if (strlen(fi->bnl[i]) != bnlen)
839 /*@innercontinue@*/ continue;
840 if (strncmp(fi->bnl[i], bn, bnlen))
841 /*@innercontinue@*/ continue;
842 rpmMessage(RPMMESS_DEBUG, _("excluding directory %s\n"), dn);
843 fi->actions[i] = FA_SKIPNSTATE;
844 /*@innerbreak@*/ break;
848 if (netsharedPaths) freeSplitString(netsharedPaths);
849 #ifdef DYING /* XXX freeFi will deal with this later. */
850 fi->flangs = _free(fi->flangs);
852 if (languages) freeSplitString((char **)languages);
856 * Return transaction element's file info.
857 * @todo Take a TFI_t refcount here.
858 * @param tei transaction element iterator
859 * @return transaction element file info
862 TFI_t teGetFi(const teIterator tei)
867 if (tei != NULL && tei->ocsave != -1) {
868 transactionElement te = tei->ts->order + tei->ocsave;
869 if ((fi = te->fi) != NULL)
872 /*@-compdef -refcounttrans -usereleased @*/
874 /*@=compdef =refcounttrans =usereleased @*/
877 #define NOTIFY(_ts, _al) if ((_ts)->notify) (void) (_ts)->notify _al
879 int rpmRunTransactions( rpmTransactionSet ts,
880 rpmCallbackFunction notify, rpmCallbackData notifyData,
881 rpmProblemSet okProbs, rpmProblemSet * newProbs,
882 rpmtransFlags transFlags, rpmprobFilterFlags ignoreSet)
886 int totalFileCount = 0;
888 struct diskspaceInfo * dip;
889 sharedFileInfo shared, sharedList;
893 fingerPrintCache fpc;
894 PSM_t psm = memset(alloca(sizeof(*psm)), 0, sizeof(*psm));
895 teIterator pi; transactionElement p;
896 teIterator qi; transactionElement q;
899 /* FIXME: what if the same package is included in ts twice? */
901 ts->transFlags = transFlags;
902 if (ts->transFlags & RPMTRANS_FLAG_NOSCRIPTS)
903 ts->transFlags |= (_noTransScripts | _noTransTriggers);
904 if (ts->transFlags & RPMTRANS_FLAG_NOTRIGGERS)
905 ts->transFlags |= _noTransTriggers;
907 /* XXX MULTILIB is broken, as packages can and do execute /sbin/ldconfig. */
908 if (ts->transFlags & (RPMTRANS_FLAG_JUSTDB | RPMTRANS_FLAG_MULTILIB))
909 ts->transFlags |= (_noTransScripts | _noTransTriggers);
912 ts->notifyData = notifyData;
913 ts->probs = rpmProblemSetFree(ts->probs);
914 ts->probs = rpmProblemSetCreate();
915 *newProbs = rpmpsLink(ts->probs, "RunTransactions");
916 ts->ignoreSet = ignoreSet;
917 ts->currDir = _free(ts->currDir);
918 ts->currDir = currentDirectory();
920 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
921 ts->id = (int_32) time(NULL);
923 memset(psm, 0, sizeof(*psm));
924 psm->ts = rpmtsLink(ts, "tsRun");
926 /* Get available space on mounted file systems. */
927 if (!(ts->ignoreSet & RPMPROB_FILTER_DISKSPACE) &&
928 !rpmGetFilesystemList(&ts->filesystems, &ts->filesystemCount))
932 rpmMessage(RPMMESS_DEBUG, _("getting list of mounted filesystems\n"));
934 ts->di = _free(ts->di);
935 dip = ts->di = xcalloc((ts->filesystemCount + 1), sizeof(*ts->di));
937 for (i = 0; (i < ts->filesystemCount) && dip; i++) {
938 #if STATFS_IN_SYS_STATVFS
940 memset(&sfb, 0, sizeof(sfb));
941 if (statvfs(ts->filesystems[i], &sfb))
945 /* This platform has the 4-argument version of the statfs call. The last two
946 * should be the size of struct statfs and 0, respectively. The 0 is the
947 * filesystem type, and is always 0 when statfs is called on a mounted
948 * filesystem, as we're doing.
950 memset(&sfb, 0, sizeof(sfb));
951 if (statfs(ts->filesystems[i], &sfb, sizeof(sfb), 0))
953 memset(&sfb, 0, sizeof(sfb));
954 if (statfs(ts->filesystems[i], &sfb))
960 ts->di[i].bsize = sfb.f_bsize;
961 ts->di[i].bneeded = 0;
962 ts->di[i].ineeded = 0;
963 #ifdef STATFS_HAS_F_BAVAIL
964 ts->di[i].bavail = sfb.f_bavail;
966 /* FIXME: the statfs struct doesn't have a member to tell how many blocks are
967 * available for non-superusers. f_blocks - f_bfree is probably too big, but
968 * it's about all we can do.
970 ts->di[i].bavail = sfb.f_blocks - sfb.f_bfree;
972 /* XXX Avoid FAT and other file systems that have not inodes. */
973 ts->di[i].iavail = !(sfb.f_ffree == 0 && sfb.f_files == 0)
976 xx = stat(ts->filesystems[i], &sb);
977 ts->di[i].dev = sb.st_dev;
981 if (dip) ts->di[i].bsize = 0;
984 /* ===============================================
985 * For packages being installed:
986 * - verify package arch/os.
987 * - verify package epoch:version-release is newer.
989 * For packages being removed:
992 /* The ordering doesn't matter here */
993 pi = teInitIterator(ts);
994 while ((p = teNext(pi, TR_ADDED)) != NULL) {
995 rpmdbMatchIterator mi;
997 if (!(ts->ignoreSet & RPMPROB_FILTER_IGNOREARCH))
998 if (!archOkay(p->arch))
999 rpmProblemSetAppend(ts->probs, RPMPROB_BADARCH,
1004 if (!(ts->ignoreSet & RPMPROB_FILTER_IGNOREOS))
1006 rpmProblemSetAppend(ts->probs, RPMPROB_BADOS,
1011 if (!(ts->ignoreSet & RPMPROB_FILTER_OLDPACKAGE)) {
1013 mi = rpmtsInitIterator(ts, RPMTAG_NAME, p->name, 0);
1014 while ((h = rpmdbNextIterator(mi)) != NULL)
1015 xx = ensureOlder(ts, p, h);
1016 mi = rpmdbFreeIterator(mi);
1019 /* XXX multilib should not display "already installed" problems */
1020 if (!(ts->ignoreSet & RPMPROB_FILTER_REPLACEPKG) && !p->multiLib) {
1021 mi = rpmtsInitIterator(ts, RPMTAG_NAME, p->name, 0);
1022 xx = rpmdbSetIteratorRE(mi, RPMTAG_VERSION, RPMMIRE_DEFAULT,
1024 xx = rpmdbSetIteratorRE(mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT,
1027 while (rpmdbNextIterator(mi) != NULL) {
1028 rpmProblemSetAppend(ts->probs, RPMPROB_PKG_INSTALLED,
1032 /*@innerbreak@*/ break;
1034 mi = rpmdbFreeIterator(mi);
1037 /* Count no. of files (if any). */
1039 totalFileCount += p->fi->fc;
1042 pi = teFreeIterator(pi);
1044 /* The ordering doesn't matter here */
1045 pi = teInitIterator(ts);
1046 while ((p = teNext(pi, TR_REMOVED)) != NULL) {
1050 if (fi->bnl == NULL)
1051 continue; /* XXX can't happen */
1052 if (fi->dnl == NULL)
1053 continue; /* XXX can't happen */
1054 if (fi->dil == NULL)
1055 continue; /* XXX can't happen */
1056 totalFileCount += fi->fc;
1058 pi = teFreeIterator(pi);
1060 /* ===============================================
1061 * Initialize transaction element file info for package:
1065 * FIXME?: we'd be better off assembling one very large file list and
1066 * calling fpLookupList only once. I'm not sure that the speedup is
1067 * worth the trouble though.
1069 pi = teInitIterator(ts);
1070 while ((p = teNextIterator(pi)) != NULL) {
1072 if ((fi = teGetFi(pi)) == NULL)
1073 continue; /* XXX can't happen */
1075 #ifdef DYING /* XXX W2DO? this is now done teGetFi, okay ??? */
1076 fi->magic = TFIMAGIC;
1084 /* Skip netshared paths, not our i18n files, and excluded docs */
1087 /*@switchbreak@*/ break;
1089 fi->record = p->u.removed.dboffset;
1090 /*@switchbreak@*/ break;
1094 fi->fps = (fi->fc > 0 ? xmalloc(fi->fc * sizeof(*fi->fps)) : NULL);
1096 pi = teFreeIterator(pi);
1098 if (!ts->chrootDone) {
1100 /*@-superuser -noeffect @*/
1101 xx = chroot(ts->rootDir);
1102 /*@=superuser =noeffect @*/
1104 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 1;
1107 chroot_prefix = ts->rootDir;
1112 ts->ht = htCreate(totalFileCount * 2, 0, 0, fpHashFunction, fpEqual);
1113 fpc = fpCacheCreate(totalFileCount);
1115 /* ===============================================
1116 * Add fingerprint for each file not skipped.
1118 pi = teInitIterator(ts);
1119 while ((p = teNextIterator(pi)) != NULL) {
1121 if ((fi = teGetFi(pi)) == NULL)
1122 continue; /* XXX can't happen */
1124 fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fi->fc, fi->fps);
1126 for (i = 0; i < fi->fc; i++) {
1127 if (XFA_SKIPPING(fi->actions[i]))
1128 /*@innercontinue@*/ continue;
1129 /*@-dependenttrans@*/
1130 htAddEntry(ts->ht, fi->fps + i, fi);
1131 /*@=dependenttrans@*/
1135 pi = teFreeIterator(pi);
1137 /*@-noeffectuncon @*/ /* FIX: check rc */
1138 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_START, 6, ts->orderCount,
1139 NULL, ts->notifyData));
1140 /*@=noeffectuncon@*/
1142 /* ===============================================
1143 * Compute file disposition for each package in transaction set.
1145 pi = teInitIterator(ts);
1146 while ((p = teNextIterator(pi)) != NULL) {
1147 dbiIndexSet * matches;
1150 if ((fi = teGetFi(pi)) == NULL)
1151 continue; /* XXX can't happen */
1153 /*@-noeffectuncon @*/ /* FIX: check rc */
1154 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_PROGRESS, teGetOc(pi),
1155 ts->orderCount, NULL, ts->notifyData));
1156 /*@=noeffectuncon@*/
1158 if (fi->fc == 0) continue;
1160 /* Extract file info for all files in this package from the database. */
1161 matches = xcalloc(fi->fc, sizeof(*matches));
1162 if (rpmdbFindFpList(ts->rpmdb, fi->fps, matches, fi->fc)) {
1163 psm->ts = rpmtsUnlink(ts, "tsRun (rpmFindFpList fail)");
1164 return 1; /* XXX WTFO? */
1168 for (i = 0; i < fi->fc; i++)
1169 numShared += dbiIndexSetCount(matches[i]);
1171 /* Build sorted file info list for this package. */
1172 shared = sharedList = xcalloc((numShared + 1), sizeof(*sharedList));
1173 for (i = 0; i < fi->fc; i++) {
1175 * Take care not to mark files as replaced in packages that will
1176 * have been removed before we will get here.
1178 for (j = 0; j < dbiIndexSetCount(matches[i]); j++) {
1180 ro = dbiIndexRecordOffset(matches[i], j);
1182 qi = teInitIterator(ts);
1183 while ((q = teNext(qi, TR_REMOVED)) != NULL) {
1185 /*@innerbreak@*/ break;
1186 if (q->u.removed.dboffset == ro)
1189 qi = teFreeIterator(qi);
1191 shared->pkgFileNum = i;
1192 shared->otherPkg = dbiIndexRecordOffset(matches[i], j);
1193 shared->otherFileNum = dbiIndexRecordFileNumber(matches[i], j);
1194 shared->isRemoved = (knownBad == ro);
1197 matches[i] = dbiFreeIndexSet(matches[i]);
1199 numShared = shared - sharedList;
1200 shared->otherPkg = -1;
1201 matches = _free(matches);
1203 /* Sort file info by other package index (otherPkg) */
1204 qsort(sharedList, numShared, sizeof(*shared), sharedCmp);
1206 /* For all files from this package that are in the database ... */
1207 for (i = 0; i < numShared; i = nexti) {
1210 shared = sharedList + i;
1212 /* Find the end of the files in the other package. */
1213 for (nexti = i + 1; nexti < numShared; nexti++) {
1214 if (sharedList[nexti].otherPkg != shared->otherPkg)
1215 /*@innerbreak@*/ break;
1218 /* Is this file from a package being removed? */
1220 if (ts->removedPackages != NULL)
1221 for (j = 0; j < ts->numRemovedPackages; j++) {
1222 if (ts->removedPackages[j] != shared->otherPkg)
1223 /*@innercontinue@*/ continue;
1225 /*@innerbreak@*/ break;
1228 /* Determine the fate of each file. */
1231 xx = handleInstInstalledFiles(ts, p, fi, shared, nexti - i,
1232 !(beingRemoved || (ts->ignoreSet & RPMPROB_FILTER_REPLACEOLDFILES)));
1233 /*@switchbreak@*/ break;
1236 xx = handleRmvdInstalledFiles(ts, fi, shared, nexti - i);
1237 /*@switchbreak@*/ break;
1243 /* Update disk space needs on each partition for this package. */
1244 handleOverlappedFiles(ts, p, fi);
1246 /* Check added package has sufficient space on each partition used. */
1249 if (!(ts->di && fi->fc))
1250 /*@switchbreak@*/ break;
1251 for (i = 0; i < ts->filesystemCount; i++) {
1255 /* XXX Avoid FAT and other file systems that have not inodes. */
1256 if (dip->iavail <= 0)
1257 /*@innercontinue@*/ continue;
1259 if (adj_fs_blocks(dip->bneeded) > dip->bavail) {
1260 rpmProblemSetAppend(ts->probs, RPMPROB_DISKSPACE,
1262 ts->filesystems[i], NULL, NULL,
1263 (adj_fs_blocks(dip->bneeded) - dip->bavail) * dip->bsize);
1266 if (adj_fs_blocks(dip->ineeded) > dip->iavail) {
1267 rpmProblemSetAppend(ts->probs, RPMPROB_DISKNODES,
1269 ts->filesystems[i], NULL, NULL,
1270 (adj_fs_blocks(dip->ineeded) - dip->iavail));
1273 /*@switchbreak@*/ break;
1275 /*@switchbreak@*/ break;
1278 pi = teFreeIterator(pi);
1280 if (ts->chrootDone) {
1281 /*@-superuser -noeffect @*/
1283 /*@=superuser =noeffect @*/
1285 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
1287 chroot_prefix = NULL;
1289 xx = chdir(ts->currDir);
1292 /*@-noeffectuncon @*/ /* FIX: check rc */
1293 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_STOP, 6, ts->orderCount,
1294 NULL, ts->notifyData));
1295 /*@=noeffectuncon @*/
1297 /* ===============================================
1298 * Free unused memory as soon as possible.
1300 pi = teInitIterator(ts);
1301 while ((p = teNextIterator(pi)) != NULL) {
1302 if ((fi = teGetFi(pi)) == NULL)
1303 continue; /* XXX can't happen */
1306 fi->fps = _free(fi->fps);
1308 pi = teFreeIterator(pi);
1314 /* ===============================================
1315 * If unfiltered problems exist, free memory and return.
1317 if ((ts->transFlags & RPMTRANS_FLAG_BUILD_PROBS)
1318 || (ts->probs->numProblems &&
1319 (okProbs != NULL || rpmProblemSetTrim(ts->probs, okProbs)))
1322 if (psm->ts != NULL)
1323 psm->ts = rpmtsUnlink(psm->ts, "tsRun (problems)");
1324 return ts->orderCount;
1327 /* ===============================================
1328 * Save removed files before erasing.
1330 if (ts->transFlags & (RPMTRANS_FLAG_DIRSTASH | RPMTRANS_FLAG_REPACKAGE)) {
1331 pi = teInitIterator(ts);
1332 while ((p = teNextIterator(pi)) != NULL) {
1336 /*@switchbreak@*/ break;
1338 if (!(ts->transFlags & RPMTRANS_FLAG_REPACKAGE))
1339 /*@switchbreak@*/ break;
1341 psm->fi = rpmfiLink(fi, "tsRepackage");
1342 xx = psmStage(psm, PSM_PKGSAVE);
1343 (void) rpmfiUnlink(fi, "tsRepackage");
1346 /*@switchbreak@*/ break;
1349 pi = teFreeIterator(pi);
1352 /* ===============================================
1353 * Install and remove packages.
1355 lastKey = (alKey)-2; /* erased packages have -1 */
1356 pi = teInitIterator(ts);
1357 /*@-branchstate@*/ /* FIX: fi reload needs work */
1358 while ((p = teNextIterator(pi)) != NULL) {
1364 if ((fi = teGetFi(pi)) == NULL)
1365 continue; /* XXX can't happen */
1368 psm->fi = rpmfiLink(fi, "tsInstall");
1372 pkgKey = p->u.addedKey;
1374 rpmMessage(RPMMESS_DEBUG, "========== +++ %s\n", p->NEVR);
1377 p->fd = ts->notify(fi->h, RPMCALLBACK_INST_OPEN_FILE, 0, 0,
1378 p->key, ts->notifyData);
1379 if (p->fd != NULL) {
1382 /*@=mustmod@*/ /* LCL: segfault */
1383 rpmrc = rpmReadPackageFile(ts, p->fd,
1384 "rpmRunTransactions", &h);
1387 if (!(rpmrc == RPMRC_OK || rpmrc == RPMRC_BADSIZE)) {
1388 p->fd = ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE,
1390 p->key, ts->notifyData);
1394 if (p->fd != NULL) gotfd = 1;
1400 if (p->fd != NULL) {
1402 char * fstates = fi->fstates;
1403 fileAction * actions = fi->actions;
1407 (void) fiFree(fi, 0);
1409 fi->magic = TFIMAGIC;
1412 (void) fiNew(ts, fi, h, RPMTAG_BASENAMES, 1);
1413 fi->fstates = _free(fi->fstates);
1414 fi->fstates = fstates;
1415 fi->actions = _free(fi->actions);
1416 fi->actions = actions;
1421 ts->transFlags |= RPMTRANS_FLAG_MULTILIB;
1423 if (psmStage(psm, PSM_PKGINSTALL)) {
1427 fi->h = headerFree(fi->h, "TR_ADDED fi->h free");
1434 h = headerFree(h, "TR_ADDED h free");
1437 /*@-noeffectuncon @*/ /* FIX: check rc */
1438 (void)ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE, 0, 0,
1439 p->key, ts->notifyData);
1440 /*@=noeffectuncon @*/
1443 (void) fiFree(fi, 0);
1444 /*@switchbreak@*/ break;
1446 rpmMessage(RPMMESS_DEBUG, "========== --- %s\n", p->NEVR);
1447 /* If install failed, then we shouldn't erase. */
1448 if (p->u.removed.dependsOnKey != lastKey) {
1449 if (psmStage(psm, PSM_PKGERASE))
1452 (void) fiFree(fi, 0);
1453 /*@switchbreak@*/ break;
1455 xx = rpmdbSync(ts->rpmdb);
1456 (void) rpmfiUnlink(psm->fi, "tsInstall");
1461 pi = teFreeIterator(pi);
1463 psm->ts = rpmtsUnlink(psm->ts, "tsRun");
1465 /*@-nullstate@*/ /* FIX: ts->flList may be NULL */