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 * dirName,
161 const char * baseName, short dbMode,
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;
174 char * filespec = alloca(strlen(dirName) + strlen(baseName) + 1);
176 (void) stpcpy( stpcpy(filespec, dirName), baseName);
178 if (lstat(filespec, &sb)) {
180 * The file doesn't exist on the disk. Create it unless the new
181 * package has marked it as missingok, or allfiles is requested.
183 if (!(transFlags & RPMTRANS_FLAG_ALLFILES) &&
184 (newFlags & RPMFILE_MISSINGOK)) {
185 rpmMessage(RPMMESS_DEBUG, _("%s skipped due to missingok flag\n"),
193 diskWhat = whatis(sb.st_mode);
194 dbWhat = whatis(dbMode);
195 newWhat = whatis(newMode);
197 /* RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
198 them in older packages as well */
199 if (newWhat == XDIR) {
203 if (diskWhat != newWhat) {
205 } else if (newWhat != dbWhat && diskWhat != dbWhat) {
207 } else if (dbWhat != newWhat) {
209 } else if (dbWhat != LINK && dbWhat != REG) {
214 rc = mdfile(filespec, buffer);
217 /* assume the file has been removed, don't freak */
222 } else /* dbWhat == LINK */ {
223 memset(buffer, 0, sizeof(buffer));
224 i = readlink(filespec, buffer, sizeof(buffer) - 1);
226 /* assume the file has been removed, don't freak */
233 /* this order matters - we'd prefer to CREATE the file if at all
234 possible in case something else (like the timestamp) has changed */
236 if (!strcmp(dbAttr, buffer)) {
237 /* this config file has never been modified, so just replace it */
241 if (!strcmp(dbAttr, newAttr)) {
242 /* this file is the same in all versions of this package */
247 * The config file on the disk has been modified, but
248 * the ones in the two packages are different. It would
249 * be nice if RPM was smart enough to at least try and
250 * merge the difference ala CVS, but...
257 static int filecmp(short mode1, const char * md51, const char * link1,
258 short mode2, const char * md52, const char * link2)
261 fileTypes what1 = whatis(mode1);
262 fileTypes what2 = whatis(mode2);
264 if (what1 != what2) return 1;
267 return strcmp(link1, link2);
268 else if (what1 == REG)
269 return strcmp(md51, md52);
276 /* XXX only ts->{probs,rpmdb} modified */
277 static int handleInstInstalledFiles(const rpmTransactionSet ts,
278 transactionElement p, TFI_t fi,
279 sharedFileInfo shared,
280 int sharedCount, int reportConflicts)
281 /*@globals fileSystem @*/
282 /*@modifies ts, fi, fileSystem @*/
285 HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
286 rpmtransFlags transFlags = ts->transFlags;
287 rpmTagType oltype, omtype;
290 const char ** otherMd5s;
291 const char ** otherLinks;
292 const char * otherStates;
293 uint_32 * otherFlags;
294 uint_32 * otherSizes;
295 uint_16 * otherModes;
299 rpmdbMatchIterator mi;
301 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
302 &shared->otherPkg, sizeof(shared->otherPkg));
303 h = rpmdbNextIterator(mi);
305 mi = rpmdbFreeIterator(mi);
309 xx = hge(h, RPMTAG_FILEMD5S, &omtype, (void **) &otherMd5s, NULL);
310 xx = hge(h, RPMTAG_FILELINKTOS, &oltype, (void **) &otherLinks, NULL);
311 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
312 xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &otherModes, NULL);
313 xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &otherFlags, NULL);
314 xx = hge(h, RPMTAG_FILESIZES, NULL, (void **) &otherSizes, NULL);
316 fi->replaced = xmalloc(sharedCount * sizeof(*fi->replaced));
318 for (i = 0; i < sharedCount; i++, shared++) {
319 int otherFileNum, fileNum;
320 otherFileNum = shared->otherFileNum;
321 fileNum = shared->pkgFileNum;
323 /* XXX another tedious segfault, assume file state normal. */
324 if (otherStates && otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
327 if (XFA_SKIPPING(fi->actions[fileNum]))
330 if (filecmp(otherModes[otherFileNum],
331 otherMd5s[otherFileNum],
332 otherLinks[otherFileNum],
335 fi->flinks[fileNum])) {
336 if (reportConflicts) {
337 const char * altNEVR = hGetNEVR(h, NULL);
338 rpmProblemSetAppend(ts->probs, RPMPROB_FILE_CONFLICT,
340 fi->dnl[fi->dil[fileNum]], fi->bnl[fileNum],
343 altNEVR = _free(altNEVR);
345 if (!(otherFlags[otherFileNum] | fi->fflags[fileNum])
347 /*@-assignexpose@*/ /* FIX: p->replaced, not fi */
348 if (!shared->isRemoved)
349 fi->replaced[numReplaced++] = *shared;
354 if ((otherFlags[otherFileNum] | fi->fflags[fileNum]) & RPMFILE_CONFIG) {
355 fi->actions[fileNum] = decideFileFate(
356 fi->dnl[fi->dil[fileNum]],
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;
434 char * filespec = NULL;
435 int fileSpecAlloced = 0;
438 for (i = 0; i < fi->fc; i++) {
439 int otherPkgNum, otherFileNum;
443 if (XFA_SKIPPING(fi->actions[i]))
446 j = strlen(fi->dnl[fi->dil[i]]) + strlen(fi->bnl[i]) + 1;
448 if (j > fileSpecAlloced) {
449 fileSpecAlloced = j * 2;
450 filespec = xrealloc(filespec, fileSpecAlloced);
454 (void) stpcpy( stpcpy( filespec, fi->dnl[fi->dil[i]]), fi->bnl[i]);
458 while (ds->bsize && ds->dev != fi->fps[i].entry->dev) ds++;
459 if (!ds->bsize) ds = NULL;
464 * Retrieve all records that apply to this file. Note that the
465 * file info records were built in the same order as the packages
466 * will be installed and removed so the records for an overlapped
467 * files will be sorted in exactly the same order.
469 (void) htGetEntry(ts->ht, &fi->fps[i],
470 (const void ***) &recs, &numRecs, NULL);
473 * If this package is being added, look only at other packages
474 * being added -- removed packages dance to a different tune.
475 * If both this and the other package are being added, overlapped
476 * files must be identical (or marked as a conflict). The
477 * disposition of already installed config files leads to
478 * a small amount of extra complexity.
480 * If this package is being removed, then there are two cases that
481 * need to be worried about:
482 * If the other package is being added, then skip any overlapped files
483 * so that this package removal doesn't nuke the overlapped files
484 * that were just installed.
485 * If both this and the other package are being removed, then each
486 * file removal from preceding packages needs to be skipped so that
487 * the file removal occurs only on the last occurence of an overlapped
488 * file in the transaction set.
492 /* Locate this overlapped file in the set of added/removed packages. */
493 for (j = 0; j < numRecs && recs[j] != fi; j++)
496 /* Find what the previous disposition of this file was. */
497 otherFileNum = -1; /* keep gcc quiet */
498 for (otherPkgNum = j - 1; otherPkgNum >= 0; otherPkgNum--) {
499 /* Added packages need only look at other added packages. */
500 if (p->type == TR_ADDED && recs[otherPkgNum]->te->type != TR_ADDED)
501 /*@innercontinue@*/ continue;
503 /* TESTME: there are more efficient searches in the world... */
504 for (otherFileNum = 0;
505 otherFileNum < recs[otherPkgNum]->fc;
509 /* If the addresses are the same, so are the values. */
510 if ((fi->fps + i) == (recs[otherPkgNum]->fps + otherFileNum))
511 /*@innerbreak@*/ break;
513 /* Otherwise, compare fingerprints by value. */
514 /*@-nullpass@*/ /* LCL: looks good to me */
515 if (FP_EQUAL(fi->fps[i], recs[otherPkgNum]->fps[otherFileNum]))
516 /*@innerbreak@*/ break;
520 /* XXX is this test still necessary? */
521 if (recs[otherPkgNum]->actions[otherFileNum] != FA_UNKNOWN)
522 /*@innerbreak@*/ break;
528 if (otherPkgNum < 0) {
529 /* XXX is this test still necessary? */
530 if (fi->actions[i] != FA_UNKNOWN)
531 /*@switchbreak@*/ break;
532 if ((fi->fflags[i] & RPMFILE_CONFIG) &&
533 !lstat(filespec, &sb)) {
534 /* Here is a non-overlapped pre-existing config file. */
535 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
536 ? FA_ALTNAME : FA_BACKUP;
538 fi->actions[i] = FA_CREATE;
540 /*@switchbreak@*/ break;
543 /* Mark added overlapped non-identical files as a conflict. */
544 if ((ts->ignoreSet & RPMPROB_FILTER_REPLACENEWFILES)
545 && filecmp(recs[otherPkgNum]->fmodes[otherFileNum],
546 recs[otherPkgNum]->fmd5s[otherFileNum],
547 recs[otherPkgNum]->flinks[otherFileNum],
552 const char * altNEVR = recs[otherPkgNum]->te->NEVR;
553 rpmProblemSetAppend(ts->probs, RPMPROB_NEW_FILE_CONFLICT,
560 /* Try to get the disk accounting correct even if a conflict. */
561 fixupSize = recs[otherPkgNum]->fsizes[otherFileNum];
563 if ((fi->fflags[i] & RPMFILE_CONFIG) && !lstat(filespec, &sb)) {
564 /* Here is an overlapped pre-existing config file. */
565 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
566 ? FA_ALTNAME : FA_SKIP;
568 fi->actions[i] = FA_CREATE;
570 } /*@switchbreak@*/ break;
572 if (otherPkgNum >= 0) {
573 /* Here is an overlapped added file we don't want to nuke. */
574 if (recs[otherPkgNum]->actions[otherFileNum] != FA_ERASE) {
575 /* On updates, don't remove files. */
576 fi->actions[i] = FA_SKIP;
577 /*@switchbreak@*/ break;
579 /* Here is an overlapped removed file: skip in previous. */
580 recs[otherPkgNum]->actions[otherFileNum] = FA_SKIP;
582 if (XFA_SKIPPING(fi->actions[i]))
583 /*@switchbreak@*/ break;
584 if (fi->fstates && fi->fstates[i] != RPMFILE_STATE_NORMAL)
585 /*@switchbreak@*/ break;
586 if (!(S_ISREG(fi->fmodes[i]) && (fi->fflags[i] & RPMFILE_CONFIG))) {
587 fi->actions[i] = FA_ERASE;
588 /*@switchbreak@*/ break;
591 /* Here is a pre-existing modified config file that needs saving. */
593 if (!mdfile(filespec, mdsum) && strcmp(fi->fmd5s[i], mdsum)) {
594 fi->actions[i] = FA_BACKUP;
595 /*@switchbreak@*/ break;
598 fi->actions[i] = FA_ERASE;
599 /*@switchbreak@*/ break;
603 uint_32 s = BLOCK_ROUND(fi->fsizes[i], ds->bsize);
605 switch (fi->actions[i]) {
611 /*@switchbreak@*/ break;
614 * FIXME: If two packages share a file (same md5sum), and
615 * that file is being replaced on disk, will ds->bneeded get
616 * decremented twice? Quite probably!
620 ds->bneeded -= BLOCK_ROUND(fi->replacedSizes[i], ds->bsize);
621 /*@switchbreak@*/ break;
626 /*@switchbreak@*/ break;
629 /*@switchbreak@*/ break;
632 ds->bneeded -= BLOCK_ROUND(fixupSize, ds->bsize);
635 filespec = _free(filespec);
639 * Ensure that current package is newer than installed package.
640 * @param ts transaction set
641 * @param p current transaction element
642 * @param h installed header
643 * @return 0 if not newer, 1 if okay
645 static int ensureOlder(rpmTransactionSet ts,
646 const transactionElement p, const Header h)
649 int_32 reqFlags = (RPMSENSE_LESS | RPMSENSE_EQUAL);
655 if (p == NULL || h == NULL)
658 t = alloca(strlen(p->NEVR) + (p->epoch != NULL ? strlen(p->epoch) : 0) + 1);
661 if (p->epoch != NULL) t = stpcpy( stpcpy(t, p->epoch), ":");
662 if (p->version != NULL) t = stpcpy(t, p->version);
664 if (p->release != NULL) t = stpcpy(t, p->release);
666 req = dsSingle(RPMTAG_REQUIRENAME, p->name, reqEVR, reqFlags);
667 rc = headerMatchesDepFlags(h, req);
671 const char * altNEVR = hGetNEVR(h, NULL);
672 rpmProblemSetAppend(ts->probs, RPMPROB_OLDPACKAGE,
677 altNEVR = _free(altNEVR);
687 static void skipFiles(const rpmTransactionSet ts, TFI_t fi)
688 /*@globals rpmGlobalMacroContext @*/
689 /*@modifies fi, rpmGlobalMacroContext @*/
691 int noDocs = (ts->transFlags & RPMTRANS_FLAG_NODOCS);
692 char ** netsharedPaths = NULL;
693 const char ** languages;
694 const char * dn, * bn;
695 int dnlen, bnlen, ix;
702 noDocs = rpmExpandNumeric("%{_excludedocs}");
704 { const char *tmpPath = rpmExpand("%{_netsharedpath}", NULL);
706 if (tmpPath && *tmpPath != '%')
707 netsharedPaths = splitString(tmpPath, strlen(tmpPath), ':');
709 tmpPath = _free(tmpPath);
712 s = rpmExpand("%{_install_langs}", NULL);
714 if (!(s && *s != '%'))
717 languages = (const char **) splitString(s, strlen(s), ':');
723 /* Compute directory refcount, skip directory if now empty. */
724 drc = alloca(fi->dc * sizeof(*drc));
725 memset(drc, 0, fi->dc * sizeof(*drc));
726 dff = alloca(fi->dc * sizeof(*dff));
727 memset(dff, 0, fi->dc * sizeof(*dff));
729 for (i = 0; i < fi->fc; i++) {
740 /* Don't bother with skipped files */
741 if (XFA_SKIPPING(fi->actions[i])) {
747 * Skip net shared paths.
748 * Net shared paths are not relative to the current root (though
749 * they do need to take package relocations into account).
751 for (nsp = netsharedPaths; nsp && *nsp; nsp++) {
756 if (strncmp(dn, *nsp, len))
757 /*@innercontinue@*/ continue;
758 /* Only directories or complete file paths can be net shared */
759 if (!(dn[len] == '/' || dn[len] == '\0'))
760 /*@innercontinue@*/ continue;
762 if (len < (dnlen + bnlen))
763 /*@innercontinue@*/ continue;
764 if (strncmp(dn, *nsp, dnlen))
765 /*@innercontinue@*/ continue;
766 if (strncmp(bn, (*nsp) + dnlen, bnlen))
767 /*@innercontinue@*/ continue;
769 /* Only directories or complete file paths can be net shared */
770 if (!((*nsp)[len] == '/' || (*nsp)[len] == '\0'))
771 /*@innercontinue@*/ continue;
774 /*@innerbreak@*/ break;
778 drc[ix]--; dff[ix] = 1;
779 fi->actions[i] = FA_SKIPNETSHARED;
784 * Skip i18n language specific files.
786 if (fi->flangs && languages && *fi->flangs[i]) {
787 const char **lang, *l, *le;
788 for (lang = languages; *lang != NULL; lang++) {
789 if (!strcmp(*lang, "all"))
790 /*@innerbreak@*/ break;
791 for (l = fi->flangs[i]; *l != '\0'; l = le) {
792 for (le = l; *le != '\0' && *le != '|'; le++)
794 if ((le-l) > 0 && !strncmp(*lang, l, (le-l)))
795 /*@innerbreak@*/ break;
796 if (*le == '|') le++; /* skip over | */
799 /*@innerbreak@*/ break;
802 drc[ix]--; dff[ix] = 1;
803 fi->actions[i] = FA_SKIPNSTATE;
809 * Skip documentation if requested.
811 if (noDocs && (fi->fflags[i] & RPMFILE_DOC)) {
812 drc[ix]--; dff[ix] = 1;
813 fi->actions[i] = FA_SKIPNSTATE;
818 /* Skip (now empty) directories that had skipped files. */
819 for (j = 0; j < fi->dc; j++) {
821 if (drc[j]) continue; /* dir still has files. */
822 if (!dff[j]) continue; /* dir was not emptied here. */
824 /* Find parent directory and basename. */
825 dn = fi->dnl[j]; dnlen = strlen(dn) - 1;
826 bn = dn + dnlen; bnlen = 0;
827 while (bn > dn && bn[-1] != '/') {
833 /* If explicitly included in the package, skip the directory. */
834 for (i = 0; i < fi->fc; i++) {
837 if (XFA_SKIPPING(fi->actions[i]))
838 /*@innercontinue@*/ continue;
839 if (whatis(fi->fmodes[i]) != XDIR)
840 /*@innercontinue@*/ continue;
841 dir = fi->dnl[fi->dil[i]];
842 if (strlen(dir) != dnlen)
843 /*@innercontinue@*/ continue;
844 if (strncmp(dir, dn, dnlen))
845 /*@innercontinue@*/ continue;
846 if (strlen(fi->bnl[i]) != bnlen)
847 /*@innercontinue@*/ continue;
848 if (strncmp(fi->bnl[i], bn, bnlen))
849 /*@innercontinue@*/ continue;
850 rpmMessage(RPMMESS_DEBUG, _("excluding directory %s\n"), dn);
851 fi->actions[i] = FA_SKIPNSTATE;
852 /*@innerbreak@*/ break;
856 if (netsharedPaths) freeSplitString(netsharedPaths);
857 #ifdef DYING /* XXX freeFi will deal with this later. */
858 fi->flangs = _free(fi->flangs);
860 if (languages) freeSplitString((char **)languages);
863 #define NOTIFY(_ts, _al) if ((_ts)->notify) (void) (_ts)->notify _al
865 int rpmRunTransactions( rpmTransactionSet ts,
866 rpmCallbackFunction notify, rpmCallbackData notifyData,
867 rpmProblemSet okProbs, rpmProblemSet * newProbs,
868 rpmtransFlags transFlags, rpmprobFilterFlags ignoreSet)
872 int totalFileCount = 0;
874 struct diskspaceInfo * dip;
875 sharedFileInfo shared, sharedList;
879 fingerPrintCache fpc;
880 PSM_t psm = memset(alloca(sizeof(*psm)), 0, sizeof(*psm));
881 teIterator pi; transactionElement p;
882 teIterator qi; transactionElement q;
885 /* FIXME: what if the same package is included in ts twice? */
887 ts->transFlags = transFlags;
888 if (ts->transFlags & RPMTRANS_FLAG_NOSCRIPTS)
889 ts->transFlags |= (_noTransScripts | _noTransTriggers);
890 if (ts->transFlags & RPMTRANS_FLAG_NOTRIGGERS)
891 ts->transFlags |= _noTransTriggers;
893 /* XXX MULTILIB is broken, as packages can and do execute /sbin/ldconfig. */
894 if (ts->transFlags & (RPMTRANS_FLAG_JUSTDB | RPMTRANS_FLAG_MULTILIB))
895 ts->transFlags |= (_noTransScripts | _noTransTriggers);
898 ts->notifyData = notifyData;
899 ts->probs = rpmProblemSetFree(ts->probs);
900 ts->probs = rpmProblemSetCreate();
901 *newProbs = rpmpsLink(ts->probs, "RunTransactions");
902 ts->ignoreSet = ignoreSet;
903 ts->currDir = _free(ts->currDir);
904 ts->currDir = currentDirectory();
906 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
907 ts->id = (int_32) time(NULL);
909 memset(psm, 0, sizeof(*psm));
910 psm->ts = rpmtsLink(ts, "tsRun");
912 /* Get available space on mounted file systems. */
913 if (!(ts->ignoreSet & RPMPROB_FILTER_DISKSPACE) &&
914 !rpmGetFilesystemList(&ts->filesystems, &ts->filesystemCount))
918 rpmMessage(RPMMESS_DEBUG, _("getting list of mounted filesystems\n"));
920 ts->di = _free(ts->di);
921 dip = ts->di = xcalloc((ts->filesystemCount + 1), sizeof(*ts->di));
923 for (i = 0; (i < ts->filesystemCount) && dip; i++) {
924 #if STATFS_IN_SYS_STATVFS
926 memset(&sfb, 0, sizeof(sfb));
927 if (statvfs(ts->filesystems[i], &sfb))
931 /* This platform has the 4-argument version of the statfs call. The last two
932 * should be the size of struct statfs and 0, respectively. The 0 is the
933 * filesystem type, and is always 0 when statfs is called on a mounted
934 * filesystem, as we're doing.
936 memset(&sfb, 0, sizeof(sfb));
937 if (statfs(ts->filesystems[i], &sfb, sizeof(sfb), 0))
939 memset(&sfb, 0, sizeof(sfb));
940 if (statfs(ts->filesystems[i], &sfb))
946 ts->di[i].bsize = sfb.f_bsize;
947 ts->di[i].bneeded = 0;
948 ts->di[i].ineeded = 0;
949 #ifdef STATFS_HAS_F_BAVAIL
950 ts->di[i].bavail = sfb.f_bavail;
952 /* FIXME: the statfs struct doesn't have a member to tell how many blocks are
953 * available for non-superusers. f_blocks - f_bfree is probably too big, but
954 * it's about all we can do.
956 ts->di[i].bavail = sfb.f_blocks - sfb.f_bfree;
958 /* XXX Avoid FAT and other file systems that have not inodes. */
959 ts->di[i].iavail = !(sfb.f_ffree == 0 && sfb.f_files == 0)
962 xx = stat(ts->filesystems[i], &sb);
963 ts->di[i].dev = sb.st_dev;
967 if (dip) ts->di[i].bsize = 0;
970 /* ===============================================
971 * For packages being installed:
972 * - verify package arch/os.
973 * - verify package epoch:version-release is newer.
975 * For packages being removed:
978 /* The ordering doesn't matter here */
979 pi = teInitIterator(ts);
980 while ((p = teNext(pi, TR_ADDED)) != NULL) {
981 rpmdbMatchIterator mi;
983 if (!(ts->ignoreSet & RPMPROB_FILTER_IGNOREARCH))
984 if (!archOkay(p->arch))
985 rpmProblemSetAppend(ts->probs, RPMPROB_BADARCH,
990 if (!(ts->ignoreSet & RPMPROB_FILTER_IGNOREOS))
992 rpmProblemSetAppend(ts->probs, RPMPROB_BADOS,
997 if (!(ts->ignoreSet & RPMPROB_FILTER_OLDPACKAGE)) {
999 mi = rpmtsInitIterator(ts, RPMTAG_NAME, p->name, 0);
1000 while ((h = rpmdbNextIterator(mi)) != NULL)
1001 xx = ensureOlder(ts, p, h);
1002 mi = rpmdbFreeIterator(mi);
1005 /* XXX multilib should not display "already installed" problems */
1006 if (!(ts->ignoreSet & RPMPROB_FILTER_REPLACEPKG) && !p->multiLib) {
1007 mi = rpmtsInitIterator(ts, RPMTAG_NAME, p->name, 0);
1008 xx = rpmdbSetIteratorRE(mi, RPMTAG_VERSION, RPMMIRE_DEFAULT,
1010 xx = rpmdbSetIteratorRE(mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT,
1013 while (rpmdbNextIterator(mi) != NULL) {
1014 rpmProblemSetAppend(ts->probs, RPMPROB_PKG_INSTALLED,
1018 /*@innerbreak@*/ break;
1020 mi = rpmdbFreeIterator(mi);
1023 /* Count no. of files (if any). */
1025 totalFileCount += p->fi->fc;
1028 pi = teFreeIterator(pi);
1030 /* The ordering doesn't matter here */
1031 pi = teInitIterator(ts);
1032 while ((p = teNext(pi, TR_REMOVED)) != NULL) {
1036 if (fi->bnl == NULL)
1037 continue; /* XXX can't happen */
1038 if (fi->dnl == NULL)
1039 continue; /* XXX can't happen */
1040 if (fi->dil == NULL)
1041 continue; /* XXX can't happen */
1042 totalFileCount += fi->fc;
1044 pi = teFreeIterator(pi);
1046 /* ===============================================
1047 * Initialize transaction element file info for package:
1051 * FIXME?: we'd be better off assembling one very large file list and
1052 * calling fpLookupList only once. I'm not sure that the speedup is
1053 * worth the trouble though.
1055 pi = teInitIterator(ts);
1056 while ((p = teNextIterator(pi)) != NULL) {
1059 if ((fi = teGetFi(pi)) == NULL)
1060 continue; /* XXX can't happen */
1062 #ifdef DYING /* XXX W2DO? this is now done teGetFi, okay ??? */
1063 fi->magic = TFIMAGIC;
1071 /* Skip netshared paths, not our i18n files, and excluded docs */
1074 /*@switchbreak@*/ break;
1076 fi->record = p->u.removed.dboffset;
1077 /*@switchbreak@*/ break;
1081 fi->fps = (fi->fc > 0 ? xmalloc(fi->fc * sizeof(*fi->fps)) : NULL);
1083 pi = teFreeIterator(pi);
1085 if (!ts->chrootDone) {
1087 /*@-superuser -noeffect @*/
1088 xx = chroot(ts->rootDir);
1089 /*@=superuser =noeffect @*/
1091 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 1;
1094 chroot_prefix = ts->rootDir;
1099 ts->ht = htCreate(totalFileCount * 2, 0, 0, fpHashFunction, fpEqual);
1100 fpc = fpCacheCreate(totalFileCount);
1102 /* ===============================================
1103 * Add fingerprint for each file not skipped.
1105 pi = teInitIterator(ts);
1106 while ((p = teNextIterator(pi)) != NULL) {
1108 if ((fi = teGetFi(pi)) == NULL)
1109 continue; /* XXX can't happen */
1111 fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fi->fc, fi->fps);
1113 for (i = 0; i < fi->fc; i++) {
1114 if (XFA_SKIPPING(fi->actions[i]))
1115 /*@innercontinue@*/ continue;
1116 /*@-dependenttrans@*/
1117 htAddEntry(ts->ht, fi->fps + i, fi);
1118 /*@=dependenttrans@*/
1122 pi = teFreeIterator(pi);
1124 /*@-noeffectuncon @*/ /* FIX: check rc */
1125 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_START, 6, ts->orderCount,
1126 NULL, ts->notifyData));
1127 /*@=noeffectuncon@*/
1129 /* ===============================================
1130 * Compute file disposition for each package in transaction set.
1132 pi = teInitIterator(ts);
1133 while ((p = teNextIterator(pi)) != NULL) {
1134 dbiIndexSet * matches;
1137 if ((fi = teGetFi(pi)) == NULL)
1138 continue; /* XXX can't happen */
1140 /*@-noeffectuncon @*/ /* FIX: check rc */
1141 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_PROGRESS, teGetOc(pi),
1142 ts->orderCount, NULL, ts->notifyData));
1143 /*@=noeffectuncon@*/
1145 if (fi->fc == 0) continue;
1147 /* Extract file info for all files in this package from the database. */
1148 matches = xcalloc(fi->fc, sizeof(*matches));
1149 if (rpmdbFindFpList(ts->rpmdb, fi->fps, matches, fi->fc)) {
1150 psm->ts = rpmtsUnlink(ts, "tsRun (rpmFindFpList fail)");
1151 return 1; /* XXX WTFO? */
1155 for (i = 0; i < fi->fc; i++)
1156 numShared += dbiIndexSetCount(matches[i]);
1158 /* Build sorted file info list for this package. */
1159 shared = sharedList = xcalloc((numShared + 1), sizeof(*sharedList));
1160 for (i = 0; i < fi->fc; i++) {
1162 * Take care not to mark files as replaced in packages that will
1163 * have been removed before we will get here.
1165 for (j = 0; j < dbiIndexSetCount(matches[i]); j++) {
1167 ro = dbiIndexRecordOffset(matches[i], j);
1169 qi = teInitIterator(ts);
1170 while ((q = teNext(qi, TR_REMOVED)) != NULL) {
1172 /*@innerbreak@*/ break;
1173 if (q->u.removed.dboffset == ro)
1176 qi = teFreeIterator(qi);
1178 shared->pkgFileNum = i;
1179 shared->otherPkg = dbiIndexRecordOffset(matches[i], j);
1180 shared->otherFileNum = dbiIndexRecordFileNumber(matches[i], j);
1181 shared->isRemoved = (knownBad == ro);
1184 matches[i] = dbiFreeIndexSet(matches[i]);
1186 numShared = shared - sharedList;
1187 shared->otherPkg = -1;
1188 matches = _free(matches);
1190 /* Sort file info by other package index (otherPkg) */
1191 qsort(sharedList, numShared, sizeof(*shared), sharedCmp);
1193 /* For all files from this package that are in the database ... */
1194 for (i = 0; i < numShared; i = nexti) {
1197 shared = sharedList + i;
1199 /* Find the end of the files in the other package. */
1200 for (nexti = i + 1; nexti < numShared; nexti++) {
1201 if (sharedList[nexti].otherPkg != shared->otherPkg)
1202 /*@innerbreak@*/ break;
1205 /* Is this file from a package being removed? */
1207 if (ts->removedPackages != NULL)
1208 for (j = 0; j < ts->numRemovedPackages; j++) {
1209 if (ts->removedPackages[j] != shared->otherPkg)
1210 /*@innercontinue@*/ continue;
1212 /*@innerbreak@*/ break;
1215 /* Determine the fate of each file. */
1218 xx = handleInstInstalledFiles(ts, p, fi, shared, nexti - i,
1219 !(beingRemoved || (ts->ignoreSet & RPMPROB_FILTER_REPLACEOLDFILES)));
1220 /*@switchbreak@*/ break;
1223 xx = handleRmvdInstalledFiles(ts, fi, shared, nexti - i);
1224 /*@switchbreak@*/ break;
1230 /* Update disk space needs on each partition for this package. */
1231 handleOverlappedFiles(ts, p, fi);
1233 /* Check added package has sufficient space on each partition used. */
1236 if (!(ts->di && fi->fc))
1237 /*@switchbreak@*/ break;
1238 for (i = 0; i < ts->filesystemCount; i++) {
1242 /* XXX Avoid FAT and other file systems that have not inodes. */
1243 if (dip->iavail <= 0)
1244 /*@innercontinue@*/ continue;
1246 if (adj_fs_blocks(dip->bneeded) > dip->bavail) {
1247 rpmProblemSetAppend(ts->probs, RPMPROB_DISKSPACE,
1249 ts->filesystems[i], NULL, NULL,
1250 (adj_fs_blocks(dip->bneeded) - dip->bavail) * dip->bsize);
1253 if (adj_fs_blocks(dip->ineeded) > dip->iavail) {
1254 rpmProblemSetAppend(ts->probs, RPMPROB_DISKNODES,
1256 ts->filesystems[i], NULL, NULL,
1257 (adj_fs_blocks(dip->ineeded) - dip->iavail));
1260 /*@switchbreak@*/ break;
1262 /*@switchbreak@*/ break;
1265 pi = teFreeIterator(pi);
1267 if (ts->chrootDone) {
1268 /*@-superuser -noeffect @*/
1270 /*@=superuser =noeffect @*/
1272 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
1274 chroot_prefix = NULL;
1276 xx = chdir(ts->currDir);
1279 /*@-noeffectuncon @*/ /* FIX: check rc */
1280 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_STOP, 6, ts->orderCount,
1281 NULL, ts->notifyData));
1282 /*@=noeffectuncon @*/
1284 /* ===============================================
1285 * Free unused memory as soon as possible.
1287 pi = teInitIterator(ts);
1288 while ((p = teNextIterator(pi)) != NULL) {
1289 if ((fi = teGetFi(pi)) == NULL)
1290 continue; /* XXX can't happen */
1293 fi->fps = _free(fi->fps);
1295 pi = teFreeIterator(pi);
1301 /* ===============================================
1302 * If unfiltered problems exist, free memory and return.
1304 if ((ts->transFlags & RPMTRANS_FLAG_BUILD_PROBS)
1305 || (ts->probs->numProblems &&
1306 (okProbs != NULL || rpmProblemSetTrim(ts->probs, okProbs)))
1309 if (psm->ts != NULL)
1310 psm->ts = rpmtsUnlink(psm->ts, "tsRun (problems)");
1311 return ts->orderCount;
1314 /* ===============================================
1315 * Save removed files before erasing.
1317 if (ts->transFlags & (RPMTRANS_FLAG_DIRSTASH | RPMTRANS_FLAG_REPACKAGE)) {
1318 pi = teInitIterator(ts);
1319 while ((p = teNextIterator(pi)) != NULL) {
1323 /*@switchbreak@*/ break;
1325 if (!(ts->transFlags & RPMTRANS_FLAG_REPACKAGE))
1326 /*@switchbreak@*/ break;
1328 psm->fi = rpmfiLink(fi, "tsRepackage");
1329 xx = psmStage(psm, PSM_PKGSAVE);
1330 (void) rpmfiUnlink(fi, "tsRepackage");
1333 /*@switchbreak@*/ break;
1336 pi = teFreeIterator(pi);
1339 /* ===============================================
1340 * Install and remove packages.
1342 lastKey = (alKey)-2; /* erased packages have -1 */
1343 pi = teInitIterator(ts);
1344 /*@-branchstate@*/ /* FIX: fi reload needs work */
1345 while ((p = teNextIterator(pi)) != NULL) {
1351 if ((fi = teGetFi(pi)) == NULL)
1352 continue; /* XXX can't happen */
1355 psm->fi = rpmfiLink(fi, "tsInstall");
1359 pkgKey = p->u.addedKey;
1361 rpmMessage(RPMMESS_DEBUG, "========== +++ %s\n", p->NEVR);
1364 p->fd = ts->notify(fi->h, RPMCALLBACK_INST_OPEN_FILE, 0, 0,
1365 p->key, ts->notifyData);
1366 if (p->fd != NULL) {
1369 /*@=mustmod@*/ /* LCL: segfault */
1370 rpmrc = rpmReadPackageFile(ts, p->fd,
1371 "rpmRunTransactions", &h);
1374 if (!(rpmrc == RPMRC_OK || rpmrc == RPMRC_BADSIZE)) {
1375 p->fd = ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE,
1377 p->key, ts->notifyData);
1381 if (p->fd != NULL) gotfd = 1;
1387 if (p->fd != NULL) {
1389 char * fstates = fi->fstates;
1390 fileAction * actions = fi->actions;
1394 (void) fiFree(fi, 0);
1396 fi->magic = TFIMAGIC;
1399 (void) fiNew(ts, fi, h, RPMTAG_BASENAMES, 1);
1400 fi->fstates = _free(fi->fstates);
1401 fi->fstates = fstates;
1402 fi->actions = _free(fi->actions);
1403 fi->actions = actions;
1408 ts->transFlags |= RPMTRANS_FLAG_MULTILIB;
1410 if (psmStage(psm, PSM_PKGINSTALL)) {
1414 fi->h = headerFree(fi->h, "TR_ADDED fi->h free");
1421 h = headerFree(h, "TR_ADDED h free");
1424 /*@-noeffectuncon @*/ /* FIX: check rc */
1425 (void)ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE, 0, 0,
1426 p->key, ts->notifyData);
1427 /*@=noeffectuncon @*/
1430 (void) fiFree(fi, 0);
1431 /*@switchbreak@*/ break;
1433 rpmMessage(RPMMESS_DEBUG, "========== --- %s\n", p->NEVR);
1434 /* If install failed, then we shouldn't erase. */
1435 if (p->u.removed.dependsOnKey != lastKey) {
1436 if (psmStage(psm, PSM_PKGERASE))
1439 (void) fiFree(fi, 0);
1440 /*@switchbreak@*/ break;
1442 xx = rpmdbSync(ts->rpmdb);
1443 (void) rpmfiUnlink(psm->fi, "tsInstall");
1448 pi = teFreeIterator(pi);
1450 psm->ts = rpmtsUnlink(psm->ts, "tsRun");
1452 /*@-nullstate@*/ /* FIX: ts->flList may be NULL */