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