00001 #include "system.h"
00002
00003 #include <signal.h>
00004
00005 #include <rpmbuild.h>
00006 #include <argv.h>
00007 #include <rpmfc.h>
00008
00009 #define _RPMDS_INTERNAL
00010 #include <rpmds.h>
00011 #include <rpmfi.h>
00012
00013 #if HAVE_GELF_H
00014 #include <gelf.h>
00015 #endif
00016
00017 #include "debug.h"
00018
00019
00020
00023 static int rpmfcExpandAppend( ARGV_t * argvp, const ARGV_t av)
00024
00025
00026
00027 {
00028 ARGV_t argv = *argvp;
00029 int argc = argvCount(argv);
00030 int ac = argvCount(av);
00031 int i;
00032
00033
00034 argv = xrealloc(argv, (argc + ac + 1) * sizeof(*argv));
00035
00036 for (i = 0; i < ac; i++)
00037 argv[argc + i] = rpmExpand(av[i], NULL);
00038 argv[argc + ac] = NULL;
00039 *argvp = argv;
00040 return 0;
00041 }
00042
00053
00054 static StringBuf getOutputFrom( const char * dir, ARGV_t argv,
00055 const char * writePtr, int writeBytesLeft,
00056 int failNonZero)
00057
00058
00059 {
00060 pid_t child, reaped;
00061 int toProg[2];
00062 int fromProg[2];
00063 int status;
00064 void *oldhandler;
00065 StringBuf readBuff;
00066 int done;
00067
00068
00069 oldhandler = signal(SIGPIPE, SIG_IGN);
00070
00071
00072 toProg[0] = toProg[1] = 0;
00073 (void) pipe(toProg);
00074 fromProg[0] = fromProg[1] = 0;
00075 (void) pipe(fromProg);
00076
00077 if (!(child = fork())) {
00078 (void) close(toProg[1]);
00079 (void) close(fromProg[0]);
00080
00081 (void) dup2(toProg[0], STDIN_FILENO);
00082 (void) dup2(fromProg[1], STDOUT_FILENO);
00083
00084 (void) close(toProg[0]);
00085 (void) close(fromProg[1]);
00086
00087 if (dir) {
00088 (void) chdir(dir);
00089 }
00090
00091 rpmMessage(RPMMESS_DEBUG, _("\texecv(%s) pid %d\n"),
00092 argv[0], (unsigned)getpid());
00093
00094 unsetenv("MALLOC_CHECK_");
00095 (void) execvp(argv[0], (char *const *)argv);
00096
00097 rpmError(RPMERR_EXEC, _("Couldn't exec %s: %s\n"),
00098 argv[0], strerror(errno));
00099 _exit(RPMERR_EXEC);
00100 }
00101 if (child < 0) {
00102 rpmError(RPMERR_FORK, _("Couldn't fork %s: %s\n"),
00103 argv[0], strerror(errno));
00104 return NULL;
00105 }
00106
00107 (void) close(toProg[0]);
00108 (void) close(fromProg[1]);
00109
00110
00111 (void) fcntl(fromProg[0], F_SETFL, O_NONBLOCK);
00112 (void) fcntl(toProg[1], F_SETFL, O_NONBLOCK);
00113
00114 readBuff = newStringBuf();
00115
00116 do {
00117 fd_set ibits, obits;
00118 struct timeval tv;
00119 int nfd, nbw, nbr;
00120 int rc;
00121
00122 done = 0;
00123 top:
00124 FD_ZERO(&ibits);
00125 FD_ZERO(&obits);
00126 if (fromProg[0] >= 0) {
00127 FD_SET(fromProg[0], &ibits);
00128 }
00129 if (toProg[1] >= 0) {
00130 FD_SET(toProg[1], &obits);
00131 }
00132
00133 tv.tv_sec = 0;
00134 tv.tv_usec = 10000;
00135 nfd = ((fromProg[0] > toProg[1]) ? fromProg[0] : toProg[1]);
00136 if ((rc = select(nfd, &ibits, &obits, NULL, &tv)) < 0) {
00137 if (errno == EINTR)
00138 goto top;
00139 break;
00140 }
00141
00142
00143 if (toProg[1] >= 0 && FD_ISSET(toProg[1], &obits)) {
00144 if (writePtr && writeBytesLeft > 0) {
00145 if ((nbw = write(toProg[1], writePtr,
00146 (1024<writeBytesLeft) ? 1024 : writeBytesLeft)) < 0) {
00147 if (errno != EAGAIN) {
00148 perror("getOutputFrom()");
00149 exit(EXIT_FAILURE);
00150 }
00151 nbw = 0;
00152 }
00153 writeBytesLeft -= nbw;
00154 writePtr += nbw;
00155 } else if (toProg[1] >= 0) {
00156 (void) close(toProg[1]);
00157 toProg[1] = -1;
00158 }
00159 }
00160
00161
00162
00163 { char buf[BUFSIZ+1];
00164 while ((nbr = read(fromProg[0], buf, sizeof(buf)-1)) > 0) {
00165 buf[nbr] = '\0';
00166 appendStringBuf(readBuff, buf);
00167 }
00168 }
00169
00170
00171
00172 done = (nbr == 0 || (nbr < 0 && errno != EAGAIN));
00173
00174 } while (!done);
00175
00176
00177 if (toProg[1] >= 0)
00178 (void) close(toProg[1]);
00179 if (fromProg[0] >= 0)
00180 (void) close(fromProg[0]);
00181
00182 (void) signal(SIGPIPE, oldhandler);
00183
00184
00185
00186 reaped = waitpid(child, &status, 0);
00187 rpmMessage(RPMMESS_DEBUG, _("\twaitpid(%d) rc %d status %x\n"),
00188 (unsigned)child, (unsigned)reaped, status);
00189
00190 if (failNonZero && (!WIFEXITED(status) || WEXITSTATUS(status))) {
00191 rpmError(RPMERR_EXEC, _("%s failed\n"), argv[0]);
00192 return NULL;
00193 }
00194 if (writeBytesLeft) {
00195 rpmError(RPMERR_EXEC, _("failed to write all data to %s\n"), argv[0]);
00196 return NULL;
00197 }
00198 return readBuff;
00199 }
00200
00201 int rpmfcExec(ARGV_t av, StringBuf sb_stdin, StringBuf * sb_stdoutp,
00202 int failnonzero)
00203 {
00204 const char * s = NULL;
00205 ARGV_t xav = NULL;
00206 ARGV_t pav = NULL;
00207 int pac = 0;
00208 int ec = -1;
00209 StringBuf sb = NULL;
00210 const char * buf_stdin = NULL;
00211 int buf_stdin_len = 0;
00212 int xx;
00213
00214 if (sb_stdoutp)
00215 *sb_stdoutp = NULL;
00216 if (!(av && *av))
00217 goto exit;
00218
00219
00220 s = rpmExpand(av[0], NULL);
00221 if (!(s && *s))
00222 goto exit;
00223
00224
00225 pac = 0;
00226 xx = poptParseArgvString(s, &pac, (const char ***)&pav);
00227 if (!(xx == 0 && pac > 0 && pav != NULL))
00228 goto exit;
00229
00230
00231 xav = NULL;
00232
00233 xx = argvAppend(&xav, pav);
00234 if (av[1])
00235 xx = rpmfcExpandAppend(&xav, av + 1);
00236
00237
00238 if (sb_stdin != NULL) {
00239 buf_stdin = getStringBuf(sb_stdin);
00240 buf_stdin_len = strlen(buf_stdin);
00241 }
00242
00243
00244 sb = getOutputFrom(NULL, xav, buf_stdin, buf_stdin_len, failnonzero);
00245
00246
00247 if (sb_stdoutp != NULL) {
00248 *sb_stdoutp = sb;
00249 sb = NULL;
00250 }
00251
00252
00253 ec = 0;
00254
00255 exit:
00256 sb = freeStringBuf(sb);
00257 xav = argvFree(xav);
00258 pav = _free(pav);
00259 s = _free(s);
00260 return ec;
00261 }
00262
00265 static int rpmfcSaveArg( ARGV_t * argvp, const char * key)
00266
00267
00268 {
00269 int rc = 0;
00270
00271 if (argvSearch(*argvp, key, NULL) == NULL) {
00272 rc = argvAdd(argvp, key);
00273 rc = argvSort(*argvp, NULL);
00274 }
00275 return rc;
00276 }
00277
00278 static char * rpmfcFileDep( char * buf, int ix,
00279 rpmds ds)
00280
00281
00282
00283 {
00284 int_32 tagN = rpmdsTagN(ds);
00285 char deptype = 'X';
00286
00287 buf[0] = '\0';
00288 switch (tagN) {
00289 case RPMTAG_PROVIDENAME:
00290 deptype = 'P';
00291 break;
00292 case RPMTAG_REQUIRENAME:
00293 deptype = 'R';
00294 break;
00295 }
00296
00297 if (ds != NULL)
00298 sprintf(buf, "%08d%c %s %s 0x%08x", ix, deptype,
00299 rpmdsN(ds), rpmdsEVR(ds), rpmdsFlags(ds));
00300
00301 return buf;
00302 };
00303
00311 static int rpmfcHelper(rpmfc fc, unsigned char deptype, const char * nsdep)
00312
00313
00314 {
00315 const char * fn = fc->fn[fc->ix];
00316 char buf[BUFSIZ];
00317 StringBuf sb_stdout = NULL;
00318 StringBuf sb_stdin;
00319 const char *av[2];
00320 rpmds * depsp, ds;
00321 const char * N;
00322 const char * EVR;
00323 int_32 Flags, dsContext, tagN;
00324 ARGV_t pav;
00325 const char * s;
00326 int pac;
00327 int xx;
00328 int i;
00329
00330 switch (deptype) {
00331 default:
00332 return -1;
00333 break;
00334 case 'P':
00335 if (fc->skipProv)
00336 return 0;
00337 xx = snprintf(buf, sizeof(buf), "%%{?__%s_provides}", nsdep);
00338 depsp = &fc->provides;
00339 dsContext = RPMSENSE_FIND_PROVIDES;
00340 tagN = RPMTAG_PROVIDENAME;
00341 break;
00342 case 'R':
00343 if (fc->skipReq)
00344 return 0;
00345 xx = snprintf(buf, sizeof(buf), "%%{?__%s_requires}", nsdep);
00346 depsp = &fc->requires;
00347 dsContext = RPMSENSE_FIND_REQUIRES;
00348 tagN = RPMTAG_REQUIRENAME;
00349 break;
00350 }
00351 buf[sizeof(buf)-1] = '\0';
00352 av[0] = buf;
00353 av[1] = NULL;
00354
00355 sb_stdin = newStringBuf();
00356 appendLineStringBuf(sb_stdin, fn);
00357 sb_stdout = NULL;
00358
00359 xx = rpmfcExec(av, sb_stdin, &sb_stdout, 0);
00360
00361 sb_stdin = freeStringBuf(sb_stdin);
00362
00363 if (xx == 0 && sb_stdout != NULL) {
00364 pav = NULL;
00365 xx = argvSplit(&pav, getStringBuf(sb_stdout), " \t\n\r");
00366 pac = argvCount(pav);
00367 if (pav)
00368 for (i = 0; i < pac; i++) {
00369 N = pav[i];
00370 EVR = "";
00371 Flags = dsContext;
00372
00373 if (pav[i+1] && strchr("=<>", *pav[i+1])) {
00374 i++;
00375 for (s = pav[i]; *s; s++) {
00376 switch(*s) {
00377 default:
00378 assert(*s != '\0');
00379 break;
00380 case '=':
00381 Flags |= RPMSENSE_EQUAL;
00382 break;
00383 case '<':
00384 Flags |= RPMSENSE_LESS;
00385 break;
00386 case '>':
00387 Flags |= RPMSENSE_GREATER;
00388 break;
00389 }
00390 }
00391 i++;
00392 EVR = pav[i];
00393 assert(EVR != NULL);
00394 }
00395
00396
00397
00398
00399 if (!fc->tracked && deptype == 'P' && *EVR != '\0') {
00400 ds = rpmdsSingle(RPMTAG_REQUIRENAME,
00401 "rpmlib(VersionedDependencies)", "3.0.3-1",
00402 RPMSENSE_RPMLIB|(RPMSENSE_LESS|RPMSENSE_EQUAL));
00403 xx = rpmdsMerge(&fc->requires, ds);
00404 ds = rpmdsFree(ds);
00405 fc->tracked = 1;
00406 }
00407
00408 ds = rpmdsSingle(tagN, N, EVR, Flags);
00409
00410
00411 xx = rpmdsMerge(depsp, ds);
00412
00413
00414
00415 xx = rpmfcSaveArg(&fc->ddict, rpmfcFileDep(buf, fc->ix, ds));
00416
00417
00418 ds = rpmdsFree(ds);
00419 }
00420
00421 pav = argvFree(pav);
00422 }
00423 sb_stdout = freeStringBuf(sb_stdout);
00424
00425 return 0;
00426 }
00427
00430
00431 static struct rpmfcTokens_s rpmfcTokens[] = {
00432 { "directory", RPMFC_DIRECTORY|RPMFC_INCLUDE },
00433
00434 { " shared object", RPMFC_LIBRARY },
00435 { " executable", RPMFC_EXECUTABLE },
00436 { " statically linked", RPMFC_STATIC },
00437 { " not stripped", RPMFC_NOTSTRIPPED },
00438 { " archive", RPMFC_ARCHIVE },
00439
00440 { "ELF 32-bit", RPMFC_ELF32|RPMFC_INCLUDE },
00441 { "ELF 64-bit", RPMFC_ELF64|RPMFC_INCLUDE },
00442
00443 { " script", RPMFC_SCRIPT },
00444 { " text", RPMFC_TEXT },
00445 { " document", RPMFC_DOCUMENT },
00446
00447 { " compressed", RPMFC_COMPRESSED },
00448
00449 { "troff or preprocessor input", RPMFC_MANPAGE|RPMFC_INCLUDE },
00450 { "GNU Info", RPMFC_MANPAGE|RPMFC_INCLUDE },
00451
00452 { "perl script text", RPMFC_PERL|RPMFC_INCLUDE },
00453 { "Perl5 module source text", RPMFC_PERL|RPMFC_MODULE|RPMFC_INCLUDE },
00454
00455 { " /usr/bin/python", RPMFC_PYTHON|RPMFC_INCLUDE },
00456
00457
00458
00459 { "python ", RPMFC_PYTHON|RPMFC_INCLUDE },
00460
00461
00462
00463 { "PE executable", RPMFC_MONO|RPMFC_INCLUDE },
00464
00465 { "current ar archive", RPMFC_STATIC|RPMFC_LIBRARY|RPMFC_ARCHIVE|RPMFC_INCLUDE },
00466
00467 { "Zip archive data", RPMFC_COMPRESSED|RPMFC_ARCHIVE|RPMFC_INCLUDE },
00468 { "tar archive", RPMFC_ARCHIVE|RPMFC_INCLUDE },
00469 { "cpio archive", RPMFC_ARCHIVE|RPMFC_INCLUDE },
00470 { "RPM v3", RPMFC_ARCHIVE|RPMFC_INCLUDE },
00471 { "RPM v4", RPMFC_ARCHIVE|RPMFC_INCLUDE },
00472
00473 { " image", RPMFC_IMAGE|RPMFC_INCLUDE },
00474 { " font", RPMFC_FONT|RPMFC_INCLUDE },
00475 { " Font", RPMFC_FONT|RPMFC_INCLUDE },
00476
00477 { " commands", RPMFC_SCRIPT|RPMFC_INCLUDE },
00478 { " script", RPMFC_SCRIPT|RPMFC_INCLUDE },
00479
00480 { "empty", RPMFC_WHITE|RPMFC_INCLUDE },
00481
00482 { "HTML", RPMFC_WHITE|RPMFC_INCLUDE },
00483 { "SGML", RPMFC_WHITE|RPMFC_INCLUDE },
00484 { "XML", RPMFC_WHITE|RPMFC_INCLUDE },
00485
00486 { " program text", RPMFC_WHITE|RPMFC_INCLUDE },
00487 { " source", RPMFC_WHITE|RPMFC_INCLUDE },
00488 { "GLS_BINARY_LSB_FIRST", RPMFC_WHITE|RPMFC_INCLUDE },
00489 { " DB ", RPMFC_WHITE|RPMFC_INCLUDE },
00490
00491 { "ASCII English text", RPMFC_WHITE|RPMFC_INCLUDE },
00492 { "ASCII text", RPMFC_WHITE|RPMFC_INCLUDE },
00493 { "ISO-8859 text", RPMFC_WHITE|RPMFC_INCLUDE },
00494
00495 { "symbolic link to", RPMFC_SYMLINK|RPMFC_INCLUDE },
00496 { "socket", RPMFC_DEVICE },
00497 { "special", RPMFC_DEVICE },
00498
00499 { "ASCII", RPMFC_WHITE },
00500 { "ISO-8859", RPMFC_WHITE },
00501
00502 { "data", RPMFC_WHITE },
00503
00504 { "application", RPMFC_WHITE },
00505 { "boot", RPMFC_WHITE },
00506 { "catalog", RPMFC_WHITE },
00507 { "code", RPMFC_WHITE },
00508 { "file", RPMFC_WHITE },
00509 { "format", RPMFC_WHITE },
00510 { "message", RPMFC_WHITE },
00511 { "program", RPMFC_WHITE },
00512
00513 { "broken symbolic link to ", RPMFC_WHITE|RPMFC_ERROR },
00514 { "can't read", RPMFC_WHITE|RPMFC_ERROR },
00515 { "can't stat", RPMFC_WHITE|RPMFC_ERROR },
00516 { "executable, can't read", RPMFC_WHITE|RPMFC_ERROR },
00517 { "core file", RPMFC_WHITE|RPMFC_ERROR },
00518
00519 { NULL, RPMFC_BLACK }
00520 };
00521
00522 int rpmfcColoring(const char * fmstr)
00523 {
00524 rpmfcToken fct;
00525 int fcolor = RPMFC_BLACK;
00526
00527 for (fct = rpmfcTokens; fct->token != NULL; fct++) {
00528 if (strstr(fmstr, fct->token) == NULL)
00529 continue;
00530 fcolor |= fct->colors;
00531 if (fcolor & RPMFC_INCLUDE)
00532 return fcolor;
00533 }
00534 return fcolor;
00535 }
00536
00537 void rpmfcPrint(const char * msg, rpmfc fc, FILE * fp)
00538 {
00539 int fcolor;
00540 int ndx;
00541 int cx;
00542 int dx;
00543 int fx;
00544
00545 int nprovides;
00546 int nrequires;
00547
00548 if (fp == NULL) fp = stderr;
00549
00550 if (msg)
00551 fprintf(fp, "===================================== %s\n", msg);
00552
00553 nprovides = rpmdsCount(fc->provides);
00554 nrequires = rpmdsCount(fc->requires);
00555
00556 if (fc)
00557 for (fx = 0; fx < fc->nfiles; fx++) {
00558 assert(fx < fc->fcdictx->nvals);
00559 cx = fc->fcdictx->vals[fx];
00560 assert(fx < fc->fcolor->nvals);
00561 fcolor = fc->fcolor->vals[fx];
00562
00563 fprintf(fp, "%3d %s", fx, fc->fn[fx]);
00564 if (fcolor != RPMFC_BLACK)
00565 fprintf(fp, "\t0x%x", fc->fcolor->vals[fx]);
00566 else
00567 fprintf(fp, "\t%s", fc->cdict[cx]);
00568 fprintf(fp, "\n");
00569
00570 if (fc->fddictx == NULL || fc->fddictn == NULL)
00571 continue;
00572
00573 assert(fx < fc->fddictx->nvals);
00574 dx = fc->fddictx->vals[fx];
00575 assert(fx < fc->fddictn->nvals);
00576 ndx = fc->fddictn->vals[fx];
00577
00578 while (ndx-- > 0) {
00579 const char * depval;
00580 unsigned char deptype;
00581 unsigned ix;
00582
00583 ix = fc->ddictx->vals[dx++];
00584 deptype = ((ix >> 24) & 0xff);
00585 ix &= 0x00ffffff;
00586 depval = NULL;
00587 switch (deptype) {
00588 default:
00589 assert(depval != NULL);
00590 break;
00591 case 'P':
00592 if (nprovides > 0) {
00593 assert(ix < nprovides);
00594 (void) rpmdsSetIx(fc->provides, ix-1);
00595 if (rpmdsNext(fc->provides) >= 0)
00596 depval = rpmdsDNEVR(fc->provides);
00597 }
00598 break;
00599 case 'R':
00600 if (nrequires > 0) {
00601 assert(ix < nrequires);
00602 (void) rpmdsSetIx(fc->requires, ix-1);
00603 if (rpmdsNext(fc->requires) >= 0)
00604 depval = rpmdsDNEVR(fc->requires);
00605 }
00606 break;
00607 }
00608 if (depval)
00609 fprintf(fp, "\t%s\n", depval);
00610 }
00611 }
00612 }
00613
00614 rpmfc rpmfcFree(rpmfc fc)
00615 {
00616 if (fc) {
00617 fc->fn = argvFree(fc->fn);
00618 fc->fcolor = argiFree(fc->fcolor);
00619 fc->fcdictx = argiFree(fc->fcdictx);
00620 fc->fddictx = argiFree(fc->fddictx);
00621 fc->fddictn = argiFree(fc->fddictn);
00622 fc->cdict = argvFree(fc->cdict);
00623 fc->ddict = argvFree(fc->ddict);
00624 fc->ddictx = argiFree(fc->ddictx);
00625
00626 fc->provides = rpmdsFree(fc->provides);
00627 fc->requires = rpmdsFree(fc->requires);
00628
00629 fc->sb_java = freeStringBuf(fc->sb_java);
00630 fc->sb_perl = freeStringBuf(fc->sb_perl);
00631 fc->sb_python = freeStringBuf(fc->sb_python);
00632
00633 }
00634 fc = _free(fc);
00635 return NULL;
00636 }
00637
00638 rpmfc rpmfcNew(void)
00639 {
00640 rpmfc fc = xcalloc(1, sizeof(*fc));
00641 return fc;
00642 }
00643
00649 static int rpmfcSYMLINK(rpmfc fc)
00650 {
00651 const char * fn = fc->fn[fc->ix];
00652 struct stat sb;
00653 int fdno;
00654
00655 if (stat(fn, &sb) < 0)
00656 return -1;
00657 if (S_ISLNK(sb.st_mode))
00658 return -1;
00659
00660 fdno = open(fn, O_RDONLY);
00661 if (fdno < 0) {
00662 return fdno;
00663 }
00664
00665 #if HAVE_GELF_H && HAVE_LIBELF
00666 Elf * elf = NULL;
00667 GElf_Ehdr ehdr_mem, * ehdr;
00668 int isElf64;
00669 int i, cnt;
00670 char * soname = NULL;
00671 char buf[BUFSIZ];
00672 char * t;
00673 rpmds ds;
00674
00675 (void) elf_version(EV_CURRENT);
00676 elf = NULL;
00677 if ((elf = elf_begin (fdno, ELF_C_READ_MMAP, NULL)) == NULL
00678 || elf_kind(elf) != ELF_K_ELF
00679 || (ehdr = gelf_getehdr(elf, &ehdr_mem)) == NULL
00680 || ehdr->e_type != ET_DYN)
00681 goto exit;
00682
00683 isElf64 = ehdr->e_ident[EI_CLASS] == ELFCLASS64;
00684
00685 for (i = 0; i < ehdr->e_phnum; ++i) {
00686 GElf_Phdr phdr_mem;
00687 GElf_Phdr *phdr = gelf_getphdr (elf, i, &phdr_mem);
00688 GElf_Shdr shdr_mem;
00689 Elf_Data * data = NULL;
00690 Elf_Scn * scn;
00691 GElf_Shdr *shdr;
00692
00693 if (phdr == NULL || phdr->p_type != PT_DYNAMIC)
00694 continue;
00695
00696 scn = gelf_offscn(elf, phdr->p_offset);
00697 shdr = gelf_getshdr(scn, &shdr_mem);
00698
00699 if (shdr != NULL && shdr->sh_type == SHT_DYNAMIC)
00700 data = elf_getdata (scn, NULL);
00701 if (data == NULL)
00702 continue;
00703
00704 for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt) {
00705 GElf_Dyn dynmem;
00706 GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dynmem);
00707 if (dyn == NULL)
00708 break;
00709 if (dyn->d_tag != DT_SONAME)
00710 continue;
00711
00712
00713 soname = elf_strptr(elf, shdr->sh_link, dyn->d_un.d_val);
00714 if (soname == NULL)
00715 break;
00716 buf[0] = '\0';
00717 t = buf;
00718 t = stpcpy(t, soname);
00719 #if !defined(__alpha__)
00720 if (isElf64)
00721 t = stpcpy(t, "()(64bit)");
00722 #endif
00723 t++;
00724
00725 if (!fc->skipReq) {
00726 ds = rpmdsSingle(RPMTAG_REQUIRENAME,
00727 buf, "", RPMSENSE_FIND_REQUIRES);
00728 rpmdsMerge(&fc->requires, ds);
00729 rpmfcSaveArg(&fc->ddict, rpmfcFileDep(t, fc->ix, ds));
00730 ds = rpmdsFree(ds);
00731 }
00732 break;
00733 }
00734 }
00735 exit:
00736 if (elf) (void) elf_end(elf);
00737 close(fdno);
00738 return 0;
00739 #endif
00740 return -1;
00741 }
00742
00748 static int rpmfcSCRIPT(rpmfc fc)
00749
00750
00751 {
00752 const char * fn = fc->fn[fc->ix];
00753 const char * bn;
00754 rpmds ds;
00755 char buf[BUFSIZ];
00756 FILE * fp;
00757 char * s, * se;
00758 int i;
00759 struct stat sb, * st = &sb;
00760 int is_executable;
00761 int xx;
00762
00763
00764 if (stat(fn, st) < 0)
00765 return -1;
00766 is_executable = (st->st_mode & (S_IXUSR|S_IXGRP|S_IXOTH));
00767
00768 fp = fopen(fn, "r");
00769 if (fp == NULL || ferror(fp)) {
00770 if (fp) (void) fclose(fp);
00771 return -1;
00772 }
00773
00774
00775
00776 for (i = 0; i < 10; i++) {
00777
00778 s = fgets(buf, sizeof(buf) - 1, fp);
00779 if (s == NULL || ferror(fp) || feof(fp))
00780 break;
00781 s[sizeof(buf)-1] = '\0';
00782 if (!(s[0] == '#' && s[1] == '!'))
00783 continue;
00784 s += 2;
00785
00786 while (*s && strchr(" \t\n\r", *s) != NULL)
00787 s++;
00788 if (*s == '\0')
00789 continue;
00790 if (*s != '/')
00791 continue;
00792
00793 for (se = s+1; *se; se++) {
00794 if (strchr(" \t\n\r", *se) != NULL)
00795 break;
00796 }
00797 *se = '\0';
00798 se++;
00799
00800 if (is_executable) {
00801
00802 ds = rpmdsSingle(RPMTAG_REQUIRENAME, s, "", RPMSENSE_FIND_REQUIRES);
00803 xx = rpmdsMerge(&fc->requires, ds);
00804
00805
00806 xx = rpmfcSaveArg(&fc->ddict, rpmfcFileDep(se, fc->ix, ds));
00807
00808 ds = rpmdsFree(ds);
00809 }
00810
00811
00812 bn = basename(s);
00813 if (!strcmp(bn, "perl"))
00814 fc->fcolor->vals[fc->ix] |= RPMFC_PERL;
00815 else if (!strncmp(bn, "python", sizeof("python")-1))
00816 fc->fcolor->vals[fc->ix] |= RPMFC_PYTHON;
00817
00818 break;
00819 }
00820
00821
00822 (void) fclose(fp);
00823
00824 if (fc->fcolor->vals[fc->ix] & RPMFC_PERL) {
00825 if (fc->fcolor->vals[fc->ix] & RPMFC_MODULE)
00826 xx = rpmfcHelper(fc, 'P', "perl");
00827 if (is_executable || (fc->fcolor->vals[fc->ix] & RPMFC_MODULE))
00828 xx = rpmfcHelper(fc, 'R', "perl");
00829 }
00830 if (fc->fcolor->vals[fc->ix] & RPMFC_PYTHON) {
00831 xx = rpmfcHelper(fc, 'P', "python");
00832 #ifdef NOTYET
00833 if (is_executable)
00834 #endif
00835 xx = rpmfcHelper(fc, 'R', "python");
00836 }
00837 if (fc->fcolor->vals[fc->ix] & RPMFC_MONO) {
00838 xx = rpmfcHelper(fc, 'P', "mono");
00839 if (is_executable)
00840 xx = rpmfcHelper(fc, 'R', "mono");
00841 }
00842
00843 return 0;
00844 }
00845
00851 static int rpmfcELF(rpmfc fc)
00852
00853
00854 {
00855 #if HAVE_GELF_H && HAVE_LIBELF
00856 const char * fn = fc->fn[fc->ix];
00857 Elf * elf;
00858 Elf_Scn * scn;
00859 Elf_Data * data;
00860 GElf_Ehdr ehdr_mem, * ehdr;
00861 GElf_Shdr shdr_mem, * shdr;
00862 GElf_Verdef def_mem, * def;
00863 GElf_Verneed need_mem, * need;
00864 GElf_Dyn dyn_mem, * dyn;
00865 unsigned int auxoffset;
00866 unsigned int offset;
00867 int fdno;
00868 int cnt2;
00869 int cnt;
00870 char buf[BUFSIZ];
00871 const char * s;
00872 struct stat sb, * st = &sb;
00873 const char * soname = NULL;
00874 rpmds * depsp, ds;
00875 int_32 tagN, dsContext;
00876 char * t;
00877 int xx;
00878 int isElf64;
00879 int isDSO;
00880 int gotSONAME = 0;
00881 int gotDEBUG = 0;
00882 static int filter_GLIBC_PRIVATE = 0;
00883 static int oneshot = 0;
00884
00885 if (oneshot == 0) {
00886 oneshot = 1;
00887 filter_GLIBC_PRIVATE = rpmExpandNumeric("%{?_filter_GLIBC_PRIVATE}");
00888 }
00889
00890
00891 if (stat(fn, st) != 0)
00892 return(-1);
00893
00894 fdno = open(fn, O_RDONLY);
00895 if (fdno < 0)
00896 return fdno;
00897
00898 (void) elf_version(EV_CURRENT);
00899
00900
00901 elf = NULL;
00902 if ((elf = elf_begin (fdno, ELF_C_READ, NULL)) == NULL
00903 || elf_kind(elf) != ELF_K_ELF
00904 || (ehdr = gelf_getehdr(elf, &ehdr_mem)) == NULL
00905 || !(ehdr->e_type == ET_DYN || ehdr->e_type == ET_EXEC))
00906 goto exit;
00907
00908
00909 isElf64 = ehdr->e_ident[EI_CLASS] == ELFCLASS64;
00910 isDSO = ehdr->e_type == ET_DYN;
00911
00912
00913 scn = NULL;
00914 while ((scn = elf_nextscn(elf, scn)) != NULL) {
00915 shdr = gelf_getshdr(scn, &shdr_mem);
00916 if (shdr == NULL)
00917 break;
00918
00919 soname = _free(soname);
00920 switch (shdr->sh_type) {
00921 default:
00922 continue;
00923 break;
00924 case SHT_GNU_verdef:
00925 data = NULL;
00926 if (!fc->skipProv)
00927 while ((data = elf_getdata (scn, data)) != NULL) {
00928 offset = 0;
00929 for (cnt = shdr->sh_info; --cnt >= 0; ) {
00930
00931 def = gelf_getverdef (data, offset, &def_mem);
00932 if (def == NULL)
00933 break;
00934 auxoffset = offset + def->vd_aux;
00935 for (cnt2 = def->vd_cnt; --cnt2 >= 0; ) {
00936 GElf_Verdaux aux_mem, * aux;
00937
00938 aux = gelf_getverdaux (data, auxoffset, &aux_mem);
00939 if (aux == NULL)
00940 break;
00941
00942 s = elf_strptr(elf, shdr->sh_link, aux->vda_name);
00943 if (s == NULL)
00944 break;
00945 if (def->vd_flags & VER_FLG_BASE) {
00946 soname = _free(soname);
00947 soname = xstrdup(s);
00948 auxoffset += aux->vda_next;
00949 continue;
00950 } else
00951 if (soname != NULL
00952 && !(filter_GLIBC_PRIVATE != 0
00953 && !strcmp(s, "GLIBC_PRIVATE")))
00954 {
00955 buf[0] = '\0';
00956 t = buf;
00957 t = stpcpy( stpcpy( stpcpy( stpcpy(t, soname), "("), s), ")");
00958
00959 #if !defined(__alpha__)
00960 if (isElf64)
00961 t = stpcpy(t, "(64bit)");
00962 #endif
00963 t++;
00964
00965
00966 ds = rpmdsSingle(RPMTAG_PROVIDES,
00967 buf, "", RPMSENSE_FIND_PROVIDES);
00968 xx = rpmdsMerge(&fc->provides, ds);
00969
00970
00971 xx = rpmfcSaveArg(&fc->ddict,
00972 rpmfcFileDep(t, fc->ix, ds));
00973
00974 ds = rpmdsFree(ds);
00975 }
00976 auxoffset += aux->vda_next;
00977 }
00978 offset += def->vd_next;
00979 }
00980 }
00981 break;
00982 case SHT_GNU_verneed:
00983 data = NULL;
00984
00985 if (!fc->skipReq && (st->st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)))
00986 while ((data = elf_getdata (scn, data)) != NULL) {
00987 offset = 0;
00988 for (cnt = shdr->sh_info; --cnt >= 0; ) {
00989 need = gelf_getverneed (data, offset, &need_mem);
00990 if (need == NULL)
00991 break;
00992
00993 s = elf_strptr(elf, shdr->sh_link, need->vn_file);
00994 if (s == NULL)
00995 break;
00996 soname = _free(soname);
00997 soname = xstrdup(s);
00998 auxoffset = offset + need->vn_aux;
00999 for (cnt2 = need->vn_cnt; --cnt2 >= 0; ) {
01000 GElf_Vernaux aux_mem, * aux;
01001
01002 aux = gelf_getvernaux (data, auxoffset, &aux_mem);
01003 if (aux == NULL)
01004 break;
01005
01006 s = elf_strptr(elf, shdr->sh_link, aux->vna_name);
01007 if (s == NULL)
01008 break;
01009
01010
01011 if (soname != NULL
01012 && !(filter_GLIBC_PRIVATE != 0
01013 && !strcmp(s, "GLIBC_PRIVATE")))
01014 {
01015 buf[0] = '\0';
01016 t = buf;
01017 t = stpcpy( stpcpy( stpcpy( stpcpy(t, soname), "("), s), ")");
01018
01019 #if !defined(__alpha__)
01020 if (isElf64)
01021 t = stpcpy(t, "(64bit)");
01022 #endif
01023 t++;
01024
01025
01026 ds = rpmdsSingle(RPMTAG_REQUIRENAME,
01027 buf, "", RPMSENSE_FIND_REQUIRES);
01028 xx = rpmdsMerge(&fc->requires, ds);
01029
01030
01031 xx = rpmfcSaveArg(&fc->ddict,
01032 rpmfcFileDep(t, fc->ix, ds));
01033 ds = rpmdsFree(ds);
01034 }
01035 auxoffset += aux->vna_next;
01036 }
01037 offset += need->vn_next;
01038 }
01039 }
01040 break;
01041 case SHT_DYNAMIC:
01042 data = NULL;
01043 while ((data = elf_getdata (scn, data)) != NULL) {
01044
01045 for (cnt = 0; cnt < (shdr->sh_size / shdr->sh_entsize); ++cnt) {
01046 dyn = gelf_getdyn (data, cnt, &dyn_mem);
01047 if (dyn == NULL)
01048 break;
01049 s = NULL;
01050 switch (dyn->d_tag) {
01051 default:
01052 continue;
01053 break;
01054 case DT_DEBUG:
01055 gotDEBUG = 1;
01056 continue;
01057 case DT_NEEDED:
01058
01059 if (fc->skipReq || !(st->st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)))
01060 continue;
01061
01062 depsp = &fc->requires;
01063 tagN = RPMTAG_REQUIRENAME;
01064 dsContext = RPMSENSE_FIND_REQUIRES;
01065 s = elf_strptr(elf, shdr->sh_link, dyn->d_un.d_val);
01066 assert(s != NULL);
01067 break;
01068 case DT_SONAME:
01069 gotSONAME = 1;
01070
01071 if (fc->skipProv)
01072 continue;
01073 depsp = &fc->provides;
01074 tagN = RPMTAG_PROVIDENAME;
01075 dsContext = RPMSENSE_FIND_PROVIDES;
01076 s = elf_strptr(elf, shdr->sh_link, dyn->d_un.d_val);
01077 assert(s != NULL);
01078 break;
01079 }
01080 if (s == NULL)
01081 continue;
01082
01083 buf[0] = '\0';
01084 t = buf;
01085 t = stpcpy(t, s);
01086
01087 #if !defined(__alpha__)
01088 if (isElf64)
01089 t = stpcpy(t, "()(64bit)");
01090 #endif
01091 t++;
01092
01093
01094 ds = rpmdsSingle(tagN, buf, "", dsContext);
01095 xx = rpmdsMerge(depsp, ds);
01096
01097
01098 xx = rpmfcSaveArg(&fc->ddict,
01099 rpmfcFileDep(t, fc->ix, ds));
01100
01101 ds = rpmdsFree(ds);
01102 }
01103
01104 }
01105 break;
01106 }
01107 }
01108
01109
01110
01111 if (!fc->skipProv && isDSO && !gotDEBUG && !gotSONAME) {
01112 depsp = &fc->provides;
01113 tagN = RPMTAG_PROVIDENAME;
01114 dsContext = RPMSENSE_FIND_PROVIDES;
01115
01116 s = strrchr(fn, '/');
01117 if (s)
01118 s++;
01119 else
01120 s = fn;
01121
01122
01123 buf[0] = '\0';
01124 t = buf;
01125
01126 t = stpcpy(t, s);
01127
01128
01129 #if !defined(__alpha__)
01130 if (isElf64)
01131 t = stpcpy(t, "()(64bit)");
01132 #endif
01133
01134 t++;
01135
01136
01137 ds = rpmdsSingle(tagN, buf, "", dsContext);
01138 xx = rpmdsMerge(depsp, ds);
01139
01140
01141
01142 xx = rpmfcSaveArg(&fc->ddict, rpmfcFileDep(t, fc->ix, ds));
01143
01144
01145 ds = rpmdsFree(ds);
01146 }
01147
01148 exit:
01149 soname = _free(soname);
01150 if (elf) (void) elf_end(elf);
01151 xx = close(fdno);
01152 return 0;
01153 #else
01154 return -1;
01155 #endif
01156 }
01157
01158 typedef struct rpmfcApplyTbl_s {
01159 int (*func) (rpmfc fc);
01160 int colormask;
01161 } * rpmfcApplyTbl;
01162
01165
01166 static struct rpmfcApplyTbl_s rpmfcApplyTable[] = {
01167 { rpmfcELF, RPMFC_ELF },
01168 { rpmfcSCRIPT, (RPMFC_SCRIPT|RPMFC_PERL) },
01169 { rpmfcSCRIPT, (RPMFC_SCRIPT|RPMFC_PYTHON) },
01170 { rpmfcSCRIPT, RPMFC_MONO },
01171 { rpmfcSYMLINK, RPMFC_SYMLINK },
01172 { NULL, 0 }
01173 };
01174
01175 int rpmfcApply(rpmfc fc)
01176 {
01177 rpmfcApplyTbl fcat;
01178 const char * s;
01179 char * se;
01180 rpmds ds;
01181 const char * N;
01182 const char * EVR;
01183 int_32 Flags;
01184 unsigned char deptype;
01185 int nddict;
01186 int previx;
01187 unsigned int val;
01188 int dix;
01189 int ix;
01190 int i;
01191 int xx;
01192 int skipping;
01193
01194
01195 for (fc->ix = 0; fc->fn[fc->ix] != NULL; fc->ix++) {
01196
01197
01198
01199 { const char *fn = strstr(fc->fn[fc->ix], "/usr/lib");
01200 if (fn) {
01201 fn += sizeof("/usr/lib")-1;
01202 if (fn[0] == '6' && fn[1] == '4')
01203 fn += 2;
01204 if (!strncmp(fn, "/python", sizeof("/python")-1))
01205 fc->fcolor->vals[fc->ix] |= RPMFC_PYTHON;
01206 }
01207 }
01208
01209 if (fc->fcolor->vals[fc->ix])
01210 for (fcat = rpmfcApplyTable; fcat->func != NULL; fcat++) {
01211 if (!(fc->fcolor->vals[fc->ix] & fcat->colormask))
01212 continue;
01213 xx = (*fcat->func) (fc);
01214 }
01215 }
01216
01217
01218
01219 nddict = argvCount(fc->ddict);
01220 previx = -1;
01221 for (i = 0; i < nddict; i++) {
01222 s = fc->ddict[i];
01223
01224
01225 ix = strtol(s, &se, 10);
01226 assert(se != NULL);
01227 deptype = *se++;
01228 se++;
01229 N = se;
01230 while (*se && *se != ' ')
01231 se++;
01232 *se++ = '\0';
01233 EVR = se;
01234 while (*se && *se != ' ')
01235 se++;
01236 *se++ = '\0';
01237 Flags = strtol(se, NULL, 16);
01238
01239 dix = -1;
01240 skipping = 0;
01241 switch (deptype) {
01242 default:
01243 break;
01244 case 'P':
01245 skipping = fc->skipProv;
01246 ds = rpmdsSingle(RPMTAG_PROVIDENAME, N, EVR, Flags);
01247 dix = rpmdsFind(fc->provides, ds);
01248 ds = rpmdsFree(ds);
01249 break;
01250 case 'R':
01251 skipping = fc->skipReq;
01252 ds = rpmdsSingle(RPMTAG_REQUIRENAME, N, EVR, Flags);
01253 dix = rpmdsFind(fc->requires, ds);
01254 ds = rpmdsFree(ds);
01255 break;
01256 }
01257
01258
01259 #if 0
01260 assert(dix >= 0);
01261 #else
01262 if (dix < 0)
01263 continue;
01264 #endif
01265
01266 val = (deptype << 24) | (dix & 0x00ffffff);
01267 xx = argiAdd(&fc->ddictx, -1, val);
01268
01269 if (previx != ix) {
01270 previx = ix;
01271 xx = argiAdd(&fc->fddictx, ix, argiCount(fc->ddictx)-1);
01272 }
01273 if (fc->fddictn && fc->fddictn->vals && !skipping)
01274 fc->fddictn->vals[ix]++;
01275 }
01276
01277
01278 return 0;
01279 }
01280
01281 int rpmfcClassify(rpmfc fc, ARGV_t argv, int_16 * fmode)
01282 {
01283 ARGV_t fcav = NULL;
01284 ARGV_t dav;
01285 const char * s, * se;
01286 size_t slen;
01287 int fcolor;
01288 int xx;
01289
01290 static const char * magicfile = "/usr/lib/rpm/magic";
01291 int msflags = MAGIC_CHECK;
01292 magic_t ms = NULL;
01293
01294 if (fc == NULL || argv == NULL)
01295 return 0;
01296
01297 fc->nfiles = argvCount(argv);
01298
01299
01300 xx = argiAdd(&fc->fddictx, fc->nfiles-1, 0);
01301 xx = argiAdd(&fc->fddictn, fc->nfiles-1, 0);
01302
01303
01304 xx = argvAdd(&fc->cdict, "");
01305 xx = argvAdd(&fc->cdict, "directory");
01306
01307 ms = magic_open(msflags);
01308 if (ms == NULL) {
01309 xx = RPMERR_EXEC;
01310 rpmError(xx, _("magic_open(0x%x) failed: %s\n"),
01311 msflags, strerror(errno));
01312 assert(ms != NULL);
01313 }
01314
01315 xx = magic_load(ms, magicfile);
01316 if (xx == -1) {
01317 xx = RPMERR_EXEC;
01318 rpmError(xx, _("magic_load(ms, \"%s\") failed: %s\n"),
01319 magicfile, magic_error(ms));
01320 assert(xx != -1);
01321 }
01322
01323 for (fc->ix = 0; fc->ix < fc->nfiles; fc->ix++) {
01324 const char * ftype;
01325 int_16 mode = (fmode ? fmode[fc->ix] : 0);
01326 char dbuf[1024];
01327
01328 s = argv[fc->ix];
01329 assert(s != NULL);
01330 slen = strlen(s);
01331
01332 switch (mode & S_IFMT) {
01333 case S_IFCHR: ftype = "character special"; break;
01334 case S_IFBLK: ftype = "block special"; break;
01335 case S_IFIFO: ftype = "fifo (named pipe)"; break;
01336 case S_IFSOCK: ftype = "socket"; break;
01337 case S_IFDIR:
01338 case S_IFLNK:
01339 case S_IFREG:
01340 default:
01341
01342
01343 if (slen >= sizeof(".pm") && !strcmp(s+slen-(sizeof(".pm")-1), ".pm"))
01344 ftype = "Perl5 module source text";
01345
01346 else if (slen >= fc->brlen+sizeof("/dev/") && !strncmp(s+fc->brlen, "/dev/", sizeof("/dev/")-1))
01347 ftype = "";
01348 else
01349 ftype = magic_file(ms, s);
01350
01351 if (ftype == NULL) {
01352 xx = RPMERR_EXEC;
01353 rpmError(xx, _("magic_file(ms, \"%s\") failed: mode %06o %s\n"),
01354 s, mode, magic_error(ms));
01355 assert(ftype != NULL);
01356 }
01357 }
01358
01359
01360 se = ftype;
01361 rpmMessage(RPMMESS_DEBUG, "%s: %s\n", s, se);
01362
01363
01364 xx = argvAdd(&fc->fn, s);
01365
01366
01367 xx = argvAdd(&fcav, se);
01368
01369
01370 fcolor = rpmfcColoring(se);
01371 xx = argiAdd(&fc->fcolor, fc->ix, fcolor);
01372
01373
01374 if (fcolor != RPMFC_WHITE && (fcolor & RPMFC_INCLUDE))
01375 xx = rpmfcSaveArg(&fc->cdict, se);
01376
01377 }
01378
01379
01380 fc->fknown = 0;
01381 for (fc->ix = 0; fc->ix < fc->nfiles; fc->ix++) {
01382 se = fcav[fc->ix];
01383 assert(se != NULL);
01384
01385 dav = argvSearch(fc->cdict, se, NULL);
01386 if (dav) {
01387 xx = argiAdd(&fc->fcdictx, fc->ix, (dav - fc->cdict));
01388 fc->fknown++;
01389 } else {
01390 xx = argiAdd(&fc->fcdictx, fc->ix, 0);
01391 fc->fwhite++;
01392 }
01393 }
01394
01395 fcav = argvFree(fcav);
01396
01397 if (ms != NULL)
01398 magic_close(ms);
01399
01400 return 0;
01401 }
01402
01405 typedef struct DepMsg_s * DepMsg_t;
01406
01409 struct DepMsg_s {
01410
01411 const char * msg;
01412
01413 const char * argv[4];
01414 rpmTag ntag;
01415 rpmTag vtag;
01416 rpmTag ftag;
01417 int mask;
01418 int xor;
01419 };
01420
01423
01424 static struct DepMsg_s depMsgs[] = {
01425 { "Provides", { "%{?__find_provides}", NULL, NULL, NULL },
01426 RPMTAG_PROVIDENAME, RPMTAG_PROVIDEVERSION, RPMTAG_PROVIDEFLAGS,
01427 0, -1 },
01428 #ifdef DYING
01429 { "PreReq", { NULL, NULL, NULL, NULL },
01430 RPMTAG_REQUIRENAME, RPMTAG_REQUIREVERSION, RPMTAG_REQUIREFLAGS,
01431 RPMSENSE_PREREQ, 0 },
01432 { "Requires(interp)", { NULL, "interp", NULL, NULL },
01433 -1, -1, RPMTAG_REQUIREFLAGS,
01434 _notpre(RPMSENSE_INTERP), 0 },
01435 #else
01436 { "Requires(interp)", { NULL, "interp", NULL, NULL },
01437 RPMTAG_REQUIRENAME, RPMTAG_REQUIREVERSION, RPMTAG_REQUIREFLAGS,
01438 _notpre(RPMSENSE_INTERP), 0 },
01439 #endif
01440 { "Requires(rpmlib)", { NULL, "rpmlib", NULL, NULL },
01441 -1, -1, RPMTAG_REQUIREFLAGS,
01442 _notpre(RPMSENSE_RPMLIB), 0 },
01443 { "Requires(verify)", { NULL, "verify", NULL, NULL },
01444 -1, -1, RPMTAG_REQUIREFLAGS,
01445 RPMSENSE_SCRIPT_VERIFY, 0 },
01446 { "Requires(pre)", { NULL, "pre", NULL, NULL },
01447 -1, -1, RPMTAG_REQUIREFLAGS,
01448 _notpre(RPMSENSE_SCRIPT_PRE), 0 },
01449 { "Requires(post)", { NULL, "post", NULL, NULL },
01450 -1, -1, RPMTAG_REQUIREFLAGS,
01451 _notpre(RPMSENSE_SCRIPT_POST), 0 },
01452 { "Requires(preun)", { NULL, "preun", NULL, NULL },
01453 -1, -1, RPMTAG_REQUIREFLAGS,
01454 _notpre(RPMSENSE_SCRIPT_PREUN), 0 },
01455 { "Requires(postun)", { NULL, "postun", NULL, NULL },
01456 -1, -1, RPMTAG_REQUIREFLAGS,
01457 _notpre(RPMSENSE_SCRIPT_POSTUN), 0 },
01458 { "Requires", { "%{?__find_requires}", NULL, NULL, NULL },
01459 -1, -1, RPMTAG_REQUIREFLAGS,
01460 RPMSENSE_PREREQ, RPMSENSE_PREREQ },
01461 { "Conflicts", { "%{?__find_conflicts}", NULL, NULL, NULL },
01462 RPMTAG_CONFLICTNAME, RPMTAG_CONFLICTVERSION, RPMTAG_CONFLICTFLAGS,
01463 0, -1 },
01464 { "Obsoletes", { "%{?__find_obsoletes}", NULL, NULL, NULL },
01465 RPMTAG_OBSOLETENAME, RPMTAG_OBSOLETEVERSION, RPMTAG_OBSOLETEFLAGS,
01466 0, -1 },
01467 { NULL, { NULL, NULL, NULL, NULL }, 0, 0, 0, 0, 0 }
01468 };
01469
01470
01471 static DepMsg_t DepMsgs = depMsgs;
01472
01475 static void printDeps(Header h)
01476
01477
01478 {
01479 DepMsg_t dm;
01480 rpmds ds = NULL;
01481 int flags = 0x2;
01482 const char * DNEVR;
01483 int_32 Flags;
01484 int bingo = 0;
01485
01486 for (dm = DepMsgs; dm->msg != NULL; dm++) {
01487 if (dm->ntag != -1) {
01488 ds = rpmdsFree(ds);
01489 ds = rpmdsNew(h, dm->ntag, flags);
01490 }
01491 if (dm->ftag == 0)
01492 continue;
01493
01494 ds = rpmdsInit(ds);
01495 if (ds == NULL)
01496 continue;
01497
01498 bingo = 0;
01499 while (rpmdsNext(ds) >= 0) {
01500
01501 Flags = rpmdsFlags(ds);
01502
01503 if (!((Flags & dm->mask) ^ dm->xor))
01504 continue;
01505 if (bingo == 0) {
01506 rpmMessage(RPMMESS_NORMAL, "%s:", (dm->msg ? dm->msg : ""));
01507 bingo = 1;
01508 }
01509 if ((DNEVR = rpmdsDNEVR(ds)) == NULL)
01510 continue;
01511 rpmMessage(RPMMESS_NORMAL, " %s", DNEVR+2);
01512 }
01513 if (bingo)
01514 rpmMessage(RPMMESS_NORMAL, "\n");
01515 }
01516 ds = rpmdsFree(ds);
01517 }
01518
01521 static int rpmfcGenerateDependsHelper(const Spec spec, Package pkg, rpmfi fi)
01522
01523
01524 {
01525 StringBuf sb_stdin;
01526 StringBuf sb_stdout;
01527 DepMsg_t dm;
01528 int failnonzero = 0;
01529 int rc = 0;
01530
01531
01532
01533
01534 sb_stdin = newStringBuf();
01535 fi = rpmfiInit(fi, 0);
01536 if (fi != NULL)
01537 while (rpmfiNext(fi) >= 0)
01538 appendLineStringBuf(sb_stdin, rpmfiFN(fi));
01539
01540 for (dm = DepMsgs; dm->msg != NULL; dm++) {
01541 int tag, tagflags;
01542 char * s;
01543 int xx;
01544
01545 tag = (dm->ftag > 0) ? dm->ftag : dm->ntag;
01546 tagflags = 0;
01547 s = NULL;
01548
01549 switch(tag) {
01550 case RPMTAG_PROVIDEFLAGS:
01551 if (!pkg->autoProv)
01552 continue;
01553 failnonzero = 1;
01554 tagflags = RPMSENSE_FIND_PROVIDES;
01555 break;
01556 case RPMTAG_REQUIREFLAGS:
01557 if (!pkg->autoReq)
01558 continue;
01559 failnonzero = 0;
01560 tagflags = RPMSENSE_FIND_REQUIRES;
01561 break;
01562 default:
01563 continue;
01564 break;
01565 }
01566
01567
01568 xx = rpmfcExec(dm->argv, sb_stdin, &sb_stdout, failnonzero);
01569
01570 if (xx == -1)
01571 continue;
01572
01573 s = rpmExpand(dm->argv[0], NULL);
01574 rpmMessage(RPMMESS_NORMAL, _("Finding %s: %s\n"), dm->msg,
01575 (s ? s : ""));
01576 s = _free(s);
01577
01578 if (sb_stdout == NULL) {
01579 rc = RPMERR_EXEC;
01580 rpmError(rc, _("Failed to find %s:\n"), dm->msg);
01581 break;
01582 }
01583
01584
01585 rc = parseRCPOT(spec, pkg, getStringBuf(sb_stdout), tag, 0, tagflags);
01586 sb_stdout = freeStringBuf(sb_stdout);
01587
01588 if (rc) {
01589 rpmError(rc, _("Failed to find %s:\n"), dm->msg);
01590 break;
01591 }
01592 }
01593
01594 sb_stdin = freeStringBuf(sb_stdin);
01595
01596 return rc;
01597 }
01598
01599 int rpmfcGenerateDepends(const Spec spec, Package pkg)
01600 {
01601 rpmfi fi = pkg->cpioList;
01602 rpmfc fc = NULL;
01603 rpmds ds;
01604 int flags = 0x2;
01605 ARGV_t av;
01606 int_16 * fmode;
01607 int ac = rpmfiFC(fi);
01608 const void ** p;
01609 char buf[BUFSIZ];
01610 const char * N;
01611 const char * EVR;
01612 int genConfigDeps;
01613 int c;
01614 int rc = 0;
01615 int xx;
01616
01617
01618 if (ac <= 0)
01619 return 0;
01620
01621
01622 if (! (pkg->autoReq || pkg->autoProv))
01623 return 0;
01624
01625
01626 if (!rpmExpandNumeric("%{?_use_internal_dependency_generator}")) {
01627
01628 rc = rpmfcGenerateDependsHelper(spec, pkg, fi);
01629 printDeps(pkg->header);
01630 return rc;
01631 }
01632
01633
01634 av = xcalloc(ac+1, sizeof(*av));
01635 fmode = xcalloc(ac+1, sizeof(*fmode));
01636
01637
01638 genConfigDeps = 0;
01639 fi = rpmfiInit(fi, 0);
01640 if (fi != NULL)
01641 while ((c = rpmfiNext(fi)) >= 0) {
01642 rpmfileAttrs fileAttrs;
01643
01644
01645 fileAttrs = rpmfiFFlags(fi);
01646 genConfigDeps |= (fileAttrs & RPMFILE_CONFIG);
01647
01648 av[c] = xstrdup(rpmfiFN(fi));
01649 fmode[c] = rpmfiFMode(fi);
01650 }
01651 av[ac] = NULL;
01652
01653
01654 fc = rpmfcNew();
01655 fc->skipProv = !pkg->autoProv;
01656 fc->skipReq = !pkg->autoReq;
01657 fc->tracked = 0;
01658 fc->brlen = (spec->buildRootURL ? strlen(spec->buildRootURL) : 0);
01659
01660
01661 if (!fc->skipProv) {
01662 ds = rpmdsNew(pkg->header, RPMTAG_PROVIDENAME, flags);
01663 xx = rpmdsMerge(&fc->provides, ds);
01664 ds = rpmdsFree(ds);
01665 xx = headerRemoveEntry(pkg->header, RPMTAG_PROVIDENAME);
01666 xx = headerRemoveEntry(pkg->header, RPMTAG_PROVIDEVERSION);
01667 xx = headerRemoveEntry(pkg->header, RPMTAG_PROVIDEFLAGS);
01668
01669
01670 if (genConfigDeps) {
01671 N = rpmdsN(pkg->ds);
01672 assert(N != NULL);
01673 EVR = rpmdsEVR(pkg->ds);
01674 assert(EVR != NULL);
01675 sprintf(buf, "config(%s)", N);
01676 ds = rpmdsSingle(RPMTAG_PROVIDENAME, buf, EVR,
01677 (RPMSENSE_EQUAL|RPMSENSE_CONFIG));
01678 xx = rpmdsMerge(&fc->provides, ds);
01679 ds = rpmdsFree(ds);
01680 }
01681 }
01682
01683 if (!fc->skipReq) {
01684 ds = rpmdsNew(pkg->header, RPMTAG_REQUIRENAME, flags);
01685 xx = rpmdsMerge(&fc->requires, ds);
01686 ds = rpmdsFree(ds);
01687 xx = headerRemoveEntry(pkg->header, RPMTAG_REQUIRENAME);
01688 xx = headerRemoveEntry(pkg->header, RPMTAG_REQUIREVERSION);
01689 xx = headerRemoveEntry(pkg->header, RPMTAG_REQUIREFLAGS);
01690
01691
01692 if (genConfigDeps) {
01693 N = rpmdsN(pkg->ds);
01694 assert(N != NULL);
01695 EVR = rpmdsEVR(pkg->ds);
01696 assert(EVR != NULL);
01697 sprintf(buf, "config(%s)", N);
01698 ds = rpmdsSingle(RPMTAG_REQUIRENAME, buf, EVR,
01699 (RPMSENSE_EQUAL|RPMSENSE_CONFIG));
01700 xx = rpmdsMerge(&fc->requires, ds);
01701 ds = rpmdsFree(ds);
01702 }
01703 }
01704
01705
01706 xx = rpmfcClassify(fc, av, fmode);
01707
01708
01709 xx = rpmfcApply(fc);
01710
01711
01712 p = (const void **) argiData(fc->fcolor);
01713 c = argiCount(fc->fcolor);
01714 assert(ac == c);
01715 if (p != NULL && c > 0) {
01716 int_32 * fcolors = (int_32 *)p;
01717 int i;
01718
01719
01720 for (i = 0; i < c; i++)
01721 fcolors[i] &= 0x0f;
01722 xx = headerAddEntry(pkg->header, RPMTAG_FILECOLORS, RPM_INT32_TYPE,
01723 p, c);
01724 }
01725
01726
01727 p = (const void **) argvData(fc->cdict);
01728 c = argvCount(fc->cdict);
01729 if (p != NULL && c > 0)
01730 xx = headerAddEntry(pkg->header, RPMTAG_CLASSDICT, RPM_STRING_ARRAY_TYPE,
01731 p, c);
01732
01733
01734 p = (const void **) argiData(fc->fcdictx);
01735 c = argiCount(fc->fcdictx);
01736 assert(ac == c);
01737 if (p != NULL && c > 0)
01738 xx = headerAddEntry(pkg->header, RPMTAG_FILECLASS, RPM_INT32_TYPE,
01739 p, c);
01740
01741
01742
01743 if (fc->provides != NULL && (c = rpmdsCount(fc->provides)) > 0 && !fc->skipProv) {
01744 p = (const void **) fc->provides->N;
01745 xx = headerAddEntry(pkg->header, RPMTAG_PROVIDENAME, RPM_STRING_ARRAY_TYPE,
01746 p, c);
01747
01748
01749 p = (const void **) fc->provides->EVR;
01750 assert(p != NULL);
01751 xx = headerAddEntry(pkg->header, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
01752 p, c);
01753 p = (const void **) fc->provides->Flags;
01754 assert(p != NULL);
01755 xx = headerAddEntry(pkg->header, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
01756 p, c);
01757
01758 }
01759
01760
01761
01762
01763 if (fc->requires != NULL && (c = rpmdsCount(fc->requires)) > 0 && !fc->skipReq) {
01764 p = (const void **) fc->requires->N;
01765 xx = headerAddEntry(pkg->header, RPMTAG_REQUIRENAME, RPM_STRING_ARRAY_TYPE,
01766 p, c);
01767
01768
01769 p = (const void **) fc->requires->EVR;
01770 assert(p != NULL);
01771 xx = headerAddEntry(pkg->header, RPMTAG_REQUIREVERSION, RPM_STRING_ARRAY_TYPE,
01772 p, c);
01773 p = (const void **) fc->requires->Flags;
01774 assert(p != NULL);
01775 xx = headerAddEntry(pkg->header, RPMTAG_REQUIREFLAGS, RPM_INT32_TYPE,
01776 p, c);
01777
01778 }
01779
01780
01781
01782 p = (const void **) argiData(fc->ddictx);
01783 c = argiCount(fc->ddictx);
01784 if (p != NULL)
01785 xx = headerAddEntry(pkg->header, RPMTAG_DEPENDSDICT, RPM_INT32_TYPE,
01786 p, c);
01787
01788
01789 p = (const void **) argiData(fc->fddictx);
01790 c = argiCount(fc->fddictx);
01791 assert(ac == c);
01792 if (p != NULL)
01793 xx = headerAddEntry(pkg->header, RPMTAG_FILEDEPENDSX, RPM_INT32_TYPE,
01794 p, c);
01795
01796 p = (const void **) argiData(fc->fddictn);
01797 c = argiCount(fc->fddictn);
01798 assert(ac == c);
01799 if (p != NULL)
01800 xx = headerAddEntry(pkg->header, RPMTAG_FILEDEPENDSN, RPM_INT32_TYPE,
01801 p, c);
01802
01803 printDeps(pkg->header);
01804
01805 if (fc != NULL && _rpmfc_debug) {
01806 char msg[BUFSIZ];
01807 sprintf(msg, "final: files %d cdict[%d] %d%% ddictx[%d]", fc->nfiles, argvCount(fc->cdict), ((100 * fc->fknown)/fc->nfiles), argiCount(fc->ddictx));
01808 rpmfcPrint(msg, fc, NULL);
01809 }
01810
01811
01812 fmode = _free(fmode);
01813 fc = rpmfcFree(fc);
01814 av = argvFree(av);
01815
01816 return rc;
01817 }