2 * \file lib/transaction.c
7 #include <rpmmacro.h> /* XXX for rpmExpand */
10 #define _NEED_TEITERATOR 1
14 #include "legacy.h" /* XXX mdfile */
15 #include "misc.h" /* XXX stripTrailingChar, splitString, currentDirectory */
18 /*@-redecl -exportheadervar@*/
20 extern const char * chroot_prefix;
21 /*@=redecl =exportheadervar@*/
23 /* XXX FIXME: merge with existing (broken?) tests in system.h */
24 /* portability fiddles */
25 #if STATFS_IN_SYS_STATVFS
27 # include <sys/statvfs.h>
28 #if defined(__LCLINT__)
29 /*@-declundef -exportheader -protoparammatch @*/ /* LCL: missing annotation */
30 extern int statvfs (const char * file, /*@out@*/ struct statvfs * buf)
31 /*@globals fileSystem @*/
32 /*@modifies *buf, fileSystem @*/;
33 /*@=declundef =exportheader =protoparammatch @*/
37 # if STATFS_IN_SYS_VFS
40 # if STATFS_IN_SYS_MOUNT
41 # include <sys/mount.h>
43 # if STATFS_IN_SYS_STATFS
44 # include <sys/statfs.h>
52 /*@access FD_t @*/ /* XXX compared with NULL */
53 /*@access Header @*/ /* XXX compared with NULL */
54 /*@access rpmProblemSet @*/ /* XXX need rpmProblemSetOK() */
55 /*@access dbiIndexSet @*/
65 /*@access teIterator @*/
66 /*@access transactionElement @*/
67 /*@access rpmTransactionSet @*/
71 struct diskspaceInfo {
72 dev_t dev; /*!< File system device number. */
73 signed long bneeded; /*!< No. of blocks needed. */
74 signed long ineeded; /*!< No. of inodes needed. */
75 int bsize; /*!< File system block size. */
76 signed long bavail; /*!< No. of blocks available. */
77 signed long iavail; /*!< No. of inodes available. */
81 * Adjust for root only reserved space. On linux e2fs, this is 5%.
83 #define adj_fs_blocks(_nb) (((_nb) * 21) / 20)
85 /* argon thought a shift optimization here was a waste of time... he's
87 #define BLOCK_ROUND(size, block) (((size) + (block) - 1) / (block))
89 void rpmtransSetScriptFd(rpmTransactionSet ts, FD_t fd)
91 ts->scriptFd = (fd ? fdLink(fd, "rpmtransSetScriptFd") : NULL);
94 int rpmtransGetKeys(const rpmTransactionSet ts, fnpyKey ** ep, int * nep)
98 if (nep) *nep = ts->orderCount;
100 teIterator pi; transactionElement p;
103 *ep = e = xmalloc(ts->orderCount * sizeof(*e));
104 pi = teInitIterator(ts);
105 while ((p = teNextIterator(pi)) != NULL) {
108 /*@-dependenttrans@*/
110 /*@=dependenttrans@*/
111 /*@switchbreak@*/ break;
115 /*@switchbreak@*/ break;
119 pi = teFreeIterator(pi);
126 static int archOkay(/*@null@*/ const char * pkgArch)
129 if (pkgArch == NULL) return 0;
130 return (rpmMachineScore(RPM_MACHTABLE_INSTARCH, pkgArch) ? 1 : 0);
135 static int osOkay(/*@null@*/ const char * pkgOs)
138 if (pkgOs == NULL) return 0;
139 return (rpmMachineScore(RPM_MACHTABLE_INSTOS, pkgOs) ? 1 : 0);
144 static int sharedCmp(const void * one, const void * two)
147 sharedFileInfo a = (sharedFileInfo) one;
148 sharedFileInfo b = (sharedFileInfo) two;
150 if (a->otherPkg < b->otherPkg)
152 else if (a->otherPkg > b->otherPkg)
160 static fileAction decideFileFate(const char * fn,
162 const char * dbMd5, const char * dbLink, short newMode,
163 const char * newMd5, const char * newLink, int newFlags,
164 rpmtransFlags transFlags)
165 /*@globals fileSystem @*/
166 /*@modifies fileSystem @*/
169 const char * dbAttr, * newAttr;
170 fileTypes dbWhat, newWhat, diskWhat;
173 int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
175 if (lstat(fn, &sb)) {
177 * The file doesn't exist on the disk. Create it unless the new
178 * package has marked it as missingok, or allfiles is requested.
180 if (!(transFlags & RPMTRANS_FLAG_ALLFILES) &&
181 (newFlags & RPMFILE_MISSINGOK)) {
182 rpmMessage(RPMMESS_DEBUG, _("%s skipped due to missingok flag\n"),
190 diskWhat = whatis(sb.st_mode);
191 dbWhat = whatis(dbMode);
192 newWhat = whatis(newMode);
194 /* RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
195 them in older packages as well */
196 if (newWhat == XDIR) {
200 if (diskWhat != newWhat) {
202 } else if (newWhat != dbWhat && diskWhat != dbWhat) {
204 } else if (dbWhat != newWhat) {
206 } else if (dbWhat != LINK && dbWhat != REG) {
211 rc = mdfile(fn, buffer);
214 /* assume the file has been removed, don't freak */
219 } else /* dbWhat == LINK */ {
220 memset(buffer, 0, sizeof(buffer));
221 i = readlink(fn, buffer, sizeof(buffer) - 1);
223 /* assume the file has been removed, don't freak */
230 /* this order matters - we'd prefer to CREATE the file if at all
231 possible in case something else (like the timestamp) has changed */
233 if (!strcmp(dbAttr, buffer)) {
234 /* this config file has never been modified, so just replace it */
238 if (!strcmp(dbAttr, newAttr)) {
239 /* this file is the same in all versions of this package */
244 * The config file on the disk has been modified, but
245 * the ones in the two packages are different. It would
246 * be nice if RPM was smart enough to at least try and
247 * merge the difference ala CVS, but...
254 static int filecmp(short mode1, const char * md51, const char * link1,
255 short mode2, const char * md52, const char * link2)
258 fileTypes what1 = whatis(mode1);
259 fileTypes what2 = whatis(mode2);
261 if (what1 != what2) return 1;
264 return strcmp(link1, link2);
265 else if (what1 == REG)
266 return strcmp(md51, md52);
273 /* XXX only ts->{probs,rpmdb} modified */
274 static int handleInstInstalledFiles(const rpmTransactionSet ts,
275 transactionElement p, TFI_t fi,
276 sharedFileInfo shared,
277 int sharedCount, int reportConflicts)
278 /*@globals fileSystem @*/
279 /*@modifies ts, fi, fileSystem @*/
282 HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
283 rpmtransFlags transFlags = ts->transFlags;
284 rpmTagType oltype, omtype;
287 const char ** otherMd5s;
288 const char ** otherLinks;
289 const char * otherStates;
290 uint_32 * otherFlags;
291 uint_32 * otherSizes;
292 uint_16 * otherModes;
296 rpmdbMatchIterator mi;
298 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
299 &shared->otherPkg, sizeof(shared->otherPkg));
300 h = rpmdbNextIterator(mi);
302 mi = rpmdbFreeIterator(mi);
306 xx = hge(h, RPMTAG_FILEMD5S, &omtype, (void **) &otherMd5s, NULL);
307 xx = hge(h, RPMTAG_FILELINKTOS, &oltype, (void **) &otherLinks, NULL);
308 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
309 xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &otherModes, NULL);
310 xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &otherFlags, NULL);
311 xx = hge(h, RPMTAG_FILESIZES, NULL, (void **) &otherSizes, NULL);
313 fi->replaced = xcalloc(sharedCount, sizeof(*fi->replaced));
315 for (i = 0; i < sharedCount; i++, shared++) {
316 int otherFileNum, fileNum;
319 otherFileNum = shared->otherFileNum;
320 fileNum = shared->pkgFileNum;
322 (void) tfiSetFX(fi, fileNum);
325 /* XXX another tedious segfault, assume file state normal. */
326 if (otherStates && otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
329 if (XFA_SKIPPING(fi->actions[fileNum]))
332 if (filecmp(otherModes[otherFileNum],
333 otherMd5s[otherFileNum],
334 otherLinks[otherFileNum],
337 fi->flinks[fileNum])) {
338 if (reportConflicts) {
339 const char * altNEVR = hGetNEVR(h, NULL);
340 rpmProblemSetAppend(ts->probs, RPMPROB_FILE_CONFLICT,
342 tfiGetDN(fi), tfiGetBN(fi),
345 altNEVR = _free(altNEVR);
347 if (!(otherFlags[otherFileNum] | fi->fflags[fileNum])
349 /*@-assignexpose@*/ /* FIX: p->replaced, not fi */
350 if (!shared->isRemoved)
351 fi->replaced[numReplaced++] = *shared;
356 if ((otherFlags[otherFileNum] | fi->fflags[fileNum]) & RPMFILE_CONFIG) {
357 fi->actions[fileNum] = decideFileFate(fn,
358 otherModes[otherFileNum],
359 otherMd5s[otherFileNum],
360 otherLinks[otherFileNum],
368 fi->replacedSizes[fileNum] = otherSizes[otherFileNum];
371 otherMd5s = hfd(otherMd5s, omtype);
372 otherLinks = hfd(otherLinks, oltype);
373 mi = rpmdbFreeIterator(mi);
375 fi->replaced = xrealloc(fi->replaced, /* XXX memory leak */
376 sizeof(*fi->replaced) * (numReplaced + 1));
377 fi->replaced[numReplaced].otherPkg = 0;
384 /* XXX only ts->rpmdb modified */
385 static int handleRmvdInstalledFiles(const rpmTransactionSet ts, TFI_t fi,
386 sharedFileInfo shared, int sharedCount)
387 /*@globals fileSystem @*/
388 /*@modifies ts, fi, fileSystem @*/
392 const char * otherStates;
395 rpmdbMatchIterator mi;
397 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
398 &shared->otherPkg, sizeof(shared->otherPkg));
399 h = rpmdbNextIterator(mi);
401 mi = rpmdbFreeIterator(mi);
405 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
407 for (i = 0; i < sharedCount; i++, shared++) {
408 int otherFileNum, fileNum;
409 otherFileNum = shared->otherFileNum;
410 fileNum = shared->pkgFileNum;
412 if (otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
415 fi->actions[fileNum] = FA_SKIP;
418 mi = rpmdbFreeIterator(mi);
423 #define ISROOT(_d) (((_d)[0] == '/' && (_d)[1] == '\0') ? "" : (_d))
426 static int _fps_debug = 0;
428 static int fpsCompare (const void * one, const void * two)
430 const struct fingerPrint_s * a = (const struct fingerPrint_s *)one;
431 const struct fingerPrint_s * b = (const struct fingerPrint_s *)two;
432 int adnlen = strlen(a->entry->dirName);
433 int asnlen = (a->subDir ? strlen(a->subDir) : 0);
434 int abnlen = strlen(a->baseName);
435 int bdnlen = strlen(b->entry->dirName);
436 int bsnlen = (b->subDir ? strlen(b->subDir) : 0);
437 int bbnlen = strlen(b->baseName);
438 char * afn, * bfn, * t;
441 if (adnlen == 1 && asnlen != 0) adnlen = 0;
442 if (bdnlen == 1 && bsnlen != 0) bdnlen = 0;
444 afn = t = alloca(adnlen+asnlen+abnlen+2);
445 if (adnlen) t = stpcpy(t, a->entry->dirName);
447 if (a->subDir && asnlen) t = stpcpy(t, a->subDir);
448 if (abnlen) t = stpcpy(t, a->baseName);
449 if (afn[0] == '/' && afn[1] == '/') afn++;
451 bfn = t = alloca(bdnlen+bsnlen+bbnlen+2);
452 if (bdnlen) t = stpcpy(t, b->entry->dirName);
454 if (b->subDir && bsnlen) t = stpcpy(t, b->subDir);
455 if (bbnlen) t = stpcpy(t, b->baseName);
456 if (bfn[0] == '/' && bfn[1] == '/') bfn++;
458 rc = strcmp(afn, bfn);
461 fprintf(stderr, "\trc(%d) = strcmp(\"%s\", \"%s\")\n", rc, afn, bfn);
466 fprintf(stderr, "\t%s/%s%s\trc %d\n",
467 ISROOT(b->entry->dirName),
468 (b->subDir ? b->subDir : ""),
478 static int _linear_fps_search = 0;
480 static int findFps(const struct fingerPrint_s * fiFps,
481 const struct fingerPrint_s * otherFps,
489 fprintf(stderr, "==> %s/%s%s\n",
490 ISROOT(fiFps->entry->dirName),
491 (fiFps->subDir ? fiFps->subDir : ""),
495 if (_linear_fps_search) {
498 for (otherFileNum = 0; otherFileNum < otherFc; otherFileNum++, otherFps++) {
502 fprintf(stderr, "\t%4d %s/%s%s\n", otherFileNum,
503 ISROOT(otherFps->entry->dirName),
504 (otherFps->subDir ? otherFps->subDir : ""),
508 /* If the addresses are the same, so are the values. */
509 if (fiFps == otherFps)
512 /* Otherwise, compare fingerprints by value. */
513 /*@-nullpass@*/ /* LCL: looks good to me */
514 if (FP_EQUAL((*fiFps), (*otherFps)))
519 if (otherFileNum == otherFc) {
522 fprintf(stderr, "*** NULL %s/%s%s\n",
523 ISROOT(fiFps->entry->dirName),
524 (fiFps->subDir ? fiFps->subDir : ""),
533 const struct fingerPrint_s * bingoFps;
535 bingoFps = bsearch(fiFps, otherFps, otherFc, sizeof(*otherFps), fpsCompare);
536 if (bingoFps == NULL) {
538 fprintf(stderr, "*** NULL %s/%s%s\n",
539 ISROOT(fiFps->entry->dirName),
540 (fiFps->subDir ? fiFps->subDir : ""),
546 /* If the addresses are the same, so are the values. */
547 /*@-nullpass@*/ /* LCL: looks good to me */
548 if (!(fiFps == bingoFps || FP_EQUAL((*fiFps), (*bingoFps)))) {
550 fprintf(stderr, "*** BAD %s/%s%s\n",
551 ISROOT(bingoFps->entry->dirName),
552 (bingoFps->subDir ? bingoFps->subDir : ""),
558 otherFileNum = (bingoFps != NULL ? (bingoFps - otherFps) : 0);
566 * Update disk space needs on each partition for this package.
568 /* XXX only ts->{probs,di} modified */
569 static void handleOverlappedFiles(const rpmTransactionSet ts,
570 const transactionElement p, TFI_t fi)
571 /*@globals fileSystem @*/
572 /*@modifies ts, fi, fileSystem @*/
574 struct diskspaceInfo * ds = NULL;
575 uint_32 fixupSize = 0;
581 while ((i = tfiNext(fi)) >= 0) {
582 struct fingerPrint_s * fiFps;
583 int otherPkgNum, otherFileNum;
588 if (XFA_SKIPPING(fi->actions[i]))
596 while (ds->bsize && ds->dev != fi->fps[i].entry->dev) ds++;
597 if (!ds->bsize) ds = NULL;
602 * Retrieve all records that apply to this file. Note that the
603 * file info records were built in the same order as the packages
604 * will be installed and removed so the records for an overlapped
605 * files will be sorted in exactly the same order.
607 (void) htGetEntry(ts->ht, fiFps,
608 (const void ***) &recs, &numRecs, NULL);
611 * If this package is being added, look only at other packages
612 * being added -- removed packages dance to a different tune.
613 * If both this and the other package are being added, overlapped
614 * files must be identical (or marked as a conflict). The
615 * disposition of already installed config files leads to
616 * a small amount of extra complexity.
618 * If this package is being removed, then there are two cases that
619 * need to be worried about:
620 * If the other package is being added, then skip any overlapped files
621 * so that this package removal doesn't nuke the overlapped files
622 * that were just installed.
623 * If both this and the other package are being removed, then each
624 * file removal from preceding packages needs to be skipped so that
625 * the file removal occurs only on the last occurence of an overlapped
626 * file in the transaction set.
630 /* Locate this overlapped file in the set of added/removed packages. */
631 for (j = 0; j < numRecs && recs[j] != fi; j++)
634 /* Find what the previous disposition of this file was. */
635 otherFileNum = -1; /* keep gcc quiet */
636 for (otherPkgNum = j - 1; otherPkgNum >= 0; otherPkgNum--) {
637 struct fingerPrint_s * otherFps;
640 otherFi = recs[otherPkgNum];
642 /* Added packages need only look at other added packages. */
643 if (p->type == TR_ADDED && otherFi->te->type != TR_ADDED)
644 /*@innercontinue@*/ continue;
646 otherFps = otherFi->fps;
647 otherFc = tfiGetFC(otherFi);
649 otherFileNum = findFps(fiFps, otherFps, otherFc);
651 /* XXX is this test still necessary? */
652 if (otherFi->actions[otherFileNum] != FA_UNKNOWN)
653 /*@innerbreak@*/ break;
659 if (otherPkgNum < 0) {
660 /* XXX is this test still necessary? */
661 if (fi->actions[i] != FA_UNKNOWN)
662 /*@switchbreak@*/ break;
663 if ((fi->fflags[i] & RPMFILE_CONFIG) &&
665 /* Here is a non-overlapped pre-existing config file. */
666 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
667 ? FA_ALTNAME : FA_BACKUP;
669 fi->actions[i] = FA_CREATE;
671 /*@switchbreak@*/ break;
674 assert(otherFi != NULL);
675 /* Mark added overlapped non-identical files as a conflict. */
676 if ((ts->ignoreSet & RPMPROB_FILTER_REPLACENEWFILES)
677 && filecmp(otherFi->fmodes[otherFileNum],
678 otherFi->fmd5s[otherFileNum],
679 otherFi->flinks[otherFileNum],
684 const char * altNEVR = otherFi->te->NEVR;
685 rpmProblemSetAppend(ts->probs, RPMPROB_NEW_FILE_CONFLICT,
692 /* Try to get the disk accounting correct even if a conflict. */
693 fixupSize = otherFi->fsizes[otherFileNum];
695 if ((fi->fflags[i] & RPMFILE_CONFIG) && !lstat(fn, &sb)) {
696 /* Here is an overlapped pre-existing config file. */
697 fi->actions[i] = (fi->fflags[i] & RPMFILE_NOREPLACE)
698 ? FA_ALTNAME : FA_SKIP;
700 fi->actions[i] = FA_CREATE;
702 } /*@switchbreak@*/ break;
705 if (otherPkgNum >= 0) {
706 assert(otherFi != NULL);
707 /* Here is an overlapped added file we don't want to nuke. */
708 if (otherFi->actions[otherFileNum] != FA_ERASE) {
709 /* On updates, don't remove files. */
710 fi->actions[i] = FA_SKIP;
711 /*@switchbreak@*/ break;
713 /* Here is an overlapped removed file: skip in previous. */
714 otherFi->actions[otherFileNum] = FA_SKIP;
716 if (XFA_SKIPPING(fi->actions[i]))
717 /*@switchbreak@*/ break;
718 if (fi->fstates && fi->fstates[i] != RPMFILE_STATE_NORMAL)
719 /*@switchbreak@*/ break;
720 if (!(S_ISREG(fi->fmodes[i]) && (fi->fflags[i] & RPMFILE_CONFIG))) {
721 fi->actions[i] = FA_ERASE;
722 /*@switchbreak@*/ break;
725 /* Here is a pre-existing modified config file that needs saving. */
727 if (!mdfile(fn, mdsum) && strcmp(fi->fmd5s[i], mdsum)) {
728 fi->actions[i] = FA_BACKUP;
729 /*@switchbreak@*/ break;
732 fi->actions[i] = FA_ERASE;
733 /*@switchbreak@*/ break;
737 uint_32 s = BLOCK_ROUND(fi->fsizes[i], ds->bsize);
739 switch (fi->actions[i]) {
745 /*@switchbreak@*/ break;
748 * FIXME: If two packages share a file (same md5sum), and
749 * that file is being replaced on disk, will ds->bneeded get
750 * decremented twice? Quite probably!
754 ds->bneeded -= BLOCK_ROUND(fi->replacedSizes[i], ds->bsize);
755 /*@switchbreak@*/ break;
760 /*@switchbreak@*/ break;
763 /*@switchbreak@*/ break;
766 ds->bneeded -= BLOCK_ROUND(fixupSize, ds->bsize);
772 * Ensure that current package is newer than installed package.
773 * @param ts transaction set
774 * @param p current transaction element
775 * @param h installed header
776 * @return 0 if not newer, 1 if okay
778 static int ensureOlder(rpmTransactionSet ts,
779 const transactionElement p, const Header h)
782 int_32 reqFlags = (RPMSENSE_LESS | RPMSENSE_EQUAL);
788 if (p == NULL || h == NULL)
791 t = alloca(strlen(p->NEVR) + (p->epoch != NULL ? strlen(p->epoch) : 0) + 1);
794 if (p->epoch != NULL) t = stpcpy( stpcpy(t, p->epoch), ":");
795 if (p->version != NULL) t = stpcpy(t, p->version);
797 if (p->release != NULL) t = stpcpy(t, p->release);
799 req = dsSingle(RPMTAG_REQUIRENAME, p->name, reqEVR, reqFlags);
800 rc = headerMatchesDepFlags(h, req);
804 const char * altNEVR = hGetNEVR(h, NULL);
805 rpmProblemSetAppend(ts->probs, RPMPROB_OLDPACKAGE,
810 altNEVR = _free(altNEVR);
820 /*@-mustmod@*/ /* FIX: fi->actions is modified. */
821 static void skipFiles(const rpmTransactionSet ts, TFI_t fi)
822 /*@globals rpmGlobalMacroContext @*/
823 /*@modifies fi, rpmGlobalMacroContext @*/
825 int noDocs = (ts->transFlags & RPMTRANS_FLAG_NODOCS);
826 char ** netsharedPaths = NULL;
827 const char ** languages;
828 const char * dn, * bn;
829 int dnlen, bnlen, ix;
837 noDocs = rpmExpandNumeric("%{_excludedocs}");
839 { const char *tmpPath = rpmExpand("%{_netsharedpath}", NULL);
841 if (tmpPath && *tmpPath != '%')
842 netsharedPaths = splitString(tmpPath, strlen(tmpPath), ':');
844 tmpPath = _free(tmpPath);
847 s = rpmExpand("%{_install_langs}", NULL);
849 if (!(s && *s != '%'))
852 languages = (const char **) splitString(s, strlen(s), ':');
858 /* Compute directory refcount, skip directory if now empty. */
860 drc = alloca(dc * sizeof(*drc));
861 memset(drc, 0, dc * sizeof(*drc));
862 dff = alloca(dc * sizeof(*dff));
863 memset(dff, 0, dc * sizeof(*dff));
865 if ((fi = tfiInit(fi, 0)) != NULL)
866 while ((i = tfiNext(fi)) >= 0)
876 continue; /* XXX can't happen */
880 /* Don't bother with skipped files */
881 if (XFA_SKIPPING(fi->actions[i])) {
887 * Skip net shared paths.
888 * Net shared paths are not relative to the current root (though
889 * they do need to take package relocations into account).
891 for (nsp = netsharedPaths; nsp && *nsp; nsp++) {
896 if (strncmp(dn, *nsp, len))
897 /*@innercontinue@*/ continue;
898 /* Only directories or complete file paths can be net shared */
899 if (!(dn[len] == '/' || dn[len] == '\0'))
900 /*@innercontinue@*/ continue;
902 if (len < (dnlen + bnlen))
903 /*@innercontinue@*/ continue;
904 if (strncmp(dn, *nsp, dnlen))
905 /*@innercontinue@*/ continue;
906 if (strncmp(bn, (*nsp) + dnlen, bnlen))
907 /*@innercontinue@*/ continue;
909 /* Only directories or complete file paths can be net shared */
910 if (!((*nsp)[len] == '/' || (*nsp)[len] == '\0'))
911 /*@innercontinue@*/ continue;
914 /*@innerbreak@*/ break;
918 drc[ix]--; dff[ix] = 1;
919 fi->actions[i] = FA_SKIPNETSHARED;
924 * Skip i18n language specific files.
926 if (fi->flangs && languages && *fi->flangs[i]) {
927 const char **lang, *l, *le;
928 for (lang = languages; *lang != NULL; lang++) {
929 if (!strcmp(*lang, "all"))
930 /*@innerbreak@*/ break;
931 for (l = fi->flangs[i]; *l != '\0'; l = le) {
932 for (le = l; *le != '\0' && *le != '|'; le++)
934 if ((le-l) > 0 && !strncmp(*lang, l, (le-l)))
935 /*@innerbreak@*/ break;
936 if (*le == '|') le++; /* skip over | */
939 /*@innerbreak@*/ break;
942 drc[ix]--; dff[ix] = 1;
943 fi->actions[i] = FA_SKIPNSTATE;
949 * Skip documentation if requested.
951 if (noDocs && (fi->fflags[i] & RPMFILE_DOC)) {
952 drc[ix]--; dff[ix] = 1;
953 fi->actions[i] = FA_SKIPNSTATE;
958 /* Skip (now empty) directories that had skipped files. */
960 if (fi != NULL) /* XXX can't happen */
961 for (j = 0; j < dc; j++)
963 if ((fi = tdiInit(fi)) != NULL)
964 while (j = tdiNext(fi) >= 0)
968 if (drc[j]) continue; /* dir still has files. */
969 if (!dff[j]) continue; /* dir was not emptied here. */
971 /* Find parent directory and basename. */
972 dn = fi->dnl[j]; dnlen = strlen(dn) - 1;
973 bn = dn + dnlen; bnlen = 0;
974 while (bn > dn && bn[-1] != '/') {
980 /* If explicitly included in the package, skip the directory. */
981 if ((fi = tfiInit(fi, 0)) != NULL)
982 while ((i = tfiNext(fi)) >= 0) {
985 if (XFA_SKIPPING(fi->actions[i]))
986 /*@innercontinue@*/ continue;
987 if (whatis(fi->fmodes[i]) != XDIR)
988 /*@innercontinue@*/ continue;
989 dir = fi->dnl[fi->dil[i]];
990 if (strlen(dir) != dnlen)
991 /*@innercontinue@*/ continue;
992 if (strncmp(dir, dn, dnlen))
993 /*@innercontinue@*/ continue;
994 if (strlen(fi->bnl[i]) != bnlen)
995 /*@innercontinue@*/ continue;
996 if (strncmp(fi->bnl[i], bn, bnlen))
997 /*@innercontinue@*/ continue;
998 rpmMessage(RPMMESS_DEBUG, _("excluding directory %s\n"), dn);
999 fi->actions[i] = FA_SKIPNSTATE;
1000 /*@innerbreak@*/ break;
1004 if (netsharedPaths) freeSplitString(netsharedPaths);
1005 #ifdef DYING /* XXX freeFi will deal with this later. */
1006 fi->flangs = _free(fi->flangs);
1008 if (languages) freeSplitString((char **)languages);
1013 * Return transaction element's file info.
1014 * @todo Take a TFI_t refcount here.
1015 * @param tei transaction element iterator
1016 * @return transaction element file info
1019 TFI_t teGetFi(const teIterator tei)
1024 if (tei != NULL && tei->ocsave != -1) {
1025 transactionElement te = tei->ts->order + tei->ocsave;
1026 if ((fi = te->fi) != NULL)
1029 /*@-compdef -refcounttrans -usereleased @*/
1031 /*@=compdef =refcounttrans =usereleased @*/
1034 #define NOTIFY(_ts, _al) if ((_ts)->notify) (void) (_ts)->notify _al
1036 int rpmRunTransactions( rpmTransactionSet ts,
1037 rpmCallbackFunction notify, rpmCallbackData notifyData,
1038 rpmProblemSet okProbs, rpmProblemSet * newProbs,
1039 rpmtransFlags transFlags, rpmprobFilterFlags ignoreSet)
1043 int totalFileCount = 0;
1045 struct diskspaceInfo * dip;
1046 sharedFileInfo shared, sharedList;
1050 fingerPrintCache fpc;
1051 PSM_t psm = memset(alloca(sizeof(*psm)), 0, sizeof(*psm));
1052 teIterator pi; transactionElement p;
1053 teIterator qi; transactionElement q;
1056 /* FIXME: what if the same package is included in ts twice? */
1058 ts->transFlags = transFlags;
1059 if (ts->transFlags & RPMTRANS_FLAG_NOSCRIPTS)
1060 ts->transFlags |= (_noTransScripts | _noTransTriggers);
1061 if (ts->transFlags & RPMTRANS_FLAG_NOTRIGGERS)
1062 ts->transFlags |= _noTransTriggers;
1064 /* XXX MULTILIB is broken, as packages can and do execute /sbin/ldconfig. */
1065 if (ts->transFlags & (RPMTRANS_FLAG_JUSTDB | RPMTRANS_FLAG_MULTILIB))
1066 ts->transFlags |= (_noTransScripts | _noTransTriggers);
1068 ts->notify = notify;
1069 ts->notifyData = notifyData;
1070 ts->probs = rpmProblemSetFree(ts->probs);
1071 ts->probs = rpmProblemSetCreate();
1072 *newProbs = rpmpsLink(ts->probs, "RunTransactions");
1073 ts->ignoreSet = ignoreSet;
1074 ts->currDir = _free(ts->currDir);
1075 ts->currDir = currentDirectory();
1077 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
1078 ts->id = (int_32) time(NULL);
1080 memset(psm, 0, sizeof(*psm));
1081 psm->ts = rpmtsLink(ts, "tsRun");
1083 /* Get available space on mounted file systems. */
1084 if (!(ts->ignoreSet & RPMPROB_FILTER_DISKSPACE) &&
1085 !rpmGetFilesystemList(&ts->filesystems, &ts->filesystemCount))
1089 rpmMessage(RPMMESS_DEBUG, _("getting list of mounted filesystems\n"));
1091 ts->di = _free(ts->di);
1092 dip = ts->di = xcalloc((ts->filesystemCount + 1), sizeof(*ts->di));
1094 for (i = 0; (i < ts->filesystemCount) && dip; i++) {
1095 #if STATFS_IN_SYS_STATVFS
1097 memset(&sfb, 0, sizeof(sfb));
1098 if (statvfs(ts->filesystems[i], &sfb))
1102 /* This platform has the 4-argument version of the statfs call. The last two
1103 * should be the size of struct statfs and 0, respectively. The 0 is the
1104 * filesystem type, and is always 0 when statfs is called on a mounted
1105 * filesystem, as we're doing.
1107 memset(&sfb, 0, sizeof(sfb));
1108 if (statfs(ts->filesystems[i], &sfb, sizeof(sfb), 0))
1110 memset(&sfb, 0, sizeof(sfb));
1111 if (statfs(ts->filesystems[i], &sfb))
1117 ts->di[i].bsize = sfb.f_bsize;
1118 ts->di[i].bneeded = 0;
1119 ts->di[i].ineeded = 0;
1120 #ifdef STATFS_HAS_F_BAVAIL
1121 ts->di[i].bavail = sfb.f_bavail;
1123 /* FIXME: the statfs struct doesn't have a member to tell how many blocks are
1124 * available for non-superusers. f_blocks - f_bfree is probably too big, but
1125 * it's about all we can do.
1127 ts->di[i].bavail = sfb.f_blocks - sfb.f_bfree;
1129 /* XXX Avoid FAT and other file systems that have not inodes. */
1130 ts->di[i].iavail = !(sfb.f_ffree == 0 && sfb.f_files == 0)
1133 xx = stat(ts->filesystems[i], &sb);
1134 ts->di[i].dev = sb.st_dev;
1138 if (dip) ts->di[i].bsize = 0;
1141 /* ===============================================
1142 * For packages being installed:
1143 * - verify package arch/os.
1144 * - verify package epoch:version-release is newer.
1146 * For packages being removed:
1149 /* The ordering doesn't matter here */
1150 pi = teInitIterator(ts);
1151 while ((p = teNext(pi, TR_ADDED)) != NULL) {
1152 rpmdbMatchIterator mi;
1155 if ((fi = teGetFi(pi)) == NULL)
1156 continue; /* XXX can't happen */
1159 if (!(ts->ignoreSet & RPMPROB_FILTER_IGNOREARCH))
1160 if (!archOkay(p->arch))
1161 rpmProblemSetAppend(ts->probs, RPMPROB_BADARCH,
1166 if (!(ts->ignoreSet & RPMPROB_FILTER_IGNOREOS))
1168 rpmProblemSetAppend(ts->probs, RPMPROB_BADOS,
1173 if (!(ts->ignoreSet & RPMPROB_FILTER_OLDPACKAGE)) {
1175 mi = rpmtsInitIterator(ts, RPMTAG_NAME, p->name, 0);
1176 while ((h = rpmdbNextIterator(mi)) != NULL)
1177 xx = ensureOlder(ts, p, h);
1178 mi = rpmdbFreeIterator(mi);
1181 /* XXX multilib should not display "already installed" problems */
1182 if (!(ts->ignoreSet & RPMPROB_FILTER_REPLACEPKG) && !p->multiLib) {
1183 mi = rpmtsInitIterator(ts, RPMTAG_NAME, p->name, 0);
1184 xx = rpmdbSetIteratorRE(mi, RPMTAG_VERSION, RPMMIRE_DEFAULT,
1186 xx = rpmdbSetIteratorRE(mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT,
1189 while (rpmdbNextIterator(mi) != NULL) {
1190 rpmProblemSetAppend(ts->probs, RPMPROB_PKG_INSTALLED,
1194 /*@innerbreak@*/ break;
1196 mi = rpmdbFreeIterator(mi);
1199 /* Count no. of files (if any). */
1200 totalFileCount += fc;
1203 pi = teFreeIterator(pi);
1205 /* The ordering doesn't matter here */
1206 pi = teInitIterator(ts);
1207 while ((p = teNext(pi, TR_REMOVED)) != NULL) {
1210 if ((fi = teGetFi(pi)) == NULL)
1211 continue; /* XXX can't happen */
1214 totalFileCount += fc;
1216 pi = teFreeIterator(pi);
1218 /* ===============================================
1219 * Initialize transaction element file info for package:
1223 * FIXME?: we'd be better off assembling one very large file list and
1224 * calling fpLookupList only once. I'm not sure that the speedup is
1225 * worth the trouble though.
1227 pi = teInitIterator(ts);
1228 while ((p = teNextIterator(pi)) != NULL) {
1231 if ((fi = teGetFi(pi)) == NULL)
1232 continue; /* XXX can't happen */
1235 #ifdef DYING /* XXX W2DO? this is now done teGetFi, okay ??? */
1236 fi->magic = TFIMAGIC;
1244 /* Skip netshared paths, not our i18n files, and excluded docs */
1247 /*@switchbreak@*/ break;
1249 fi->record = p->u.removed.dboffset;
1250 /*@switchbreak@*/ break;
1254 fi->fps = (fc > 0 ? xmalloc(fc * sizeof(*fi->fps)) : NULL);
1256 pi = teFreeIterator(pi);
1258 if (!ts->chrootDone) {
1260 /*@-superuser -noeffect @*/
1261 xx = chroot(ts->rootDir);
1262 /*@=superuser =noeffect @*/
1264 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 1;
1267 chroot_prefix = ts->rootDir;
1272 ts->ht = htCreate(totalFileCount * 2, 0, 0, fpHashFunction, fpEqual);
1273 fpc = fpCacheCreate(totalFileCount);
1275 /* ===============================================
1276 * Add fingerprint for each file not skipped.
1278 pi = teInitIterator(ts);
1279 while ((p = teNextIterator(pi)) != NULL) {
1282 if ((fi = teGetFi(pi)) == NULL)
1283 continue; /* XXX can't happen */
1286 fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fc, fi->fps);
1288 if ((fi = tfiInit(fi, 0)) != NULL)
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 = teGetFi(pi)) == NULL)
1315 continue; /* XXX can't happen */
1318 /*@-noeffectuncon @*/ /* FIX: check rc */
1319 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_PROGRESS, teGetOc(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 if ((fi = tfiInit(fi, 0)) != NULL)
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 if ((fi = tfiInit(fi, 0)) != NULL)
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 (q->u.removed.dboffset == 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 ... */
1375 for (i = 0; i < numShared; i = nexti) {
1378 shared = sharedList + i;
1380 /* Find the end of the files in the other package. */
1381 for (nexti = i + 1; nexti < numShared; nexti++) {
1382 if (sharedList[nexti].otherPkg != shared->otherPkg)
1383 /*@innerbreak@*/ break;
1386 /* Is this file from a package being removed? */
1388 if (ts->removedPackages != NULL)
1389 for (j = 0; j < ts->numRemovedPackages; j++) {
1390 if (ts->removedPackages[j] != shared->otherPkg)
1391 /*@innercontinue@*/ continue;
1393 /*@innerbreak@*/ break;
1396 /* Determine the fate of each file. */
1399 xx = handleInstInstalledFiles(ts, p, fi, shared, nexti - i,
1400 !(beingRemoved || (ts->ignoreSet & RPMPROB_FILTER_REPLACEOLDFILES)));
1401 /*@switchbreak@*/ break;
1404 xx = handleRmvdInstalledFiles(ts, fi, shared, nexti - i);
1405 /*@switchbreak@*/ break;
1411 /* Update disk space needs on each partition for this package. */
1412 handleOverlappedFiles(ts, p, fi);
1414 /* Check added package has sufficient space on each partition used. */
1417 if (!(ts->di && tfiGetFC(fi) > 0))
1418 /*@switchbreak@*/ break;
1419 for (i = 0; i < ts->filesystemCount; i++) {
1423 /* XXX Avoid FAT and other file systems that have not inodes. */
1424 if (dip->iavail <= 0)
1425 /*@innercontinue@*/ continue;
1427 if (adj_fs_blocks(dip->bneeded) > dip->bavail) {
1428 rpmProblemSetAppend(ts->probs, RPMPROB_DISKSPACE,
1430 ts->filesystems[i], NULL, NULL,
1431 (adj_fs_blocks(dip->bneeded) - dip->bavail) * dip->bsize);
1434 if (adj_fs_blocks(dip->ineeded) > dip->iavail) {
1435 rpmProblemSetAppend(ts->probs, RPMPROB_DISKNODES,
1437 ts->filesystems[i], NULL, NULL,
1438 (adj_fs_blocks(dip->ineeded) - dip->iavail));
1441 /*@switchbreak@*/ break;
1443 /*@switchbreak@*/ break;
1446 pi = teFreeIterator(pi);
1448 if (ts->chrootDone) {
1449 /*@-superuser -noeffect @*/
1451 /*@=superuser =noeffect @*/
1453 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
1455 chroot_prefix = NULL;
1457 xx = chdir(ts->currDir);
1460 /*@-noeffectuncon @*/ /* FIX: check rc */
1461 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_STOP, 6, ts->orderCount,
1462 NULL, ts->notifyData));
1463 /*@=noeffectuncon @*/
1465 /* ===============================================
1466 * Free unused memory as soon as possible.
1468 pi = teInitIterator(ts);
1469 while ((p = teNextIterator(pi)) != NULL) {
1470 if ((fi = teGetFi(pi)) == NULL)
1471 continue; /* XXX can't happen */
1472 if (tfiGetFC(fi) == 0)
1474 fi->fps = _free(fi->fps);
1476 pi = teFreeIterator(pi);
1482 /* ===============================================
1483 * If unfiltered problems exist, free memory and return.
1485 if ((ts->transFlags & RPMTRANS_FLAG_BUILD_PROBS)
1486 || (ts->probs->numProblems &&
1487 (okProbs != NULL || rpmProblemSetTrim(ts->probs, okProbs)))
1490 if (psm->ts != NULL)
1491 psm->ts = rpmtsUnlink(psm->ts, "tsRun (problems)");
1492 return ts->orderCount;
1495 /* ===============================================
1496 * Save removed files before erasing.
1498 if (ts->transFlags & (RPMTRANS_FLAG_DIRSTASH | RPMTRANS_FLAG_REPACKAGE)) {
1499 pi = teInitIterator(ts);
1500 while ((p = teNextIterator(pi)) != NULL) {
1504 /*@switchbreak@*/ break;
1506 if (!(ts->transFlags & RPMTRANS_FLAG_REPACKAGE))
1507 /*@switchbreak@*/ break;
1509 psm->fi = rpmfiLink(fi, "tsRepackage");
1510 xx = psmStage(psm, PSM_PKGSAVE);
1511 (void) rpmfiUnlink(fi, "tsRepackage");
1514 /*@switchbreak@*/ break;
1517 pi = teFreeIterator(pi);
1520 /* ===============================================
1521 * Install and remove packages.
1523 lastKey = (alKey)-2; /* erased packages have -1 */
1524 pi = teInitIterator(ts);
1525 /*@-branchstate@*/ /* FIX: fi reload needs work */
1526 while ((p = teNextIterator(pi)) != NULL) {
1532 if ((fi = teGetFi(pi)) == NULL)
1533 continue; /* XXX can't happen */
1536 psm->fi = rpmfiLink(fi, "tsInstall");
1540 pkgKey = p->u.addedKey;
1542 rpmMessage(RPMMESS_DEBUG, "========== +++ %s\n", p->NEVR);
1545 p->fd = ts->notify(fi->h, RPMCALLBACK_INST_OPEN_FILE, 0, 0,
1546 p->key, ts->notifyData);
1547 if (p->fd != NULL) {
1550 /*@=mustmod@*/ /* LCL: segfault */
1551 rpmrc = rpmReadPackageFile(ts, p->fd,
1552 "rpmRunTransactions", &h);
1555 if (!(rpmrc == RPMRC_OK || rpmrc == RPMRC_BADSIZE)) {
1556 p->fd = ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE,
1558 p->key, ts->notifyData);
1562 if (p->fd != NULL) gotfd = 1;
1566 if (p->fd != NULL) {
1568 char * fstates = fi->fstates;
1569 fileAction * actions = fi->actions;
1573 (void) fiFree(fi, 0);
1575 fi->magic = TFIMAGIC;
1578 (void) fiNew(ts, fi, h, RPMTAG_BASENAMES, 1);
1579 fi->fstates = _free(fi->fstates);
1580 fi->fstates = fstates;
1581 fi->actions = _free(fi->actions);
1582 fi->actions = actions;
1587 ts->transFlags |= RPMTRANS_FLAG_MULTILIB;
1589 if (psmStage(psm, PSM_PKGINSTALL)) {
1593 fi->h = headerFree(fi->h, "TR_ADDED fi->h free");
1599 h = headerFree(h, "TR_ADDED h free");
1602 /*@-noeffectuncon @*/ /* FIX: check rc */
1603 (void)ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE, 0, 0,
1604 p->key, ts->notifyData);
1605 /*@=noeffectuncon @*/
1608 (void) fiFree(fi, 0);
1609 /*@switchbreak@*/ break;
1611 rpmMessage(RPMMESS_DEBUG, "========== --- %s\n", p->NEVR);
1612 /* If install failed, then we shouldn't erase. */
1613 if (p->u.removed.dependsOnKey != lastKey) {
1614 if (psmStage(psm, PSM_PKGERASE))
1617 (void) fiFree(fi, 0);
1618 /*@switchbreak@*/ break;
1620 xx = rpmdbSync(ts->rpmdb);
1621 (void) rpmfiUnlink(psm->fi, "tsInstall");
1626 pi = teFreeIterator(pi);
1628 psm->ts = rpmtsUnlink(psm->ts, "tsRun");
1630 /*@-nullstate@*/ /* FIX: ts->flList may be NULL */