build/rpmfc.c

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

Generated on Tue Feb 19 22:53:13 2008 for rpm by  doxygen 1.5.1