2 * \file lib/transaction.c
8 #include <rpmmacro.h> /* XXX for rpmExpand */
19 #include "legacy.h" /* XXX domd5 */
20 #include "misc.h" /* XXX stripTrailingChar, splitString, currentDirectory */
22 /* XXX FIXME: merge with existing (broken?) tests in system.h */
23 /* portability fiddles */
24 #if STATFS_IN_SYS_STATVFS
26 # include <sys/statvfs.h>
27 #if defined(__LCLINT__)
28 /*@-declundef -exportheader -protoparammatch @*/ /* LCL: missing annotation */
29 extern int statvfs (const char * file, /*@out@*/ struct statvfs * buf)
30 /*@globals fileSystem @*/
31 /*@modifies *buf, fileSystem @*/;
32 /*@=declundef =exportheader =protoparammatch @*/
36 # if STATFS_IN_SYS_VFS
39 # if STATFS_IN_SYS_MOUNT
40 # include <sys/mount.h>
42 # if STATFS_IN_SYS_STATFS
43 # include <sys/statfs.h>
51 /*@access FD_t @*/ /* XXX compared with NULL */
52 /*@access Header @*/ /* XXX compared with NULL */
53 /*@access rpmProblemSet @*/ /* XXX need rpmProblemSetOK() */
54 /*@access dbiIndexSet @*/
64 /*@access teIterator @*/
65 /*@access rpmTransactionSet @*/
69 struct diskspaceInfo {
70 dev_t dev; /*!< File system device number. */
71 signed long bneeded; /*!< No. of blocks needed. */
72 signed long ineeded; /*!< No. of inodes needed. */
73 int bsize; /*!< File system block size. */
74 signed long bavail; /*!< No. of blocks available. */
75 signed long iavail; /*!< No. of inodes available. */
79 * Adjust for root only reserved space. On linux e2fs, this is 5%.
81 #define adj_fs_blocks(_nb) (((_nb) * 21) / 20)
83 /* argon thought a shift optimization here was a waste of time... he's
85 #define BLOCK_ROUND(size, block) (((size) + (block) - 1) / (block))
87 void rpmtransSetScriptFd(rpmTransactionSet ts, FD_t fd)
89 ts->scriptFd = (fd ? fdLink(fd, "rpmtransSetScriptFd") : NULL);
92 int rpmtransGetKeys(const rpmTransactionSet ts, fnpyKey ** ep, int * nep)
96 if (nep) *nep = ts->orderCount;
98 teIterator pi; transactionElement p;
101 *ep = e = xmalloc(ts->orderCount * sizeof(*e));
102 pi = teInitIterator(ts);
103 while ((p = teNextIterator(pi)) != NULL) {
104 switch (teGetType(p)) {
106 /*@-dependenttrans@*/
108 /*@=dependenttrans@*/
109 /*@switchbreak@*/ break;
113 /*@switchbreak@*/ break;
117 pi = teFreeIterator(pi);
124 static int archOkay(/*@null@*/ const char * pkgArch)
127 if (pkgArch == NULL) return 0;
128 return (rpmMachineScore(RPM_MACHTABLE_INSTARCH, pkgArch) ? 1 : 0);
133 static int osOkay(/*@null@*/ const char * pkgOs)
136 if (pkgOs == NULL) return 0;
137 return (rpmMachineScore(RPM_MACHTABLE_INSTOS, pkgOs) ? 1 : 0);
142 static int sharedCmp(const void * one, const void * two)
145 sharedFileInfo a = (sharedFileInfo) one;
146 sharedFileInfo b = (sharedFileInfo) two;
148 if (a->otherPkg < b->otherPkg)
150 else if (a->otherPkg > b->otherPkg)
158 static fileAction decideFileFate(const rpmTransactionSet ts,
159 const TFI_t ofi, TFI_t nfi)
160 /*@globals fileSystem @*/
161 /*@modifies nfi, fileSystem @*/
163 const char * fn = tfiGetFN(nfi);
164 int newFlags = tfiGetFFlags(nfi);
166 fileTypes dbWhat, newWhat, diskWhat;
168 int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
170 if (lstat(fn, &sb)) {
172 * The file doesn't exist on the disk. Create it unless the new
173 * package has marked it as missingok, or allfiles is requested.
175 if (!(ts->transFlags & RPMTRANS_FLAG_ALLFILES)
176 && (newFlags & RPMFILE_MISSINGOK))
178 rpmMessage(RPMMESS_DEBUG, _("%s skipped due to missingok flag\n"),
186 diskWhat = whatis(sb.st_mode);
187 dbWhat = whatis(ofi->fmodes[ofi->i]);
188 newWhat = whatis(nfi->fmodes[nfi->i]);
191 * RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
192 * them in older packages as well.
197 if (diskWhat != newWhat)
199 else if (newWhat != dbWhat && diskWhat != dbWhat)
201 else if (dbWhat != newWhat)
203 else if (dbWhat != LINK && dbWhat != REG)
207 * This order matters - we'd prefer to CREATE the file if at all
208 * possible in case something else (like the timestamp) has changed.
211 if (ofi->md5s != NULL && nfi->md5s != NULL) {
212 const unsigned char * omd5 = ofi->md5s + (16 * ofi->i);
213 const unsigned char * nmd5 = nfi->md5s + (16 * nfi->i);
214 if (domd5(fn, buffer, 0))
215 return FA_CREATE; /* assume file has been removed */
216 if (!memcmp(omd5, buffer, 16))
217 return FA_CREATE; /* unmodified config file, replace. */
218 if (!memcmp(omd5, nmd5, 16))
219 return FA_SKIP; /* identical file, don't bother. */
221 const char * omd5 = ofi->fmd5s[ofi->i];
222 const char * nmd5 = nfi->fmd5s[nfi->i];
223 if (domd5(fn, buffer, 1))
224 return FA_CREATE; /* assume file has been removed */
225 if (!strcmp(omd5, buffer))
226 return FA_CREATE; /* unmodified config file, replace. */
227 if (!strcmp(omd5, nmd5))
228 return FA_SKIP; /* identical file, don't bother. */
230 } else /* dbWhat == LINK */ {
231 memset(buffer, 0, sizeof(buffer));
232 if (readlink(fn, buffer, sizeof(buffer) - 1) == -1)
233 return FA_CREATE; /* assume file has been removed */
234 if (!strcmp(ofi->flinks[ofi->i], buffer))
235 return FA_CREATE; /* unmodified config file, replace. */
236 if (!strcmp(ofi->flinks[ofi->i], nfi->flinks[nfi->i]))
237 return FA_SKIP; /* identical file, don't bother. */
241 * The config file on the disk has been modified, but
242 * the ones in the two packages are different. It would
243 * be nice if RPM was smart enough to at least try and
244 * merge the difference ala CVS, but...
251 static int filecmp(TFI_t afi, TFI_t bfi)
254 fileTypes awhat = whatis(afi->fmodes[afi->i]);
255 fileTypes bwhat = whatis(bfi->fmodes[bfi->i]);
257 if (awhat != bwhat) return 1;
260 const char * alink = afi->flinks[afi->i];
261 const char * blink = bfi->flinks[bfi->i];
262 return strcmp(alink, blink);
263 } else if (awhat == REG) {
264 if (afi->md5s != NULL && bfi->md5s != NULL) {
265 const unsigned char * amd5 = afi->md5s + (16 * afi->i);
266 const unsigned char * bmd5 = bfi->md5s + (16 * bfi->i);
267 return memcmp(amd5, bmd5, 16);
269 const char * amd5 = afi->fmd5s[afi->i];
270 const char * bmd5 = bfi->fmd5s[bfi->i];
271 return strcmp(amd5, bmd5);
280 /* XXX only ts->{probs,rpmdb} modified */
281 static int handleInstInstalledFiles(const rpmTransactionSet ts,
282 transactionElement p, TFI_t fi,
283 sharedFileInfo shared,
284 int sharedCount, int reportConflicts)
285 /*@globals fileSystem @*/
286 /*@modifies ts, fi, fileSystem @*/
288 const char * altNEVR = NULL;
289 TFI_t otherFi = NULL;
293 { rpmdbMatchIterator mi;
297 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
298 &shared->otherPkg, sizeof(shared->otherPkg));
299 while ((h = rpmdbNextIterator(mi)) != NULL) {
300 altNEVR = hGetNEVR(h, NULL);
301 otherFi = fiNew(ts, NULL, h, RPMTAG_BASENAMES, scareMem);
304 mi = rpmdbFreeIterator(mi);
310 fi->replaced = xcalloc(sharedCount, sizeof(*fi->replaced));
312 for (i = 0; i < sharedCount; i++, shared++) {
313 int otherFileNum, fileNum;
315 otherFileNum = shared->otherFileNum;
316 (void) tfiSetFX(otherFi, otherFileNum);
318 fileNum = shared->pkgFileNum;
319 (void) tfiSetFX(fi, fileNum);
322 /* XXX another tedious segfault, assume file state normal. */
323 if (otherStates && otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
327 if (XFA_SKIPPING(fi->actions[fileNum]))
330 if (filecmp(otherFi, fi)) {
331 if (reportConflicts) {
332 rpmProblemSetAppend(ts->probs, RPMPROB_FILE_CONFLICT,
333 teGetNEVR(p), teGetKey(p),
334 tfiGetDN(fi), tfiGetBN(fi),
338 if (!(tfiGetFFlags(otherFi) | tfiGetFFlags(fi)) & RPMFILE_CONFIG) {
339 /*@-assignexpose@*/ /* FIX: p->replaced, not fi */
340 if (!shared->isRemoved)
341 fi->replaced[numReplaced++] = *shared;
346 if ((tfiGetFFlags(otherFi) | tfiGetFFlags(fi)) & RPMFILE_CONFIG) {
348 action = decideFileFate(ts, otherFi, fi);
349 fi->actions[fileNum] = action;
351 fi->replacedSizes[fileNum] = otherFi->fsizes[otherFi->i];
355 altNEVR = _free(altNEVR);
356 otherFi = fiFree(otherFi, 1);
358 fi->replaced = xrealloc(fi->replaced, /* XXX memory leak */
359 sizeof(*fi->replaced) * (numReplaced + 1));
360 fi->replaced[numReplaced].otherPkg = 0;
367 /* XXX only ts->rpmdb modified */
368 static int handleRmvdInstalledFiles(const rpmTransactionSet ts, TFI_t fi,
369 sharedFileInfo shared, int sharedCount)
370 /*@globals fileSystem @*/
371 /*@modifies ts, fi, fileSystem @*/
375 const char * otherStates;
378 rpmdbMatchIterator mi;
380 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
381 &shared->otherPkg, sizeof(shared->otherPkg));
382 h = rpmdbNextIterator(mi);
384 mi = rpmdbFreeIterator(mi);
388 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
390 for (i = 0; i < sharedCount; i++, shared++) {
391 int otherFileNum, fileNum;
392 otherFileNum = shared->otherFileNum;
393 fileNum = shared->pkgFileNum;
395 if (otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
398 fi->actions[fileNum] = FA_SKIP;
401 mi = rpmdbFreeIterator(mi);
406 #define ISROOT(_d) (((_d)[0] == '/' && (_d)[1] == '\0') ? "" : (_d))
409 static int _fps_debug = 0;
411 static int fpsCompare (const void * one, const void * two)
413 const struct fingerPrint_s * a = (const struct fingerPrint_s *)one;
414 const struct fingerPrint_s * b = (const struct fingerPrint_s *)two;
415 int adnlen = strlen(a->entry->dirName);
416 int asnlen = (a->subDir ? strlen(a->subDir) : 0);
417 int abnlen = strlen(a->baseName);
418 int bdnlen = strlen(b->entry->dirName);
419 int bsnlen = (b->subDir ? strlen(b->subDir) : 0);
420 int bbnlen = strlen(b->baseName);
421 char * afn, * bfn, * t;
424 if (adnlen == 1 && asnlen != 0) adnlen = 0;
425 if (bdnlen == 1 && bsnlen != 0) bdnlen = 0;
427 afn = t = alloca(adnlen+asnlen+abnlen+2);
428 if (adnlen) t = stpcpy(t, a->entry->dirName);
430 if (a->subDir && asnlen) t = stpcpy(t, a->subDir);
431 if (abnlen) t = stpcpy(t, a->baseName);
432 if (afn[0] == '/' && afn[1] == '/') afn++;
434 bfn = t = alloca(bdnlen+bsnlen+bbnlen+2);
435 if (bdnlen) t = stpcpy(t, b->entry->dirName);
437 if (b->subDir && bsnlen) t = stpcpy(t, b->subDir);
438 if (bbnlen) t = stpcpy(t, b->baseName);
439 if (bfn[0] == '/' && bfn[1] == '/') bfn++;
441 rc = strcmp(afn, bfn);
444 fprintf(stderr, "\trc(%d) = strcmp(\"%s\", \"%s\")\n", rc, afn, bfn);
449 fprintf(stderr, "\t%s/%s%s\trc %d\n",
450 ISROOT(b->entry->dirName),
451 (b->subDir ? b->subDir : ""),
461 static int _linear_fps_search = 0;
463 static int findFps(const struct fingerPrint_s * fiFps,
464 const struct fingerPrint_s * otherFps,
472 fprintf(stderr, "==> %s/%s%s\n",
473 ISROOT(fiFps->entry->dirName),
474 (fiFps->subDir ? fiFps->subDir : ""),
478 if (_linear_fps_search) {
481 for (otherFileNum = 0; otherFileNum < otherFc; otherFileNum++, otherFps++) {
485 fprintf(stderr, "\t%4d %s/%s%s\n", otherFileNum,
486 ISROOT(otherFps->entry->dirName),
487 (otherFps->subDir ? otherFps->subDir : ""),
491 /* If the addresses are the same, so are the values. */
492 if (fiFps == otherFps)
495 /* Otherwise, compare fingerprints by value. */
496 /*@-nullpass@*/ /* LCL: looks good to me */
497 if (FP_EQUAL((*fiFps), (*otherFps)))
502 if (otherFileNum == otherFc) {
505 fprintf(stderr, "*** NULL %s/%s%s\n",
506 ISROOT(fiFps->entry->dirName),
507 (fiFps->subDir ? fiFps->subDir : ""),
516 const struct fingerPrint_s * bingoFps;
518 bingoFps = bsearch(fiFps, otherFps, otherFc, sizeof(*otherFps), fpsCompare);
519 if (bingoFps == NULL) {
521 fprintf(stderr, "*** NULL %s/%s%s\n",
522 ISROOT(fiFps->entry->dirName),
523 (fiFps->subDir ? fiFps->subDir : ""),
529 /* If the addresses are the same, so are the values. */
530 /*@-nullpass@*/ /* LCL: looks good to me */
531 if (!(fiFps == bingoFps || FP_EQUAL((*fiFps), (*bingoFps)))) {
533 fprintf(stderr, "*** BAD %s/%s%s\n",
534 ISROOT(bingoFps->entry->dirName),
535 (bingoFps->subDir ? bingoFps->subDir : ""),
541 otherFileNum = (bingoFps != NULL ? (bingoFps - otherFps) : 0);
549 * Update disk space needs on each partition for this package.
551 /* XXX only ts->{probs,di} modified */
552 static void handleOverlappedFiles(const rpmTransactionSet ts,
553 const transactionElement p, TFI_t fi)
554 /*@globals fileSystem @*/
555 /*@modifies ts, fi, fileSystem @*/
557 struct diskspaceInfo * ds = NULL;
558 uint_32 fixupSize = 0;
563 if (fi != NULL) /* XXX lclint */
564 while ((i = tfiNext(fi)) >= 0) {
565 struct fingerPrint_s * fiFps;
566 int otherPkgNum, otherFileNum;
571 if (XFA_SKIPPING(fi->actions[i]))
579 while (ds->bsize && ds->dev != fi->fps[i].entry->dev) ds++;
580 if (!ds->bsize) ds = NULL;
585 * Retrieve all records that apply to this file. Note that the
586 * file info records were built in the same order as the packages
587 * will be installed and removed so the records for an overlapped
588 * files will be sorted in exactly the same order.
590 (void) htGetEntry(ts->ht, fiFps,
591 (const void ***) &recs, &numRecs, NULL);
594 * If this package is being added, look only at other packages
595 * being added -- removed packages dance to a different tune.
596 * If both this and the other package are being added, overlapped
597 * files must be identical (or marked as a conflict). The
598 * disposition of already installed config files leads to
599 * a small amount of extra complexity.
601 * If this package is being removed, then there are two cases that
602 * need to be worried about:
603 * If the other package is being added, then skip any overlapped files
604 * so that this package removal doesn't nuke the overlapped files
605 * that were just installed.
606 * If both this and the other package are being removed, then each
607 * file removal from preceding packages needs to be skipped so that
608 * the file removal occurs only on the last occurence of an overlapped
609 * file in the transaction set.
613 /* Locate this overlapped file in the set of added/removed packages. */
614 for (j = 0; j < numRecs && recs[j] != fi; j++)
617 /* Find what the previous disposition of this file was. */
618 otherFileNum = -1; /* keep gcc quiet */
620 for (otherPkgNum = j - 1; otherPkgNum >= 0; otherPkgNum--) {
621 struct fingerPrint_s * otherFps;
624 otherFi = recs[otherPkgNum];
626 /* Added packages need only look at other added packages. */
627 if (teGetType(p) == TR_ADDED && teGetType(otherFi->te) != TR_ADDED)
628 /*@innercontinue@*/ continue;
630 otherFps = otherFi->fps;
631 otherFc = tfiGetFC(otherFi);
633 otherFileNum = findFps(fiFps, otherFps, otherFc);
634 (void) tfiSetFX(otherFi, otherFileNum);
636 /* XXX is this test still necessary? */
637 if (otherFi->actions[otherFileNum] != FA_UNKNOWN)
638 /*@innerbreak@*/ break;
641 switch (teGetType(p)) {
644 if (otherPkgNum < 0) {
645 /* XXX is this test still necessary? */
646 if (fi->actions[i] != FA_UNKNOWN)
647 /*@switchbreak@*/ break;
648 if ((tfiGetFFlags(fi) & RPMFILE_CONFIG) &&
650 /* Here is a non-overlapped pre-existing config file. */
651 fi->actions[i] = (tfiGetFFlags(fi) & RPMFILE_NOREPLACE)
652 ? FA_ALTNAME : FA_BACKUP;
654 fi->actions[i] = FA_CREATE;
656 /*@switchbreak@*/ break;
659 assert(otherFi != NULL);
660 /* Mark added overlapped non-identical files as a conflict. */
661 if ((ts->ignoreSet & RPMPROB_FILTER_REPLACENEWFILES)
662 && filecmp(otherFi, fi))
664 rpmProblemSetAppend(ts->probs, RPMPROB_NEW_FILE_CONFLICT,
665 teGetNEVR(p), teGetKey(p),
667 teGetNEVR(otherFi->te),
671 /* Try to get the disk accounting correct even if a conflict. */
672 fixupSize = otherFi->fsizes[otherFileNum];
674 if ((tfiGetFFlags(fi) & RPMFILE_CONFIG) && !lstat(fn, &sb)) {
675 /* Here is an overlapped pre-existing config file. */
676 fi->actions[i] = (tfiGetFFlags(fi) & RPMFILE_NOREPLACE)
677 ? FA_ALTNAME : FA_SKIP;
679 fi->actions[i] = FA_CREATE;
681 } /*@switchbreak@*/ break;
684 if (otherPkgNum >= 0) {
685 assert(otherFi != NULL);
686 /* Here is an overlapped added file we don't want to nuke. */
687 if (otherFi->actions[otherFileNum] != FA_ERASE) {
688 /* On updates, don't remove files. */
689 fi->actions[i] = FA_SKIP;
690 /*@switchbreak@*/ break;
692 /* Here is an overlapped removed file: skip in previous. */
693 otherFi->actions[otherFileNum] = FA_SKIP;
695 if (XFA_SKIPPING(fi->actions[i]))
696 /*@switchbreak@*/ break;
697 if (fi->fstates && fi->fstates[i] != RPMFILE_STATE_NORMAL)
698 /*@switchbreak@*/ break;
699 if (!(S_ISREG(fi->fmodes[i]) && (tfiGetFFlags(fi) & RPMFILE_CONFIG))) {
700 fi->actions[i] = FA_ERASE;
701 /*@switchbreak@*/ break;
704 /* Here is a pre-existing modified config file that needs saving. */
706 if (fi->md5s != NULL) {
707 const unsigned char * fmd5 = fi->md5s + (16 * i);
708 if (!domd5(fn, md5sum, 0) && memcmp(fmd5, md5sum, 16)) {
709 fi->actions[i] = FA_BACKUP;
710 /*@switchbreak@*/ break;
713 const char * fmd5 = fi->fmd5s[i];
714 if (!domd5(fn, md5sum, 1) && strcmp(fmd5, md5sum)) {
715 fi->actions[i] = FA_BACKUP;
716 /*@switchbreak@*/ break;
720 fi->actions[i] = FA_ERASE;
721 /*@switchbreak@*/ break;
725 uint_32 s = BLOCK_ROUND(fi->fsizes[i], ds->bsize);
727 switch (fi->actions[i]) {
733 /*@switchbreak@*/ break;
736 * FIXME: If two packages share a file (same md5sum), and
737 * that file is being replaced on disk, will ds->bneeded get
738 * decremented twice? Quite probably!
742 ds->bneeded -= BLOCK_ROUND(fi->replacedSizes[i], ds->bsize);
743 /*@switchbreak@*/ break;
748 /*@switchbreak@*/ break;
751 /*@switchbreak@*/ break;
754 ds->bneeded -= BLOCK_ROUND(fixupSize, ds->bsize);
760 * Ensure that current package is newer than installed package.
761 * @param ts transaction set
762 * @param p current transaction element
763 * @param h installed header
764 * @return 0 if not newer, 1 if okay
766 static int ensureOlder(rpmTransactionSet ts,
767 const transactionElement p, const Header h)
770 int_32 reqFlags = (RPMSENSE_LESS | RPMSENSE_EQUAL);
777 if (p == NULL || h == NULL)
780 nb = strlen(teGetNEVR(p)) + (teGetE(p) != NULL ? strlen(teGetE(p)) : 0) + 1;
784 if (teGetE(p) != NULL) t = stpcpy( stpcpy(t, teGetE(p)), ":");
785 if (teGetV(p) != NULL) t = stpcpy(t, teGetV(p));
787 if (teGetR(p) != NULL) t = stpcpy(t, teGetR(p));
789 req = dsSingle(RPMTAG_REQUIRENAME, teGetN(p), reqEVR, reqFlags);
790 rc = headerMatchesDepFlags(h, req);
794 const char * altNEVR = hGetNEVR(h, NULL);
795 rpmProblemSetAppend(ts->probs, RPMPROB_OLDPACKAGE,
796 teGetNEVR(p), teGetKey(p),
800 altNEVR = _free(altNEVR);
810 /*@-mustmod@*/ /* FIX: fi->actions is modified. */
811 static void skipFiles(const rpmTransactionSet ts, TFI_t fi)
812 /*@globals rpmGlobalMacroContext @*/
813 /*@modifies fi, rpmGlobalMacroContext @*/
815 int noDocs = (ts->transFlags & RPMTRANS_FLAG_NODOCS);
816 char ** netsharedPaths = NULL;
817 const char ** languages;
818 const char * dn, * bn;
819 int dnlen, bnlen, ix;
827 noDocs = rpmExpandNumeric("%{_excludedocs}");
829 { const char *tmpPath = rpmExpand("%{_netsharedpath}", NULL);
831 if (tmpPath && *tmpPath != '%')
832 netsharedPaths = splitString(tmpPath, strlen(tmpPath), ':');
834 tmpPath = _free(tmpPath);
837 s = rpmExpand("%{_install_langs}", NULL);
839 if (!(s && *s != '%'))
842 languages = (const char **) splitString(s, strlen(s), ':');
848 /* Compute directory refcount, skip directory if now empty. */
850 drc = alloca(dc * sizeof(*drc));
851 memset(drc, 0, dc * sizeof(*drc));
852 dff = alloca(dc * sizeof(*dff));
853 memset(dff, 0, dc * sizeof(*dff));
856 if (fi != NULL) /* XXX lclint */
857 while ((i = tfiNext(fi)) >= 0)
867 continue; /* XXX can't happen */
871 /* Don't bother with skipped files */
872 if (XFA_SKIPPING(fi->actions[i])) {
878 * Skip net shared paths.
879 * Net shared paths are not relative to the current root (though
880 * they do need to take package relocations into account).
882 for (nsp = netsharedPaths; nsp && *nsp; nsp++) {
887 if (strncmp(dn, *nsp, len))
888 /*@innercontinue@*/ continue;
889 /* Only directories or complete file paths can be net shared */
890 if (!(dn[len] == '/' || dn[len] == '\0'))
891 /*@innercontinue@*/ continue;
893 if (len < (dnlen + bnlen))
894 /*@innercontinue@*/ continue;
895 if (strncmp(dn, *nsp, dnlen))
896 /*@innercontinue@*/ continue;
897 if (strncmp(bn, (*nsp) + dnlen, bnlen))
898 /*@innercontinue@*/ continue;
900 /* Only directories or complete file paths can be net shared */
901 if (!((*nsp)[len] == '/' || (*nsp)[len] == '\0'))
902 /*@innercontinue@*/ continue;
905 /*@innerbreak@*/ break;
909 drc[ix]--; dff[ix] = 1;
910 fi->actions[i] = FA_SKIPNETSHARED;
915 * Skip i18n language specific files.
917 if (fi->flangs && languages && *fi->flangs[i]) {
918 const char **lang, *l, *le;
919 for (lang = languages; *lang != NULL; lang++) {
920 if (!strcmp(*lang, "all"))
921 /*@innerbreak@*/ break;
922 for (l = fi->flangs[i]; *l != '\0'; l = le) {
923 for (le = l; *le != '\0' && *le != '|'; le++)
925 if ((le-l) > 0 && !strncmp(*lang, l, (le-l)))
926 /*@innerbreak@*/ break;
927 if (*le == '|') le++; /* skip over | */
930 /*@innerbreak@*/ break;
933 drc[ix]--; dff[ix] = 1;
934 fi->actions[i] = FA_SKIPNSTATE;
940 * Skip documentation if requested.
942 if (noDocs && (tfiGetFFlags(fi) & RPMFILE_DOC)) {
943 drc[ix]--; dff[ix] = 1;
944 fi->actions[i] = FA_SKIPNSTATE;
949 /* Skip (now empty) directories that had skipped files. */
951 if (fi != NULL) /* XXX can't happen */
952 for (j = 0; j < dc; j++)
954 if ((fi = tdiInit(fi)) != NULL)
955 while (j = tdiNext(fi) >= 0)
959 if (drc[j]) continue; /* dir still has files. */
960 if (!dff[j]) continue; /* dir was not emptied here. */
962 /* Find parent directory and basename. */
963 dn = fi->dnl[j]; dnlen = strlen(dn) - 1;
964 bn = dn + dnlen; bnlen = 0;
965 while (bn > dn && bn[-1] != '/') {
971 /* If explicitly included in the package, skip the directory. */
973 if (fi != NULL) /* XXX lclint */
974 while ((i = tfiNext(fi)) >= 0) {
977 if (XFA_SKIPPING(fi->actions[i]))
978 /*@innercontinue@*/ continue;
979 if (whatis(fi->fmodes[i]) != XDIR)
980 /*@innercontinue@*/ continue;
981 dir = fi->dnl[fi->dil[i]];
982 if (strlen(dir) != dnlen)
983 /*@innercontinue@*/ continue;
984 if (strncmp(dir, dn, dnlen))
985 /*@innercontinue@*/ continue;
986 if (strlen(fi->bnl[i]) != bnlen)
987 /*@innercontinue@*/ continue;
988 if (strncmp(fi->bnl[i], bn, bnlen))
989 /*@innercontinue@*/ continue;
990 rpmMessage(RPMMESS_DEBUG, _("excluding directory %s\n"), dn);
991 fi->actions[i] = FA_SKIPNSTATE;
992 /*@innerbreak@*/ break;
996 if (netsharedPaths) freeSplitString(netsharedPaths);
997 #ifdef DYING /* XXX freeFi will deal with this later. */
998 fi->flangs = _free(fi->flangs);
1000 if (languages) freeSplitString((char **)languages);
1005 * Return transaction element's file info.
1006 * @todo Take a TFI_t refcount here.
1007 * @param tei transaction element iterator
1008 * @return transaction element file info
1011 TFI_t teiGetFi(const teIterator tei)
1016 if (tei != NULL && tei->ocsave != -1) {
1017 /*@-type -abstract@*/ /* FIX: transactionElement not opaque */
1018 transactionElement te = tei->ts->order[tei->ocsave];
1020 if ((fi = te->fi) != NULL)
1023 /*@=type =abstract@*/
1025 /*@-compdef -refcounttrans -usereleased @*/
1027 /*@=compdef =refcounttrans =usereleased @*/
1030 #define NOTIFY(_ts, _al) if ((_ts)->notify) (void) (_ts)->notify _al
1032 int rpmRunTransactions( rpmTransactionSet ts,
1033 rpmProblemSet okProbs, rpmProblemSet * newProbs,
1034 rpmprobFilterFlags ignoreSet)
1038 int totalFileCount = 0;
1040 struct diskspaceInfo * dip;
1041 sharedFileInfo shared, sharedList;
1045 fingerPrintCache fpc;
1046 PSM_t psm = memset(alloca(sizeof(*psm)), 0, sizeof(*psm));
1047 teIterator pi; transactionElement p;
1048 teIterator qi; transactionElement q;
1051 /* FIXME: what if the same package is included in ts twice? */
1053 if (ts->transFlags & RPMTRANS_FLAG_NOSCRIPTS)
1054 ts->transFlags |= (_noTransScripts | _noTransTriggers);
1055 if (ts->transFlags & RPMTRANS_FLAG_NOTRIGGERS)
1056 ts->transFlags |= _noTransTriggers;
1058 /* XXX MULTILIB is broken, as packages can and do execute /sbin/ldconfig. */
1059 if (ts->transFlags & (RPMTRANS_FLAG_JUSTDB | RPMTRANS_FLAG_MULTILIB))
1060 ts->transFlags |= (_noTransScripts | _noTransTriggers);
1062 ts->probs = rpmProblemSetFree(ts->probs);
1063 ts->probs = rpmProblemSetCreate();
1064 *newProbs = rpmpsLink(ts->probs, "RunTransactions");
1065 ts->ignoreSet = ignoreSet;
1066 ts->currDir = _free(ts->currDir);
1067 ts->currDir = currentDirectory();
1069 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
1070 ts->id = (int_32) time(NULL);
1072 memset(psm, 0, sizeof(*psm));
1073 psm->ts = rpmtsLink(ts, "tsRun");
1075 /* Get available space on mounted file systems. */
1076 if (!(ts->ignoreSet & RPMPROB_FILTER_DISKSPACE) &&
1077 !rpmGetFilesystemList(&ts->filesystems, &ts->filesystemCount))
1081 rpmMessage(RPMMESS_DEBUG, _("getting list of mounted filesystems\n"));
1083 ts->di = _free(ts->di);
1084 dip = ts->di = xcalloc((ts->filesystemCount + 1), sizeof(*ts->di));
1086 for (i = 0; (i < ts->filesystemCount) && dip; i++) {
1087 #if STATFS_IN_SYS_STATVFS
1089 memset(&sfb, 0, sizeof(sfb));
1090 if (statvfs(ts->filesystems[i], &sfb))
1094 /* This platform has the 4-argument version of the statfs call. The last two
1095 * should be the size of struct statfs and 0, respectively. The 0 is the
1096 * filesystem type, and is always 0 when statfs is called on a mounted
1097 * filesystem, as we're doing.
1099 memset(&sfb, 0, sizeof(sfb));
1100 if (statfs(ts->filesystems[i], &sfb, sizeof(sfb), 0))
1102 memset(&sfb, 0, sizeof(sfb));
1103 if (statfs(ts->filesystems[i], &sfb))
1109 ts->di[i].bsize = sfb.f_bsize;
1110 ts->di[i].bneeded = 0;
1111 ts->di[i].ineeded = 0;
1112 #ifdef STATFS_HAS_F_BAVAIL
1113 ts->di[i].bavail = sfb.f_bavail;
1115 /* FIXME: the statfs struct doesn't have a member to tell how many blocks are
1116 * available for non-superusers. f_blocks - f_bfree is probably too big, but
1117 * it's about all we can do.
1119 ts->di[i].bavail = sfb.f_blocks - sfb.f_bfree;
1121 /* XXX Avoid FAT and other file systems that have not inodes. */
1122 ts->di[i].iavail = !(sfb.f_ffree == 0 && sfb.f_files == 0)
1125 xx = stat(ts->filesystems[i], &sb);
1126 ts->di[i].dev = sb.st_dev;
1130 if (dip) ts->di[i].bsize = 0;
1133 /* ===============================================
1134 * For packages being installed:
1135 * - verify package arch/os.
1136 * - verify package epoch:version-release is newer.
1138 * For packages being removed:
1141 /* The ordering doesn't matter here */
1142 pi = teInitIterator(ts);
1143 while ((p = teNext(pi, TR_ADDED)) != NULL) {
1144 rpmdbMatchIterator mi;
1147 if ((fi = teiGetFi(pi)) == NULL)
1148 continue; /* XXX can't happen */
1151 if (!(ts->ignoreSet & RPMPROB_FILTER_IGNOREARCH))
1152 if (!archOkay(teGetA(p)))
1153 rpmProblemSetAppend(ts->probs, RPMPROB_BADARCH,
1154 teGetNEVR(p), teGetKey(p),
1158 if (!(ts->ignoreSet & RPMPROB_FILTER_IGNOREOS))
1159 if (!osOkay(teGetO(p)))
1160 rpmProblemSetAppend(ts->probs, RPMPROB_BADOS,
1161 teGetNEVR(p), teGetKey(p),
1165 if (!(ts->ignoreSet & RPMPROB_FILTER_OLDPACKAGE)) {
1167 mi = rpmtsInitIterator(ts, RPMTAG_NAME, teGetN(p), 0);
1168 while ((h = rpmdbNextIterator(mi)) != NULL)
1169 xx = ensureOlder(ts, p, h);
1170 mi = rpmdbFreeIterator(mi);
1173 /* XXX multilib should not display "already installed" problems */
1174 if (!(ts->ignoreSet & RPMPROB_FILTER_REPLACEPKG) && !teGetMultiLib(p)) {
1175 mi = rpmtsInitIterator(ts, RPMTAG_NAME, teGetN(p), 0);
1176 xx = rpmdbSetIteratorRE(mi, RPMTAG_VERSION, RPMMIRE_DEFAULT,
1178 xx = rpmdbSetIteratorRE(mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT,
1181 while (rpmdbNextIterator(mi) != NULL) {
1182 rpmProblemSetAppend(ts->probs, RPMPROB_PKG_INSTALLED,
1183 teGetNEVR(p), teGetKey(p),
1186 /*@innerbreak@*/ break;
1188 mi = rpmdbFreeIterator(mi);
1191 /* Count no. of files (if any). */
1192 totalFileCount += fc;
1195 pi = teFreeIterator(pi);
1197 /* The ordering doesn't matter here */
1198 pi = teInitIterator(ts);
1199 while ((p = teNext(pi, TR_REMOVED)) != NULL) {
1202 if ((fi = teiGetFi(pi)) == NULL)
1203 continue; /* XXX can't happen */
1206 totalFileCount += fc;
1208 pi = teFreeIterator(pi);
1210 /* ===============================================
1211 * Initialize transaction element file info for package:
1215 * FIXME?: we'd be better off assembling one very large file list and
1216 * calling fpLookupList only once. I'm not sure that the speedup is
1217 * worth the trouble though.
1219 pi = teInitIterator(ts);
1220 while ((p = teNextIterator(pi)) != NULL) {
1223 if ((fi = teiGetFi(pi)) == NULL)
1224 continue; /* XXX can't happen */
1227 #ifdef DYING /* XXX W2DO? this is now done in teiGetFi, okay ??? */
1228 fi->magic = TFIMAGIC;
1233 switch (teGetType(p)) {
1236 /* Skip netshared paths, not our i18n files, and excluded docs */
1239 /*@switchbreak@*/ break;
1241 fi->record = teGetDBOffset(p);
1242 /*@switchbreak@*/ break;
1246 fi->fps = (fc > 0 ? xmalloc(fc * sizeof(*fi->fps)) : NULL);
1248 pi = teFreeIterator(pi);
1250 if (!ts->chrootDone) {
1252 /*@-superuser -noeffect @*/
1253 xx = chroot(ts->rootDir);
1254 /*@=superuser =noeffect @*/
1256 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 1;
1259 ts->ht = htCreate(totalFileCount * 2, 0, 0, fpHashFunction, fpEqual);
1260 fpc = fpCacheCreate(totalFileCount);
1262 /* ===============================================
1263 * Add fingerprint for each file not skipped.
1265 pi = teInitIterator(ts);
1266 while ((p = teNextIterator(pi)) != NULL) {
1269 if ((fi = teiGetFi(pi)) == NULL)
1270 continue; /* XXX can't happen */
1273 fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fc, fi->fps);
1275 fi = tfiInit(fi, 0);
1276 if (fi != NULL) /* XXX lclint */
1277 while ((i = tfiNext(fi)) >= 0) {
1278 if (XFA_SKIPPING(fi->actions[i]))
1279 /*@innercontinue@*/ continue;
1280 /*@-dependenttrans@*/
1281 htAddEntry(ts->ht, fi->fps + i, (void *) fi);
1282 /*@=dependenttrans@*/
1286 pi = teFreeIterator(pi);
1288 /*@-noeffectuncon @*/ /* FIX: check rc */
1289 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_START, 6, ts->orderCount,
1290 NULL, ts->notifyData));
1291 /*@=noeffectuncon@*/
1293 /* ===============================================
1294 * Compute file disposition for each package in transaction set.
1296 pi = teInitIterator(ts);
1297 while ((p = teNextIterator(pi)) != NULL) {
1298 dbiIndexSet * matches;
1302 if ((fi = teiGetFi(pi)) == NULL)
1303 continue; /* XXX can't happen */
1306 /*@-noeffectuncon @*/ /* FIX: check rc */
1307 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_PROGRESS, teiGetOc(pi),
1308 ts->orderCount, NULL, ts->notifyData));
1309 /*@=noeffectuncon@*/
1311 if (fc == 0) continue;
1313 /* Extract file info for all files in this package from the database. */
1314 matches = xcalloc(fc, sizeof(*matches));
1315 if (rpmdbFindFpList(ts->rpmdb, fi->fps, matches, fc)) {
1316 psm->ts = rpmtsUnlink(ts, "tsRun (rpmFindFpList fail)");
1317 return 1; /* XXX WTFO? */
1321 fi = tfiInit(fi, 0);
1322 while ((i = tfiNext(fi)) >= 0)
1323 numShared += dbiIndexSetCount(matches[i]);
1325 /* Build sorted file info list for this package. */
1326 shared = sharedList = xcalloc((numShared + 1), sizeof(*sharedList));
1328 fi = tfiInit(fi, 0);
1329 while ((i = tfiNext(fi)) >= 0) {
1331 * Take care not to mark files as replaced in packages that will
1332 * have been removed before we will get here.
1334 for (j = 0; j < dbiIndexSetCount(matches[i]); j++) {
1336 ro = dbiIndexRecordOffset(matches[i], j);
1338 qi = teInitIterator(ts);
1339 while ((q = teNext(qi, TR_REMOVED)) != NULL) {
1341 /*@innerbreak@*/ break;
1342 if (teGetDBOffset(q) == ro)
1345 qi = teFreeIterator(qi);
1347 shared->pkgFileNum = i;
1348 shared->otherPkg = dbiIndexRecordOffset(matches[i], j);
1349 shared->otherFileNum = dbiIndexRecordFileNumber(matches[i], j);
1350 shared->isRemoved = (knownBad == ro);
1353 matches[i] = dbiFreeIndexSet(matches[i]);
1355 numShared = shared - sharedList;
1356 shared->otherPkg = -1;
1357 matches = _free(matches);
1359 /* Sort file info by other package index (otherPkg) */
1360 qsort(sharedList, numShared, sizeof(*shared), sharedCmp);
1362 /* For all files from this package that are in the database ... */
1364 for (i = 0; i < numShared; i = nexti) {
1367 shared = sharedList + i;
1369 /* Find the end of the files in the other package. */
1370 for (nexti = i + 1; nexti < numShared; nexti++) {
1371 if (sharedList[nexti].otherPkg != shared->otherPkg)
1372 /*@innerbreak@*/ break;
1375 /* Is this file from a package being removed? */
1377 if (ts->removedPackages != NULL)
1378 for (j = 0; j < ts->numRemovedPackages; j++) {
1379 if (ts->removedPackages[j] != shared->otherPkg)
1380 /*@innercontinue@*/ continue;
1382 /*@innerbreak@*/ break;
1385 /* Determine the fate of each file. */
1386 switch (teGetType(p)) {
1388 xx = handleInstInstalledFiles(ts, p, fi, shared, nexti - i,
1389 !(beingRemoved || (ts->ignoreSet & RPMPROB_FILTER_REPLACEOLDFILES)));
1390 /*@switchbreak@*/ break;
1393 xx = handleRmvdInstalledFiles(ts, fi, shared, nexti - i);
1394 /*@switchbreak@*/ break;
1401 /* Update disk space needs on each partition for this package. */
1402 handleOverlappedFiles(ts, p, fi);
1404 /* Check added package has sufficient space on each partition used. */
1405 switch (teGetType(p)) {
1407 if (!(ts->di && tfiGetFC(fi) > 0))
1408 /*@switchbreak@*/ break;
1409 for (i = 0; i < ts->filesystemCount; i++) {
1413 /* XXX Avoid FAT and other file systems that have not inodes. */
1414 if (dip->iavail <= 0)
1415 /*@innercontinue@*/ continue;
1417 if (adj_fs_blocks(dip->bneeded) > dip->bavail) {
1418 rpmProblemSetAppend(ts->probs, RPMPROB_DISKSPACE,
1419 teGetNEVR(p), teGetKey(p),
1420 ts->filesystems[i], NULL, NULL,
1421 (adj_fs_blocks(dip->bneeded) - dip->bavail) * dip->bsize);
1424 if (adj_fs_blocks(dip->ineeded) > dip->iavail) {
1425 rpmProblemSetAppend(ts->probs, RPMPROB_DISKNODES,
1426 teGetNEVR(p), teGetKey(p),
1427 ts->filesystems[i], NULL, NULL,
1428 (adj_fs_blocks(dip->ineeded) - dip->iavail));
1431 /*@switchbreak@*/ break;
1433 /*@switchbreak@*/ break;
1436 pi = teFreeIterator(pi);
1438 if (ts->chrootDone) {
1439 /*@-superuser -noeffect @*/
1441 /*@=superuser =noeffect @*/
1443 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
1444 xx = chdir(ts->currDir);
1447 /*@-noeffectuncon @*/ /* FIX: check rc */
1448 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_STOP, 6, ts->orderCount,
1449 NULL, ts->notifyData));
1450 /*@=noeffectuncon @*/
1452 /* ===============================================
1453 * Free unused memory as soon as possible.
1455 pi = teInitIterator(ts);
1456 while ((p = teNextIterator(pi)) != NULL) {
1457 if ((fi = teiGetFi(pi)) == NULL)
1458 continue; /* XXX can't happen */
1459 if (tfiGetFC(fi) == 0)
1461 fi->fps = _free(fi->fps);
1463 pi = teFreeIterator(pi);
1469 /* ===============================================
1470 * If unfiltered problems exist, free memory and return.
1472 if ((ts->transFlags & RPMTRANS_FLAG_BUILD_PROBS)
1473 || (ts->probs->numProblems &&
1474 (okProbs != NULL || rpmProblemSetTrim(ts->probs, okProbs)))
1477 if (psm->ts != NULL)
1478 psm->ts = rpmtsUnlink(psm->ts, "tsRun (problems)");
1479 return ts->orderCount;
1482 /* ===============================================
1483 * Save removed files before erasing.
1485 if (ts->transFlags & (RPMTRANS_FLAG_DIRSTASH | RPMTRANS_FLAG_REPACKAGE)) {
1486 pi = teInitIterator(ts);
1487 while ((p = teNextIterator(pi)) != NULL) {
1489 switch (teGetType(p)) {
1491 /*@switchbreak@*/ break;
1493 if (!(ts->transFlags & RPMTRANS_FLAG_REPACKAGE))
1494 /*@switchbreak@*/ break;
1496 psm->fi = rpmfiLink(fi, "tsRepackage");
1497 xx = psmStage(psm, PSM_PKGSAVE);
1498 (void) rpmfiUnlink(fi, "tsRepackage");
1501 /*@switchbreak@*/ break;
1504 pi = teFreeIterator(pi);
1507 /* ===============================================
1508 * Install and remove packages.
1510 lastKey = (alKey)-2; /* erased packages have -1 */
1511 pi = teInitIterator(ts);
1512 /*@-branchstate@*/ /* FIX: fi reload needs work */
1513 while ((p = teNextIterator(pi)) != NULL) {
1519 if ((fi = teiGetFi(pi)) == NULL)
1520 continue; /* XXX can't happen */
1523 psm->fi = rpmfiLink(fi, "tsInstall");
1524 switch (teGetType(p)) {
1527 pkgKey = teGetAddedKey(p);
1529 rpmMessage(RPMMESS_DEBUG, "========== +++ %s\n", teGetNEVR(p));
1531 /*@-type@*/ /* FIX: transactionElement not opaque */
1533 /*@-noeffectuncon@*/ /* FIX: notify annotations */
1534 p->fd = ts->notify(fi->h, RPMCALLBACK_INST_OPEN_FILE, 0, 0,
1535 teGetKey(p), ts->notifyData);
1536 /*@=noeffectuncon@*/
1537 if (teGetFd(p) != NULL) {
1540 rpmrc = rpmReadPackageFile(ts, teGetFd(p),
1541 "rpmRunTransactions", &h);
1543 if (!(rpmrc == RPMRC_OK || rpmrc == RPMRC_BADSIZE)) {
1544 /*@-noeffectuncon@*/ /* FIX: notify annotations */
1545 p->fd = ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE,
1547 teGetKey(p), ts->notifyData);
1548 /*@=noeffectuncon@*/
1552 if (teGetFd(p) != NULL) gotfd = 1;
1557 if (teGetFd(p) != NULL) {
1559 char * fstates = fi->fstates;
1560 fileAction * actions = fi->actions;
1564 (void) fiFree(fi, 0);
1566 fi->magic = TFIMAGIC;
1569 (void) fiNew(ts, fi, h, RPMTAG_BASENAMES, 1);
1570 fi->fstates = _free(fi->fstates);
1571 fi->fstates = fstates;
1572 fi->actions = _free(fi->actions);
1573 fi->actions = actions;
1577 if (teGetMultiLib(p))
1578 ts->transFlags |= RPMTRANS_FLAG_MULTILIB;
1580 ts->transFlags &= ~RPMTRANS_FLAG_MULTILIB;
1582 if (psmStage(psm, PSM_PKGINSTALL)) {
1586 fi->h = headerFree(fi->h, "TR_ADDED fi->h free");
1592 h = headerFree(h, "TR_ADDED h free");
1595 /*@-noeffectuncon @*/ /* FIX: check rc */
1596 (void) ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE, 0, 0,
1597 teGetKey(p), ts->notifyData);
1598 /*@=noeffectuncon @*/
1603 (void) fiFree(fi, 0);
1604 /*@switchbreak@*/ break;
1606 rpmMessage(RPMMESS_DEBUG, "========== --- %s\n", teGetNEVR(p));
1607 /* If install failed, then we shouldn't erase. */
1608 if (teGetDependsOnKey(p) != lastKey) {
1609 if (psmStage(psm, PSM_PKGERASE))
1612 (void) fiFree(fi, 0);
1613 /*@switchbreak@*/ break;
1615 xx = rpmdbSync(ts->rpmdb);
1616 (void) rpmfiUnlink(psm->fi, "tsInstall");
1621 pi = teFreeIterator(pi);
1623 psm->ts = rpmtsUnlink(psm->ts, "tsRun");
1625 /*@-nullstate@*/ /* FIX: ts->flList may be NULL */