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