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