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 @*/
63 /*@access rpmFNSet @*/
66 /*@access teIterator @*/
67 /*@access transactionElement @*/
68 /*@access rpmTransactionSet @*/
72 struct diskspaceInfo {
73 dev_t dev; /*!< File system device number. */
74 signed long bneeded; /*!< No. of blocks needed. */
75 signed long ineeded; /*!< No. of inodes needed. */
76 int bsize; /*!< File system block size. */
77 signed long bavail; /*!< No. of blocks available. */
78 signed long iavail; /*!< No. of inodes available. */
82 * Adjust for root only reserved space. On linux e2fs, this is 5%.
84 #define adj_fs_blocks(_nb) (((_nb) * 21) / 20)
86 /* argon thought a shift optimization here was a waste of time... he's
88 #define BLOCK_ROUND(size, block) (((size) + (block) - 1) / (block))
92 static /*@null@*/ void * freeFl(rpmTransactionSet ts,
93 /*@only@*/ /*@null@*/ TFI_t flList)
100 /*@-usereleased -onlytrans @*/ /* FIX: fi needs to be only */
101 for (oc = 0, fi = flList; oc < ts->orderCount; oc++, fi++)
103 flList = _free(flList);
104 /*@=usereleased =onlytrans @*/
109 void rpmtransSetScriptFd(rpmTransactionSet ts, FD_t fd)
111 /*@-type@*/ /* FIX: cast? */
112 ts->scriptFd = (fd ? fdLink(fd, "rpmtransSetScriptFd") : NULL);
116 int rpmtransGetKeys(const rpmTransactionSet ts, fnpyKey ** ep, int * nep)
120 if (nep) *nep = ts->orderCount;
125 *ep = e = xmalloc(ts->orderCount * sizeof(*e));
126 for (oc = 0; oc < ts->orderCount; oc++, e++) {
127 switch (ts->order[oc].type) {
129 *e = ts->order[oc].key;
130 /*@switchbreak@*/ break;
133 /*@-mods@*/ /* FIX: double indirection. */
136 /*@switchbreak@*/ break;
145 static int archOkay(/*@null@*/ const char * pkgArch)
148 if (pkgArch == NULL) return 0;
149 return (rpmMachineScore(RPM_MACHTABLE_INSTARCH, pkgArch) ? 1 : 0);
154 static int osOkay(/*@null@*/ const char * pkgOs)
157 if (pkgOs == NULL) return 0;
158 return (rpmMachineScore(RPM_MACHTABLE_INSTOS, pkgOs) ? 1 : 0);
163 static int sharedCmp(const void * one, const void * two)
166 sharedFileInfo a = (sharedFileInfo) one;
167 sharedFileInfo b = (sharedFileInfo) two;
169 if (a->otherPkg < b->otherPkg)
171 else if (a->otherPkg > b->otherPkg)
179 static fileAction decideFileFate(const char * dirName,
180 const char * baseName, short dbMode,
181 const char * dbMd5, const char * dbLink, short newMode,
182 const char * newMd5, const char * newLink, int newFlags,
183 rpmtransFlags transFlags)
184 /*@globals fileSystem @*/
185 /*@modifies fileSystem @*/
188 const char * dbAttr, * newAttr;
189 fileTypes dbWhat, newWhat, diskWhat;
192 int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
193 char * filespec = alloca(strlen(dirName) + strlen(baseName) + 1);
195 (void) stpcpy( stpcpy(filespec, dirName), baseName);
197 if (lstat(filespec, &sb)) {
199 * The file doesn't exist on the disk. Create it unless the new
200 * package has marked it as missingok, or allfiles is requested.
202 if (!(transFlags & RPMTRANS_FLAG_ALLFILES) &&
203 (newFlags & RPMFILE_MISSINGOK)) {
204 rpmMessage(RPMMESS_DEBUG, _("%s skipped due to missingok flag\n"),
212 diskWhat = whatis(sb.st_mode);
213 dbWhat = whatis(dbMode);
214 newWhat = whatis(newMode);
216 /* RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
217 them in older packages as well */
218 if (newWhat == XDIR) {
222 if (diskWhat != newWhat) {
224 } else if (newWhat != dbWhat && diskWhat != dbWhat) {
226 } else if (dbWhat != newWhat) {
228 } else if (dbWhat != LINK && dbWhat != REG) {
233 rc = mdfile(filespec, buffer);
236 /* assume the file has been removed, don't freak */
241 } else /* dbWhat == LINK */ {
242 memset(buffer, 0, sizeof(buffer));
243 i = readlink(filespec, buffer, sizeof(buffer) - 1);
245 /* assume the file has been removed, don't freak */
252 /* this order matters - we'd prefer to CREATE the file if at all
253 possible in case something else (like the timestamp) has changed */
255 if (!strcmp(dbAttr, buffer)) {
256 /* this config file has never been modified, so just replace it */
260 if (!strcmp(dbAttr, newAttr)) {
261 /* this file is the same in all versions of this package */
266 * The config file on the disk has been modified, but
267 * the ones in the two packages are different. It would
268 * be nice if RPM was smart enough to at least try and
269 * merge the difference ala CVS, but...
276 static int filecmp(short mode1, const char * md51, const char * link1,
277 short mode2, const char * md52, const char * link2)
280 fileTypes what1 = whatis(mode1);
281 fileTypes what2 = whatis(mode2);
283 if (what1 != what2) return 1;
286 return strcmp(link1, link2);
287 else if (what1 == REG)
288 return strcmp(md51, md52);
295 /* XXX only ts->{probs,rpmdb} modified */
296 static int handleInstInstalledFiles(const rpmTransactionSet ts, TFI_t fi,
297 sharedFileInfo shared,
298 int sharedCount, int reportConflicts)
299 /*@globals fileSystem @*/
300 /*@modifies ts, fi, fileSystem @*/
303 HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
304 rpmtransFlags transFlags = ts->transFlags;
305 rpmTagType oltype, omtype;
308 const char ** otherMd5s;
309 const char ** otherLinks;
310 const char * otherStates;
311 uint_32 * otherFlags;
312 uint_32 * otherSizes;
313 uint_16 * otherModes;
317 rpmdbMatchIterator mi;
319 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
320 &shared->otherPkg, sizeof(shared->otherPkg));
321 h = rpmdbNextIterator(mi);
323 mi = rpmdbFreeIterator(mi);
327 xx = hge(h, RPMTAG_FILEMD5S, &omtype, (void **) &otherMd5s, NULL);
328 xx = hge(h, RPMTAG_FILELINKTOS, &oltype, (void **) &otherLinks, NULL);
329 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
330 xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &otherModes, NULL);
331 xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &otherFlags, NULL);
332 xx = hge(h, RPMTAG_FILESIZES, NULL, (void **) &otherSizes, NULL);
334 fi->replaced = xmalloc(sharedCount * sizeof(*fi->replaced));
336 for (i = 0; i < sharedCount; i++, shared++) {
337 int otherFileNum, fileNum;
338 otherFileNum = shared->otherFileNum;
339 fileNum = shared->pkgFileNum;
341 /* XXX another tedious segfault, assume file state normal. */
342 if (otherStates && otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
345 if (XFA_SKIPPING(fi->actions[fileNum]))
348 if (filecmp(otherModes[otherFileNum],
349 otherMd5s[otherFileNum],
350 otherLinks[otherFileNum],
353 fi->flinks[fileNum])) {
354 /*@-compdef@*/ /* FIX: *fi->replaced undefined */
355 if (reportConflicts) {
356 const char * pkgNEVR = fiGetNEVR(fi);
357 const char * altNEVR = hGetNEVR(h, NULL);
358 rpmProblemSetAppend(ts->probs, RPMPROB_FILE_CONFLICT,
360 fi->dnl[fi->dil[fileNum]], fi->bnl[fileNum],
363 pkgNEVR = _free(pkgNEVR);
364 altNEVR = _free(altNEVR);
367 if (!(otherFlags[otherFileNum] | fi->fflags[fileNum])
370 if (!shared->isRemoved)
371 fi->replaced[numReplaced++] = *shared;
376 if ((otherFlags[otherFileNum] | fi->fflags[fileNum]) & RPMFILE_CONFIG) {
377 fi->actions[fileNum] = decideFileFate(
378 fi->dnl[fi->dil[fileNum]],
380 otherModes[otherFileNum],
381 otherMd5s[otherFileNum],
382 otherLinks[otherFileNum],
390 fi->replacedSizes[fileNum] = otherSizes[otherFileNum];
393 otherMd5s = hfd(otherMd5s, omtype);
394 otherLinks = hfd(otherLinks, oltype);
395 mi = rpmdbFreeIterator(mi);
397 fi->replaced = xrealloc(fi->replaced, /* XXX memory leak */
398 sizeof(*fi->replaced) * (numReplaced + 1));
399 fi->replaced[numReplaced].otherPkg = 0;
406 /* XXX only ts->rpmdb modified */
407 static int handleRmvdInstalledFiles(const rpmTransactionSet ts, TFI_t fi,
408 sharedFileInfo shared, int sharedCount)
409 /*@globals fileSystem @*/
410 /*@modifies fi, fileSystem @*/
414 const char * otherStates;
417 rpmdbMatchIterator mi;
419 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
420 &shared->otherPkg, sizeof(shared->otherPkg));
421 h = rpmdbNextIterator(mi);
423 mi = rpmdbFreeIterator(mi);
427 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
429 for (i = 0; i < sharedCount; i++, shared++) {
430 int otherFileNum, fileNum;
431 otherFileNum = shared->otherFileNum;
432 fileNum = shared->pkgFileNum;
434 if (otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
437 fi->actions[fileNum] = FA_SKIP;
440 mi = rpmdbFreeIterator(mi);
446 * Update disk space needs on each partition for this package.
448 /* XXX only ts->{probs,di} modified */
449 static void handleOverlappedFiles(const rpmTransactionSet ts, TFI_t fi)
450 /*@globals fileSystem @*/
451 /*@modifies ts, fi, fileSystem @*/
453 struct diskspaceInfo * ds = NULL;
454 uint_32 fixupSize = 0;
455 char * filespec = NULL;
456 int fileSpecAlloced = 0;
459 for (i = 0; i < fi->fc; i++) {
460 int otherPkgNum, otherFileNum;
464 if (XFA_SKIPPING(fi->actions[i]))
467 j = strlen(fi->dnl[fi->dil[i]]) + strlen(fi->bnl[i]) + 1;
469 if (j > fileSpecAlloced) {
470 fileSpecAlloced = j * 2;
471 filespec = xrealloc(filespec, fileSpecAlloced);
475 (void) stpcpy( stpcpy( filespec, fi->dnl[fi->dil[i]]), fi->bnl[i]);
479 while (ds->bsize && ds->dev != fi->fps[i].entry->dev) ds++;
480 if (!ds->bsize) ds = NULL;
485 * Retrieve all records that apply to this file. Note that the
486 * file info records were built in the same order as the packages
487 * will be installed and removed so the records for an overlapped
488 * files will be sorted in exactly the same order.
490 (void) htGetEntry(ts->ht, &fi->fps[i],
491 (const void ***) &recs, &numRecs, NULL);
494 * If this package is being added, look only at other packages
495 * being added -- removed packages dance to a different tune.
496 * If both this and the other package are being added, overlapped
497 * files must be identical (or marked as a conflict). The
498 * disposition of already installed config files leads to
499 * a small amount of extra complexity.
501 * If this package is being removed, then there are two cases that
502 * need to be worried about:
503 * If the other package is being added, then skip any overlapped files
504 * so that this package removal doesn't nuke the overlapped files
505 * that were just installed.
506 * If both this and the other package are being removed, then each
507 * file removal from preceding packages needs to be skipped so that
508 * the file removal occurs only on the last occurence of an overlapped
509 * file in the transaction set.
513 /* Locate this overlapped file in the set of added/removed packages. */
514 for (j = 0; j < numRecs && recs[j] != fi; j++)
517 /* Find what the previous disposition of this file was. */
518 otherFileNum = -1; /* keep gcc quiet */
519 for (otherPkgNum = j - 1; otherPkgNum >= 0; otherPkgNum--) {
520 /* Added packages need only look at other added packages. */
521 if (fi->type == TR_ADDED && recs[otherPkgNum]->type != TR_ADDED)
522 /*@innercontinue@*/ continue;
524 /* TESTME: there are more efficient searches in the world... */
525 for (otherFileNum = 0; otherFileNum < recs[otherPkgNum]->fc;
528 /* If the addresses are the same, so are the values. */
529 if ((fi->fps + i) == (recs[otherPkgNum]->fps + otherFileNum))
530 /*@innerbreak@*/ break;
532 /* Otherwise, compare fingerprints by value. */
533 /*@-nullpass@*/ /* LCL: looks good to me */
534 if (FP_EQUAL(fi->fps[i], recs[otherPkgNum]->fps[otherFileNum]))
535 /*@innerbreak@*/ break;
539 /* XXX is this test still necessary? */
540 if (recs[otherPkgNum]->actions[otherFileNum] != FA_UNKNOWN)
541 /*@innerbreak@*/ break;
547 if (otherPkgNum < 0) {
548 /* XXX is this test still necessary? */
549 if (fi->actions[i] != FA_UNKNOWN)
550 /*@switchbreak@*/ break;
551 if ((fi->fflags[i] & RPMFILE_CONFIG) &&
552 !lstat(filespec, &sb)) {
553 /* Here is a non-overlapped pre-existing config file. */
554 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
555 ? FA_ALTNAME : FA_BACKUP;
557 fi->actions[i] = FA_CREATE;
559 /*@switchbreak@*/ break;
562 /* Mark added overlapped non-identical files as a conflict. */
563 if ((ts->ignoreSet & RPMPROB_FILTER_REPLACENEWFILES)
564 && filecmp(recs[otherPkgNum]->fmodes[otherFileNum],
565 recs[otherPkgNum]->fmd5s[otherFileNum],
566 recs[otherPkgNum]->flinks[otherFileNum],
571 const char * pkgNEVR = fiGetNEVR(fi);
572 const char * altNEVR = fiGetNEVR(recs[otherPkgNum]);
573 rpmProblemSetAppend(ts->probs, RPMPROB_NEW_FILE_CONFLICT,
578 pkgNEVR = _free(pkgNEVR);
579 altNEVR = _free(altNEVR);
582 /* Try to get the disk accounting correct even if a conflict. */
583 fixupSize = recs[otherPkgNum]->fsizes[otherFileNum];
585 if ((fi->fflags[i] & RPMFILE_CONFIG) && !lstat(filespec, &sb)) {
586 /* Here is an overlapped pre-existing config file. */
587 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
588 ? FA_ALTNAME : FA_SKIP;
590 fi->actions[i] = FA_CREATE;
592 } /*@switchbreak@*/ break;
594 if (otherPkgNum >= 0) {
595 /* Here is an overlapped added file we don't want to nuke. */
596 if (recs[otherPkgNum]->actions[otherFileNum] != FA_ERASE) {
597 /* On updates, don't remove files. */
598 fi->actions[i] = FA_SKIP;
599 /*@switchbreak@*/ break;
601 /* Here is an overlapped removed file: skip in previous. */
602 recs[otherPkgNum]->actions[otherFileNum] = FA_SKIP;
604 if (XFA_SKIPPING(fi->actions[i]))
605 /*@switchbreak@*/ break;
606 if (fi->fstates && fi->fstates[i] != RPMFILE_STATE_NORMAL)
607 /*@switchbreak@*/ break;
608 if (!(S_ISREG(fi->fmodes[i]) && (fi->fflags[i] & RPMFILE_CONFIG))) {
609 fi->actions[i] = FA_ERASE;
610 /*@switchbreak@*/ break;
613 /* Here is a pre-existing modified config file that needs saving. */
615 if (!mdfile(filespec, mdsum) && strcmp(fi->fmd5s[i], mdsum)) {
616 fi->actions[i] = FA_BACKUP;
617 /*@switchbreak@*/ break;
620 fi->actions[i] = FA_ERASE;
621 /*@switchbreak@*/ break;
625 uint_32 s = BLOCK_ROUND(fi->fsizes[i], ds->bsize);
627 switch (fi->actions[i]) {
633 /*@switchbreak@*/ break;
636 * FIXME: If two packages share a file (same md5sum), and
637 * that file is being replaced on disk, will ds->bneeded get
638 * decremented twice? Quite probably!
642 ds->bneeded -= BLOCK_ROUND(fi->replacedSizes[i], ds->bsize);
643 /*@switchbreak@*/ break;
648 /*@switchbreak@*/ break;
651 /*@switchbreak@*/ break;
654 ds->bneeded -= BLOCK_ROUND(fixupSize, ds->bsize);
657 filespec = _free(filespec);
661 * Ensure that current package is newer than installed package.
662 * @param ts transaction set
663 * @param p current transaction element
664 * @param h installed header
665 * @return 0 if not newer, 1 if okay
667 static int ensureOlder(rpmTransactionSet ts,
668 const transactionElement p, const Header h)
671 int_32 reqFlags = (RPMSENSE_LESS | RPMSENSE_EQUAL);
677 if (p == NULL || h == NULL)
680 t = alloca(strlen(p->NEVR) + (p->epoch != NULL ? strlen(p->epoch) : 0) + 1);
683 if (p->epoch != NULL) t = stpcpy( stpcpy(t, p->epoch), ":");
684 if (p->version != NULL) t = stpcpy(t, p->version);
686 if (p->release != NULL) t = stpcpy(t, p->release);
688 req = dsSingle(RPMTAG_REQUIRENAME, p->name, reqEVR, reqFlags);
689 rc = headerMatchesDepFlags(h, req);
692 /*@-branchstate@*/ /* FIX: p->key ??? */
694 const char * altNEVR = hGetNEVR(h, NULL);
695 rpmProblemSetAppend(ts->probs, RPMPROB_OLDPACKAGE,
700 altNEVR = _free(altNEVR);
711 static void skipFiles(const rpmTransactionSet ts, TFI_t fi)
712 /*@globals rpmGlobalMacroContext @*/
713 /*@modifies fi, rpmGlobalMacroContext @*/
715 int noDocs = (ts->transFlags & RPMTRANS_FLAG_NODOCS);
716 char ** netsharedPaths = NULL;
717 const char ** languages;
718 const char * dn, * bn;
719 int dnlen, bnlen, ix;
726 noDocs = rpmExpandNumeric("%{_excludedocs}");
728 { const char *tmpPath = rpmExpand("%{_netsharedpath}", NULL);
730 if (tmpPath && *tmpPath != '%')
731 netsharedPaths = splitString(tmpPath, strlen(tmpPath), ':');
733 tmpPath = _free(tmpPath);
736 s = rpmExpand("%{_install_langs}", NULL);
738 if (!(s && *s != '%'))
741 languages = (const char **) splitString(s, strlen(s), ':');
747 /* Compute directory refcount, skip directory if now empty. */
748 drc = alloca(fi->dc * sizeof(*drc));
749 memset(drc, 0, fi->dc * sizeof(*drc));
750 dff = alloca(fi->dc * sizeof(*dff));
751 memset(dff, 0, fi->dc * sizeof(*dff));
753 for (i = 0; i < fi->fc; i++) {
764 /* Don't bother with skipped files */
765 if (XFA_SKIPPING(fi->actions[i])) {
771 * Skip net shared paths.
772 * Net shared paths are not relative to the current root (though
773 * they do need to take package relocations into account).
775 for (nsp = netsharedPaths; nsp && *nsp; nsp++) {
780 if (strncmp(dn, *nsp, len))
781 /*@innercontinue@*/ continue;
782 /* Only directories or complete file paths can be net shared */
783 if (!(dn[len] == '/' || dn[len] == '\0'))
784 /*@innercontinue@*/ continue;
786 if (len < (dnlen + bnlen))
787 /*@innercontinue@*/ continue;
788 if (strncmp(dn, *nsp, dnlen))
789 /*@innercontinue@*/ continue;
790 if (strncmp(bn, (*nsp) + dnlen, bnlen))
791 /*@innercontinue@*/ continue;
793 /* Only directories or complete file paths can be net shared */
794 if (!((*nsp)[len] == '/' || (*nsp)[len] == '\0'))
795 /*@innercontinue@*/ continue;
798 /*@innerbreak@*/ break;
802 drc[ix]--; dff[ix] = 1;
803 fi->actions[i] = FA_SKIPNETSHARED;
808 * Skip i18n language specific files.
810 if (fi->flangs && languages && *fi->flangs[i]) {
811 const char **lang, *l, *le;
812 for (lang = languages; *lang != NULL; lang++) {
813 if (!strcmp(*lang, "all"))
814 /*@innerbreak@*/ break;
815 for (l = fi->flangs[i]; *l != '\0'; l = le) {
816 for (le = l; *le != '\0' && *le != '|'; le++)
818 if ((le-l) > 0 && !strncmp(*lang, l, (le-l)))
819 /*@innerbreak@*/ break;
820 if (*le == '|') le++; /* skip over | */
823 /*@innerbreak@*/ break;
826 drc[ix]--; dff[ix] = 1;
827 fi->actions[i] = FA_SKIPNSTATE;
833 * Skip documentation if requested.
835 if (noDocs && (fi->fflags[i] & RPMFILE_DOC)) {
836 drc[ix]--; dff[ix] = 1;
837 fi->actions[i] = FA_SKIPNSTATE;
842 /* Skip (now empty) directories that had skipped files. */
843 for (j = 0; j < fi->dc; j++) {
845 if (drc[j]) continue; /* dir still has files. */
846 if (!dff[j]) continue; /* dir was not emptied here. */
848 /* Find parent directory and basename. */
849 dn = fi->dnl[j]; dnlen = strlen(dn) - 1;
850 bn = dn + dnlen; bnlen = 0;
851 while (bn > dn && bn[-1] != '/') {
857 /* If explicitly included in the package, skip the directory. */
858 for (i = 0; i < fi->fc; i++) {
861 if (XFA_SKIPPING(fi->actions[i]))
862 /*@innercontinue@*/ continue;
863 if (whatis(fi->fmodes[i]) != XDIR)
864 /*@innercontinue@*/ continue;
865 dir = fi->dnl[fi->dil[i]];
866 if (strlen(dir) != dnlen)
867 /*@innercontinue@*/ continue;
868 if (strncmp(dir, dn, dnlen))
869 /*@innercontinue@*/ continue;
870 if (strlen(fi->bnl[i]) != bnlen)
871 /*@innercontinue@*/ continue;
872 if (strncmp(fi->bnl[i], bn, bnlen))
873 /*@innercontinue@*/ continue;
874 rpmMessage(RPMMESS_DEBUG, _("excluding directory %s\n"), dn);
875 fi->actions[i] = FA_SKIPNSTATE;
876 /*@innerbreak@*/ break;
880 if (netsharedPaths) freeSplitString(netsharedPaths);
881 #ifdef DYING /* XXX freeFi will deal with this later. */
882 fi->flangs = _free(fi->flangs);
884 if (languages) freeSplitString((char **)languages);
888 * Return next transaction element's file info.
889 * @param tei transaction element iterator
890 * @return nest transaction element file info, NULL on termination
892 /*@unused@*/ static inline
893 TFI_t teNextFi(teIterator tei)
898 if (teNextIterator(tei) != NULL && tei->ocsave != -1)
899 fi = tei->ts->flList + tei->ocsave;
900 /*@-compdef -onlytrans -usereleased@*/ /* FIX: ts->flList may be released */
902 /*@=compdef =onlytrans =usereleased@*/
905 #define NOTIFY(_ts, _al) if ((_ts)->notify) (void) (_ts)->notify _al
907 int rpmRunTransactions( rpmTransactionSet ts,
908 rpmCallbackFunction notify, rpmCallbackData notifyData,
909 rpmProblemSet okProbs, rpmProblemSet * newProbs,
910 rpmtransFlags transFlags, rpmprobFilterFlags ignoreSet)
914 int totalFileCount = 0;
916 struct diskspaceInfo * dip;
917 sharedFileInfo shared, sharedList;
920 alKey pkgKey, lastKey;
922 transactionElement p;
923 fingerPrintCache fpc;
931 /* FIXME: what if the same package is included in ts twice? */
933 ts->transFlags = transFlags;
934 if (ts->transFlags & RPMTRANS_FLAG_NOSCRIPTS)
935 ts->transFlags |= (_noTransScripts | _noTransTriggers);
936 if (ts->transFlags & RPMTRANS_FLAG_NOTRIGGERS)
937 ts->transFlags |= _noTransTriggers;
939 /* XXX MULTILIB is broken, as packages can and do execute /sbin/ldconfig. */
940 if (ts->transFlags & (RPMTRANS_FLAG_JUSTDB | RPMTRANS_FLAG_MULTILIB))
941 ts->transFlags |= (_noTransScripts | _noTransTriggers);
944 ts->notifyData = notifyData;
946 ts->probs = *newProbs = rpmProblemSetCreate();
947 *newProbs = rpmpsLink(ts->probs, "RunTransactions");
949 ts->ignoreSet = ignoreSet;
950 ts->currDir = _free(ts->currDir);
951 ts->currDir = currentDirectory();
953 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
954 ts->id = (int_32) time(NULL);
956 memset(psm, 0, sizeof(*psm));
958 psm->ts = rpmtsLink(ts, "tsRun");
961 /* Get available space on mounted file systems. */
962 if (!(ts->ignoreSet & RPMPROB_FILTER_DISKSPACE) &&
963 !rpmGetFilesystemList(&ts->filesystems, &ts->filesystemCount)) {
966 ts->di = _free(ts->di);
967 dip = ts->di = xcalloc((ts->filesystemCount + 1), sizeof(*ts->di));
969 for (i = 0; (i < ts->filesystemCount) && dip; i++) {
970 #if STATFS_IN_SYS_STATVFS
972 memset(&sfb, 0, sizeof(sfb));
973 if (statvfs(ts->filesystems[i], &sfb))
977 /* This platform has the 4-argument version of the statfs call. The last two
978 * should be the size of struct statfs and 0, respectively. The 0 is the
979 * filesystem type, and is always 0 when statfs is called on a mounted
980 * filesystem, as we're doing.
982 memset(&sfb, 0, sizeof(sfb));
983 if (statfs(ts->filesystems[i], &sfb, sizeof(sfb), 0))
985 memset(&sfb, 0, sizeof(sfb));
986 if (statfs(ts->filesystems[i], &sfb))
992 ts->di[i].bsize = sfb.f_bsize;
993 ts->di[i].bneeded = 0;
994 ts->di[i].ineeded = 0;
995 #ifdef STATFS_HAS_F_BAVAIL
996 ts->di[i].bavail = sfb.f_bavail;
998 /* FIXME: the statfs struct doesn't have a member to tell how many blocks are
999 * available for non-superusers. f_blocks - f_bfree is probably too big, but
1000 * it's about all we can do.
1002 ts->di[i].bavail = sfb.f_blocks - sfb.f_bfree;
1004 /* XXX Avoid FAT and other file systems that have not inodes. */
1005 ts->di[i].iavail = !(sfb.f_ffree == 0 && sfb.f_files == 0)
1008 xx = stat(ts->filesystems[i], &sb);
1009 ts->di[i].dev = sb.st_dev;
1013 if (dip) ts->di[i].bsize = 0;
1016 /* ===============================================
1017 * For packages being installed:
1018 * - verify package arch/os.
1019 * - verify package epoch:version-release is newer.
1021 * For packages being removed:
1024 /* The ordering doesn't matter here */
1025 tei = teInitIterator(ts);
1026 while ((p = teNext(tei, TR_ADDED)) != NULL) {
1027 rpmdbMatchIterator mi;
1029 /*@-branchstate@*/ /* FIX: p->key ??? */
1030 if (!archOkay(p->arch) && !(ts->ignoreSet & RPMPROB_FILTER_IGNOREARCH))
1031 rpmProblemSetAppend(ts->probs, RPMPROB_BADARCH,
1036 if (!osOkay(p->os) && !(ts->ignoreSet & RPMPROB_FILTER_IGNOREOS))
1037 rpmProblemSetAppend(ts->probs, RPMPROB_BADOS,
1043 if (!(ts->ignoreSet & RPMPROB_FILTER_OLDPACKAGE)) {
1045 mi = rpmtsInitIterator(ts, RPMTAG_NAME, p->name, 0);
1046 while ((h = rpmdbNextIterator(mi)) != NULL)
1047 xx = ensureOlder(ts, p, h);
1048 mi = rpmdbFreeIterator(mi);
1051 /* XXX multilib should not display "already installed" problems */
1052 if (!(ts->ignoreSet & RPMPROB_FILTER_REPLACEPKG)
1053 #ifdef DYING /* XXX MULTILIB multiLib from transactionElement */
1054 && !alGetMultiLib(ts->addedPackages, i)
1057 mi = rpmtsInitIterator(ts, RPMTAG_NAME, p->name, 0);
1058 xx = rpmdbSetIteratorRE(mi, RPMTAG_VERSION, RPMMIRE_DEFAULT,
1060 xx = rpmdbSetIteratorRE(mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT,
1063 while (rpmdbNextIterator(mi) != NULL) {
1064 rpmProblemSetAppend(ts->probs, RPMPROB_PKG_INSTALLED,
1068 /*@innerbreak@*/ break;
1070 mi = rpmdbFreeIterator(mi);
1073 /* Count no. of files (if any). */
1075 totalFileCount += p->fns->fc;
1078 tei = teFreeIterator(tei);
1081 /* FIXME: it seems a bit silly to read in all of these headers twice */
1082 /* The ordering doesn't matter here */
1083 if (ts->numRemovedPackages > 0) {
1084 rpmdbMatchIterator mi;
1088 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES, NULL, 0);
1089 xx = rpmdbAppendIterator(mi, ts->removedPackages, ts->numRemovedPackages);
1090 while ((h = rpmdbNextIterator(mi)) != NULL) {
1091 if (headerGetEntry(h, RPMTAG_BASENAMES, NULL, NULL, &fileCount))
1092 totalFileCount += fileCount;
1094 mi = rpmdbFreeIterator(mi);
1097 /* The ordering doesn't matter here */
1098 tei = teInitIterator(ts);
1099 while ((p = teNext(tei, TR_REMOVED)) != NULL) {
1105 if (fns->bnl == NULL)
1106 continue; /* XXX can't happen */
1107 if (fns->dnl == NULL)
1108 continue; /* XXX can't happen */
1109 if (fns->dil == NULL)
1110 continue; /* XXX can't happen */
1111 totalFileCount += fns->fc;
1113 tei = teFreeIterator(tei);
1116 /* ===============================================
1117 * Initialize transaction element file info for package:
1119 ts->flEntries = ts->numAddedPackages + ts->numRemovedPackages;
1120 ts->flList = xcalloc(ts->flEntries, sizeof(*ts->flList));
1123 * FIXME?: we'd be better off assembling one very large file list and
1124 * calling fpLookupList only once. I'm not sure that the speedup is
1125 * worth the trouble though.
1127 tei = teInitIterator(ts);
1128 while ((fi = teNextFi(tei)) != NULL) {
1131 fi->magic = TFIMAGIC;
1133 fi->type = ts->order[oc].type;
1140 /*@i@*/ fi->h = headerLink(ts->order[oc].h, "xfer to fi->h");
1141 ts->order[oc].h = headerFree(ts->order[oc].h, "xfer to fi->h");
1143 #ifdef DYING /* XXX MULTILIB multiLib from transactionElement */
1144 fi->multiLib = alGetMultiLib(ts->addedPackages, i);
1146 fi->multiLib = ts->order[oc].multiLib;
1149 /*@i@*/ fi->key = ts->order[oc].key;
1150 fi->relocs = ts->order[oc].relocs;
1151 /*@i@*/ fi->fd = ts->order[oc].fd;
1153 /* XXX availablePackage can be dumped here XXX */
1155 /* XXX header arg unused. */
1156 loadFi(ts, fi, fi->h, keep_header);
1161 /* Skip netshared paths, not our i18n files, and excluded docs */
1163 /*@switchbreak@*/ break;
1165 fi->record = ts->order[oc].u.removed.dboffset;
1166 /* Retrieve erased package header from the database. */
1167 { rpmdbMatchIterator mi;
1169 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
1170 &fi->record, sizeof(fi->record));
1171 if ((fi->h = rpmdbNextIterator(mi)) != NULL)
1172 fi->h = headerLink(fi->h, "TR_REMOVED loadFi");
1173 mi = rpmdbFreeIterator(mi);
1175 if (fi->h == NULL) {
1179 /* XXX header arg unused. */
1180 loadFi(ts, fi, fi->h, 0);
1181 /*@switchbreak@*/ break;
1186 fi->fps = xmalloc(fi->fc * sizeof(*fi->fps));
1188 tei = teFreeIterator(tei);
1190 if (!ts->chrootDone) {
1192 /*@-superuser -noeffect @*/
1193 xx = chroot(ts->rootDir);
1194 /*@=superuser =noeffect @*/
1196 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 1;
1199 chroot_prefix = ts->rootDir;
1204 ts->ht = htCreate(totalFileCount * 2, 0, 0, fpHashFunction, fpEqual);
1205 fpc = fpCacheCreate(totalFileCount);
1207 /* ===============================================
1208 * Add fingerprint for each file not skipped.
1210 tei = teInitIterator(ts);
1211 while ((fi = teNextFi(tei)) != NULL) {
1212 fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fi->fc, fi->fps);
1213 for (i = 0; i < fi->fc; i++) {
1214 if (XFA_SKIPPING(fi->actions[i]))
1215 /*@innercontinue@*/ continue;
1216 /*@-dependenttrans@*/
1217 htAddEntry(ts->ht, fi->fps + i, fi);
1218 /*@=dependenttrans@*/
1221 tei = teFreeIterator(tei);
1223 /*@-noeffectuncon @*/ /* FIX: check rc */
1224 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_START, 6, ts->flEntries,
1225 NULL, ts->notifyData));
1226 /*@=noeffectuncon@*/
1228 /* ===============================================
1229 * Compute file disposition for each package in transaction set.
1231 tei = teInitIterator(ts);
1232 while ((fi = teNextFi(tei)) != NULL) {
1233 dbiIndexSet * matches;
1236 /*@-noeffectuncon @*/ /* FIX: check rc */
1237 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_PROGRESS, (fi - ts->flList),
1238 ts->flEntries, NULL, ts->notifyData));
1239 /*@=noeffectuncon@*/
1241 if (fi->fc == 0) continue;
1243 /* Extract file info for all files in this package from the database. */
1244 matches = xcalloc(fi->fc, sizeof(*matches));
1245 if (rpmdbFindFpList(ts->rpmdb, fi->fps, matches, fi->fc)) {
1246 psm->ts = rpmtsUnlink(ts, "tsRun (rpmFindFpList fail)");
1247 return 1; /* XXX WTFO? */
1251 for (i = 0; i < fi->fc; i++)
1252 numShared += dbiIndexSetCount(matches[i]);
1254 /* Build sorted file info list for this package. */
1255 shared = sharedList = xcalloc((numShared + 1), sizeof(*sharedList));
1256 for (i = 0; i < fi->fc; i++) {
1258 * Take care not to mark files as replaced in packages that will
1259 * have been removed before we will get here.
1261 for (j = 0; j < dbiIndexSetCount(matches[i]); j++) {
1263 ro = dbiIndexRecordOffset(matches[i], j);
1265 for (k = 0; ro != knownBad && k < ts->orderCount; k++) {
1266 switch (ts->order[k].type) {
1268 if (ts->order[k].u.removed.dboffset == ro)
1270 /*@switchbreak@*/ break;
1272 /*@switchbreak@*/ break;
1276 shared->pkgFileNum = i;
1277 shared->otherPkg = dbiIndexRecordOffset(matches[i], j);
1278 shared->otherFileNum = dbiIndexRecordFileNumber(matches[i], j);
1279 shared->isRemoved = (knownBad == ro);
1282 matches[i] = dbiFreeIndexSet(matches[i]);
1284 numShared = shared - sharedList;
1285 shared->otherPkg = -1;
1286 matches = _free(matches);
1288 /* Sort file info by other package index (otherPkg) */
1289 qsort(sharedList, numShared, sizeof(*shared), sharedCmp);
1291 /* For all files from this package that are in the database ... */
1292 for (i = 0; i < numShared; i = nexti) {
1295 shared = sharedList + i;
1297 /* Find the end of the files in the other package. */
1298 for (nexti = i + 1; nexti < numShared; nexti++) {
1299 if (sharedList[nexti].otherPkg != shared->otherPkg)
1300 /*@innerbreak@*/ break;
1303 /* Is this file from a package being removed? */
1305 for (j = 0; j < ts->numRemovedPackages; j++) {
1306 if (ts->removedPackages[j] != shared->otherPkg)
1307 /*@innercontinue@*/ continue;
1309 /*@innerbreak@*/ break;
1312 /* Determine the fate of each file. */
1315 xx = handleInstInstalledFiles(ts, fi, shared, nexti - i,
1316 !(beingRemoved || (ts->ignoreSet & RPMPROB_FILTER_REPLACEOLDFILES)));
1317 /*@switchbreak@*/ break;
1320 xx = handleRmvdInstalledFiles(ts, fi, shared, nexti - i);
1321 /*@switchbreak@*/ break;
1327 /* Update disk space needs on each partition for this package. */
1328 handleOverlappedFiles(ts, fi);
1330 /* Check added package has sufficient space on each partition used. */
1333 if (!(ts->di && fi->fc))
1334 /*@switchbreak@*/ break;
1335 for (i = 0; i < ts->filesystemCount; i++) {
1339 /* XXX Avoid FAT and other file systems that have not inodes. */
1340 if (dip->iavail <= 0)
1341 /*@innercontinue@*/ continue;
1343 if (adj_fs_blocks(dip->bneeded) > dip->bavail) {
1344 const char * pkgNEVR = fiGetNEVR(fi);
1345 rpmProblemSetAppend(ts->probs, RPMPROB_DISKSPACE,
1347 ts->filesystems[i], NULL, NULL,
1348 (adj_fs_blocks(dip->bneeded) - dip->bavail) * dip->bsize);
1349 pkgNEVR = _free(pkgNEVR);
1352 if (adj_fs_blocks(dip->ineeded) > dip->iavail) {
1353 const char * pkgNEVR = fiGetNEVR(fi);
1354 rpmProblemSetAppend(ts->probs, RPMPROB_DISKNODES,
1356 ts->filesystems[i], NULL, NULL,
1357 (adj_fs_blocks(dip->ineeded) - dip->iavail));
1358 pkgNEVR = _free(pkgNEVR);
1361 /*@switchbreak@*/ break;
1363 /*@switchbreak@*/ break;
1366 tei = teFreeIterator(tei);
1368 if (ts->chrootDone) {
1369 /*@-superuser -noeffect @*/
1371 /*@=superuser =noeffect @*/
1373 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
1375 chroot_prefix = NULL;
1377 xx = chdir(ts->currDir);
1380 /*@-noeffectuncon @*/ /* FIX: check rc */
1381 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_STOP, 6, ts->flEntries,
1382 NULL, ts->notifyData));
1383 /*@=noeffectuncon @*/
1385 /* ===============================================
1386 * Free unused memory as soon as possible.
1389 tei = teInitIterator(ts);
1390 while ((fi = teNextFi(tei)) != NULL) {
1393 fi->fps = _free(fi->fps);
1395 tei = teFreeIterator(tei);
1401 /* ===============================================
1402 * If unfiltered problems exist, free memory and return.
1404 if ((ts->transFlags & RPMTRANS_FLAG_BUILD_PROBS)
1405 || (ts->probs->numProblems &&
1406 (okProbs != NULL || rpmProblemSetTrim(ts->probs, okProbs)))
1409 ts->flList = freeFl(ts, ts->flList);
1411 if (psm->ts != NULL)
1412 psm->ts = rpmtsUnlink(psm->ts, "tsRun (problems)");
1413 /*@-nullstate@*/ /* FIX: ts->flList may be NULL */
1414 return ts->orderCount;
1418 /* ===============================================
1419 * Save removed files before erasing.
1421 if (ts->transFlags & (RPMTRANS_FLAG_DIRSTASH | RPMTRANS_FLAG_REPACKAGE)) {
1422 tei = teInitIterator(ts);
1423 while ((fi = teNextFi(tei)) != NULL) {
1426 /*@switchbreak@*/ break;
1428 if (ts->transFlags & RPMTRANS_FLAG_REPACKAGE) {
1429 psm->fi = rpmfiLink(fi, "tsRepackage");
1430 xx = psmStage(psm, PSM_PKGSAVE);
1431 (void) rpmfiUnlink(fi, "tsRepackage");
1434 /*@switchbreak@*/ break;
1437 tei = teFreeIterator(tei);
1440 /* ===============================================
1441 * Install and remove packages.
1444 lastKey = (alKey)-2; /* erased packages have -1 */
1445 tei = teInitIterator(ts);
1446 /*@-branchstate@*/ /* FIX: fi reload needs work */
1447 while ((fi = teNextFi(tei)) != NULL) {
1453 psm->fi = rpmfiLink(fi, "tsInstall");
1457 pkgKey = ts->order[oc].u.addedKey;
1459 rpmMessage(RPMMESS_DEBUG, "========== +++ %s-%s-%s\n",
1460 fi->name, fi->version, fi->release);
1461 h = (fi->h ? headerLink(fi->h, "TR_ADDED install") : NULL);
1463 if (fi->fd == NULL) {
1464 /*@-noeffectuncon @*/ /* FIX: ??? */
1465 fi->fd = ts->notify(fi->h, RPMCALLBACK_INST_OPEN_FILE, 0, 0,
1466 fi->key, ts->notifyData);
1467 /*@=noeffectuncon @*/
1468 if (fi->fd != NULL) {
1471 h = headerFree(h, "TR_ADDED install");
1473 /*@-mustmod@*/ /* LCL: segfault */
1474 rpmrc = rpmReadPackageFile(ts, fi->fd,
1475 "rpmRunTransactions", &h);
1478 if (!(rpmrc == RPMRC_OK || rpmrc == RPMRC_BADSIZE)) {
1479 /*@-noeffectuncon @*/ /* FIX: check rc */
1480 (void) ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE,
1482 fi->key, ts->notifyData);
1483 /*@=noeffectuncon @*/
1486 } else if (fi->h != NULL) {
1487 Header foo = relocateFileList(ts, fi, h, NULL);
1488 h = headerFree(h, "TR_ADDED read free");
1489 h = headerLink(foo, "TR_ADDED relocate xfer");
1490 foo = headerFree(foo, "TR_ADDED relocate");
1492 if (fi->fd != NULL) gotfd = 1;
1497 if (fi->fd != NULL) {
1498 Header hsave = NULL;
1501 hsave = headerLink(fi->h, "TR_ADDED fi->h hsave");
1502 fi->h = headerFree(fi->h, "TR_ADDED fi->h free");
1503 fi->h = headerLink(h, "TR_ADDED fi->h link");
1505 char * fstates = fi->fstates;
1506 fileAction * actions = fi->actions;
1507 uint_32 multiLib = fi->multiLib;
1508 const void * key = fi->key;
1509 rpmRelocation * relocs = fi->relocs;
1519 fi->magic = TFIMAGIC;
1520 fi->type = ts->order[oc].type;
1522 loadFi(ts, fi, h, 1);
1523 fi->fstates = _free(fi->fstates);
1524 fi->fstates = fstates;
1525 fi->actions = _free(fi->actions);
1526 fi->actions = actions;
1527 fi->multiLib = multiLib;
1529 fi->relocs = relocs;
1531 /*@i@*/ fi->fd = fd;
1536 ts->transFlags |= RPMTRANS_FLAG_MULTILIB;
1538 if (psmStage(psm, PSM_PKGINSTALL)) {
1542 fi->h = headerFree(fi->h, "TR_ADDED fi->h free");
1544 fi->h = headerLink(hsave, "TR_ADDED fi->h restore");
1545 hsave = headerFree(hsave, "TR_ADDED hsave free");
1552 h = headerFree(h, "TR_ADDED h free");
1555 /*@-noeffectuncon @*/ /* FIX: check rc */
1556 (void)ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE, 0, 0,
1557 fi->key, ts->notifyData);
1558 /*@=noeffectuncon @*/
1561 fi->h = headerFree(fi->h, "TR_ADDED fini");
1563 /*@switchbreak@*/ break;
1565 rpmMessage(RPMMESS_DEBUG, "========== --- %s-%s-%s\n",
1566 fi->name, fi->version, fi->release);
1568 /* If install failed, then we shouldn't erase. */
1569 if (ts->order[oc].u.removed.dependsOnKey != lastKey) {
1570 if (psmStage(psm, PSM_PKGERASE))
1573 fi->h = headerFree(fi->h, "TR_REMOVED fini");
1575 /*@switchbreak@*/ break;
1577 xx = rpmdbSync(ts->rpmdb);
1578 (void) rpmfiUnlink(fi, "tsInstall");
1582 tei = teFreeIterator(tei);
1584 ts->flList = freeFl(ts, ts->flList);
1587 psm->ts = rpmtsUnlink(psm->ts, "tsRun");
1589 /*@-nullstate@*/ /* FIX: ts->flList may be NULL */