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 { "Desktop Entry", RPMFC_DESKTOP_FILE|RPMFC_INCLUDE },
00535
00536 { "perl script text", RPMFC_PERL|RPMFC_INCLUDE },
00537 { "Perl5 module source text", RPMFC_PERL|RPMFC_MODULE|RPMFC_INCLUDE },
00538
00539 { "PHP script text", RPMFC_PHP|RPMFC_INCLUDE },
00540
00541
00542
00543 { " /usr/bin/python", RPMFC_PYTHON|RPMFC_INCLUDE },
00544 { "python ", RPMFC_PYTHON|RPMFC_INCLUDE },
00545
00546 { "libtool library ", RPMFC_LIBTOOL|RPMFC_INCLUDE },
00547 { "pkgconfig ", RPMFC_PKGCONFIG|RPMFC_INCLUDE },
00548
00549 { "Bourne ", RPMFC_BOURNE|RPMFC_INCLUDE },
00550 { "Bourne-Again ", RPMFC_BOURNE|RPMFC_INCLUDE },
00551
00552 { "Java ", RPMFC_JAVA|RPMFC_INCLUDE },
00553
00554
00555 { "PE executable", RPMFC_MONO|RPMFC_INCLUDE },
00556 { "executable PE", RPMFC_MONO|RPMFC_INCLUDE },
00557
00558 { "current ar archive", RPMFC_STATIC|RPMFC_LIBRARY|RPMFC_ARCHIVE|RPMFC_INCLUDE },
00559
00560 { "Zip archive data", RPMFC_COMPRESSED|RPMFC_ARCHIVE|RPMFC_INCLUDE },
00561 { "tar archive", RPMFC_ARCHIVE|RPMFC_INCLUDE },
00562 { "cpio archive", RPMFC_ARCHIVE|RPMFC_INCLUDE },
00563 { "RPM v3", RPMFC_ARCHIVE|RPMFC_INCLUDE },
00564 { "RPM v4", RPMFC_ARCHIVE|RPMFC_INCLUDE },
00565
00566 { " image", RPMFC_IMAGE|RPMFC_INCLUDE },
00567 { " font", RPMFC_FONT|RPMFC_INCLUDE },
00568 { " Font", RPMFC_FONT|RPMFC_INCLUDE },
00569
00570 { " commands", RPMFC_SCRIPT|RPMFC_INCLUDE },
00571 { " script", RPMFC_SCRIPT|RPMFC_INCLUDE },
00572
00573 { "empty", RPMFC_WHITE|RPMFC_INCLUDE },
00574
00575 { "HTML", RPMFC_WHITE|RPMFC_INCLUDE },
00576 { "SGML", RPMFC_WHITE|RPMFC_INCLUDE },
00577 { "XML", RPMFC_WHITE|RPMFC_INCLUDE },
00578
00579 { " program text", RPMFC_WHITE|RPMFC_INCLUDE },
00580 { " source", RPMFC_WHITE|RPMFC_INCLUDE },
00581 { "GLS_BINARY_LSB_FIRST", RPMFC_WHITE|RPMFC_INCLUDE },
00582 { " DB ", RPMFC_WHITE|RPMFC_INCLUDE },
00583
00584 { "ASCII English text", RPMFC_WHITE|RPMFC_INCLUDE },
00585 { "ASCII text", RPMFC_WHITE|RPMFC_INCLUDE },
00586 { "ISO-8859 text", RPMFC_WHITE|RPMFC_INCLUDE },
00587
00588 { "symbolic link to", RPMFC_SYMLINK },
00589 { "socket", RPMFC_DEVICE },
00590 { "special", RPMFC_DEVICE },
00591
00592 { "ASCII", RPMFC_WHITE },
00593 { "ISO-8859", RPMFC_WHITE },
00594
00595 { "data", RPMFC_WHITE },
00596
00597 { "application", RPMFC_WHITE },
00598 { "boot", RPMFC_WHITE },
00599 { "catalog", RPMFC_WHITE },
00600 { "code", RPMFC_WHITE },
00601 { "file", RPMFC_WHITE },
00602 { "format", RPMFC_WHITE },
00603 { "message", RPMFC_WHITE },
00604 { "program", RPMFC_WHITE },
00605
00606 { "broken symbolic link to ", RPMFC_WHITE|RPMFC_ERROR },
00607 { "can't read", RPMFC_WHITE|RPMFC_ERROR },
00608 { "can't stat", RPMFC_WHITE|RPMFC_ERROR },
00609 { "executable, can't read", RPMFC_WHITE|RPMFC_ERROR },
00610 { "core file", RPMFC_WHITE|RPMFC_ERROR },
00611
00612 { NULL, RPMFC_BLACK }
00613 };
00614
00615 int rpmfcColoring(const char * fmstr)
00616 {
00617 rpmfcToken fct;
00618 int fcolor = RPMFC_BLACK;
00619
00620 for (fct = rpmfcTokens; fct->token != NULL; fct++) {
00621 if (strstr(fmstr, fct->token) == NULL)
00622 continue;
00623 fcolor |= fct->colors;
00624 if (fcolor & RPMFC_INCLUDE)
00625 return fcolor;
00626 }
00627 return fcolor;
00628 }
00629
00630 void rpmfcPrint(const char * msg, rpmfc fc, FILE * fp)
00631 {
00632 int fcolor;
00633 int ndx;
00634 int cx;
00635 int dx;
00636 int fx;
00637
00638 int nprovides;
00639 int nrequires;
00640
00641 if (fp == NULL) fp = stderr;
00642
00643 if (msg)
00644 fprintf(fp, "===================================== %s\n", msg);
00645
00646 nprovides = rpmdsCount(fc->provides);
00647 nrequires = rpmdsCount(fc->requires);
00648
00649 if (fc)
00650 for (fx = 0; fx < fc->nfiles; fx++) {
00651 assert(fx < fc->fcdictx->nvals);
00652 cx = fc->fcdictx->vals[fx];
00653 assert(fx < fc->fcolor->nvals);
00654 fcolor = fc->fcolor->vals[fx];
00655
00656 fprintf(fp, "%3d %s", fx, fc->fn[fx]);
00657 if (fcolor != RPMFC_BLACK)
00658 fprintf(fp, "\t0x%x", fc->fcolor->vals[fx]);
00659 else
00660 fprintf(fp, "\t%s", fc->cdict[cx]);
00661 fprintf(fp, "\n");
00662
00663 if (fc->fddictx == NULL || fc->fddictn == NULL)
00664 continue;
00665
00666 assert(fx < fc->fddictx->nvals);
00667 dx = fc->fddictx->vals[fx];
00668 assert(fx < fc->fddictn->nvals);
00669 ndx = fc->fddictn->vals[fx];
00670
00671 while (ndx-- > 0) {
00672 const char * depval;
00673 unsigned char deptype;
00674 unsigned ix;
00675
00676 ix = fc->ddictx->vals[dx++];
00677 deptype = ((ix >> 24) & 0xff);
00678 ix &= 0x00ffffff;
00679 depval = NULL;
00680 switch (deptype) {
00681 default:
00682 assert(depval != NULL);
00683 break;
00684 case 'P':
00685 if (nprovides > 0) {
00686 assert(ix < nprovides);
00687 (void) rpmdsSetIx(fc->provides, ix-1);
00688 if (rpmdsNext(fc->provides) >= 0)
00689 depval = rpmdsDNEVR(fc->provides);
00690 }
00691 break;
00692 case 'R':
00693 if (nrequires > 0) {
00694 assert(ix < nrequires);
00695 (void) rpmdsSetIx(fc->requires, ix-1);
00696 if (rpmdsNext(fc->requires) >= 0)
00697 depval = rpmdsDNEVR(fc->requires);
00698 }
00699 break;
00700 }
00701 if (depval)
00702 fprintf(fp, "\t%s\n", depval);
00703 }
00704 }
00705 }
00706
00707 rpmfc rpmfcFree(rpmfc fc)
00708 {
00709 if (fc) {
00710 fc->fn = argvFree(fc->fn);
00711 fc->fcolor = argiFree(fc->fcolor);
00712 fc->fcdictx = argiFree(fc->fcdictx);
00713 fc->fddictx = argiFree(fc->fddictx);
00714 fc->fddictn = argiFree(fc->fddictn);
00715 fc->cdict = argvFree(fc->cdict);
00716 fc->ddict = argvFree(fc->ddict);
00717 fc->ddictx = argiFree(fc->ddictx);
00718
00719 fc->provides = rpmdsFree(fc->provides);
00720 fc->requires = rpmdsFree(fc->requires);
00721
00722 fc->sb_java = freeStringBuf(fc->sb_java);
00723 fc->sb_perl = freeStringBuf(fc->sb_perl);
00724 fc->sb_python = freeStringBuf(fc->sb_python);
00725 fc->sb_php = freeStringBuf(fc->sb_php);
00726
00727 }
00728 fc = _free(fc);
00729 return NULL;
00730 }
00731
00732 rpmfc rpmfcNew(void)
00733 {
00734 rpmfc fc = xcalloc(1, sizeof(*fc));
00735 return fc;
00736 }
00737
00743 static int rpmfcSCRIPT(rpmfc fc)
00744
00745
00746 {
00747 const char * fn = fc->fn[fc->ix];
00748 const char * bn;
00749 rpmds ds;
00750 char buf[BUFSIZ];
00751 FILE * fp;
00752 char * s, * se;
00753 int i;
00754 int is_executable;
00755 int xx;
00756
00757
00758 { struct stat sb, * st = &sb;
00759 if (stat(fn, st) != 0)
00760 return -1;
00761 is_executable = (st->st_mode & (S_IXUSR|S_IXGRP|S_IXOTH));
00762 }
00763
00764 fp = fopen(fn, "r");
00765 if (fp == NULL || ferror(fp)) {
00766 if (fp) (void) fclose(fp);
00767 return -1;
00768 }
00769
00770
00771
00772 for (i = 0; i < 10; i++) {
00773
00774 s = fgets(buf, sizeof(buf) - 1, fp);
00775 if (s == NULL || ferror(fp) || feof(fp))
00776 break;
00777 s[sizeof(buf)-1] = '\0';
00778 if (!(s[0] == '#' && s[1] == '!'))
00779 continue;
00780 s += 2;
00781
00782 while (*s && strchr(" \t\n\r", *s) != NULL)
00783 s++;
00784 if (*s == '\0')
00785 continue;
00786 if (*s != '/')
00787 continue;
00788
00789 for (se = s+1; *se; se++) {
00790 if (strchr(" \t\n\r", *se) != NULL)
00791 break;
00792 }
00793 *se = '\0';
00794 se++;
00795
00796 if (is_executable && fc->findreq && !rpmfcMatchRegexps(fc->noautoreq, fc->noautoreq_c, s, 'R')) {
00797
00798 ds = rpmdsSingle(RPMTAG_REQUIRENAME, s, "", RPMSENSE_FIND_REQUIRES);
00799 xx = rpmdsMerge(&fc->requires, ds);
00800
00801
00802 xx = rpmfcSaveArg(&fc->ddict, rpmfcFileDep(se, fc->ix, ds));
00803
00804 ds = rpmdsFree(ds);
00805 }
00806
00807
00808
00809 bn = basename(s);
00810 if (!strcmp(bn, "perl"))
00811 fc->fcolor->vals[fc->ix] |= RPMFC_PERL;
00812 else if (!strncmp(bn, "python", sizeof("python")-1))
00813 fc->fcolor->vals[fc->ix] |= RPMFC_PYTHON;
00814 else if (!strncmp(bn, "php", sizeof("php")-1))
00815 fc->fcolor->vals[fc->ix] |= RPMFC_PHP;
00816
00817 break;
00818 }
00819
00820
00821 (void) fclose(fp);
00822
00823 if (fc->fcolor->vals[fc->ix] & RPMFC_PERL) {
00824 if (strncmp(fn, "/usr/share/doc/", sizeof("/usr/share/doc/")-1)) {
00825 if (fc->findprov && (fc->fcolor->vals[fc->ix] & RPMFC_MODULE))
00826 xx = rpmfcHelper(fc, 'P', "perl", fc->noautoprov, fc->noautoprov_c);
00827 if (fc->findreq && (is_executable || (fc->fcolor->vals[fc->ix] & RPMFC_MODULE)))
00828 xx = rpmfcHelper(fc, 'R', "perl", fc->noautoreq, fc->noautoreq_c);
00829 }
00830 } else
00831 if (fc->fcolor->vals[fc->ix] & RPMFC_PYTHON) {
00832 if (fc->findprov)
00833 xx = rpmfcHelper(fc, 'P', "python", fc->noautoprov, fc->noautoprov_c);
00834 #ifdef NOTYET
00835 if (is_executable)
00836 #endif
00837 if (fc->findreq)
00838 xx = rpmfcHelper(fc, 'R', "python", fc->noautoreq, fc->noautoreq_c);
00839 } else
00840 if (fc->fcolor->vals[fc->ix] & RPMFC_LIBTOOL) {
00841 if (fc->findprov)
00842 xx = rpmfcHelper(fc, 'P', "libtool", fc->noautoprov, fc->noautoprov_c);
00843 #ifdef NOTYET
00844 if (is_executable)
00845 #endif
00846 if (fc->findreq)
00847 xx = rpmfcHelper(fc, 'R', "libtool", fc->noautoreq, fc->noautoreq_c);
00848 } else
00849 if (fc->fcolor->vals[fc->ix] & RPMFC_PKGCONFIG) {
00850 if (fc->findprov)
00851 xx = rpmfcHelper(fc, 'P', "pkgconfig", fc->noautoprov, fc->noautoprov_c);
00852 #ifdef NOTYET
00853 if (is_executable)
00854 #endif
00855 if (fc->findreq)
00856 xx = rpmfcHelper(fc, 'R', "pkgconfig", fc->noautoreq, fc->noautoreq_c);
00857 } else
00858 if (fc->fcolor->vals[fc->ix] & RPMFC_BOURNE) {
00859 #ifdef NOTYET
00860 xx = rpmfcHelper(fc, 'P', "executable");
00861 #endif
00862 if (fc->findreq && is_executable)
00863 xx = rpmfcHelper(fc, 'R', "executable", fc->noautoreq, fc->noautoreq_c);
00864 } else
00865 if (fc->fcolor->vals[fc->ix] & RPMFC_PHP) {
00866 if (fc->findprov)
00867 xx = rpmfcHelper(fc, 'P', "php", fc->noautoprov, fc->noautoprov_c);
00868
00869 if (fc->findreq)
00870 xx = rpmfcHelper(fc, 'R', "php", fc->noautoreq, fc->noautoreq_c);
00871 } else
00872 if (fc->fcolor->vals[fc->ix] & RPMFC_JAVA) {
00873 if (fc->findprov)
00874 xx = rpmfcHelper(fc, 'P', "java", fc->noautoprov, fc->noautoprov_c);
00875 if (fc->findreq)
00876 xx = rpmfcHelper(fc, 'R', "java", fc->noautoreq, fc->noautoreq_c);
00877 } else
00878 if (fc->fcolor->vals[fc->ix] & RPMFC_DESKTOP_FILE) {
00879 if (fc->findprov)
00880 xx = rpmfcHelper(fc, 'P', "mimetype", fc->noautoprov, fc->noautoprov_c);
00881 }
00882
00883 return 0;
00884 }
00885
00886
00893 static int rpmfcMergePR(void * context, rpmds ds)
00894
00895 {
00896 rpmfc fc = context;
00897 char buf[BUFSIZ];
00898 int rc = -1;
00899
00900
00901 if (_rpmfc_debug < 0)
00902 fprintf(stderr, "*** %s(%p, %p) %s\n", __FUNCTION__, context, ds, tagName(rpmdsTagN(ds)));
00903
00904 switch(rpmdsTagN(ds)) {
00905 default:
00906 break;
00907 case RPMTAG_PROVIDENAME:
00908 if (fc->findprov && !rpmfcMatchRegexps(fc->noautoprov, fc->noautoprov_c, ds->N[0], 'P')) {
00909
00910 rc = rpmdsMerge(&fc->provides, ds);
00911
00912
00913 buf[0] = '\0';
00914 rc = rpmfcSaveArg(&fc->ddict, rpmfcFileDep(buf, fc->ix, ds));
00915 } else
00916 rc = 0;
00917 break;
00918 case RPMTAG_REQUIRENAME:
00919 if (fc->findreq && !rpmfcMatchRegexps(fc->noautoreq, fc->noautoreq_c, ds->N[0], 'R')) {
00920
00921 rc = rpmdsMerge(&fc->requires, ds);
00922
00923
00924 buf[0] = '\0';
00925 rc = rpmfcSaveArg(&fc->ddict, rpmfcFileDep(buf, fc->ix, ds));
00926 } else
00927 rc = 0;
00928 break;
00929 }
00930 return rc;
00931 }
00932
00938 static int rpmfcMONO(rpmfc fc)
00939
00940
00941 {
00942 const char * fn = fc->fn[fc->ix];
00943 FILE * fp;
00944 int xx;
00945
00946 fp = fopen(fn, "r");
00947 if (fp == NULL || ferror(fp)) {
00948 if (fp) (void) fclose(fp);
00949 return -1;
00950 }
00951
00952 (void) fclose(fp);
00953
00954 if (fc->findprov)
00955 xx = rpmfcHelper(fc, 'P', "mono", fc->noautoprov, fc->noautoprov_c);
00956 if (fc->findreq)
00957 xx = rpmfcHelper(fc, 'R', "mono", fc->noautoreq, fc->noautoreq_c);
00958
00959 return 0;
00960 }
00961
00967 static int rpmfcELF(rpmfc fc)
00968
00969
00970 {
00971 const char * fn = fc->fn[fc->ix];
00972 int flags = 0;
00973
00974 if (fc->skipProv)
00975 flags |= RPMELF_FLAG_SKIPPROVIDES;
00976 if (fc->skipReq)
00977 flags |= RPMELF_FLAG_SKIPREQUIRES;
00978
00979 return rpmdsELF(fn, flags, rpmfcMergePR, fc);
00980 }
00981
00982 typedef struct rpmfcApplyTbl_s {
00983 int (*func) (rpmfc fc);
00984 int colormask;
00985 } * rpmfcApplyTbl;
00986
00990
00991 static struct rpmfcApplyTbl_s rpmfcApplyTable[] = {
00992 { rpmfcELF, RPMFC_ELF },
00993 { rpmfcSCRIPT, (RPMFC_SCRIPT|RPMFC_PERL|RPMFC_PYTHON|RPMFC_LIBTOOL|RPMFC_PKGCONFIG|RPMFC_BOURNE|RPMFC_JAVA|RPMFC_PHP|RPMFC_DESKTOP_FILE) },
00994 { rpmfcMONO, RPMFC_MONO },
00995 { NULL, 0 }
00996 };
00997
00998 static int rpmfcFindRequiredPackages(rpmfc fc)
00999 {
01000 rpmts ts=NULL;
01001 const char * s;
01002 char * se;
01003 rpmds ds;
01004 const char * N;
01005 const char * EVR;
01006 int_32 Flags;
01007 unsigned char deptype;
01008 int nddict;
01009 int previx;
01010 int ix;
01011 int i;
01012 int j;
01013 int xx;
01014 int r;
01015 const char * hname;
01016 rpmdbMatchIterator it;
01017 Header hdr;
01018 regex_t *noautoreqdep;
01019 int noautoreqdep_c;
01020
01021 noautoreqdep=rpmfcExpandRegexps("%{__noautoreqdep}", &noautoreqdep_c);
01022
01023 ts = rpmtsCreate();
01024
01025 rpmMessage(RPMMESS_NORMAL, _("Searching for required packages....\n"));
01026
01027 nddict = argvCount(fc->ddict);
01028 previx = -1;
01029 for (i = 0; i < nddict; i++) {
01030 s = fc->ddict[i];
01031
01032
01033 ix = strtol(s, &se, 10);
01034 assert(se != NULL);
01035 deptype = *se++;
01036 se++;
01037 N = se;
01038 while (*se && *se != ' ')
01039 se++;
01040 *se++ = '\0';
01041 EVR = se;
01042 while (*se && *se != ' ')
01043 se++;
01044 *se++ = '\0';
01045 Flags = strtol(se, NULL, 16);
01046
01047 if (deptype!='R') continue;
01048
01049 rpmMessage(RPMMESS_DEBUG, _("#%i requires: %s,%s,%i\n"),ix,N,EVR,Flags);
01050 if (EVR && EVR[0]) {
01051 rpmMessage(RPMMESS_DEBUG, _("skipping #%i require\n"));
01052 continue;
01053 }
01054 for(j=0;j<noautoreqdep_c;j++)
01055 if (!regexec(&noautoreqdep[j],N,0,NULL,0)) {
01056 rpmMessage(RPMMESS_NORMAL,
01057 _("skipping %s requirement processing"
01058 " (matches noautoreqdep pattern #%i)\n"),N,j);
01059 break;
01060 }
01061 if (j<noautoreqdep_c) continue;
01062 if (N[0]=='/') {
01063 rpmMessage(RPMMESS_DEBUG, _("skipping #%i require (is file requirement)\n"));
01064 continue;
01065 }
01066 it=rpmtsInitIterator(ts, RPMTAG_PROVIDENAME, N, 0);
01067 if (!it) {
01068 rpmMessage(RPMMESS_DEBUG, _("%s -> not found\n"),N);
01069 continue;
01070 }
01071 rpmMessage(RPMMESS_DEBUG, _("Iterator: %p\n"),it);
01072 if (rpmdbGetIteratorCount(it)>1) {
01073 rpmMessage(RPMMESS_DEBUG, _("%s -> multiple (skipping)\n"),N);
01074 rpmdbFreeIterator(it);
01075 continue;
01076 }
01077 hdr=rpmdbNextIterator(it);
01078 assert(hdr!=NULL);
01079 r=headerGetEntry(hdr,RPMTAG_NAME,NULL,(void **)&hname, NULL);
01080 assert(r<2);
01081 if (!strcmp(hname,N)) {
01082 rpmMessage(RPMMESS_DEBUG, _("%s -> %s (skipping)\n"),N,hname);
01083 rpmdbFreeIterator(it);
01084 continue;
01085 }
01086
01087 rpmMessage(RPMMESS_DEBUG, "%s -> %s\n",N,hname);
01088
01089 ds = rpmdsSingle(RPMTAG_REQUIRENAME, hname, "", RPMSENSE_FIND_REQUIRES);
01090 xx = rpmdsMerge(&fc->requires, ds);
01091 ds = rpmdsFree(ds);
01092
01093 rpmdbFreeIterator(it);
01094 }
01095
01096 noautoreqdep = rpmfcFreeRegexps(noautoreqdep, noautoreqdep_c);
01097 ts = rpmtsFree(ts);
01098 return 0;
01099 }
01100
01101 int rpmfcApply(rpmfc fc)
01102 {
01103 rpmfcApplyTbl fcat;
01104 const char * s;
01105 char * se;
01106 rpmds ds;
01107 const char * N;
01108 const char * EVR;
01109 int_32 Flags;
01110 unsigned char deptype;
01111 int nddict;
01112 int previx;
01113 unsigned int val;
01114 int dix;
01115 int ix;
01116 int i;
01117 int xx;
01118 int skipping;
01119 int j;
01120 regex_t *noautoprovfiles = NULL;
01121 int noautoprovfiles_c;
01122 regex_t *noautoreqfiles = NULL;
01123 int noautoreqfiles_c;
01124 const char *buildroot;
01125 int buildroot_l;
01126
01127 fc->noautoprov = NULL;
01128 fc->noautoreq = NULL;
01129
01130 buildroot = rpmExpand("%{buildroot}",NULL);
01131 buildroot_l = strlen(buildroot);
01132
01133 noautoprovfiles = rpmfcExpandRegexps("%{__noautoprovfiles}", &noautoprovfiles_c);
01134 noautoreqfiles = rpmfcExpandRegexps("%{__noautoreqfiles}", &noautoreqfiles_c);
01135 fc->noautoprov = rpmfcExpandRegexps("%{__noautoprov}", &fc->noautoprov_c);
01136 fc->noautoreq = rpmfcExpandRegexps("%{__noautoreq}", &fc->noautoreq_c);
01137 rpmMessage(RPMMESS_DEBUG, _("%i _noautoprov patterns.\n"), fc->noautoprov_c);
01138 rpmMessage(RPMMESS_DEBUG, _("%i _noautoreq patterns.\n"), fc->noautoreq_c);
01139
01140
01141 for (fc->ix = 0; fc->fn[fc->ix] != NULL; fc->ix++) {
01142
01143
01144
01145 { const char *fn = strstr(fc->fn[fc->ix], "/usr/lib");
01146 if (fn) {
01147 fn += sizeof("/usr/lib")-1;
01148 if (fn[0] == '6' && fn[1] == '4')
01149 fn += 2;
01150 if (!strncmp(fn, "/python", sizeof("/python")-1))
01151 fc->fcolor->vals[fc->ix] |= RPMFC_PYTHON;
01152 }
01153 }
01154
01155 if (fc->fcolor->vals[fc->ix])
01156 for (fcat = rpmfcApplyTable; fcat->func != NULL; fcat++) {
01157 if (!(fc->fcolor->vals[fc->ix] & fcat->colormask))
01158 continue;
01159 fc->findprov = 1;
01160 fc->findreq = 1;
01161 if (strncmp(fc->fn[fc->ix],buildroot,buildroot_l)==0) {
01162 for(j = 0; j < noautoprovfiles_c; j++) {
01163 if (!regexec(&noautoprovfiles[j],
01164 fc->fn[fc->ix] + buildroot_l, 0, NULL, 0)) {
01165 rpmMessage(RPMMESS_NORMAL,
01166 _("skipping %s provides detection"
01167 " (matches noautoprovfiles pattern #%i)\n"),
01168 fc->fn[fc->ix], j);
01169 fc->findprov = 0;
01170 break;
01171 }
01172 }
01173 for(j = 0; j < noautoreqfiles_c; j++) {
01174 if (!regexec(&noautoreqfiles[j],
01175 fc->fn[fc->ix] + buildroot_l, 0, NULL, 0)) {
01176 rpmMessage(RPMMESS_NORMAL,
01177 _("skipping %s requires detection"
01178 " (matches noautoreqfiles pattern #%i)\n"),
01179 fc->fn[fc->ix], j);
01180 fc->findreq = 0;
01181 break;
01182 }
01183 }
01184 }
01185
01186 xx = (*fcat->func) (fc);
01187 }
01188 }
01189 noautoprovfiles = rpmfcFreeRegexps(noautoprovfiles, noautoprovfiles_c);
01190 noautoreqfiles = rpmfcFreeRegexps(noautoreqfiles, noautoreqfiles_c);
01191 fc->noautoprov = rpmfcFreeRegexps(fc->noautoprov, fc->noautoprov_c);
01192 fc->noautoreq = rpmfcFreeRegexps(fc->noautoreq, fc->noautoreq_c);
01193 #ifdef AUTODEP_PKGNAMES
01194 rpmfcFindRequiredPackages(fc);
01195 #endif
01196
01197
01198
01199 nddict = argvCount(fc->ddict);
01200 previx = -1;
01201 for (i = 0; i < nddict; i++) {
01202 s = fc->ddict[i];
01203
01204
01205 ix = strtol(s, &se, 10);
01206 assert(se != NULL);
01207 deptype = *se++;
01208 se++;
01209 N = se;
01210 while (*se && *se != ' ')
01211 se++;
01212 *se++ = '\0';
01213 EVR = se;
01214 while (*se && *se != ' ')
01215 se++;
01216 *se++ = '\0';
01217 Flags = strtol(se, NULL, 16);
01218
01219 dix = -1;
01220 skipping = 0;
01221 switch (deptype) {
01222 default:
01223 break;
01224 case 'P':
01225 skipping = fc->skipProv;
01226 ds = rpmdsSingle(RPMTAG_PROVIDENAME, N, EVR, Flags);
01227 dix = rpmdsFind(fc->provides, ds);
01228 ds = rpmdsFree(ds);
01229 break;
01230 case 'R':
01231 skipping = fc->skipReq;
01232 ds = rpmdsSingle(RPMTAG_REQUIRENAME, N, EVR, Flags);
01233 dix = rpmdsFind(fc->requires, ds);
01234 ds = rpmdsFree(ds);
01235 break;
01236 }
01237
01238
01239 #if 0
01240 assert(dix >= 0);
01241 #else
01242 if (dix < 0)
01243 continue;
01244 #endif
01245
01246 val = (deptype << 24) | (dix & 0x00ffffff);
01247 xx = argiAdd(&fc->ddictx, -1, val);
01248
01249 if (previx != ix) {
01250 previx = ix;
01251 xx = argiAdd(&fc->fddictx, ix, argiCount(fc->ddictx)-1);
01252 }
01253 if (fc->fddictn && fc->fddictn->vals && !skipping)
01254 fc->fddictn->vals[ix]++;
01255 }
01256
01257
01258 return 0;
01259 }
01260
01261 int rpmfcClassify(rpmfc fc, ARGV_t argv, int_16 * fmode)
01262 {
01263 ARGV_t fcav = NULL;
01264 ARGV_t dav;
01265 const char * s, * se;
01266 size_t slen;
01267 int fcolor;
01268 int xx;
01269 const char * magicfile;
01270 int msflags = MAGIC_CHECK;
01271 magic_t ms = NULL;
01272
01273 if (fc == NULL || argv == NULL)
01274 return 0;
01275
01276 magicfile = rpmExpand("%{?_rpmfc_magic_path}", NULL);
01277 if (magicfile == NULL || *magicfile == '\0' || *magicfile == '%')
01278 goto exit;
01279
01280 fc->nfiles = argvCount(argv);
01281
01282
01283 xx = argiAdd(&fc->fddictx, fc->nfiles-1, 0);
01284 xx = argiAdd(&fc->fddictn, fc->nfiles-1, 0);
01285
01286
01287 xx = argvAdd(&fc->cdict, "");
01288 xx = argvAdd(&fc->cdict, "directory");
01289
01290 ms = magic_open(msflags);
01291 if (ms == NULL) {
01292 xx = RPMERR_EXEC;
01293 rpmError(xx, _("magic_open(0x%x) failed: %s\n"),
01294 msflags, strerror(errno));
01295 assert(ms != NULL);
01296 }
01297
01298 xx = magic_load(ms, magicfile);
01299 if (xx == -1) {
01300 xx = RPMERR_EXEC;
01301 rpmError(xx, _("magic_load(ms, \"%s\") failed: %s\n"),
01302 magicfile, magic_error(ms));
01303 assert(xx != -1);
01304 }
01305
01306 for (fc->ix = 0; fc->ix < fc->nfiles; fc->ix++) {
01307 const char * ftype;
01308 int_16 mode = (fmode ? fmode[fc->ix] : 0);
01309 int urltype;
01310
01311 urltype = urlPath(argv[fc->ix], &s);
01312 assert(s != NULL && *s == '/');
01313 slen = strlen(s);
01314
01315
01316 switch (mode & S_IFMT) {
01317 case S_IFCHR: ftype = "character special"; break;
01318 case S_IFBLK: ftype = "block special"; break;
01319 #if defined(S_IFIFO)
01320 case S_IFIFO: ftype = "fifo (named pipe)"; break;
01321 #endif
01322 #if defined(S_IFSOCK)
01323
01324 case S_IFSOCK: ftype = "socket"; break;
01325
01326 #endif
01327 case S_IFDIR:
01328 case S_IFLNK:
01329 case S_IFREG:
01330 default:
01331
01332 #define _suffix(_s, _x) \
01333 (slen >= sizeof(_x) && !strcmp((_s)+slen-(sizeof(_x)-1), (_x)))
01334
01335
01336 if (_suffix(s, ".pm"))
01337 ftype = "Perl5 module source text";
01338
01339
01340 else if (_suffix(s, ".jar"))
01341 ftype = "Java archive file";
01342
01343
01344 else if (_suffix(s, ".class"))
01345 ftype = "Java class file";
01346
01347
01348 else if (_suffix(s, ".la"))
01349 ftype = "libtool library file";
01350
01351
01352 else if (_suffix(s, ".pc"))
01353 ftype = "pkgconfig file";
01354
01355
01356 else if (_suffix(s, ".php"))
01357 ftype = "PHP script text";
01358
01359
01360 else if (_suffix(s, ".desktop"))
01361 ftype = "Desktop Entry";
01362
01363
01364 else if (slen >= fc->brlen+sizeof("/dev/") && !strncmp(s+fc->brlen, "/dev/", sizeof("/dev/")-1))
01365 ftype = "";
01366 else
01367 ftype = magic_file(ms, s);
01368
01369 if (ftype == NULL) {
01370 xx = RPMERR_EXEC;
01371 rpmError(xx, _("magic_file(ms, \"%s\") failed: mode %06o %s\n"),
01372 s, mode, magic_error(ms));
01373 assert(ftype != NULL);
01374 }
01375 break;
01376 }
01377
01378
01379 se = ftype;
01380 rpmMessage(RPMMESS_DEBUG, "%s: %s\n", s, se);
01381
01382
01383 xx = argvAdd(&fc->fn, s);
01384
01385
01386 xx = argvAdd(&fcav, se);
01387
01388
01389 fcolor = rpmfcColoring(se);
01390 xx = argiAdd(&fc->fcolor, fc->ix, fcolor);
01391
01392
01393 if (fcolor != RPMFC_WHITE && (fcolor & RPMFC_INCLUDE))
01394 xx = rpmfcSaveArg(&fc->cdict, se);
01395
01396 }
01397
01398
01399 fc->fknown = 0;
01400 for (fc->ix = 0; fc->ix < fc->nfiles; fc->ix++) {
01401 se = fcav[fc->ix];
01402 assert(se != NULL);
01403
01404 dav = argvSearch(fc->cdict, se, NULL);
01405 if (dav) {
01406 xx = argiAdd(&fc->fcdictx, fc->ix, (dav - fc->cdict));
01407 fc->fknown++;
01408 } else {
01409 xx = argiAdd(&fc->fcdictx, fc->ix, 0);
01410 fc->fwhite++;
01411 }
01412 }
01413
01414 fcav = argvFree(fcav);
01415
01416 if (ms != NULL)
01417 magic_close(ms);
01418
01419 exit:
01420 magicfile = _free(magicfile);
01421
01422 return 0;
01423 }
01424
01427 typedef struct DepMsg_s * DepMsg_t;
01428
01431 struct DepMsg_s {
01432
01433 const char * msg;
01434
01435 const char * argv[4];
01436 rpmTag ntag;
01437 rpmTag vtag;
01438 rpmTag ftag;
01439 int mask;
01440 int xor;
01441 };
01442
01445
01446 static struct DepMsg_s depMsgs[] = {
01447 { "Provides", { "%{?__find_provides}", NULL, NULL, NULL },
01448 RPMTAG_PROVIDENAME, RPMTAG_PROVIDEVERSION, RPMTAG_PROVIDEFLAGS,
01449 0, -1 },
01450 { "Requires(interp)", { NULL, "interp", NULL, NULL },
01451 RPMTAG_REQUIRENAME, RPMTAG_REQUIREVERSION, RPMTAG_REQUIREFLAGS,
01452 _notpre(RPMSENSE_INTERP), 0 },
01453 { "Requires(rpmlib)", { NULL, "rpmlib", NULL, NULL },
01454 -1, -1, RPMTAG_REQUIREFLAGS,
01455 _notpre(RPMSENSE_RPMLIB), 0 },
01456 { "Requires(verify)", { NULL, "verify", NULL, NULL },
01457 -1, -1, RPMTAG_REQUIREFLAGS,
01458 RPMSENSE_SCRIPT_VERIFY, 0 },
01459 { "Requires(pre)", { NULL, "pre", NULL, NULL },
01460 -1, -1, RPMTAG_REQUIREFLAGS,
01461 _notpre(RPMSENSE_SCRIPT_PRE), 0 },
01462 { "Requires(post)", { NULL, "post", NULL, NULL },
01463 -1, -1, RPMTAG_REQUIREFLAGS,
01464 _notpre(RPMSENSE_SCRIPT_POST), 0 },
01465 { "Requires(preun)", { NULL, "preun", NULL, NULL },
01466 -1, -1, RPMTAG_REQUIREFLAGS,
01467 _notpre(RPMSENSE_SCRIPT_PREUN), 0 },
01468 { "Requires(postun)", { NULL, "postun", NULL, NULL },
01469 -1, -1, RPMTAG_REQUIREFLAGS,
01470 _notpre(RPMSENSE_SCRIPT_POSTUN), 0 },
01471 { "Requires", { "%{?__find_requires}", NULL, NULL, NULL },
01472 -1, -1, RPMTAG_REQUIREFLAGS,
01473 RPMSENSE_FIND_REQUIRES|RPMSENSE_TRIGGERIN|RPMSENSE_TRIGGERUN|RPMSENSE_TRIGGERPOSTUN|RPMSENSE_TRIGGERPREIN, 0 },
01474 { "Conflicts", { "%{?__find_conflicts}", NULL, NULL, NULL },
01475 RPMTAG_CONFLICTNAME, RPMTAG_CONFLICTVERSION, RPMTAG_CONFLICTFLAGS,
01476 0, -1 },
01477 { "Obsoletes", { "%{?__find_obsoletes}", NULL, NULL, NULL },
01478 RPMTAG_OBSOLETENAME, RPMTAG_OBSOLETEVERSION, RPMTAG_OBSOLETEFLAGS,
01479 0, -1 },
01480 { NULL, { NULL, NULL, NULL, NULL }, 0, 0, 0, 0, 0 }
01481 };
01482
01483
01484 static DepMsg_t DepMsgs = depMsgs;
01485
01490 static void printDeps(Header h)
01491
01492
01493 {
01494 DepMsg_t dm;
01495 rpmds ds = NULL;
01496 int flags = 0x2;
01497 const char * DNEVR;
01498 int_32 Flags;
01499 int bingo = 0;
01500
01501 for (dm = DepMsgs; dm->msg != NULL; dm++) {
01502 if (dm->ntag != -1) {
01503 ds = rpmdsFree(ds);
01504 ds = rpmdsNew(h, dm->ntag, flags);
01505 }
01506 if (dm->ftag == 0)
01507 continue;
01508
01509 ds = rpmdsInit(ds);
01510 if (ds == NULL)
01511 continue;
01512
01513 bingo = 0;
01514 while (rpmdsNext(ds) >= 0) {
01515
01516 Flags = rpmdsFlags(ds);
01517
01518 if (!((Flags & dm->mask) ^ dm->xor))
01519 continue;
01520 if (bingo == 0) {
01521 rpmMessage(RPMMESS_NORMAL, "%s:", (dm->msg ? dm->msg : ""));
01522 bingo = 1;
01523 }
01524 if ((DNEVR = rpmdsDNEVR(ds)) == NULL)
01525 continue;
01526 rpmMessage(RPMMESS_NORMAL, " %s", DNEVR+2);
01527 }
01528 if (bingo)
01529 rpmMessage(RPMMESS_NORMAL, "\n");
01530 }
01531 ds = rpmdsFree(ds);
01532 }
01533
01536 static int rpmfcGenerateDependsHelper(const Spec spec, Package pkg, rpmfi fi)
01537
01538
01539 {
01540 StringBuf sb_stdin;
01541 StringBuf sb_stdout;
01542 DepMsg_t dm;
01543 int failnonzero = 0;
01544 int rc = 0;
01545
01546
01547
01548
01549 sb_stdin = newStringBuf();
01550 fi = rpmfiInit(fi, 0);
01551 if (fi != NULL)
01552 while (rpmfiNext(fi) >= 0)
01553 appendLineStringBuf(sb_stdin, rpmfiFN(fi));
01554
01555 for (dm = DepMsgs; dm->msg != NULL; dm++) {
01556 int tag, tagflags;
01557 char * s;
01558 int xx;
01559
01560 tag = (dm->ftag > 0) ? dm->ftag : dm->ntag;
01561 tagflags = 0;
01562 s = NULL;
01563
01564 switch(tag) {
01565 case RPMTAG_PROVIDEFLAGS:
01566 if (!pkg->autoProv)
01567 continue;
01568 failnonzero = 1;
01569 tagflags = RPMSENSE_FIND_PROVIDES;
01570 break;
01571 case RPMTAG_REQUIREFLAGS:
01572 if (!pkg->autoReq)
01573 continue;
01574 failnonzero = 0;
01575 tagflags = RPMSENSE_FIND_REQUIRES;
01576 break;
01577 default:
01578 continue;
01579 break;
01580 }
01581
01582
01583 xx = rpmfcExec(dm->argv, sb_stdin, &sb_stdout, failnonzero);
01584
01585 if (xx == -1)
01586 continue;
01587
01588 s = rpmExpand(dm->argv[0], NULL);
01589 rpmMessage(RPMMESS_NORMAL, _("Finding %s: %s\n"), dm->msg,
01590 (s ? s : ""));
01591 s = _free(s);
01592
01593 if (sb_stdout == NULL) {
01594 rc = RPMERR_EXEC;
01595 rpmError(rc, _("Failed to find %s:\n"), dm->msg);
01596 break;
01597 }
01598
01599
01600 if (spec->_parseRCPOT)
01601 rc = spec->_parseRCPOT(spec, pkg, getStringBuf(sb_stdout), tag,
01602 0, tagflags);
01603 sb_stdout = freeStringBuf(sb_stdout);
01604
01605 if (rc) {
01606 rpmError(rc, _("Failed to find %s:\n"), dm->msg);
01607 break;
01608 }
01609 }
01610
01611 sb_stdin = freeStringBuf(sb_stdin);
01612
01613 return rc;
01614 }
01615
01618
01619 static struct DepMsg_s scriptMsgs[] = {
01620 { "Requires(pre)", { "%{?__scriptlet_requires}", NULL, NULL, NULL },
01621 RPMTAG_PREINPROG, RPMTAG_PREIN, RPMTAG_REQUIREFLAGS,
01622 RPMSENSE_SCRIPT_PRE, 0 },
01623 { "Requires(post)", { "%{?__scriptlet_requires}", NULL, NULL, NULL },
01624 RPMTAG_POSTINPROG, RPMTAG_POSTIN, RPMTAG_REQUIREFLAGS,
01625 RPMSENSE_SCRIPT_POST, 0 },
01626 { "Requires(preun)", { "%{?__scriptlet_requires}", NULL, NULL, NULL },
01627 RPMTAG_PREUNPROG, RPMTAG_PREUN, RPMTAG_REQUIREFLAGS,
01628 RPMSENSE_SCRIPT_PREUN, 0 },
01629 { "Requires(postun)", { "%{?__scriptlet_requires}", NULL, NULL, NULL },
01630 RPMTAG_POSTUNPROG, RPMTAG_POSTUN, RPMTAG_REQUIREFLAGS,
01631 RPMSENSE_SCRIPT_POSTUN, 0 },
01632 { NULL, { NULL, NULL, NULL, NULL }, 0, 0, 0, 0, 0 }
01633 };
01634
01635
01636 static DepMsg_t ScriptMsgs = scriptMsgs;
01637
01640 static int rpmfcGenerateScriptletDeps(const Spec spec, Package pkg)
01641
01642
01643 {
01644 HGE_t hge = (HGE_t)headerGetEntryMinMemory;
01645 StringBuf sb_stdin = newStringBuf();
01646 StringBuf sb_stdout = NULL;
01647 DepMsg_t dm;
01648 int failnonzero = 0;
01649 int rc = 0;
01650
01651
01652 for (dm = ScriptMsgs; dm->msg != NULL; dm++) {
01653 int tag, tagflags;
01654 char * s;
01655 int xx;
01656
01657 tag = dm->ftag;
01658 tagflags = RPMSENSE_FIND_REQUIRES | dm->mask;
01659
01660
01661 s = NULL;
01662 if (!hge(pkg->header, dm->ntag, NULL, (void **)&s, NULL) || s == NULL)
01663 continue;
01664 if (strcmp(s, "/bin/sh") && strcmp(s, "/bin/bash"))
01665 continue;
01666
01667
01668 s = NULL;
01669 if (!hge(pkg->header, dm->vtag, NULL, (void **)&s, NULL) || s == NULL)
01670 continue;
01671 truncStringBuf(sb_stdin);
01672 appendLineStringBuf(sb_stdin, s);
01673 stripTrailingBlanksStringBuf(sb_stdin);
01674
01675
01676 xx = rpmfcExec(dm->argv, sb_stdin, &sb_stdout, failnonzero);
01677
01678 if (xx == -1)
01679 continue;
01680
01681
01682 s = getStringBuf(sb_stdout);
01683 if (s != NULL && *s != '\0') {
01684 char * se = s;
01685
01686 while ((se = strstr(se, "executable(/")) != NULL) {
01687
01688 se = stpcpy(se, " ");
01689 *se = '/';
01690
01691 se = strchr(se, ')');
01692 if (se == NULL)
01693 break;
01694 *se++ = ' ';
01695 }
01696 if (spec->_parseRCPOT)
01697 rc = spec->_parseRCPOT(spec, pkg, s, tag, 0, tagflags);
01698 }
01699 sb_stdout = freeStringBuf(sb_stdout);
01700
01701 }
01702
01703
01704 sb_stdin = freeStringBuf(sb_stdin);
01705
01706 return rc;
01707 }
01708
01709 int rpmfcGenerateDepends(void * specp, void * pkgp)
01710 {
01711 const Spec spec = specp;
01712 Package pkg = pkgp;
01713 rpmfi fi = pkg->cpioList;
01714 rpmfc fc = NULL;
01715 rpmds ds;
01716 int flags = 0x2;
01717 ARGV_t av;
01718 int_16 * fmode;
01719 int ac = rpmfiFC(fi);
01720 const void ** p;
01721 char buf[BUFSIZ];
01722 const char * N;
01723 const char * EVR;
01724 int genConfigDeps, internaldeps;
01725 int c;
01726 int rc = 0;
01727 int xx;
01728
01729
01730 if (ac <= 0)
01731 return 0;
01732
01733
01734 if (! (pkg->autoReq || pkg->autoProv))
01735 return 0;
01736
01737
01738 internaldeps = rpmExpandNumeric("%{?_use_internal_dependency_generator}");
01739 if (internaldeps == 0) {
01740
01741 rc = rpmfcGenerateDependsHelper(spec, pkg, fi);
01742 printDeps(pkg->header);
01743 return rc;
01744 }
01745
01746
01747 if (internaldeps > 1)
01748 xx = rpmfcGenerateScriptletDeps(spec, pkg);
01749
01750
01751 av = xcalloc(ac+1, sizeof(*av));
01752 fmode = xcalloc(ac+1, sizeof(*fmode));
01753
01754
01755 genConfigDeps = 0;
01756 fi = rpmfiInit(fi, 0);
01757 if (fi != NULL)
01758 while ((c = rpmfiNext(fi)) >= 0) {
01759 rpmfileAttrs fileAttrs;
01760
01761
01762 fileAttrs = rpmfiFFlags(fi);
01763 genConfigDeps |= (fileAttrs & RPMFILE_CONFIG);
01764
01765 av[c] = xstrdup(rpmfiFN(fi));
01766 fmode[c] = rpmfiFMode(fi);
01767 }
01768 av[ac] = NULL;
01769
01770
01771 fc = rpmfcNew();
01772 fc->skipProv = !pkg->autoProv;
01773 fc->skipReq = !pkg->autoReq;
01774 fc->tracked = 0;
01775
01776 { const char * buildRootURL;
01777 const char * buildRoot;
01778 buildRootURL = rpmGenPath(spec->rootURL, "%{?buildroot}", NULL);
01779 (void) urlPath(buildRootURL, &buildRoot);
01780 if (buildRoot && !strcmp(buildRoot, "/")) buildRoot = NULL;
01781 fc->brlen = (buildRoot ? strlen(buildRoot) : 0);
01782 buildRootURL = _free(buildRootURL);
01783 }
01784
01785
01786 if (!fc->skipProv) {
01787 ds = rpmdsNew(pkg->header, RPMTAG_PROVIDENAME, flags);
01788 xx = rpmdsMerge(&fc->provides, ds);
01789 ds = rpmdsFree(ds);
01790 xx = headerRemoveEntry(pkg->header, RPMTAG_PROVIDENAME);
01791 xx = headerRemoveEntry(pkg->header, RPMTAG_PROVIDEVERSION);
01792 xx = headerRemoveEntry(pkg->header, RPMTAG_PROVIDEFLAGS);
01793
01794
01795 if (genConfigDeps) {
01796 N = rpmdsN(pkg->ds);
01797 assert(N != NULL);
01798 EVR = rpmdsEVR(pkg->ds);
01799 assert(EVR != NULL);
01800 sprintf(buf, "config(%s)", N);
01801 ds = rpmdsSingle(RPMTAG_PROVIDENAME, buf, EVR,
01802 (RPMSENSE_EQUAL|RPMSENSE_CONFIG));
01803 xx = rpmdsMerge(&fc->provides, ds);
01804 ds = rpmdsFree(ds);
01805 }
01806 }
01807
01808 if (!fc->skipReq) {
01809 ds = rpmdsNew(pkg->header, RPMTAG_REQUIRENAME, flags);
01810 xx = rpmdsMerge(&fc->requires, ds);
01811 ds = rpmdsFree(ds);
01812 xx = headerRemoveEntry(pkg->header, RPMTAG_REQUIRENAME);
01813 xx = headerRemoveEntry(pkg->header, RPMTAG_REQUIREVERSION);
01814 xx = headerRemoveEntry(pkg->header, RPMTAG_REQUIREFLAGS);
01815
01816
01817 if (genConfigDeps) {
01818 N = rpmdsN(pkg->ds);
01819 assert(N != NULL);
01820 EVR = rpmdsEVR(pkg->ds);
01821 assert(EVR != NULL);
01822 sprintf(buf, "config(%s)", N);
01823 ds = rpmdsSingle(RPMTAG_REQUIRENAME, buf, EVR,
01824 (RPMSENSE_EQUAL|RPMSENSE_CONFIG));
01825 xx = rpmdsMerge(&fc->requires, ds);
01826 ds = rpmdsFree(ds);
01827 }
01828 }
01829
01830
01831 xx = rpmfcClassify(fc, av, fmode);
01832
01833
01834 xx = rpmfcApply(fc);
01835
01836
01837 p = (const void **) argiData(fc->fcolor);
01838 c = argiCount(fc->fcolor);
01839 assert(ac == c);
01840 if (p != NULL && c > 0) {
01841 int_32 * fcolors = (int_32 *)p;
01842 int i;
01843
01844
01845 for (i = 0; i < c; i++)
01846 fcolors[i] &= 0x0f;
01847 xx = headerAddEntry(pkg->header, RPMTAG_FILECOLORS, RPM_INT32_TYPE,
01848 p, c);
01849 }
01850
01851
01852 p = (const void **) argvData(fc->cdict);
01853 c = argvCount(fc->cdict);
01854 if (p != NULL && c > 0)
01855 xx = headerAddEntry(pkg->header, RPMTAG_CLASSDICT, RPM_STRING_ARRAY_TYPE,
01856 p, c);
01857
01858
01859 p = (const void **) argiData(fc->fcdictx);
01860 c = argiCount(fc->fcdictx);
01861 assert(ac == c);
01862 if (p != NULL && c > 0)
01863 xx = headerAddEntry(pkg->header, RPMTAG_FILECLASS, RPM_INT32_TYPE,
01864 p, c);
01865
01866
01867
01868 if (fc->provides != NULL && (c = rpmdsCount(fc->provides)) > 0 && !fc->skipProv) {
01869 p = (const void **) fc->provides->N;
01870 xx = headerAddEntry(pkg->header, RPMTAG_PROVIDENAME, RPM_STRING_ARRAY_TYPE,
01871 p, c);
01872
01873
01874 p = (const void **) fc->provides->EVR;
01875 assert(p != NULL);
01876 xx = headerAddEntry(pkg->header, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
01877 p, c);
01878 p = (const void **) fc->provides->Flags;
01879 assert(p != NULL);
01880 xx = headerAddEntry(pkg->header, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
01881 p, c);
01882
01883 }
01884
01885
01886
01887
01888 if (fc->requires != NULL && (c = rpmdsCount(fc->requires)) > 0 && !fc->skipReq) {
01889 p = (const void **) fc->requires->N;
01890 xx = headerAddEntry(pkg->header, RPMTAG_REQUIRENAME, RPM_STRING_ARRAY_TYPE,
01891 p, c);
01892
01893
01894 p = (const void **) fc->requires->EVR;
01895 assert(p != NULL);
01896 xx = headerAddEntry(pkg->header, RPMTAG_REQUIREVERSION, RPM_STRING_ARRAY_TYPE,
01897 p, c);
01898 p = (const void **) fc->requires->Flags;
01899 assert(p != NULL);
01900 xx = headerAddEntry(pkg->header, RPMTAG_REQUIREFLAGS, RPM_INT32_TYPE,
01901 p, c);
01902
01903 }
01904
01905
01906
01907 p = (const void **) argiData(fc->ddictx);
01908 c = argiCount(fc->ddictx);
01909 if (p != NULL)
01910 xx = headerAddEntry(pkg->header, RPMTAG_DEPENDSDICT, RPM_INT32_TYPE,
01911 p, c);
01912
01913
01914 p = (const void **) argiData(fc->fddictx);
01915 c = argiCount(fc->fddictx);
01916 assert(ac == c);
01917 if (p != NULL)
01918 xx = headerAddEntry(pkg->header, RPMTAG_FILEDEPENDSX, RPM_INT32_TYPE,
01919 p, c);
01920
01921 p = (const void **) argiData(fc->fddictn);
01922 c = argiCount(fc->fddictn);
01923 assert(ac == c);
01924 if (p != NULL)
01925 xx = headerAddEntry(pkg->header, RPMTAG_FILEDEPENDSN, RPM_INT32_TYPE,
01926 p, c);
01927
01928 printDeps(pkg->header);
01929
01930 if (fc != NULL && _rpmfc_debug) {
01931 char msg[BUFSIZ];
01932 sprintf(msg, "final: files %d cdict[%d] %d%% ddictx[%d]", fc->nfiles, argvCount(fc->cdict), ((100 * fc->fknown)/fc->nfiles), argiCount(fc->ddictx));
01933 rpmfcPrint(msg, fc, NULL);
01934 }
01935
01936
01937 fmode = _free(fmode);
01938 fc = rpmfcFree(fc);
01939 av = argvFree(av);
01940
01941 return rc;
01942 }