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.
212 if (ofi->md5s != NULL && nfi->md5s != NULL) {
214 const unsigned char * omd5 = ofi->md5s + (16 * ofi->i);
215 const unsigned char * nmd5 = nfi->md5s + (16 * nfi->i);
216 if (domd5(fn, buffer, 0))
217 return FA_CREATE; /* assume file has been removed */
218 if (!memcmp(omd5, buffer, 16))
219 return FA_CREATE; /* unmodified config file, replace. */
220 if (!memcmp(omd5, nmd5, 16))
221 return FA_SKIP; /* identical file, don't bother. */
224 const char * omd5 = ofi->fmd5s[ofi->i];
225 const char * nmd5 = nfi->fmd5s[nfi->i];
226 if (domd5(fn, buffer, 1))
227 return FA_CREATE; /* assume file has been removed */
228 if (!strcmp(omd5, buffer))
229 return FA_CREATE; /* unmodified config file, replace. */
230 if (!strcmp(omd5, nmd5))
231 return FA_SKIP; /* identical file, don't bother. */
234 } else /* dbWhat == LINK */ {
235 memset(buffer, 0, sizeof(buffer));
236 if (readlink(fn, buffer, sizeof(buffer) - 1) == -1)
237 return FA_CREATE; /* assume file has been removed */
238 if (!strcmp(ofi->flinks[ofi->i], buffer))
239 return FA_CREATE; /* unmodified config file, replace. */
240 if (!strcmp(ofi->flinks[ofi->i], nfi->flinks[nfi->i]))
241 return FA_SKIP; /* identical file, don't bother. */
245 * The config file on the disk has been modified, but
246 * the ones in the two packages are different. It would
247 * be nice if RPM was smart enough to at least try and
248 * merge the difference ala CVS, but...
255 static int filecmp(TFI_t afi, TFI_t bfi)
258 fileTypes awhat = whatis(afi->fmodes[afi->i]);
259 fileTypes bwhat = whatis(bfi->fmodes[bfi->i]);
261 if (awhat != bwhat) return 1;
264 const char * alink = afi->flinks[afi->i];
265 const char * blink = bfi->flinks[bfi->i];
266 return strcmp(alink, blink);
267 } else if (awhat == REG) {
269 if (afi->md5s != NULL && bfi->md5s != NULL) {
271 const unsigned char * amd5 = afi->md5s + (16 * afi->i);
272 const unsigned char * bmd5 = bfi->md5s + (16 * bfi->i);
273 return memcmp(amd5, bmd5, 16);
276 const char * amd5 = afi->fmd5s[afi->i];
277 const char * bmd5 = bfi->fmd5s[bfi->i];
278 return strcmp(amd5, bmd5);
288 /* XXX only ts->{probs,rpmdb} modified */
289 static int handleInstInstalledFiles(const rpmTransactionSet ts,
290 transactionElement p, TFI_t fi,
291 sharedFileInfo shared,
292 int sharedCount, int reportConflicts)
293 /*@globals fileSystem @*/
294 /*@modifies ts, fi, fileSystem @*/
296 const char * altNEVR = NULL;
297 TFI_t otherFi = NULL;
301 { rpmdbMatchIterator mi;
305 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
306 &shared->otherPkg, sizeof(shared->otherPkg));
307 while ((h = rpmdbNextIterator(mi)) != NULL) {
308 altNEVR = hGetNEVR(h, NULL);
309 otherFi = fiNew(ts, NULL, h, RPMTAG_BASENAMES, scareMem);
312 mi = rpmdbFreeIterator(mi);
318 fi->replaced = xcalloc(sharedCount, sizeof(*fi->replaced));
320 for (i = 0; i < sharedCount; i++, shared++) {
321 int otherFileNum, fileNum;
323 otherFileNum = shared->otherFileNum;
324 (void) tfiSetFX(otherFi, otherFileNum);
326 fileNum = shared->pkgFileNum;
327 (void) tfiSetFX(fi, fileNum);
330 /* XXX another tedious segfault, assume file state normal. */
331 if (otherStates && otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
335 if (XFA_SKIPPING(fi->actions[fileNum]))
338 if (filecmp(otherFi, fi)) {
339 if (reportConflicts) {
340 rpmProblemSetAppend(ts->probs, RPMPROB_FILE_CONFLICT,
341 teGetNEVR(p), teGetKey(p),
342 tfiGetDN(fi), tfiGetBN(fi),
346 if (!(tfiGetFFlags(otherFi) | tfiGetFFlags(fi)) & RPMFILE_CONFIG) {
347 /*@-assignexpose@*/ /* FIX: p->replaced, not fi */
348 if (!shared->isRemoved)
349 fi->replaced[numReplaced++] = *shared;
354 if ((tfiGetFFlags(otherFi) | tfiGetFFlags(fi)) & RPMFILE_CONFIG) {
356 action = decideFileFate(ts, otherFi, fi);
357 fi->actions[fileNum] = action;
359 fi->replacedSizes[fileNum] = otherFi->fsizes[otherFi->i];
363 altNEVR = _free(altNEVR);
364 otherFi = fiFree(otherFi, 1);
366 fi->replaced = xrealloc(fi->replaced, /* XXX memory leak */
367 sizeof(*fi->replaced) * (numReplaced + 1));
368 fi->replaced[numReplaced].otherPkg = 0;
375 /* XXX only ts->rpmdb modified */
376 static int handleRmvdInstalledFiles(const rpmTransactionSet ts, TFI_t fi,
377 sharedFileInfo shared, int sharedCount)
378 /*@globals fileSystem @*/
379 /*@modifies ts, fi, fileSystem @*/
383 const char * otherStates;
386 rpmdbMatchIterator mi;
388 mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
389 &shared->otherPkg, sizeof(shared->otherPkg));
390 h = rpmdbNextIterator(mi);
392 mi = rpmdbFreeIterator(mi);
396 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &otherStates, NULL);
398 for (i = 0; i < sharedCount; i++, shared++) {
399 int otherFileNum, fileNum;
400 otherFileNum = shared->otherFileNum;
401 fileNum = shared->pkgFileNum;
403 if (otherStates[otherFileNum] != RPMFILE_STATE_NORMAL)
406 fi->actions[fileNum] = FA_SKIP;
409 mi = rpmdbFreeIterator(mi);
414 #define ISROOT(_d) (((_d)[0] == '/' && (_d)[1] == '\0') ? "" : (_d))
417 static int _fps_debug = 0;
419 static int fpsCompare (const void * one, const void * two)
421 const struct fingerPrint_s * a = (const struct fingerPrint_s *)one;
422 const struct fingerPrint_s * b = (const struct fingerPrint_s *)two;
423 int adnlen = strlen(a->entry->dirName);
424 int asnlen = (a->subDir ? strlen(a->subDir) : 0);
425 int abnlen = strlen(a->baseName);
426 int bdnlen = strlen(b->entry->dirName);
427 int bsnlen = (b->subDir ? strlen(b->subDir) : 0);
428 int bbnlen = strlen(b->baseName);
429 char * afn, * bfn, * t;
432 if (adnlen == 1 && asnlen != 0) adnlen = 0;
433 if (bdnlen == 1 && bsnlen != 0) bdnlen = 0;
435 afn = t = alloca(adnlen+asnlen+abnlen+2);
436 if (adnlen) t = stpcpy(t, a->entry->dirName);
438 if (a->subDir && asnlen) t = stpcpy(t, a->subDir);
439 if (abnlen) t = stpcpy(t, a->baseName);
440 if (afn[0] == '/' && afn[1] == '/') afn++;
442 bfn = t = alloca(bdnlen+bsnlen+bbnlen+2);
443 if (bdnlen) t = stpcpy(t, b->entry->dirName);
445 if (b->subDir && bsnlen) t = stpcpy(t, b->subDir);
446 if (bbnlen) t = stpcpy(t, b->baseName);
447 if (bfn[0] == '/' && bfn[1] == '/') bfn++;
449 rc = strcmp(afn, bfn);
452 fprintf(stderr, "\trc(%d) = strcmp(\"%s\", \"%s\")\n", rc, afn, bfn);
457 fprintf(stderr, "\t%s/%s%s\trc %d\n",
458 ISROOT(b->entry->dirName),
459 (b->subDir ? b->subDir : ""),
469 static int _linear_fps_search = 0;
471 static int findFps(const struct fingerPrint_s * fiFps,
472 const struct fingerPrint_s * otherFps,
480 fprintf(stderr, "==> %s/%s%s\n",
481 ISROOT(fiFps->entry->dirName),
482 (fiFps->subDir ? fiFps->subDir : ""),
486 if (_linear_fps_search) {
489 for (otherFileNum = 0; otherFileNum < otherFc; otherFileNum++, otherFps++) {
493 fprintf(stderr, "\t%4d %s/%s%s\n", otherFileNum,
494 ISROOT(otherFps->entry->dirName),
495 (otherFps->subDir ? otherFps->subDir : ""),
499 /* If the addresses are the same, so are the values. */
500 if (fiFps == otherFps)
503 /* Otherwise, compare fingerprints by value. */
504 /*@-nullpass@*/ /* LCL: looks good to me */
505 if (FP_EQUAL((*fiFps), (*otherFps)))
510 if (otherFileNum == otherFc) {
513 fprintf(stderr, "*** NULL %s/%s%s\n",
514 ISROOT(fiFps->entry->dirName),
515 (fiFps->subDir ? fiFps->subDir : ""),
524 const struct fingerPrint_s * bingoFps;
526 bingoFps = bsearch(fiFps, otherFps, otherFc, sizeof(*otherFps), fpsCompare);
527 if (bingoFps == NULL) {
529 fprintf(stderr, "*** NULL %s/%s%s\n",
530 ISROOT(fiFps->entry->dirName),
531 (fiFps->subDir ? fiFps->subDir : ""),
537 /* If the addresses are the same, so are the values. */
538 /*@-nullpass@*/ /* LCL: looks good to me */
539 if (!(fiFps == bingoFps || FP_EQUAL((*fiFps), (*bingoFps)))) {
541 fprintf(stderr, "*** BAD %s/%s%s\n",
542 ISROOT(bingoFps->entry->dirName),
543 (bingoFps->subDir ? bingoFps->subDir : ""),
549 otherFileNum = (bingoFps != NULL ? (bingoFps - otherFps) : 0);
557 * Update disk space needs on each partition for this package.
559 /* XXX only ts->{probs,di} modified */
560 static void handleOverlappedFiles(const rpmTransactionSet ts,
561 const transactionElement p, TFI_t fi)
562 /*@globals fileSystem @*/
563 /*@modifies ts, fi, fileSystem @*/
565 struct diskspaceInfo * ds = NULL;
566 uint_32 fixupSize = 0;
571 if (fi != NULL) /* XXX lclint */
572 while ((i = tfiNext(fi)) >= 0) {
573 struct fingerPrint_s * fiFps;
574 int otherPkgNum, otherFileNum;
579 if (XFA_SKIPPING(fi->actions[i]))
587 while (ds->bsize && ds->dev != fi->fps[i].entry->dev) ds++;
588 if (!ds->bsize) ds = NULL;
593 * Retrieve all records that apply to this file. Note that the
594 * file info records were built in the same order as the packages
595 * will be installed and removed so the records for an overlapped
596 * files will be sorted in exactly the same order.
598 (void) htGetEntry(ts->ht, fiFps,
599 (const void ***) &recs, &numRecs, NULL);
602 * If this package is being added, look only at other packages
603 * being added -- removed packages dance to a different tune.
604 * If both this and the other package are being added, overlapped
605 * files must be identical (or marked as a conflict). The
606 * disposition of already installed config files leads to
607 * a small amount of extra complexity.
609 * If this package is being removed, then there are two cases that
610 * need to be worried about:
611 * If the other package is being added, then skip any overlapped files
612 * so that this package removal doesn't nuke the overlapped files
613 * that were just installed.
614 * If both this and the other package are being removed, then each
615 * file removal from preceding packages needs to be skipped so that
616 * the file removal occurs only on the last occurence of an overlapped
617 * file in the transaction set.
621 /* Locate this overlapped file in the set of added/removed packages. */
622 for (j = 0; j < numRecs && recs[j] != fi; j++)
625 /* Find what the previous disposition of this file was. */
626 otherFileNum = -1; /* keep gcc quiet */
628 for (otherPkgNum = j - 1; otherPkgNum >= 0; otherPkgNum--) {
629 struct fingerPrint_s * otherFps;
632 otherFi = recs[otherPkgNum];
634 /* Added packages need only look at other added packages. */
635 if (teGetType(p) == TR_ADDED && teGetType(otherFi->te) != TR_ADDED)
636 /*@innercontinue@*/ continue;
638 otherFps = otherFi->fps;
639 otherFc = tfiGetFC(otherFi);
641 otherFileNum = findFps(fiFps, otherFps, otherFc);
642 (void) tfiSetFX(otherFi, otherFileNum);
644 /* XXX is this test still necessary? */
645 if (otherFi->actions[otherFileNum] != FA_UNKNOWN)
646 /*@innerbreak@*/ break;
649 switch (teGetType(p)) {
652 if (otherPkgNum < 0) {
653 /* XXX is this test still necessary? */
654 if (fi->actions[i] != FA_UNKNOWN)
655 /*@switchbreak@*/ break;
656 if ((tfiGetFFlags(fi) & RPMFILE_CONFIG) &&
658 /* Here is a non-overlapped pre-existing config file. */
659 fi->actions[i] = (tfiGetFFlags(fi) & RPMFILE_NOREPLACE)
660 ? FA_ALTNAME : FA_BACKUP;
662 fi->actions[i] = FA_CREATE;
664 /*@switchbreak@*/ break;
667 assert(otherFi != NULL);
668 /* Mark added overlapped non-identical files as a conflict. */
669 if ((ts->ignoreSet & RPMPROB_FILTER_REPLACENEWFILES)
670 && filecmp(otherFi, fi))
672 rpmProblemSetAppend(ts->probs, RPMPROB_NEW_FILE_CONFLICT,
673 teGetNEVR(p), teGetKey(p),
675 teGetNEVR(otherFi->te),
679 /* Try to get the disk accounting correct even if a conflict. */
680 fixupSize = otherFi->fsizes[otherFileNum];
682 if ((tfiGetFFlags(fi) & RPMFILE_CONFIG) && !lstat(fn, &sb)) {
683 /* Here is an overlapped pre-existing config file. */
684 fi->actions[i] = (tfiGetFFlags(fi) & RPMFILE_NOREPLACE)
685 ? FA_ALTNAME : FA_SKIP;
687 fi->actions[i] = FA_CREATE;
689 } /*@switchbreak@*/ break;
692 if (otherPkgNum >= 0) {
693 assert(otherFi != NULL);
694 /* Here is an overlapped added file we don't want to nuke. */
695 if (otherFi->actions[otherFileNum] != FA_ERASE) {
696 /* On updates, don't remove files. */
697 fi->actions[i] = FA_SKIP;
698 /*@switchbreak@*/ break;
700 /* Here is an overlapped removed file: skip in previous. */
701 otherFi->actions[otherFileNum] = FA_SKIP;
703 if (XFA_SKIPPING(fi->actions[i]))
704 /*@switchbreak@*/ break;
705 if (fi->fstates && fi->fstates[i] != RPMFILE_STATE_NORMAL)
706 /*@switchbreak@*/ break;
707 if (!(S_ISREG(fi->fmodes[i]) && (tfiGetFFlags(fi) & RPMFILE_CONFIG))) {
708 fi->actions[i] = FA_ERASE;
709 /*@switchbreak@*/ break;
712 /* Here is a pre-existing modified config file that needs saving. */
715 if (fi->md5s != NULL) {
717 const unsigned char * md5 = fi->md5s + (16 * i);
718 if (!domd5(fn, md5sum, 0) && memcmp(md5, md5sum, 16)) {
719 fi->actions[i] = FA_BACKUP;
720 /*@switchbreak@*/ break;
724 const char * fmd5 = fi->fmd5s[i];
725 if (!domd5(fn, md5sum, 1) && strcmp(fmd5, md5sum)) {
726 fi->actions[i] = FA_BACKUP;
727 /*@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);
789 if (p == NULL || h == NULL)
792 nb = strlen(teGetNEVR(p)) + (teGetE(p) != NULL ? strlen(teGetE(p)) : 0) + 1;
796 if (teGetE(p) != NULL) t = stpcpy( stpcpy(t, teGetE(p)), ":");
797 if (teGetV(p) != NULL) t = stpcpy(t, teGetV(p));
799 if (teGetR(p) != NULL) t = stpcpy(t, teGetR(p));
801 req = dsSingle(RPMTAG_REQUIRENAME, teGetN(p), reqEVR, reqFlags);
802 rc = headerMatchesDepFlags(h, req);
806 const char * altNEVR = hGetNEVR(h, NULL);
807 rpmProblemSetAppend(ts->probs, RPMPROB_OLDPACKAGE,
808 teGetNEVR(p), teGetKey(p),
812 altNEVR = _free(altNEVR);
822 /*@-mustmod@*/ /* FIX: fi->actions is modified. */
823 static void skipFiles(const rpmTransactionSet ts, TFI_t fi)
824 /*@globals rpmGlobalMacroContext @*/
825 /*@modifies fi, rpmGlobalMacroContext @*/
827 int noDocs = (ts->transFlags & RPMTRANS_FLAG_NODOCS);
828 char ** netsharedPaths = NULL;
829 const char ** languages;
830 const char * dn, * bn;
831 int dnlen, bnlen, ix;
839 noDocs = rpmExpandNumeric("%{_excludedocs}");
841 { const char *tmpPath = rpmExpand("%{_netsharedpath}", NULL);
843 if (tmpPath && *tmpPath != '%')
844 netsharedPaths = splitString(tmpPath, strlen(tmpPath), ':');
846 tmpPath = _free(tmpPath);
849 s = rpmExpand("%{_install_langs}", NULL);
851 if (!(s && *s != '%'))
854 languages = (const char **) splitString(s, strlen(s), ':');
860 /* Compute directory refcount, skip directory if now empty. */
862 drc = alloca(dc * sizeof(*drc));
863 memset(drc, 0, dc * sizeof(*drc));
864 dff = alloca(dc * sizeof(*dff));
865 memset(dff, 0, dc * sizeof(*dff));
868 if (fi != NULL) /* XXX lclint */
869 while ((i = tfiNext(fi)) >= 0)
879 continue; /* XXX can't happen */
883 /* Don't bother with skipped files */
884 if (XFA_SKIPPING(fi->actions[i])) {
890 * Skip net shared paths.
891 * Net shared paths are not relative to the current root (though
892 * they do need to take package relocations into account).
894 for (nsp = netsharedPaths; nsp && *nsp; nsp++) {
899 if (strncmp(dn, *nsp, len))
900 /*@innercontinue@*/ continue;
901 /* Only directories or complete file paths can be net shared */
902 if (!(dn[len] == '/' || dn[len] == '\0'))
903 /*@innercontinue@*/ continue;
905 if (len < (dnlen + bnlen))
906 /*@innercontinue@*/ continue;
907 if (strncmp(dn, *nsp, dnlen))
908 /*@innercontinue@*/ continue;
909 if (strncmp(bn, (*nsp) + dnlen, bnlen))
910 /*@innercontinue@*/ continue;
912 /* Only directories or complete file paths can be net shared */
913 if (!((*nsp)[len] == '/' || (*nsp)[len] == '\0'))
914 /*@innercontinue@*/ continue;
917 /*@innerbreak@*/ break;
921 drc[ix]--; dff[ix] = 1;
922 fi->actions[i] = FA_SKIPNETSHARED;
927 * Skip i18n language specific files.
929 if (fi->flangs && languages && *fi->flangs[i]) {
930 const char **lang, *l, *le;
931 for (lang = languages; *lang != NULL; lang++) {
932 if (!strcmp(*lang, "all"))
933 /*@innerbreak@*/ break;
934 for (l = fi->flangs[i]; *l != '\0'; l = le) {
935 for (le = l; *le != '\0' && *le != '|'; le++)
937 if ((le-l) > 0 && !strncmp(*lang, l, (le-l)))
938 /*@innerbreak@*/ break;
939 if (*le == '|') le++; /* skip over | */
942 /*@innerbreak@*/ break;
945 drc[ix]--; dff[ix] = 1;
946 fi->actions[i] = FA_SKIPNSTATE;
952 * Skip documentation if requested.
954 if (noDocs && (tfiGetFFlags(fi) & RPMFILE_DOC)) {
955 drc[ix]--; dff[ix] = 1;
956 fi->actions[i] = FA_SKIPNSTATE;
961 /* Skip (now empty) directories that had skipped files. */
963 if (fi != NULL) /* XXX can't happen */
964 for (j = 0; j < dc; j++)
966 if ((fi = tdiInit(fi)) != NULL)
967 while (j = tdiNext(fi) >= 0)
971 if (drc[j]) continue; /* dir still has files. */
972 if (!dff[j]) continue; /* dir was not emptied here. */
974 /* Find parent directory and basename. */
975 dn = fi->dnl[j]; dnlen = strlen(dn) - 1;
976 bn = dn + dnlen; bnlen = 0;
977 while (bn > dn && bn[-1] != '/') {
983 /* If explicitly included in the package, skip the directory. */
985 if (fi != NULL) /* XXX lclint */
986 while ((i = tfiNext(fi)) >= 0) {
989 if (XFA_SKIPPING(fi->actions[i]))
990 /*@innercontinue@*/ continue;
991 if (whatis(fi->fmodes[i]) != XDIR)
992 /*@innercontinue@*/ continue;
993 dir = fi->dnl[fi->dil[i]];
994 if (strlen(dir) != dnlen)
995 /*@innercontinue@*/ continue;
996 if (strncmp(dir, dn, dnlen))
997 /*@innercontinue@*/ continue;
998 if (strlen(fi->bnl[i]) != bnlen)
999 /*@innercontinue@*/ continue;
1000 if (strncmp(fi->bnl[i], bn, bnlen))
1001 /*@innercontinue@*/ continue;
1002 rpmMessage(RPMMESS_DEBUG, _("excluding directory %s\n"), dn);
1003 fi->actions[i] = FA_SKIPNSTATE;
1004 /*@innerbreak@*/ break;
1008 if (netsharedPaths) freeSplitString(netsharedPaths);
1009 #ifdef DYING /* XXX freeFi will deal with this later. */
1010 fi->flangs = _free(fi->flangs);
1012 if (languages) freeSplitString((char **)languages);
1017 * Return transaction element's file info.
1018 * @todo Take a TFI_t refcount here.
1019 * @param tei transaction element iterator
1020 * @return transaction element file info
1023 TFI_t teiGetFi(const teIterator tei)
1028 if (tei != NULL && tei->ocsave != -1) {
1029 /*@-type -abstract@*/ /* FIX: transactionElement not opaque */
1030 transactionElement te = tei->ts->order[tei->ocsave];
1032 if ((fi = te->fi) != NULL)
1035 /*@=type =abstract@*/
1037 /*@-compdef -refcounttrans -usereleased @*/
1039 /*@=compdef =refcounttrans =usereleased @*/
1042 #define NOTIFY(_ts, _al) if ((_ts)->notify) (void) (_ts)->notify _al
1044 int rpmRunTransactions( rpmTransactionSet ts,
1045 rpmProblemSet okProbs,
1046 rpmprobFilterFlags ignoreSet)
1050 int totalFileCount = 0;
1052 struct diskspaceInfo * dip;
1053 sharedFileInfo shared, sharedList;
1057 fingerPrintCache fpc;
1058 PSM_t psm = memset(alloca(sizeof(*psm)), 0, sizeof(*psm));
1059 teIterator pi; transactionElement p;
1060 teIterator qi; transactionElement q;
1063 /* FIXME: what if the same package is included in ts twice? */
1065 if (ts->transFlags & RPMTRANS_FLAG_NOSCRIPTS)
1066 ts->transFlags |= (_noTransScripts | _noTransTriggers);
1067 if (ts->transFlags & RPMTRANS_FLAG_NOTRIGGERS)
1068 ts->transFlags |= _noTransTriggers;
1070 /* XXX MULTILIB is broken, as packages can and do execute /sbin/ldconfig. */
1071 if (ts->transFlags & (RPMTRANS_FLAG_JUSTDB | RPMTRANS_FLAG_MULTILIB))
1072 ts->transFlags |= (_noTransScripts | _noTransTriggers);
1074 ts->probs = rpmProblemSetFree(ts->probs);
1075 ts->probs = rpmProblemSetCreate();
1076 ts->ignoreSet = ignoreSet;
1077 ts->currDir = _free(ts->currDir);
1078 ts->currDir = currentDirectory();
1080 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
1081 ts->id = (int_32) time(NULL);
1083 memset(psm, 0, sizeof(*psm));
1084 psm->ts = rpmtsLink(ts, "tsRun");
1086 /* Get available space on mounted file systems. */
1087 if (!(ts->ignoreSet & RPMPROB_FILTER_DISKSPACE) &&
1088 !rpmGetFilesystemList(&ts->filesystems, &ts->filesystemCount))
1092 rpmMessage(RPMMESS_DEBUG, _("getting list of mounted filesystems\n"));
1094 ts->di = _free(ts->di);
1095 dip = ts->di = xcalloc((ts->filesystemCount + 1), sizeof(*ts->di));
1097 for (i = 0; (i < ts->filesystemCount) && dip; i++) {
1098 #if STATFS_IN_SYS_STATVFS
1100 memset(&sfb, 0, sizeof(sfb));
1101 if (statvfs(ts->filesystems[i], &sfb))
1105 /* This platform has the 4-argument version of the statfs call. The last two
1106 * should be the size of struct statfs and 0, respectively. The 0 is the
1107 * filesystem type, and is always 0 when statfs is called on a mounted
1108 * filesystem, as we're doing.
1110 memset(&sfb, 0, sizeof(sfb));
1111 if (statfs(ts->filesystems[i], &sfb, sizeof(sfb), 0))
1113 memset(&sfb, 0, sizeof(sfb));
1114 if (statfs(ts->filesystems[i], &sfb))
1120 ts->di[i].bsize = sfb.f_bsize;
1121 ts->di[i].bneeded = 0;
1122 ts->di[i].ineeded = 0;
1123 #ifdef STATFS_HAS_F_BAVAIL
1124 ts->di[i].bavail = sfb.f_bavail;
1126 /* FIXME: the statfs struct doesn't have a member to tell how many blocks are
1127 * available for non-superusers. f_blocks - f_bfree is probably too big, but
1128 * it's about all we can do.
1130 ts->di[i].bavail = sfb.f_blocks - sfb.f_bfree;
1132 /* XXX Avoid FAT and other file systems that have not inodes. */
1133 ts->di[i].iavail = !(sfb.f_ffree == 0 && sfb.f_files == 0)
1136 xx = stat(ts->filesystems[i], &sb);
1137 ts->di[i].dev = sb.st_dev;
1141 if (dip) ts->di[i].bsize = 0;
1144 /* ===============================================
1145 * For packages being installed:
1146 * - verify package arch/os.
1147 * - verify package epoch:version-release is newer.
1149 * For packages being removed:
1152 /* The ordering doesn't matter here */
1153 pi = teInitIterator(ts);
1154 while ((p = teNext(pi, TR_ADDED)) != NULL) {
1155 rpmdbMatchIterator mi;
1158 if ((fi = teiGetFi(pi)) == NULL)
1159 continue; /* XXX can't happen */
1162 if (!(ts->ignoreSet & RPMPROB_FILTER_IGNOREARCH))
1163 if (!archOkay(teGetA(p)))
1164 rpmProblemSetAppend(ts->probs, RPMPROB_BADARCH,
1165 teGetNEVR(p), teGetKey(p),
1169 if (!(ts->ignoreSet & RPMPROB_FILTER_IGNOREOS))
1170 if (!osOkay(teGetO(p)))
1171 rpmProblemSetAppend(ts->probs, RPMPROB_BADOS,
1172 teGetNEVR(p), teGetKey(p),
1176 if (!(ts->ignoreSet & RPMPROB_FILTER_OLDPACKAGE)) {
1178 mi = rpmtsInitIterator(ts, RPMTAG_NAME, teGetN(p), 0);
1179 while ((h = rpmdbNextIterator(mi)) != NULL)
1180 xx = ensureOlder(ts, p, h);
1181 mi = rpmdbFreeIterator(mi);
1184 /* XXX multilib should not display "already installed" problems */
1185 if (!(ts->ignoreSet & RPMPROB_FILTER_REPLACEPKG) && !teGetMultiLib(p)) {
1186 mi = rpmtsInitIterator(ts, RPMTAG_NAME, teGetN(p), 0);
1187 xx = rpmdbSetIteratorRE(mi, RPMTAG_VERSION, RPMMIRE_DEFAULT,
1189 xx = rpmdbSetIteratorRE(mi, RPMTAG_RELEASE, RPMMIRE_DEFAULT,
1192 while (rpmdbNextIterator(mi) != NULL) {
1193 rpmProblemSetAppend(ts->probs, RPMPROB_PKG_INSTALLED,
1194 teGetNEVR(p), teGetKey(p),
1197 /*@innerbreak@*/ break;
1199 mi = rpmdbFreeIterator(mi);
1202 /* Count no. of files (if any). */
1203 totalFileCount += fc;
1206 pi = teFreeIterator(pi);
1208 /* The ordering doesn't matter here */
1209 pi = teInitIterator(ts);
1210 while ((p = teNext(pi, TR_REMOVED)) != NULL) {
1213 if ((fi = teiGetFi(pi)) == NULL)
1214 continue; /* XXX can't happen */
1217 totalFileCount += fc;
1219 pi = teFreeIterator(pi);
1221 /* ===============================================
1222 * Initialize transaction element file info for package:
1226 * FIXME?: we'd be better off assembling one very large file list and
1227 * calling fpLookupList only once. I'm not sure that the speedup is
1228 * worth the trouble though.
1230 pi = teInitIterator(ts);
1231 while ((p = teNextIterator(pi)) != NULL) {
1234 if ((fi = teiGetFi(pi)) == NULL)
1235 continue; /* XXX can't happen */
1238 #ifdef DYING /* XXX W2DO? this is now done in teiGetFi, okay ??? */
1239 fi->magic = TFIMAGIC;
1244 switch (teGetType(p)) {
1247 /* Skip netshared paths, not our i18n files, and excluded docs */
1250 /*@switchbreak@*/ break;
1252 fi->record = teGetDBOffset(p);
1253 /*@switchbreak@*/ break;
1257 fi->fps = (fc > 0 ? xmalloc(fc * sizeof(*fi->fps)) : NULL);
1259 pi = teFreeIterator(pi);
1261 if (!ts->chrootDone) {
1263 /*@-superuser -noeffect @*/
1264 xx = chroot(ts->rootDir);
1265 /*@=superuser =noeffect @*/
1267 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 1;
1270 ts->ht = htCreate(totalFileCount * 2, 0, 0, fpHashFunction, fpEqual);
1271 fpc = fpCacheCreate(totalFileCount);
1273 /* ===============================================
1274 * Add fingerprint for each file not skipped.
1276 pi = teInitIterator(ts);
1277 while ((p = teNextIterator(pi)) != NULL) {
1280 if ((fi = teiGetFi(pi)) == NULL)
1281 continue; /* XXX can't happen */
1284 fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fc, fi->fps);
1286 fi = tfiInit(fi, 0);
1287 if (fi != NULL) /* XXX lclint */
1288 while ((i = tfiNext(fi)) >= 0) {
1289 if (XFA_SKIPPING(fi->actions[i]))
1290 /*@innercontinue@*/ continue;
1291 /*@-dependenttrans@*/
1292 htAddEntry(ts->ht, fi->fps + i, (void *) fi);
1293 /*@=dependenttrans@*/
1297 pi = teFreeIterator(pi);
1299 /*@-noeffectuncon @*/ /* FIX: check rc */
1300 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_START, 6, ts->orderCount,
1301 NULL, ts->notifyData));
1302 /*@=noeffectuncon@*/
1304 /* ===============================================
1305 * Compute file disposition for each package in transaction set.
1307 pi = teInitIterator(ts);
1308 while ((p = teNextIterator(pi)) != NULL) {
1309 dbiIndexSet * matches;
1313 if ((fi = teiGetFi(pi)) == NULL)
1314 continue; /* XXX can't happen */
1317 /*@-noeffectuncon @*/ /* FIX: check rc */
1318 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_PROGRESS, teiGetOc(pi),
1319 ts->orderCount, NULL, ts->notifyData));
1320 /*@=noeffectuncon@*/
1322 if (fc == 0) continue;
1324 /* Extract file info for all files in this package from the database. */
1325 matches = xcalloc(fc, sizeof(*matches));
1326 if (rpmdbFindFpList(ts->rpmdb, fi->fps, matches, fc)) {
1327 psm->ts = rpmtsUnlink(ts, "tsRun (rpmFindFpList fail)");
1328 return 1; /* XXX WTFO? */
1332 fi = tfiInit(fi, 0);
1333 while ((i = tfiNext(fi)) >= 0)
1334 numShared += dbiIndexSetCount(matches[i]);
1336 /* Build sorted file info list for this package. */
1337 shared = sharedList = xcalloc((numShared + 1), sizeof(*sharedList));
1339 fi = tfiInit(fi, 0);
1340 while ((i = tfiNext(fi)) >= 0) {
1342 * Take care not to mark files as replaced in packages that will
1343 * have been removed before we will get here.
1345 for (j = 0; j < dbiIndexSetCount(matches[i]); j++) {
1347 ro = dbiIndexRecordOffset(matches[i], j);
1349 qi = teInitIterator(ts);
1350 while ((q = teNext(qi, TR_REMOVED)) != NULL) {
1352 /*@innerbreak@*/ break;
1353 if (teGetDBOffset(q) == ro)
1356 qi = teFreeIterator(qi);
1358 shared->pkgFileNum = i;
1359 shared->otherPkg = dbiIndexRecordOffset(matches[i], j);
1360 shared->otherFileNum = dbiIndexRecordFileNumber(matches[i], j);
1361 shared->isRemoved = (knownBad == ro);
1364 matches[i] = dbiFreeIndexSet(matches[i]);
1366 numShared = shared - sharedList;
1367 shared->otherPkg = -1;
1368 matches = _free(matches);
1370 /* Sort file info by other package index (otherPkg) */
1371 qsort(sharedList, numShared, sizeof(*shared), sharedCmp);
1373 /* 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. */
1397 switch (teGetType(p)) {
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;
1412 /* Update disk space needs on each partition for this package. */
1413 handleOverlappedFiles(ts, p, fi);
1415 /* Check added package has sufficient space on each partition used. */
1416 switch (teGetType(p)) {
1418 if (!(ts->di && tfiGetFC(fi) > 0))
1419 /*@switchbreak@*/ break;
1420 for (i = 0; i < ts->filesystemCount; i++) {
1424 /* XXX Avoid FAT and other file systems that have not inodes. */
1425 if (dip->iavail <= 0)
1426 /*@innercontinue@*/ continue;
1428 if (adj_fs_blocks(dip->bneeded) > dip->bavail) {
1429 rpmProblemSetAppend(ts->probs, RPMPROB_DISKSPACE,
1430 teGetNEVR(p), teGetKey(p),
1431 ts->filesystems[i], NULL, NULL,
1432 (adj_fs_blocks(dip->bneeded) - dip->bavail) * dip->bsize);
1435 if (adj_fs_blocks(dip->ineeded) > dip->iavail) {
1436 rpmProblemSetAppend(ts->probs, RPMPROB_DISKNODES,
1437 teGetNEVR(p), teGetKey(p),
1438 ts->filesystems[i], NULL, NULL,
1439 (adj_fs_blocks(dip->ineeded) - dip->iavail));
1442 /*@switchbreak@*/ break;
1444 /*@switchbreak@*/ break;
1447 pi = teFreeIterator(pi);
1449 if (ts->chrootDone) {
1450 /*@-superuser -noeffect @*/
1452 /*@=superuser =noeffect @*/
1454 if (ts->rpmdb) ts->rpmdb->db_chrootDone = 0;
1455 xx = chdir(ts->currDir);
1458 /*@-noeffectuncon @*/ /* FIX: check rc */
1459 NOTIFY(ts, (NULL, RPMCALLBACK_TRANS_STOP, 6, ts->orderCount,
1460 NULL, ts->notifyData));
1461 /*@=noeffectuncon @*/
1463 /* ===============================================
1464 * Free unused memory as soon as possible.
1466 pi = teInitIterator(ts);
1467 while ((p = teNextIterator(pi)) != NULL) {
1468 if ((fi = teiGetFi(pi)) == NULL)
1469 continue; /* XXX can't happen */
1470 if (tfiGetFC(fi) == 0)
1472 fi->fps = _free(fi->fps);
1474 pi = teFreeIterator(pi);
1480 /* ===============================================
1481 * If unfiltered problems exist, free memory and return.
1483 if ((ts->transFlags & RPMTRANS_FLAG_BUILD_PROBS)
1484 || (ts->probs->numProblems &&
1485 (okProbs != NULL || rpmProblemSetTrim(ts->probs, okProbs)))
1488 if (psm->ts != NULL)
1489 psm->ts = rpmtsUnlink(psm->ts, "tsRun (problems)");
1490 return ts->orderCount;
1493 /* ===============================================
1494 * Save removed files before erasing.
1496 if (ts->transFlags & (RPMTRANS_FLAG_DIRSTASH | RPMTRANS_FLAG_REPACKAGE)) {
1497 pi = teInitIterator(ts);
1498 while ((p = teNextIterator(pi)) != NULL) {
1500 switch (teGetType(p)) {
1502 /*@switchbreak@*/ break;
1504 if (!(ts->transFlags & RPMTRANS_FLAG_REPACKAGE))
1505 /*@switchbreak@*/ break;
1507 psm->fi = rpmfiLink(fi, "tsRepackage");
1508 xx = psmStage(psm, PSM_PKGSAVE);
1509 (void) rpmfiUnlink(fi, "tsRepackage");
1512 /*@switchbreak@*/ break;
1515 pi = teFreeIterator(pi);
1518 /* ===============================================
1519 * Install and remove packages.
1521 lastKey = (alKey)-2; /* erased packages have -1 */
1522 pi = teInitIterator(ts);
1523 /*@-branchstate@*/ /* FIX: fi reload needs work */
1524 while ((p = teNextIterator(pi)) != NULL) {
1530 if ((fi = teiGetFi(pi)) == NULL)
1531 continue; /* XXX can't happen */
1534 psm->fi = rpmfiLink(fi, "tsInstall");
1535 switch (teGetType(p)) {
1538 pkgKey = teGetAddedKey(p);
1540 rpmMessage(RPMMESS_DEBUG, "========== +++ %s\n", teGetNEVR(p));
1542 /*@-type@*/ /* FIX: transactionElement not opaque */
1544 /*@-noeffectuncon@*/ /* FIX: notify annotations */
1545 p->fd = ts->notify(fi->h, RPMCALLBACK_INST_OPEN_FILE, 0, 0,
1546 teGetKey(p), ts->notifyData);
1547 /*@=noeffectuncon@*/
1548 if (teGetFd(p) != NULL) {
1551 rpmrc = rpmReadPackageFile(ts, teGetFd(p),
1552 "rpmRunTransactions", &h);
1554 if (!(rpmrc == RPMRC_OK || rpmrc == RPMRC_BADSIZE)) {
1555 /*@-noeffectuncon@*/ /* FIX: notify annotations */
1556 p->fd = ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE,
1558 teGetKey(p), ts->notifyData);
1559 /*@=noeffectuncon@*/
1563 if (teGetFd(p) != NULL) gotfd = 1;
1568 if (teGetFd(p) != NULL) {
1570 char * fstates = fi->fstates;
1571 fileAction * actions = fi->actions;
1575 (void) fiFree(fi, 0);
1577 fi->magic = TFIMAGIC;
1580 (void) fiNew(ts, fi, h, RPMTAG_BASENAMES, 1);
1581 fi->fstates = _free(fi->fstates);
1582 fi->fstates = fstates;
1583 fi->actions = _free(fi->actions);
1584 fi->actions = actions;
1588 if (teGetMultiLib(p))
1589 ts->transFlags |= RPMTRANS_FLAG_MULTILIB;
1591 ts->transFlags &= ~RPMTRANS_FLAG_MULTILIB;
1593 if (psmStage(psm, PSM_PKGINSTALL)) {
1597 fi->h = headerFree(fi->h, "TR_ADDED fi->h free");
1603 h = headerFree(h, "TR_ADDED h free");
1606 /*@-noeffectuncon @*/ /* FIX: check rc */
1607 (void) ts->notify(fi->h, RPMCALLBACK_INST_CLOSE_FILE, 0, 0,
1608 teGetKey(p), ts->notifyData);
1609 /*@=noeffectuncon @*/
1614 (void) fiFree(fi, 0);
1615 /*@switchbreak@*/ break;
1617 rpmMessage(RPMMESS_DEBUG, "========== --- %s\n", teGetNEVR(p));
1618 /* If install failed, then we shouldn't erase. */
1619 if (teGetDependsOnKey(p) != lastKey) {
1620 if (psmStage(psm, PSM_PKGERASE))
1623 (void) fiFree(fi, 0);
1624 /*@switchbreak@*/ break;
1626 xx = rpmdbSync(ts->rpmdb);
1627 (void) rpmfiUnlink(psm->fi, "tsInstall");
1632 pi = teFreeIterator(pi);
1634 psm->ts = rpmtsUnlink(psm->ts, "tsRun");
1636 /*@-nullstate@*/ /* FIX: ts->flList may be NULL */