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