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))
88 void rpmtsSetScriptFd(rpmTransactionSet ts, FD_t fd)
90 ts->scriptFd = (fd ? fdLink(fd, "rpmtsSetScriptFd") : NULL);
93 int rpmtsGetKeys(const rpmTransactionSet ts, fnpyKey ** ep, int * nep)
97 if (nep) *nep = ts->orderCount;
99 teIterator pi; transactionElement p;
102 *ep = e = xmalloc(ts->orderCount * sizeof(*e));
103 pi = teInitIterator(ts);
104 while ((p = teNextIterator(pi)) != NULL) {
105 switch (teGetType(p)) {
107 /*@-dependenttrans@*/
109 /*@=dependenttrans@*/
110 /*@switchbreak@*/ break;
114 /*@switchbreak@*/ break;
118 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 rpmTransactionSet ts,
161 const TFI_t ofi, TFI_t nfi)
162 /*@globals fileSystem @*/
163 /*@modifies nfi, fileSystem @*/
165 const char * fn = tfiGetFN(nfi);
166 int newFlags = tfiGetFFlags(nfi);
168 fileTypes dbWhat, newWhat, diskWhat;
170 int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
172 if (lstat(fn, &sb)) {
174 * The file doesn't exist on the disk. Create it unless the new
175 * package has marked it as missingok, or allfiles is requested.
177 if (!(ts->transFlags & RPMTRANS_FLAG_ALLFILES)
178 && (newFlags & RPMFILE_MISSINGOK))
180 rpmMessage(RPMMESS_DEBUG, _("%s skipped due to missingok flag\n"),
188 diskWhat = whatis(sb.st_mode);
189 dbWhat = whatis(ofi->fmodes[ofi->i]);
190 newWhat = whatis(nfi->fmodes[nfi->i]);
193 * RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
194 * them in older packages as well.
199 if (diskWhat != newWhat)
201 else if (newWhat != dbWhat && diskWhat != dbWhat)
203 else if (dbWhat != newWhat)
205 else if (dbWhat != LINK && dbWhat != REG)
209 * This order matters - we'd prefer to CREATE the file if at all
210 * possible in case something else (like the timestamp) has changed.
214 if (ofi->md5s != NULL && nfi->md5s != NULL) {
216 const unsigned char * omd5 = ofi->md5s + (16 * ofi->i);
217 const unsigned char * nmd5 = nfi->md5s + (16 * nfi->i);
218 if (domd5(fn, buffer, 0))
219 return FA_CREATE; /* assume file has been removed */
220 if (!memcmp(omd5, buffer, 16))
221 return FA_CREATE; /* unmodified config file, replace. */
222 if (!memcmp(omd5, nmd5, 16))
223 return FA_SKIP; /* identical file, don't bother. */
226 const char * omd5 = ofi->fmd5s[ofi->i];
227 const char * nmd5 = nfi->fmd5s[nfi->i];
228 if (domd5(fn, buffer, 1))
229 return FA_CREATE; /* assume file has been removed */
230 if (!strcmp(omd5, buffer))
231 return FA_CREATE; /* unmodified config file, replace. */
232 if (!strcmp(omd5, nmd5))
233 return FA_SKIP; /* identical file, don't bother. */
236 } else /* dbWhat == LINK */ {
237 memset(buffer, 0, sizeof(buffer));
238 if (readlink(fn, buffer, sizeof(buffer) - 1) == -1)
239 return FA_CREATE; /* assume file has been removed */
240 if (!strcmp(ofi->flinks[ofi->i], buffer))
241 return FA_CREATE; /* unmodified config file, replace. */
242 if (!strcmp(ofi->flinks[ofi->i], nfi->flinks[nfi->i]))
243 return FA_SKIP; /* identical file, don't bother. */
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(TFI_t afi, TFI_t bfi)
260 fileTypes awhat = whatis(afi->fmodes[afi->i]);
261 fileTypes bwhat = whatis(bfi->fmodes[bfi->i]);
263 if (awhat != bwhat) return 1;
266 const char * alink = afi->flinks[afi->i];
267 const char * blink = bfi->flinks[bfi->i];
268 return strcmp(alink, blink);
269 } else if (awhat == REG) {
271 if (afi->md5s != NULL && bfi->md5s != NULL) {
273 const unsigned char * amd5 = afi->md5s + (16 * afi->i);
274 const unsigned char * bmd5 = bfi->md5s + (16 * bfi->i);
275 return memcmp(amd5, bmd5, 16);
278 const char * amd5 = afi->fmd5s[afi->i];
279 const char * bmd5 = bfi->fmd5s[bfi->i];
280 return strcmp(amd5, bmd5);
290 /* XXX only ts->{probs,rpmdb} modified */
291 static int handleInstInstalledFiles(const rpmTransactionSet ts,
292 transactionElement p, TFI_t fi,
293 sharedFileInfo shared,
294 int sharedCount, int reportConflicts)
295 /*@globals fileSystem @*/
296 /*@modifies ts, fi, fileSystem @*/
298 const char * altNEVR = NULL;
299 TFI_t otherFi = NULL;
303 { rpmdbMatchIterator mi;
307 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
308 &shared->otherPkg, sizeof(shared->otherPkg));
309 while ((h = rpmdbNextIterator(mi)) != NULL) {
310 altNEVR = hGetNEVR(h, NULL);
311 otherFi = fiNew(ts, NULL, h, RPMTAG_BASENAMES, scareMem);
314 mi = rpmdbFreeIterator(mi);
320 fi->replaced = xcalloc(sharedCount, sizeof(*fi->replaced));
322 for (i = 0; i < sharedCount; i++, shared++) {
323 int otherFileNum, fileNum;
325 otherFileNum = shared->otherFileNum;
326 (void) tfiSetFX(otherFi, otherFileNum);
328 fileNum = shared->pkgFileNum;
329 (void) tfiSetFX(fi, fileNum);
332 /* XXX another tedious segfault, assume file state normal. */
333 if (otherStates && otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
337 if (XFA_SKIPPING(fi->actions[fileNum]))
340 if (filecmp(otherFi, fi)) {
341 if (reportConflicts) {
342 rpmProblemSetAppend(ts->probs, RPMPROB_FILE_CONFLICT,
343 teGetNEVR(p), teGetKey(p),
344 tfiGetDN(fi), tfiGetBN(fi),
348 if (!(tfiGetFFlags(otherFi) | tfiGetFFlags(fi)) & RPMFILE_CONFIG) {
349 /*@-assignexpose@*/ /* FIX: p->replaced, not fi */
350 if (!shared->isRemoved)
351 fi->replaced[numReplaced++] = *shared;
356 if ((tfiGetFFlags(otherFi) | tfiGetFFlags(fi)) & RPMFILE_CONFIG) {
358 action = decideFileFate(ts, otherFi, fi);
359 fi->actions[fileNum] = action;
361 fi->replacedSizes[fileNum] = otherFi->fsizes[otherFi->i];
365 altNEVR = _free(altNEVR);
366 otherFi = fiFree(otherFi, 1);
368 fi->replaced = xrealloc(fi->replaced, /* XXX memory leak */
369 sizeof(*fi->replaced) * (numReplaced + 1));
370 fi->replaced[numReplaced].otherPkg = 0;
377 /* XXX only ts->rpmdb modified */
378 static int handleRmvdInstalledFiles(const rpmTransactionSet ts, TFI_t fi,
379 sharedFileInfo shared, int sharedCount)
380 /*@globals fileSystem @*/
381 /*@modifies ts, fi, fileSystem @*/
385 const char * otherStates;
388 rpmdbMatchIterator mi;
390 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
391 &shared->otherPkg, sizeof(shared->otherPkg));
392 h = rpmdbNextIterator(mi);
394 mi = rpmdbFreeIterator(mi);
398 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
400 for (i = 0; i < sharedCount; i++, shared++) {
401 int otherFileNum, fileNum;
402 otherFileNum = shared->otherFileNum;
403 fileNum = shared->pkgFileNum;
405 if (otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
408 fi->actions[fileNum] = FA_SKIP;
411 mi = rpmdbFreeIterator(mi);
416 #define ISROOT(_d) (((_d)[0] == '/' && (_d)[1] == '\0') ? "" : (_d))
419 static int _fps_debug = 0;
421 static int fpsCompare (const void * one, const void * two)
423 const struct fingerPrint_s * a = (const struct fingerPrint_s *)one;
424 const struct fingerPrint_s * b = (const struct fingerPrint_s *)two;
425 int adnlen = strlen(a->entry->dirName);
426 int asnlen = (a->subDir ? strlen(a->subDir) : 0);
427 int abnlen = strlen(a->baseName);
428 int bdnlen = strlen(b->entry->dirName);
429 int bsnlen = (b->subDir ? strlen(b->subDir) : 0);
430 int bbnlen = strlen(b->baseName);
431 char * afn, * bfn, * t;
434 if (adnlen == 1 && asnlen != 0) adnlen = 0;
435 if (bdnlen == 1 && bsnlen != 0) bdnlen = 0;
437 afn = t = alloca(adnlen+asnlen+abnlen+2);
438 if (adnlen) t = stpcpy(t, a->entry->dirName);
440 if (a->subDir && asnlen) t = stpcpy(t, a->subDir);
441 if (abnlen) t = stpcpy(t, a->baseName);
442 if (afn[0] == '/' && afn[1] == '/') afn++;
444 bfn = t = alloca(bdnlen+bsnlen+bbnlen+2);
445 if (bdnlen) t = stpcpy(t, b->entry->dirName);
447 if (b->subDir && bsnlen) t = stpcpy(t, b->subDir);
448 if (bbnlen) t = stpcpy(t, b->baseName);
449 if (bfn[0] == '/' && bfn[1] == '/') bfn++;
451 rc = strcmp(afn, bfn);
454 fprintf(stderr, "\trc(%d) = strcmp(\"%s\", \"%s\")\n", rc, afn, bfn);
459 fprintf(stderr, "\t%s/%s%s\trc %d\n",
460 ISROOT(b->entry->dirName),
461 (b->subDir ? b->subDir : ""),
471 static int _linear_fps_search = 0;
473 static int findFps(const struct fingerPrint_s * fiFps,
474 const struct fingerPrint_s * otherFps,
482 fprintf(stderr, "==> %s/%s%s\n",
483 ISROOT(fiFps->entry->dirName),
484 (fiFps->subDir ? fiFps->subDir : ""),
488 if (_linear_fps_search) {
491 for (otherFileNum = 0; otherFileNum < otherFc; otherFileNum++, otherFps++) {
495 fprintf(stderr, "\t%4d %s/%s%s\n", otherFileNum,
496 ISROOT(otherFps->entry->dirName),
497 (otherFps->subDir ? otherFps->subDir : ""),
501 /* If the addresses are the same, so are the values. */
502 if (fiFps == otherFps)
505 /* Otherwise, compare fingerprints by value. */
506 /*@-nullpass@*/ /* LCL: looks good to me */
507 if (FP_EQUAL((*fiFps), (*otherFps)))
512 if (otherFileNum == otherFc) {
515 fprintf(stderr, "*** NULL %s/%s%s\n",
516 ISROOT(fiFps->entry->dirName),
517 (fiFps->subDir ? fiFps->subDir : ""),
526 const struct fingerPrint_s * bingoFps;
528 bingoFps = bsearch(fiFps, otherFps, otherFc, sizeof(*otherFps), fpsCompare);
529 if (bingoFps == NULL) {
531 fprintf(stderr, "*** NULL %s/%s%s\n",
532 ISROOT(fiFps->entry->dirName),
533 (fiFps->subDir ? fiFps->subDir : ""),
539 /* If the addresses are the same, so are the values. */
540 /*@-nullpass@*/ /* LCL: looks good to me */
541 if (!(fiFps == bingoFps || FP_EQUAL((*fiFps), (*bingoFps)))) {
543 fprintf(stderr, "*** BAD %s/%s%s\n",
544 ISROOT(bingoFps->entry->dirName),
545 (bingoFps->subDir ? bingoFps->subDir : ""),
551 otherFileNum = (bingoFps != NULL ? (bingoFps - otherFps) : 0);
559 * Update disk space needs on each partition for this package.
561 /* XXX only ts->{probs,di} modified */
562 static void handleOverlappedFiles(const rpmTransactionSet ts,
563 const transactionElement p, TFI_t fi)
564 /*@globals fileSystem @*/
565 /*@modifies ts, fi, fileSystem @*/
567 struct diskspaceInfo * ds = NULL;
568 uint_32 fixupSize = 0;
573 if (fi != NULL) /* XXX lclint */
574 while ((i = tfiNext(fi)) >= 0) {
575 struct fingerPrint_s * fiFps;
576 int otherPkgNum, otherFileNum;
581 if (XFA_SKIPPING(fi->actions[i]))
589 while (ds->bsize && ds->dev != fi->fps[i].entry->dev) ds++;
590 if (!ds->bsize) ds = NULL;
595 * Retrieve all records that apply to this file. Note that the
596 * file info records were built in the same order as the packages
597 * will be installed and removed so the records for an overlapped
598 * files will be sorted in exactly the same order.
600 (void) htGetEntry(ts->ht, fiFps,
601 (const void ***) &recs, &numRecs, NULL);
604 * If this package is being added, look only at other packages
605 * being added -- removed packages dance to a different tune.
606 * If both this and the other package are being added, overlapped
607 * files must be identical (or marked as a conflict). The
608 * disposition of already installed config files leads to
609 * a small amount of extra complexity.
611 * If this package is being removed, then there are two cases that
612 * need to be worried about:
613 * If the other package is being added, then skip any overlapped files
614 * so that this package removal doesn't nuke the overlapped files
615 * that were just installed.
616 * If both this and the other package are being removed, then each
617 * file removal from preceding packages needs to be skipped so that
618 * the file removal occurs only on the last occurence of an overlapped
619 * file in the transaction set.
623 /* Locate this overlapped file in the set of added/removed packages. */
624 for (j = 0; j < numRecs && recs[j] != fi; j++)
627 /* Find what the previous disposition of this file was. */
628 otherFileNum = -1; /* keep gcc quiet */
630 for (otherPkgNum = j - 1; otherPkgNum >= 0; otherPkgNum--) {
631 struct fingerPrint_s * otherFps;
634 otherFi = recs[otherPkgNum];
636 /* Added packages need only look at other added packages. */
637 if (teGetType(p) == TR_ADDED && teGetType(otherFi->te) != TR_ADDED)
638 /*@innercontinue@*/ continue;
640 otherFps = otherFi->fps;
641 otherFc = tfiGetFC(otherFi);
643 otherFileNum = findFps(fiFps, otherFps, otherFc);
644 (void) tfiSetFX(otherFi, otherFileNum);
646 /* XXX is this test still necessary? */
647 if (otherFi->actions[otherFileNum] != FA_UNKNOWN)
648 /*@innerbreak@*/ break;
651 switch (teGetType(p)) {
654 if (otherPkgNum < 0) {
655 /* XXX is this test still necessary? */
656 if (fi->actions[i] != FA_UNKNOWN)
657 /*@switchbreak@*/ break;
658 if ((tfiGetFFlags(fi) & RPMFILE_CONFIG) &&
660 /* Here is a non-overlapped pre-existing config file. */
661 fi->actions[i] = (tfiGetFFlags(fi) & RPMFILE_NOREPLACE)
662 ? FA_ALTNAME : FA_BACKUP;
664 fi->actions[i] = FA_CREATE;
666 /*@switchbreak@*/ break;
669 assert(otherFi != NULL);
670 /* Mark added overlapped non-identical files as a conflict. */
671 if ((ts->ignoreSet & RPMPROB_FILTER_REPLACENEWFILES)
672 && filecmp(otherFi, fi))
674 rpmProblemSetAppend(ts->probs, RPMPROB_NEW_FILE_CONFLICT,
675 teGetNEVR(p), teGetKey(p),
677 teGetNEVR(otherFi->te),
681 /* Try to get the disk accounting correct even if a conflict. */
682 fixupSize = otherFi->fsizes[otherFileNum];
684 if ((tfiGetFFlags(fi) & RPMFILE_CONFIG) && !lstat(fn, &sb)) {
685 /* Here is an overlapped pre-existing config file. */
686 fi->actions[i] = (tfiGetFFlags(fi) & RPMFILE_NOREPLACE)
687 ? FA_ALTNAME : FA_SKIP;
689 fi->actions[i] = FA_CREATE;
691 } /*@switchbreak@*/ break;
694 if (otherPkgNum >= 0) {
695 assert(otherFi != NULL);
696 /* Here is an overlapped added file we don't want to nuke. */
697 if (otherFi->actions[otherFileNum] != FA_ERASE) {
698 /* On updates, don't remove files. */
699 fi->actions[i] = FA_SKIP;
700 /*@switchbreak@*/ break;
702 /* Here is an overlapped removed file: skip in previous. */
703 otherFi->actions[otherFileNum] = FA_SKIP;
705 if (XFA_SKIPPING(fi->actions[i]))
706 /*@switchbreak@*/ break;
707 if (fi->fstates && fi->fstates[i] != RPMFILE_STATE_NORMAL)
708 /*@switchbreak@*/ break;
709 if (!(S_ISREG(fi->fmodes[i]) && (tfiGetFFlags(fi) & RPMFILE_CONFIG))) {
710 fi->actions[i] = FA_ERASE;
711 /*@switchbreak@*/ break;
714 /* Here is a pre-existing modified config file that needs saving. */
717 if (fi->md5s != NULL) {
719 const unsigned char * md5 = fi->md5s + (16 * i);
720 if (!domd5(fn, md5sum, 0) && memcmp(md5, md5sum, 16)) {
721 fi->actions[i] = FA_BACKUP;
722 /*@switchbreak@*/ break;
726 const char * fmd5 = fi->fmd5s[i];
727 if (!domd5(fn, md5sum, 1) && strcmp(fmd5, md5sum)) {
728 fi->actions[i] = FA_BACKUP;
729 /*@switchbreak@*/ break;
734 fi->actions[i] = FA_ERASE;
735 /*@switchbreak@*/ break;
739 uint_32 s = BLOCK_ROUND(fi->fsizes[i], ds->bsize);
741 switch (fi->actions[i]) {
747 /*@switchbreak@*/ break;
750 * FIXME: If two packages share a file (same md5sum), and
751 * that file is being replaced on disk, will ds->bneeded get
752 * decremented twice? Quite probably!
756 ds->bneeded -= BLOCK_ROUND(fi->replacedSizes[i], ds->bsize);
757 /*@switchbreak@*/ break;
762 /*@switchbreak@*/ break;
765 /*@switchbreak@*/ break;
768 ds->bneeded -= BLOCK_ROUND(fixupSize, ds->bsize);
774 * Ensure that current package is newer than installed package.
775 * @param ts transaction set
776 * @param p current transaction element
777 * @param h installed header
778 * @return 0 if not newer, 1 if okay
780 static int ensureOlder(rpmTransactionSet ts,
781 const transactionElement p, const Header h)
784 int_32 reqFlags = (RPMSENSE_LESS | RPMSENSE_EQUAL);
791 if (p == NULL || h == NULL)
794 nb = strlen(teGetNEVR(p)) + (teGetE(p) != NULL ? strlen(teGetE(p)) : 0) + 1;
798 if (teGetE(p) != NULL) t = stpcpy( stpcpy(t, teGetE(p)), ":");
799 if (teGetV(p) != NULL) t = stpcpy(t, teGetV(p));
801 if (teGetR(p) != NULL) t = stpcpy(t, teGetR(p));
803 req = dsSingle(RPMTAG_REQUIRENAME, teGetN(p), reqEVR, reqFlags);
804 rc = headerMatchesDepFlags(h, req);
808 const char * altNEVR = hGetNEVR(h, NULL);
809 rpmProblemSetAppend(ts->probs, RPMPROB_OLDPACKAGE,
810 teGetNEVR(p), teGetKey(p),
814 altNEVR = _free(altNEVR);
824 /*@-mustmod@*/ /* FIX: fi->actions is modified. */
825 static void skipFiles(const rpmTransactionSet ts, TFI_t fi)
826 /*@globals rpmGlobalMacroContext @*/
827 /*@modifies fi, rpmGlobalMacroContext @*/
829 int noDocs = (ts->transFlags & RPMTRANS_FLAG_NODOCS);
830 char ** netsharedPaths = NULL;
831 const char ** languages;
832 const char * dn, * bn;
833 int dnlen, bnlen, ix;
841 noDocs = rpmExpandNumeric("%{_excludedocs}");
843 { const char *tmpPath = rpmExpand("%{_netsharedpath}", NULL);
845 if (tmpPath && *tmpPath != '%')
846 netsharedPaths = splitString(tmpPath, strlen(tmpPath), ':');
848 tmpPath = _free(tmpPath);
851 s = rpmExpand("%{_install_langs}", NULL);
853 if (!(s && *s != '%'))
856 languages = (const char **) splitString(s, strlen(s), ':');
862 /* Compute directory refcount, skip directory if now empty. */
864 drc = alloca(dc * sizeof(*drc));
865 memset(drc, 0, dc * sizeof(*drc));
866 dff = alloca(dc * sizeof(*dff));
867 memset(dff, 0, dc * sizeof(*dff));
870 if (fi != NULL) /* XXX lclint */
871 while ((i = tfiNext(fi)) >= 0)
881 continue; /* XXX can't happen */
885 /* Don't bother with skipped files */
886 if (XFA_SKIPPING(fi->actions[i])) {
892 * Skip net shared paths.
893 * Net shared paths are not relative to the current root (though
894 * they do need to take package relocations into account).
896 for (nsp = netsharedPaths; nsp && *nsp; nsp++) {
901 if (strncmp(dn, *nsp, len))
902 /*@innercontinue@*/ continue;
903 /* Only directories or complete file paths can be net shared */
904 if (!(dn[len] == '/' || dn[len] == '\0'))
905 /*@innercontinue@*/ continue;
907 if (len < (dnlen + bnlen))
908 /*@innercontinue@*/ continue;
909 if (strncmp(dn, *nsp, dnlen))
910 /*@innercontinue@*/ continue;
911 if (strncmp(bn, (*nsp) + dnlen, bnlen))
912 /*@innercontinue@*/ continue;
914 /* Only directories or complete file paths can be net shared */
915 if (!((*nsp)[len] == '/' || (*nsp)[len] == '\0'))
916 /*@innercontinue@*/ continue;
919 /*@innerbreak@*/ break;
923 drc[ix]--; dff[ix] = 1;
924 fi->actions[i] = FA_SKIPNETSHARED;
929 * Skip i18n language specific files.
931 if (fi->flangs && languages && *fi->flangs[i]) {
932 const char **lang, *l, *le;
933 for (lang = languages; *lang != NULL; lang++) {
934 if (!strcmp(*lang, "all"))
935 /*@innerbreak@*/ break;
936 for (l = fi->flangs[i]; *l != '\0'; l = le) {
937 for (le = l; *le != '\0' && *le != '|'; le++)
939 if ((le-l) > 0 && !strncmp(*lang, l, (le-l)))
940 /*@innerbreak@*/ break;
941 if (*le == '|') le++; /* skip over | */
944 /*@innerbreak@*/ break;
947 drc[ix]--; dff[ix] = 1;
948 fi->actions[i] = FA_SKIPNSTATE;
954 * Skip documentation if requested.
956 if (noDocs && (tfiGetFFlags(fi) & RPMFILE_DOC)) {
957 drc[ix]--; dff[ix] = 1;
958 fi->actions[i] = FA_SKIPNSTATE;
963 /* Skip (now empty) directories that had skipped files. */
965 if (fi != NULL) /* XXX can't happen */
966 for (j = 0; j < dc; j++)
968 if ((fi = tdiInit(fi)) != NULL)
969 while (j = tdiNext(fi) >= 0)
973 if (drc[j]) continue; /* dir still has files. */
974 if (!dff[j]) continue; /* dir was not emptied here. */
976 /* Find parent directory and basename. */
977 dn = fi->dnl[j]; dnlen = strlen(dn) - 1;
978 bn = dn + dnlen; bnlen = 0;
979 while (bn > dn && bn[-1] != '/') {
985 /* If explicitly included in the package, skip the directory. */
987 if (fi != NULL) /* XXX lclint */
988 while ((i = tfiNext(fi)) >= 0) {
991 if (XFA_SKIPPING(fi->actions[i]))
992 /*@innercontinue@*/ continue;
993 if (whatis(fi->fmodes[i]) != XDIR)
994 /*@innercontinue@*/ continue;
995 dir = fi->dnl[fi->dil[i]];
996 if (strlen(dir) != dnlen)
997 /*@innercontinue@*/ continue;
998 if (strncmp(dir, dn, dnlen))
999 /*@innercontinue@*/ continue;
1000 if (strlen(fi->bnl[i]) != bnlen)
1001 /*@innercontinue@*/ continue;
1002 if (strncmp(fi->bnl[i], bn, bnlen))
1003 /*@innercontinue@*/ continue;
1004 rpmMessage(RPMMESS_DEBUG, _("excluding directory %s\n"), dn);
1005 fi->actions[i] = FA_SKIPNSTATE;
1006 /*@innerbreak@*/ break;
1010 if (netsharedPaths) freeSplitString(netsharedPaths);
1011 #ifdef DYING /* XXX freeFi will deal with this later. */
1012 fi->flangs = _free(fi->flangs);
1014 if (languages) freeSplitString((char **)languages);
1019 * Return transaction element's file info.
1020 * @todo Take a TFI_t refcount here.
1021 * @param tei transaction element iterator
1022 * @return transaction element file info
1025 TFI_t teiGetFi(const teIterator tei)
1030 if (tei != NULL && tei->ocsave != -1) {
1031 /*@-type -abstract@*/ /* FIX: transactionElement not opaque */
1032 transactionElement te = tei->ts->order[tei->ocsave];
1034 if ((fi = te->fi) != NULL)
1037 /*@=type =abstract@*/
1039 /*@-compdef -refcounttrans -usereleased @*/
1041 /*@=compdef =refcounttrans =usereleased @*/
1044 #define NOTIFY(_ts, _al) if ((_ts)->notify) (void) (_ts)->notify _al
1046 int rpmtsRun(rpmTransactionSet ts,
1047 rpmProblemSet okProbs, rpmprobFilterFlags ignoreSet)
1051 int totalFileCount = 0;
1053 struct diskspaceInfo * dip;
1054 sharedFileInfo shared, sharedList;
1058 fingerPrintCache fpc;
1059 PSM_t psm = memset(alloca(sizeof(*psm)), 0, sizeof(*psm));
1060 teIterator pi; transactionElement p;
1061 teIterator qi; transactionElement q;
1064 /* FIXME: what if the same package is included in ts twice? */
1066 if (ts->transFlags & RPMTRANS_FLAG_NOSCRIPTS)
1067 ts->transFlags |= (_noTransScripts | _noTransTriggers);
1068 if (ts->transFlags & RPMTRANS_FLAG_NOTRIGGERS)
1069 ts->transFlags |= _noTransTriggers;
1071 /* XXX MULTILIB is broken, as packages can and do execute /sbin/ldconfig. */
1072 if (ts->transFlags & (RPMTRANS_FLAG_JUSTDB | RPMTRANS_FLAG_MULTILIB))
1073 ts->transFlags |= (_noTransScripts | _noTransTriggers);
1075 ts->probs = rpmProblemSetFree(ts->probs);
1076 ts->probs = rpmProblemSetCreate();
1077 ts->ignoreSet = ignoreSet;
1078 ts->currDir = _free(ts->currDir);
1079 ts->currDir = currentDirectory();
1081 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
1082 ts->id = (int_32) time(NULL);
1084 memset(psm, 0, sizeof(*psm));
1085 psm->ts = rpmtsLink(ts, "tsRun");
1087 /* Get available space on mounted file systems. */
1088 if (!(ts->ignoreSet & RPMPROB_FILTER_DISKSPACE) &&
1089 !rpmGetFilesystemList(&ts->filesystems, &ts->filesystemCount))
1093 rpmMessage(RPMMESS_DEBUG, _("getting list of mounted filesystems\n"));
1095 ts->di = _free(ts->di);
1096 dip = ts->di = xcalloc((ts->filesystemCount + 1), sizeof(*ts->di));
1098 for (i = 0; (i < ts->filesystemCount) && dip; i++) {
1099 #if STATFS_IN_SYS_STATVFS
1101 memset(&sfb, 0, sizeof(sfb));
1102 if (statvfs(ts->filesystems[i], &sfb))
1106 /* This platform has the 4-argument version of the statfs call. The last two
1107 * should be the size of struct statfs and 0, respectively. The 0 is the
1108 * filesystem type, and is always 0 when statfs is called on a mounted
1109 * filesystem, as we're doing.
1111 memset(&sfb, 0, sizeof(sfb));
1112 if (statfs(ts->filesystems[i], &sfb, sizeof(sfb), 0))
1114 memset(&sfb, 0, sizeof(sfb));
1115 if (statfs(ts->filesystems[i], &sfb))
1121 ts->di[i].bsize = sfb.f_bsize;
1122 ts->di[i].bneeded = 0;
1123 ts->di[i].ineeded = 0;
1124 #ifdef STATFS_HAS_F_BAVAIL
1125 ts->di[i].bavail = sfb.f_bavail;
1127 /* FIXME: the statfs struct doesn't have a member to tell how many blocks are
1128 * available for non-superusers. f_blocks - f_bfree is probably too big, but
1129 * it's about all we can do.
1131 ts->di[i].bavail = sfb.f_blocks - sfb.f_bfree;
1133 /* XXX Avoid FAT and other file systems that have not inodes. */
1134 ts->di[i].iavail = !(sfb.f_ffree == 0 && sfb.f_files == 0)
1137 xx = stat(ts->filesystems[i], &sb);
1138 ts->di[i].dev = sb.st_dev;
1142 if (dip) ts->di[i].bsize = 0;
1145 /* ===============================================
1146 * For packages being installed:
1147 * - verify package arch/os.
1148 * - verify package epoch:version-release is newer.
1150 * For packages being removed:
1153 /* The ordering doesn't matter here */
1154 pi = teInitIterator(ts);
1155 while ((p = teNext(pi, TR_ADDED)) != NULL) {
1156 rpmdbMatchIterator mi;
1159 if ((fi = teiGetFi(pi)) == NULL)
1160 continue; /* XXX can't happen */
1163 if (!(ts->ignoreSet & RPMPROB_FILTER_IGNOREARCH))
1164 if (!archOkay(teGetA(p)))
1165 rpmProblemSetAppend(ts->probs, RPMPROB_BADARCH,
1166 teGetNEVR(p), teGetKey(p),
1170 if (!(ts->ignoreSet & RPMPROB_FILTER_IGNOREOS))
1171 if (!osOkay(teGetO(p)))
1172 rpmProblemSetAppend(ts->probs, RPMPROB_BADOS,
1173 teGetNEVR(p), teGetKey(p),
1177 if (!(ts->ignoreSet & RPMPROB_FILTER_OLDPACKAGE)) {
1179 mi = rpmtsInitIterator(ts, RPMTAG_NAME, teGetN(p), 0);
1180 while ((h = rpmdbNextIterator(mi)) != NULL)
1181 xx = ensureOlder(ts, p, h);
1182 mi = rpmdbFreeIterator(mi);
1185 /* XXX multilib should not display "already installed" problems */
1186 if (!(ts->ignoreSet & RPMPROB_FILTER_REPLACEPKG) && !teGetMultiLib(p)) {
1187 mi = rpmtsInitIterator(ts, RPMTAG_NAME, teGetN(p), 0);
1188 xx = rpmdbSetIteratorRE(mi, RPMTAG_VERSION, RPMMIRE_DEFAULT,
1190 xx = rpmdbSetIteratorRE(mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT,
1193 while (rpmdbNextIterator(mi) != NULL) {
1194 rpmProblemSetAppend(ts->probs, RPMPROB_PKG_INSTALLED,
1195 teGetNEVR(p), teGetKey(p),
1198 /*@innerbreak@*/ break;
1200 mi = rpmdbFreeIterator(mi);
1203 /* Count no. of files (if any). */
1204 totalFileCount += fc;
1207 pi = teFreeIterator(pi);
1209 /* The ordering doesn't matter here */
1210 pi = teInitIterator(ts);
1211 while ((p = teNext(pi, TR_REMOVED)) != NULL) {
1214 if ((fi = teiGetFi(pi)) == NULL)
1215 continue; /* XXX can't happen */
1218 totalFileCount += fc;
1220 pi = teFreeIterator(pi);
1222 /* ===============================================
1223 * Initialize transaction element file info for package:
1227 * FIXME?: we'd be better off assembling one very large file list and
1228 * calling fpLookupList only once. I'm not sure that the speedup is
1229 * worth the trouble though.
1231 pi = teInitIterator(ts);
1232 while ((p = teNextIterator(pi)) != NULL) {
1235 if ((fi = teiGetFi(pi)) == NULL)
1236 continue; /* XXX can't happen */
1239 #ifdef DYING /* XXX W2DO? this is now done in teiGetFi, okay ??? */
1240 fi->magic = TFIMAGIC;
1245 switch (teGetType(p)) {
1248 /* Skip netshared paths, not our i18n files, and excluded docs */
1251 /*@switchbreak@*/ break;
1253 fi->record = teGetDBOffset(p);
1254 /*@switchbreak@*/ break;
1258 fi->fps = (fc > 0 ? xmalloc(fc * sizeof(*fi->fps)) : NULL);
1260 pi = teFreeIterator(pi);
1262 if (!ts->chrootDone) {
1264 /*@-superuser -noeffect @*/
1265 xx = chroot(ts->rootDir);
1266 /*@=superuser =noeffect @*/
1268 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 1;
1271 ts->ht = htCreate(totalFileCount * 2, 0, 0, fpHashFunction, fpEqual);
1272 fpc = fpCacheCreate(totalFileCount);
1274 /* ===============================================
1275 * Add fingerprint for each file not skipped.
1277 pi = teInitIterator(ts);
1278 while ((p = teNextIterator(pi)) != NULL) {
1281 if ((fi = teiGetFi(pi)) == NULL)
1282 continue; /* XXX can't happen */
1285 fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fc, fi->fps);
1287 fi = tfiInit(fi, 0);
1288 if (fi != NULL) /* XXX lclint */
1289 while ((i = tfiNext(fi)) >= 0) {
1290 if (XFA_SKIPPING(fi->actions[i]))
1291 /*@innercontinue@*/ continue;
1292 /*@-dependenttrans@*/
1293 htAddEntry(ts->ht, fi->fps + i, (void *) fi);
1294 /*@=dependenttrans@*/
1298 pi = teFreeIterator(pi);
1300 /*@-noeffectuncon @*/ /* FIX: check rc */
1301 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_START, 6, ts->orderCount,
1302 NULL, ts->notifyData));
1303 /*@=noeffectuncon@*/
1305 /* ===============================================
1306 * Compute file disposition for each package in transaction set.
1308 pi = teInitIterator(ts);
1309 while ((p = teNextIterator(pi)) != NULL) {
1310 dbiIndexSet * matches;
1314 if ((fi = teiGetFi(pi)) == NULL)
1315 continue; /* XXX can't happen */
1318 /*@-noeffectuncon @*/ /* FIX: check rc */
1319 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_PROGRESS, teiGetOc(pi),
1320 ts->orderCount, NULL, ts->notifyData));
1321 /*@=noeffectuncon@*/
1323 if (fc == 0) continue;
1325 /* Extract file info for all files in this package from the database. */
1326 matches = xcalloc(fc, sizeof(*matches));
1327 if (rpmdbFindFpList(ts->rpmdb, fi->fps, matches, fc)) {
1328 psm->ts = rpmtsUnlink(ts, "tsRun (rpmFindFpList fail)");
1329 return 1; /* XXX WTFO? */
1333 fi = tfiInit(fi, 0);
1334 while ((i = tfiNext(fi)) >= 0)
1335 numShared += dbiIndexSetCount(matches[i]);
1337 /* Build sorted file info list for this package. */
1338 shared = sharedList = xcalloc((numShared + 1), sizeof(*sharedList));
1340 fi = tfiInit(fi, 0);
1341 while ((i = tfiNext(fi)) >= 0) {
1343 * Take care not to mark files as replaced in packages that will
1344 * have been removed before we will get here.
1346 for (j = 0; j < dbiIndexSetCount(matches[i]); j++) {
1348 ro = dbiIndexRecordOffset(matches[i], j);
1350 qi = teInitIterator(ts);
1351 while ((q = teNext(qi, TR_REMOVED)) != NULL) {
1353 /*@innerbreak@*/ break;
1354 if (teGetDBOffset(q) == ro)
1357 qi = teFreeIterator(qi);
1359 shared->pkgFileNum = i;
1360 shared->otherPkg = dbiIndexRecordOffset(matches[i], j);
1361 shared->otherFileNum = dbiIndexRecordFileNumber(matches[i], j);
1362 shared->isRemoved = (knownBad == ro);
1365 matches[i] = dbiFreeIndexSet(matches[i]);
1367 numShared = shared - sharedList;
1368 shared->otherPkg = -1;
1369 matches = _free(matches);
1371 /* Sort file info by other package index (otherPkg) */
1372 qsort(sharedList, numShared, sizeof(*shared), sharedCmp);
1374 /* For all files from this package that are in the database ... */
1376 for (i = 0; i < numShared; i = nexti) {
1379 shared = sharedList + i;
1381 /* Find the end of the files in the other package. */
1382 for (nexti = i + 1; nexti < numShared; nexti++) {
1383 if (sharedList[nexti].otherPkg != shared->otherPkg)
1384 /*@innerbreak@*/ break;
1387 /* Is this file from a package being removed? */
1389 if (ts->removedPackages != NULL)
1390 for (j = 0; j < ts->numRemovedPackages; j++) {
1391 if (ts->removedPackages[j] != shared->otherPkg)
1392 /*@innercontinue@*/ continue;
1394 /*@innerbreak@*/ break;
1397 /* Determine the fate of each file. */
1398 switch (teGetType(p)) {
1400 xx = handleInstInstalledFiles(ts, p, fi, shared, nexti - i,
1401 !(beingRemoved || (ts->ignoreSet & RPMPROB_FILTER_REPLACEOLDFILES)));
1402 /*@switchbreak@*/ break;
1405 xx = handleRmvdInstalledFiles(ts, fi, shared, nexti - i);
1406 /*@switchbreak@*/ break;
1413 /* Update disk space needs on each partition for this package. */
1414 handleOverlappedFiles(ts, p, fi);
1416 /* Check added package has sufficient space on each partition used. */
1417 switch (teGetType(p)) {
1419 if (!(ts->di && tfiGetFC(fi) > 0))
1420 /*@switchbreak@*/ break;
1421 for (i = 0; i < ts->filesystemCount; i++) {
1425 /* XXX Avoid FAT and other file systems that have not inodes. */
1426 if (dip->iavail <= 0)
1427 /*@innercontinue@*/ continue;
1429 if (adj_fs_blocks(dip->bneeded) > dip->bavail) {
1430 rpmProblemSetAppend(ts->probs, RPMPROB_DISKSPACE,
1431 teGetNEVR(p), teGetKey(p),
1432 ts->filesystems[i], NULL, NULL,
1433 (adj_fs_blocks(dip->bneeded) - dip->bavail) * dip->bsize);
1436 if (adj_fs_blocks(dip->ineeded) > dip->iavail) {
1437 rpmProblemSetAppend(ts->probs, RPMPROB_DISKNODES,
1438 teGetNEVR(p), teGetKey(p),
1439 ts->filesystems[i], NULL, NULL,
1440 (adj_fs_blocks(dip->ineeded) - dip->iavail));
1443 /*@switchbreak@*/ break;
1445 /*@switchbreak@*/ break;
1448 pi = teFreeIterator(pi);
1450 if (ts->chrootDone) {
1451 /*@-superuser -noeffect @*/
1453 /*@=superuser =noeffect @*/
1455 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
1456 xx = chdir(ts->currDir);
1459 /*@-noeffectuncon @*/ /* FIX: check rc */
1460 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_STOP, 6, ts->orderCount,
1461 NULL, ts->notifyData));
1462 /*@=noeffectuncon @*/
1464 /* ===============================================
1465 * Free unused memory as soon as possible.
1467 pi = teInitIterator(ts);
1468 while ((p = teNextIterator(pi)) != NULL) {
1469 if ((fi = teiGetFi(pi)) == NULL)
1470 continue; /* XXX can't happen */
1471 if (tfiGetFC(fi) == 0)
1473 fi->fps = _free(fi->fps);
1475 pi = teFreeIterator(pi);
1481 /* ===============================================
1482 * If unfiltered problems exist, free memory and return.
1484 if ((ts->transFlags & RPMTRANS_FLAG_BUILD_PROBS)
1485 || (ts->probs->numProblems &&
1486 (okProbs != NULL || rpmProblemSetTrim(ts->probs, okProbs)))
1489 if (psm->ts != NULL)
1490 psm->ts = rpmtsUnlink(psm->ts, "tsRun (problems)");
1491 return ts->orderCount;
1494 /* ===============================================
1495 * Save removed files before erasing.
1497 if (ts->transFlags & (RPMTRANS_FLAG_DIRSTASH | RPMTRANS_FLAG_REPACKAGE)) {
1498 pi = teInitIterator(ts);
1499 while ((p = teNextIterator(pi)) != NULL) {
1501 switch (teGetType(p)) {
1503 /*@switchbreak@*/ break;
1505 if (!(ts->transFlags & RPMTRANS_FLAG_REPACKAGE))
1506 /*@switchbreak@*/ break;
1508 psm->fi = rpmfiLink(fi, "tsRepackage");
1509 xx = psmStage(psm, PSM_PKGSAVE);
1510 (void) rpmfiUnlink(fi, "tsRepackage");
1513 /*@switchbreak@*/ break;
1516 pi = teFreeIterator(pi);
1519 /* ===============================================
1520 * Install and remove packages.
1522 lastKey = (alKey)-2; /* erased packages have -1 */
1523 pi = teInitIterator(ts);
1524 /*@-branchstate@*/ /* FIX: fi reload needs work */
1525 while ((p = teNextIterator(pi)) != NULL) {
1531 if ((fi = teiGetFi(pi)) == NULL)
1532 continue; /* XXX can't happen */
1535 psm->fi = rpmfiLink(fi, "tsInstall");
1536 switch (teGetType(p)) {
1539 pkgKey = teGetAddedKey(p);
1541 rpmMessage(RPMMESS_DEBUG, "========== +++ %s\n", teGetNEVR(p));
1543 /*@-type@*/ /* FIX: transactionElement not opaque */
1545 /*@-noeffectuncon@*/ /* FIX: notify annotations */
1546 p->fd = ts->notify(fi->h, RPMCALLBACK_INST_OPEN_FILE, 0, 0,
1547 teGetKey(p), ts->notifyData);
1548 /*@=noeffectuncon@*/
1549 if (teGetFd(p) != NULL) {
1552 rpmrc = rpmReadPackageFile(ts, teGetFd(p),
1555 if (!(rpmrc == RPMRC_OK || rpmrc == RPMRC_BADSIZE)) {
1556 /*@-noeffectuncon@*/ /* FIX: notify annotations */
1557 p->fd = ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE,
1559 teGetKey(p), ts->notifyData);
1560 /*@=noeffectuncon@*/
1564 if (teGetFd(p) != NULL) gotfd = 1;
1569 if (teGetFd(p) != NULL) {
1571 char * fstates = fi->fstates;
1572 fileAction * actions = fi->actions;
1576 psm->fi = fiFree(psm->fi, 1);
1577 (void) fiFree(fi, 0);
1579 fi->magic = TFIMAGIC;
1582 (void) fiNew(ts, fi, h, RPMTAG_BASENAMES, 1);
1583 psm->fi = rpmfiLink(fi, "tsInstall");
1584 fi->fstates = _free(fi->fstates);
1585 fi->fstates = fstates;
1586 fi->actions = _free(fi->actions);
1587 fi->actions = actions;
1591 if (teGetMultiLib(p))
1592 ts->transFlags |= RPMTRANS_FLAG_MULTILIB;
1594 ts->transFlags &= ~RPMTRANS_FLAG_MULTILIB;
1596 if (psmStage(psm, PSM_PKGINSTALL)) {
1600 fi->h = headerFree(fi->h, "TR_ADDED fi->h free");
1606 h = headerFree(h, "TR_ADDED h free");
1609 /*@-noeffectuncon @*/ /* FIX: check rc */
1610 (void) ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE, 0, 0,
1611 teGetKey(p), ts->notifyData);
1612 /*@=noeffectuncon @*/
1617 /*@switchbreak@*/ break;
1619 rpmMessage(RPMMESS_DEBUG, "========== --- %s\n", teGetNEVR(p));
1620 /* If install failed, then we shouldn't erase. */
1621 if (teGetDependsOnKey(p) != lastKey) {
1622 if (psmStage(psm, PSM_PKGERASE))
1625 /*@switchbreak@*/ break;
1627 xx = rpmdbSync(ts->rpmdb);
1628 (void) rpmfiUnlink(psm->fi, "tsInstall");
1631 /*@-type@*/ /* FIX: p is almost opaque */
1632 p->fi = fiFree(fi, 1);
1636 pi = teFreeIterator(pi);
1638 psm->ts = rpmtsUnlink(psm->ts, "tsRun");
1640 /*@-nullstate@*/ /* FIX: ts->flList may be NULL */