f46deae9a7c3a2645a4669c5b2fce1119dad84e8
[platform/upstream/rpm.git] / build / rpmfc.c
1 #include "system.h"
2
3 #include <signal.h>     /* getOutputFrom() */
4
5 #include <rpmbuild.h>
6 #include <argv.h>
7 #include <rpmfc.h>
8
9 #define _RPMDS_INTERNAL
10 #include <rpmds.h>
11 #include <rpmfi.h>
12
13 #if HAVE_GELF_H
14 #include <gelf.h>
15
16 #if !defined(DT_GNU_HASH)
17 #define DT_GNU_HASH             0x6ffffef5
18 #endif
19
20 #endif
21
22 #include "debug.h"
23
24 /**
25  */
26 struct rpmfc_s {
27     int nfiles;         /*!< no. of files */
28     int fknown;         /*!< no. of classified files */
29     int fwhite;         /*!< no. of "white" files */
30     int ix;             /*!< current file index */
31     int skipProv;       /*!< Don't auto-generate Provides:? */
32     int skipReq;        /*!< Don't auto-generate Requires:? */
33     int tracked;        /*!< Versioned Provides: tracking dependency added? */
34     size_t brlen;       /*!< strlen(spec->buildRoot) */
35
36     ARGV_t fn;          /*!< (no. files) file names */
37     ARGI_t fcolor;      /*!< (no. files) file colors */
38     ARGI_t fcdictx;     /*!< (no. files) file class dictionary indices */
39     ARGI_t fddictx;     /*!< (no. files) file depends dictionary start */
40     ARGI_t fddictn;     /*!< (no. files) file depends dictionary no. entries */
41     ARGV_t cdict;       /*!< (no. classes) file class dictionary */
42     ARGV_t ddict;       /*!< (no. dependencies) file depends dictionary */
43     ARGI_t ddictx;      /*!< (no. dependencies) file->dependency mapping */
44
45     rpmds provides;     /*!< (no. provides) package provides */
46     rpmds requires;     /*!< (no. requires) package requires */
47
48     StringBuf sb_java;  /*!< concatenated list of java colored files. */
49     StringBuf sb_perl;  /*!< concatenated list of perl colored files. */
50     StringBuf sb_python;/*!< concatenated list of python colored files. */
51
52 };
53
54
55 /**
56  */
57 static int rpmfcExpandAppend(ARGV_t * argvp, const ARGV_t av)
58 {
59     ARGV_t argv = *argvp;
60     int argc = argvCount(argv);
61     int ac = argvCount(av);
62     int i;
63
64     argv = xrealloc(argv, (argc + ac + 1) * sizeof(*argv));
65     for (i = 0; i < ac; i++)
66         argv[argc + i] = rpmExpand(av[i], NULL);
67     argv[argc + ac] = NULL;
68     *argvp = argv;
69     return 0;
70 }
71
72 /** \ingroup rpmbuild
73  * Return output from helper script.
74  * @todo Use poll(2) rather than select(2), if available.
75  * @param dir           directory to run in (or NULL)
76  * @param argv          program and arguments to run
77  * @param writePtr      bytes to feed to script on stdin (or NULL)
78  * @param writeBytesLeft no. of bytes to feed to script on stdin
79  * @param failNonZero   is script failure an error?
80  * @return              buffered stdout from script, NULL on error
81  */     
82 static StringBuf getOutputFrom(const char * dir, ARGV_t argv,
83                         const char * writePtr, int writeBytesLeft,
84                         int failNonZero)
85 {
86     pid_t child, reaped;
87     int toProg[2];
88     int fromProg[2];
89     int status;
90     void *oldhandler;
91     StringBuf readBuff;
92     int done;
93
94     /* FIX: cast? */
95     oldhandler = signal(SIGPIPE, SIG_IGN);
96
97     toProg[0] = toProg[1] = 0;
98     (void) pipe(toProg);
99     fromProg[0] = fromProg[1] = 0;
100     (void) pipe(fromProg);
101     
102     if (!(child = fork())) {
103         (void) close(toProg[1]);
104         (void) close(fromProg[0]);
105         
106         (void) dup2(toProg[0], STDIN_FILENO);   /* Make stdin the in pipe */
107         (void) dup2(fromProg[1], STDOUT_FILENO); /* Make stdout the out pipe */
108
109         (void) close(toProg[0]);
110         (void) close(fromProg[1]);
111
112         if (dir) {
113             (void) chdir(dir);
114         }
115         
116         rpmlog(RPMLOG_DEBUG, _("\texecv(%s) pid %d\n"),
117                         argv[0], (unsigned)getpid());
118
119         unsetenv("MALLOC_CHECK_");
120         (void) execvp(argv[0], (char *const *)argv);
121         /* XXX this error message is probably not seen. */
122         rpmlog(RPMERR_EXEC, _("Couldn't exec %s: %s\n"),
123                 argv[0], strerror(errno));
124         _exit(RPMERR_EXEC);
125     }
126     if (child < 0) {
127         rpmlog(RPMERR_FORK, _("Couldn't fork %s: %s\n"),
128                 argv[0], strerror(errno));
129         return NULL;
130     }
131
132     (void) close(toProg[0]);
133     (void) close(fromProg[1]);
134
135     /* Do not block reading or writing from/to prog. */
136     (void) fcntl(fromProg[0], F_SETFL, O_NONBLOCK);
137     (void) fcntl(toProg[1], F_SETFL, O_NONBLOCK);
138     
139     readBuff = newStringBuf();
140
141     do {
142         fd_set ibits, obits;
143         struct timeval tv;
144         int nfd, nbw, nbr;
145         int rc;
146
147         done = 0;
148 top:
149         FD_ZERO(&ibits);
150         FD_ZERO(&obits);
151         if (fromProg[0] >= 0) {
152             FD_SET(fromProg[0], &ibits);
153         }
154         if (toProg[1] >= 0) {
155             FD_SET(toProg[1], &obits);
156         }
157         /* XXX values set to limit spinning with perl doing ~100 forks/sec. */
158         tv.tv_sec = 0;
159         tv.tv_usec = 10000;
160         nfd = ((fromProg[0] > toProg[1]) ? fromProg[0] : toProg[1]);
161         if ((rc = select(nfd, &ibits, &obits, NULL, &tv)) < 0) {
162             if (errno == EINTR)
163                 goto top;
164             break;
165         }
166
167         /* Write any data to program */
168         if (toProg[1] >= 0 && FD_ISSET(toProg[1], &obits)) {
169           if (writePtr && writeBytesLeft > 0) {
170             if ((nbw = write(toProg[1], writePtr,
171                     (1024<writeBytesLeft) ? 1024 : writeBytesLeft)) < 0) {
172                 if (errno != EAGAIN) {
173                     perror("getOutputFrom()");
174                     exit(EXIT_FAILURE);
175                 }
176                 nbw = 0;
177             }
178             writeBytesLeft -= nbw;
179             writePtr += nbw;
180           } else if (toProg[1] >= 0) {  /* close write fd */
181             (void) close(toProg[1]);
182             toProg[1] = -1;
183           }
184         }
185         
186         /* Read any data from prog */
187         {   char buf[BUFSIZ+1];
188             while ((nbr = read(fromProg[0], buf, sizeof(buf)-1)) > 0) {
189                 buf[nbr] = '\0';
190                 appendStringBuf(readBuff, buf);
191             }
192         }
193
194         /* terminate on (non-blocking) EOF or error */
195         done = (nbr == 0 || (nbr < 0 && errno != EAGAIN));
196
197     } while (!done);
198
199     /* Clean up */
200     if (toProg[1] >= 0)
201         (void) close(toProg[1]);
202     if (fromProg[0] >= 0)
203         (void) close(fromProg[0]);
204     /* FIX: cast? */
205     (void) signal(SIGPIPE, oldhandler);
206
207     /* Collect status from prog */
208     reaped = waitpid(child, &status, 0);
209     rpmlog(RPMLOG_DEBUG, _("\twaitpid(%d) rc %d status %x\n"),
210         (unsigned)child, (unsigned)reaped, status);
211
212     if (failNonZero && (!WIFEXITED(status) || WEXITSTATUS(status))) {
213         rpmlog(RPMERR_EXEC, _("%s failed\n"), argv[0]);
214         return NULL;
215     }
216     if (writeBytesLeft) {
217         rpmlog(RPMERR_EXEC, _("failed to write all data to %s\n"), argv[0]);
218         return NULL;
219     }
220     return readBuff;
221 }
222
223 int rpmfcExec(ARGV_t av, StringBuf sb_stdin, StringBuf * sb_stdoutp,
224                 int failnonzero)
225 {
226     const char * s = NULL;
227     ARGV_t xav = NULL;
228     ARGV_t pav = NULL;
229     int pac = 0;
230     int ec = -1;
231     StringBuf sb = NULL;
232     const char * buf_stdin = NULL;
233     int buf_stdin_len = 0;
234     int xx;
235
236     if (sb_stdoutp)
237         *sb_stdoutp = NULL;
238     if (!(av && *av))
239         goto exit;
240
241     /* Find path to executable with (possible) args. */
242     s = rpmExpand(av[0], NULL);
243     if (!(s && *s))
244         goto exit;
245
246     /* Parse args buried within expanded exacutable. */
247     pac = 0;
248     xx = poptParseArgvString(s, &pac, (const char ***)&pav);
249     if (!(xx == 0 && pac > 0 && pav != NULL))
250         goto exit;
251
252     /* Build argv, appending args to the executable args. */
253     xav = NULL;
254     xx = argvAppend(&xav, pav);
255     if (av[1])
256         xx = rpmfcExpandAppend(&xav, av + 1);
257
258     if (sb_stdin != NULL) {
259         buf_stdin = getStringBuf(sb_stdin);
260         buf_stdin_len = strlen(buf_stdin);
261     }
262
263     /* Read output from exec'd helper. */
264     sb = getOutputFrom(NULL, xav, buf_stdin, buf_stdin_len, failnonzero);
265
266     if (sb_stdoutp != NULL) {
267         *sb_stdoutp = sb;
268         sb = NULL;      /* XXX don't free */
269     }
270
271     ec = 0;
272
273 exit:
274     sb = freeStringBuf(sb);
275     xav = argvFree(xav);
276     pav = _free(pav);   /* XXX popt mallocs in single blob. */
277     s = _free(s);
278     return ec;
279 }
280
281 /**
282  */
283 static int rpmfcSaveArg(ARGV_t * argvp, const char * key)
284 {
285     int rc = 0;
286
287     if (argvSearch(*argvp, key, NULL) == NULL) {
288         rc = argvAdd(argvp, key);
289         rc = argvSort(*argvp, NULL);
290     }
291     return rc;
292 }
293
294 static char * rpmfcFileDep(char * buf, int ix,
295                 rpmds ds)
296 {
297     int_32 tagN = rpmdsTagN(ds);
298     char deptype = 'X';
299
300     buf[0] = '\0';
301     switch (tagN) {
302     case RPMTAG_PROVIDENAME:
303         deptype = 'P';
304         break;
305     case RPMTAG_REQUIRENAME:
306         deptype = 'R';
307         break;
308     }
309     if (ds != NULL)
310         sprintf(buf, "%08d%c %s %s 0x%08x", ix, deptype,
311                 rpmdsN(ds), rpmdsEVR(ds), rpmdsFlags(ds));
312     return buf;
313 };
314
315 /**
316  * Run per-interpreter dependency helper.
317  * @param fc            file classifier
318  * @param deptype       'P' == Provides:, 'R' == Requires:, helper
319  * @param nsdep         class name for interpreter (e.g. "perl")
320  * @return              0 on success
321  */
322 static int rpmfcHelper(rpmfc fc, unsigned char deptype, const char * nsdep)
323 {
324     const char * fn = fc->fn[fc->ix];
325     char buf[BUFSIZ];
326     StringBuf sb_stdout = NULL;
327     StringBuf sb_stdin;
328     const char *av[2];
329     rpmds * depsp, ds;
330     const char * N;
331     const char * EVR;
332     int_32 Flags, dsContext, tagN;
333     ARGV_t pav;
334     const char * s;
335     int pac;
336     int xx;
337     int i;
338
339     switch (deptype) {
340     default:
341         return -1;
342         break;
343     case 'P':
344         if (fc->skipProv)
345             return 0;
346         xx = snprintf(buf, sizeof(buf), "%%{?__%s_provides}", nsdep);
347         depsp = &fc->provides;
348         dsContext = RPMSENSE_FIND_PROVIDES;
349         tagN = RPMTAG_PROVIDENAME;
350         break;
351     case 'R':
352         if (fc->skipReq)
353             return 0;
354         xx = snprintf(buf, sizeof(buf), "%%{?__%s_requires}", nsdep);
355         depsp = &fc->requires;
356         dsContext = RPMSENSE_FIND_REQUIRES;
357         tagN = RPMTAG_REQUIRENAME;
358         break;
359     }
360     buf[sizeof(buf)-1] = '\0';
361     av[0] = buf;
362     av[1] = NULL;
363
364     sb_stdin = newStringBuf();
365     appendLineStringBuf(sb_stdin, fn);
366     sb_stdout = NULL;
367     xx = rpmfcExec(av, sb_stdin, &sb_stdout, 0);
368     sb_stdin = freeStringBuf(sb_stdin);
369
370     if (xx == 0 && sb_stdout != NULL) {
371         pav = NULL;
372         xx = argvSplit(&pav, getStringBuf(sb_stdout), " \t\n\r");
373         pac = argvCount(pav);
374         if (pav)
375         for (i = 0; i < pac; i++) {
376             N = pav[i];
377             EVR = "";
378             Flags = dsContext;
379             if (pav[i+1] && strchr("=<>", *pav[i+1])) {
380                 i++;
381                 for (s = pav[i]; *s; s++) {
382                     switch(*s) {
383                     default:
384 assert(*s != '\0');
385                         break;
386                     case '=':
387                         Flags |= RPMSENSE_EQUAL;
388                         break;
389                     case '<':
390                         Flags |= RPMSENSE_LESS;
391                         break;
392                     case '>':
393                         Flags |= RPMSENSE_GREATER;
394                         break;
395                     }
396                 }
397                 i++;
398                 EVR = pav[i];
399 assert(EVR != NULL);
400             }
401
402
403             /* Add tracking dependency for versioned Provides: */
404             if (!fc->tracked && deptype == 'P' && *EVR != '\0') {
405                 ds = rpmdsSingle(RPMTAG_REQUIRENAME,
406                         "rpmlib(VersionedDependencies)", "3.0.3-1",
407                         RPMSENSE_RPMLIB|(RPMSENSE_LESS|RPMSENSE_EQUAL));
408                 xx = rpmdsMerge(&fc->requires, ds);
409                 ds = rpmdsFree(ds);
410                 fc->tracked = 1;
411             }
412
413             ds = rpmdsSingle(tagN, N, EVR, Flags);
414
415             /* Add to package dependencies. */
416             xx = rpmdsMerge(depsp, ds);
417
418             /* Add to file dependencies. */
419             xx = rpmfcSaveArg(&fc->ddict, rpmfcFileDep(buf, fc->ix, ds));
420
421             ds = rpmdsFree(ds);
422         }
423
424         pav = argvFree(pav);
425     }
426     sb_stdout = freeStringBuf(sb_stdout);
427
428     return 0;
429 }
430
431 /**
432  */
433 static struct rpmfcTokens_s rpmfcTokens[] = {
434   { "directory",                RPMFC_DIRECTORY|RPMFC_INCLUDE },
435
436   { " shared object",           RPMFC_LIBRARY },
437   { " executable",              RPMFC_EXECUTABLE },
438   { " statically linked",       RPMFC_STATIC },
439   { " not stripped",            RPMFC_NOTSTRIPPED },
440   { " archive",                 RPMFC_ARCHIVE },
441
442   { "ELF 32-bit",               RPMFC_ELF32|RPMFC_INCLUDE },
443   { "ELF 64-bit",               RPMFC_ELF64|RPMFC_INCLUDE },
444
445   { " script",                  RPMFC_SCRIPT },
446   { " text",                    RPMFC_TEXT },
447   { " document",                RPMFC_DOCUMENT },
448
449   { " compressed",              RPMFC_COMPRESSED },
450
451   { "troff or preprocessor input",      RPMFC_MANPAGE|RPMFC_INCLUDE },
452   { "GNU Info",                 RPMFC_MANPAGE|RPMFC_INCLUDE },
453
454   { "perl script text",         RPMFC_PERL|RPMFC_INCLUDE },
455   { "Perl5 module source text", RPMFC_PERL|RPMFC_MODULE|RPMFC_INCLUDE },
456
457   { " /usr/bin/python",         RPMFC_PYTHON|RPMFC_INCLUDE },
458
459   /* XXX "a /usr/bin/python -t script text executable" */
460   /* XXX "python 2.3 byte-compiled" */
461   { "python ",                  RPMFC_PYTHON|RPMFC_INCLUDE },
462
463   { "libtool library ",         RPMFC_LIBTOOL|RPMFC_INCLUDE },
464   { "pkgconfig ",               RPMFC_PKGCONFIG|RPMFC_INCLUDE },
465
466   /* XXX .NET executables and libraries.  file(1) cannot differ from win32 
467    * executables unfortunately :( */
468   { "PE executable",            RPMFC_MONO|RPMFC_INCLUDE },
469
470   { "current ar archive",       RPMFC_STATIC|RPMFC_LIBRARY|RPMFC_ARCHIVE|RPMFC_INCLUDE },
471
472   { "Zip archive data",         RPMFC_COMPRESSED|RPMFC_ARCHIVE|RPMFC_INCLUDE },
473   { "tar archive",              RPMFC_ARCHIVE|RPMFC_INCLUDE },
474   { "cpio archive",             RPMFC_ARCHIVE|RPMFC_INCLUDE },
475   { "RPM v3",                   RPMFC_ARCHIVE|RPMFC_INCLUDE },
476   { "RPM v4",                   RPMFC_ARCHIVE|RPMFC_INCLUDE },
477
478   { " image",                   RPMFC_IMAGE|RPMFC_INCLUDE },
479   { " font",                    RPMFC_FONT|RPMFC_INCLUDE },
480   { " Font",                    RPMFC_FONT|RPMFC_INCLUDE },
481
482   { " commands",                RPMFC_SCRIPT|RPMFC_INCLUDE },
483   { " script",                  RPMFC_SCRIPT|RPMFC_INCLUDE },
484
485   { "empty",                    RPMFC_WHITE|RPMFC_INCLUDE },
486
487   { "HTML",                     RPMFC_WHITE|RPMFC_INCLUDE },
488   { "SGML",                     RPMFC_WHITE|RPMFC_INCLUDE },
489   { "XML",                      RPMFC_WHITE|RPMFC_INCLUDE },
490
491   { " program text",            RPMFC_WHITE|RPMFC_INCLUDE },
492   { " source",                  RPMFC_WHITE|RPMFC_INCLUDE },
493   { "GLS_BINARY_LSB_FIRST",     RPMFC_WHITE|RPMFC_INCLUDE },
494   { " DB ",                     RPMFC_WHITE|RPMFC_INCLUDE },
495
496   { "ASCII English text",       RPMFC_WHITE|RPMFC_INCLUDE },
497   { "ASCII text",               RPMFC_WHITE|RPMFC_INCLUDE },
498   { "ISO-8859 text",            RPMFC_WHITE|RPMFC_INCLUDE },
499
500   { "symbolic link to",         RPMFC_SYMLINK },
501   { "socket",                   RPMFC_DEVICE },
502   { "special",                  RPMFC_DEVICE },
503
504   { "ASCII",                    RPMFC_WHITE },
505   { "ISO-8859",                 RPMFC_WHITE },
506
507   { "data",                     RPMFC_WHITE },
508
509   { "application",              RPMFC_WHITE },
510   { "boot",                     RPMFC_WHITE },
511   { "catalog",                  RPMFC_WHITE },
512   { "code",                     RPMFC_WHITE },
513   { "file",                     RPMFC_WHITE },
514   { "format",                   RPMFC_WHITE },
515   { "message",                  RPMFC_WHITE },
516   { "program",                  RPMFC_WHITE },
517
518   { "broken symbolic link to ", RPMFC_WHITE|RPMFC_ERROR },
519   { "can't read",               RPMFC_WHITE|RPMFC_ERROR },
520   { "can't stat",               RPMFC_WHITE|RPMFC_ERROR },
521   { "executable, can't read",   RPMFC_WHITE|RPMFC_ERROR },
522   { "core file",                RPMFC_WHITE|RPMFC_ERROR },
523
524   { NULL,                       RPMFC_BLACK }
525 };
526
527 int rpmfcColoring(const char * fmstr)
528 {
529     rpmfcToken fct;
530     int fcolor = RPMFC_BLACK;
531
532     for (fct = rpmfcTokens; fct->token != NULL; fct++) {
533         if (strstr(fmstr, fct->token) == NULL)
534             continue;
535         fcolor |= fct->colors;
536         if (fcolor & RPMFC_INCLUDE)
537             return fcolor;
538     }
539     return fcolor;
540 }
541
542 void rpmfcPrint(const char * msg, rpmfc fc, FILE * fp)
543 {
544     int fcolor;
545     int ndx;
546     int cx;
547     int dx;
548     int fx;
549
550 int nprovides;
551 int nrequires;
552
553     if (fp == NULL) fp = stderr;
554
555     if (msg)
556         fprintf(fp, "===================================== %s\n", msg);
557
558 nprovides = rpmdsCount(fc->provides);
559 nrequires = rpmdsCount(fc->requires);
560
561     if (fc)
562     for (fx = 0; fx < fc->nfiles; fx++) {
563 assert(fx < fc->fcdictx->nvals);
564         cx = fc->fcdictx->vals[fx];
565 assert(fx < fc->fcolor->nvals);
566         fcolor = fc->fcolor->vals[fx];
567
568         fprintf(fp, "%3d %s", fx, fc->fn[fx]);
569         if (fcolor != RPMFC_BLACK)
570                 fprintf(fp, "\t0x%x", fc->fcolor->vals[fx]);
571         else
572                 fprintf(fp, "\t%s", fc->cdict[cx]);
573         fprintf(fp, "\n");
574
575         if (fc->fddictx == NULL || fc->fddictn == NULL)
576             continue;
577
578 assert(fx < fc->fddictx->nvals);
579         dx = fc->fddictx->vals[fx];
580 assert(fx < fc->fddictn->nvals);
581         ndx = fc->fddictn->vals[fx];
582
583         while (ndx-- > 0) {
584             const char * depval;
585             unsigned char deptype;
586             unsigned ix;
587
588             ix = fc->ddictx->vals[dx++];
589             deptype = ((ix >> 24) & 0xff);
590             ix &= 0x00ffffff;
591             depval = NULL;
592             switch (deptype) {
593             default:
594 assert(depval != NULL);
595                 break;
596             case 'P':
597                 if (nprovides > 0) {
598 assert(ix < nprovides);
599                     (void) rpmdsSetIx(fc->provides, ix-1);
600                     if (rpmdsNext(fc->provides) >= 0)
601                         depval = rpmdsDNEVR(fc->provides);
602                 }
603                 break;
604             case 'R':
605                 if (nrequires > 0) {
606 assert(ix < nrequires);
607                     (void) rpmdsSetIx(fc->requires, ix-1);
608                     if (rpmdsNext(fc->requires) >= 0)
609                         depval = rpmdsDNEVR(fc->requires);
610                 }
611                 break;
612             }
613             if (depval)
614                 fprintf(fp, "\t%s\n", depval);
615         }
616     }
617 }
618
619 rpmfc rpmfcFree(rpmfc fc)
620 {
621     if (fc) {
622         fc->fn = argvFree(fc->fn);
623         fc->fcolor = argiFree(fc->fcolor);
624         fc->fcdictx = argiFree(fc->fcdictx);
625         fc->fddictx = argiFree(fc->fddictx);
626         fc->fddictn = argiFree(fc->fddictn);
627         fc->cdict = argvFree(fc->cdict);
628         fc->ddict = argvFree(fc->ddict);
629         fc->ddictx = argiFree(fc->ddictx);
630
631         fc->provides = rpmdsFree(fc->provides);
632         fc->requires = rpmdsFree(fc->requires);
633
634         fc->sb_java = freeStringBuf(fc->sb_java);
635         fc->sb_perl = freeStringBuf(fc->sb_perl);
636         fc->sb_python = freeStringBuf(fc->sb_python);
637
638     }
639     fc = _free(fc);
640     return NULL;
641 }
642
643 rpmfc rpmfcNew(void)
644 {
645     rpmfc fc = xcalloc(1, sizeof(*fc));
646     return fc;
647 }
648
649 rpmds rpmfcProvides(rpmfc fc)
650 {
651     return (fc != NULL ? fc->provides : NULL);
652 }
653
654 rpmds rpmfcRequires(rpmfc fc)
655 {
656     return (fc != NULL ? fc->requires : NULL);
657 }
658
659
660 /**
661  * Extract script dependencies.
662  * @param fc            file classifier
663  * @return              0 on success
664  */
665 static int rpmfcSCRIPT(rpmfc fc)
666 {
667     const char * fn = fc->fn[fc->ix];
668     const char * bn;
669     rpmds ds;
670     char buf[BUFSIZ];
671     FILE * fp;
672     char * s, * se;
673     int i;
674     struct stat sb, * st = &sb;
675     int is_executable;
676     int xx;
677
678     /* Only executable scripts are searched. */
679     if (stat(fn, st) < 0)
680         return -1;
681     is_executable = (st->st_mode & (S_IXUSR|S_IXGRP|S_IXOTH));
682
683     fp = fopen(fn, "r");
684     if (fp == NULL || ferror(fp)) {
685         if (fp) (void) fclose(fp);
686         return -1;
687     }
688
689     /* Look for #! interpreter in first 10 lines. */
690     for (i = 0; i < 10; i++) {
691
692         s = fgets(buf, sizeof(buf) - 1, fp);
693         if (s == NULL || ferror(fp) || feof(fp))
694             break;
695         s[sizeof(buf)-1] = '\0';
696         if (!(s[0] == '#' && s[1] == '!'))
697             continue;
698         s += 2;
699
700         while (*s && strchr(" \t\n\r", *s) != NULL)
701             s++;
702         if (*s == '\0')
703             continue;
704         if (*s != '/')
705             continue;
706
707         for (se = s+1; *se; se++) {
708             if (strchr(" \t\n\r", *se) != NULL)
709                 break;
710         }
711         *se = '\0';
712         se++;
713
714         if (is_executable) {
715             /* Add to package requires. */
716             ds = rpmdsSingle(RPMTAG_REQUIRENAME, s, "", RPMSENSE_FIND_REQUIRES);
717             xx = rpmdsMerge(&fc->requires, ds);
718
719             /* Add to file requires. */
720             xx = rpmfcSaveArg(&fc->ddict, rpmfcFileDep(se, fc->ix, ds));
721
722             ds = rpmdsFree(ds);
723         }
724
725         /* Set color based on interpreter name. */
726         bn = basename(s);
727         if (!strcmp(bn, "perl"))
728             fc->fcolor->vals[fc->ix] |= RPMFC_PERL;
729         else if (!strncmp(bn, "python", sizeof("python")-1))
730             fc->fcolor->vals[fc->ix] |= RPMFC_PYTHON;
731
732         break;
733     }
734
735     (void) fclose(fp);
736
737     if (fc->fcolor->vals[fc->ix] & RPMFC_PERL) {
738         if (fc->fcolor->vals[fc->ix] & RPMFC_MODULE)
739             xx = rpmfcHelper(fc, 'P', "perl");
740         if (is_executable || (fc->fcolor->vals[fc->ix] & RPMFC_MODULE))
741             xx = rpmfcHelper(fc, 'R', "perl");
742     }
743     if (fc->fcolor->vals[fc->ix] & RPMFC_PYTHON) {
744         xx = rpmfcHelper(fc, 'P', "python");
745 #ifdef  NOTYET
746         if (is_executable)
747 #endif
748             xx = rpmfcHelper(fc, 'R', "python");
749     } else 
750     if (fc->fcolor->vals[fc->ix] & RPMFC_LIBTOOL) {
751         xx = rpmfcHelper(fc, 'P', "libtool");
752 #ifdef  NOTYET
753         if (is_executable)
754 #endif
755             xx = rpmfcHelper(fc, 'R', "libtool");
756     } else
757     if (fc->fcolor->vals[fc->ix] & RPMFC_PKGCONFIG) {
758         xx = rpmfcHelper(fc, 'P', "pkgconfig");
759 #ifdef  NOTYET
760         if (is_executable)
761 #endif
762             xx = rpmfcHelper(fc, 'R', "pkgconfig");
763     } else 
764     if (fc->fcolor->vals[fc->ix] & RPMFC_MONO) {
765         xx = rpmfcHelper(fc, 'P', "mono");
766         if (is_executable)
767             xx = rpmfcHelper(fc, 'R', "mono");
768     }
769
770     return 0;
771 }
772
773 /**
774  * Extract Elf dependencies.
775  * @param fc            file classifier
776  * @return              0 on success
777  */
778 static int rpmfcELF(rpmfc fc)
779 {
780 #if HAVE_GELF_H && HAVE_LIBELF
781     const char * fn = fc->fn[fc->ix];
782     Elf * elf;
783     Elf_Scn * scn;
784     Elf_Data * data;
785     GElf_Ehdr ehdr_mem, * ehdr;
786     GElf_Shdr shdr_mem, * shdr;
787     GElf_Verdef def_mem, * def;
788     GElf_Verneed need_mem, * need;
789     GElf_Dyn dyn_mem, * dyn;
790     unsigned int auxoffset;
791     unsigned int offset;
792     int fdno;
793     int cnt2;
794     int cnt;
795     char buf[BUFSIZ];
796     const char * s;
797     struct stat sb, * st = &sb;
798     const char * soname = NULL;
799     rpmds * depsp, ds;
800     int_32 tagN, dsContext;
801     char * t;
802     int xx;
803     int isElf64;
804     int isDSO;
805     int gotSONAME = 0;
806     int gotDEBUG = 0;
807     int gotHASH = 0;
808     int gotGNUHASH = 0;
809     static int filter_GLIBC_PRIVATE = 0;
810     static int oneshot = 0;
811
812     if (oneshot == 0) {
813         oneshot = 1;
814         filter_GLIBC_PRIVATE = rpmExpandNumeric("%{?_filter_GLIBC_PRIVATE}");
815     }
816
817     /* Files with executable bit set only. */
818     if (stat(fn, st) != 0)
819         return(-1);
820
821     fdno = open(fn, O_RDONLY);
822     if (fdno < 0)
823         return fdno;
824
825     (void) elf_version(EV_CURRENT);
826
827     elf = NULL;
828     if ((elf = elf_begin (fdno, ELF_C_READ, NULL)) == NULL
829      || elf_kind(elf) != ELF_K_ELF
830      || (ehdr = gelf_getehdr(elf, &ehdr_mem)) == NULL
831      || !(ehdr->e_type == ET_DYN || ehdr->e_type == ET_EXEC))
832         goto exit;
833
834     isElf64 = ehdr->e_ident[EI_CLASS] == ELFCLASS64;
835     isDSO = ehdr->e_type == ET_DYN;
836
837     scn = NULL;
838     while ((scn = elf_nextscn(elf, scn)) != NULL) {
839         shdr = gelf_getshdr(scn, &shdr_mem);
840         if (shdr == NULL)
841             break;
842
843         soname = _free(soname);
844         switch (shdr->sh_type) {
845         default:
846             continue;
847             break;
848         case SHT_GNU_verdef:
849             data = NULL;
850             if (!fc->skipProv)
851             while ((data = elf_getdata (scn, data)) != NULL) {
852                 offset = 0;
853                 for (cnt = shdr->sh_info; --cnt >= 0; ) {
854                 
855                     def = gelf_getverdef (data, offset, &def_mem);
856                     if (def == NULL)
857                         break;
858                     auxoffset = offset + def->vd_aux;
859                     for (cnt2 = def->vd_cnt; --cnt2 >= 0; ) {
860                         GElf_Verdaux aux_mem, * aux;
861
862                         aux = gelf_getverdaux (data, auxoffset, &aux_mem);
863                         if (aux == NULL)
864                             break;
865
866                         s = elf_strptr(elf, shdr->sh_link, aux->vda_name);
867                         if (s == NULL)
868                             break;
869                         if (def->vd_flags & VER_FLG_BASE) {
870                             soname = _free(soname);
871                             soname = xstrdup(s);
872                             auxoffset += aux->vda_next;
873                             continue;
874                         } else
875                         if (soname != NULL
876                          && !(filter_GLIBC_PRIVATE != 0
877                                 && !strcmp(s, "GLIBC_PRIVATE")))
878                         {
879                             buf[0] = '\0';
880                             t = buf;
881                             t = stpcpy( stpcpy( stpcpy( stpcpy(t, soname), "("), s), ")");
882
883 #if !defined(__alpha__)
884                             if (isElf64)
885                                 t = stpcpy(t, "(64bit)");
886 #endif
887                             t++;
888
889                             /* Add to package provides. */
890                             ds = rpmdsSingle(RPMTAG_PROVIDES,
891                                         buf, "", RPMSENSE_FIND_PROVIDES);
892                             xx = rpmdsMerge(&fc->provides, ds);
893
894                             /* Add to file dependencies. */
895                             xx = rpmfcSaveArg(&fc->ddict,
896                                         rpmfcFileDep(t, fc->ix, ds));
897
898                             ds = rpmdsFree(ds);
899                         }
900                         auxoffset += aux->vda_next;
901                     }
902                     offset += def->vd_next;
903                 }
904             }
905             break;
906         case SHT_GNU_verneed:
907             data = NULL;
908             /* Files with executable bit set only. */
909             if (!fc->skipReq && (st->st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)))
910             while ((data = elf_getdata (scn, data)) != NULL) {
911                 offset = 0;
912                 for (cnt = shdr->sh_info; --cnt >= 0; ) {
913                     need = gelf_getverneed (data, offset, &need_mem);
914                     if (need == NULL)
915                         break;
916
917                     s = elf_strptr(elf, shdr->sh_link, need->vn_file);
918                     if (s == NULL)
919                         break;
920                     soname = _free(soname);
921                     soname = xstrdup(s);
922                     auxoffset = offset + need->vn_aux;
923                     for (cnt2 = need->vn_cnt; --cnt2 >= 0; ) {
924                         GElf_Vernaux aux_mem, * aux;
925
926                         aux = gelf_getvernaux (data, auxoffset, &aux_mem);
927                         if (aux == NULL)
928                             break;
929
930                         s = elf_strptr(elf, shdr->sh_link, aux->vna_name);
931                         if (s == NULL)
932                             break;
933
934                         /* Filter dependencies that contain GLIBC_PRIVATE */
935                         if (soname != NULL
936                          && !(filter_GLIBC_PRIVATE != 0
937                                 && !strcmp(s, "GLIBC_PRIVATE")))
938                         {
939                             buf[0] = '\0';
940                             t = buf;
941                             t = stpcpy( stpcpy( stpcpy( stpcpy(t, soname), "("), s), ")");
942
943 #if !defined(__alpha__)
944                             if (isElf64)
945                                 t = stpcpy(t, "(64bit)");
946 #endif
947                             t++;
948
949                             /* Add to package dependencies. */
950                             ds = rpmdsSingle(RPMTAG_REQUIRENAME,
951                                         buf, "", RPMSENSE_FIND_REQUIRES);
952                             xx = rpmdsMerge(&fc->requires, ds);
953
954                             /* Add to file dependencies. */
955                             xx = rpmfcSaveArg(&fc->ddict,
956                                         rpmfcFileDep(t, fc->ix, ds));
957                             ds = rpmdsFree(ds);
958                         }
959                         auxoffset += aux->vna_next;
960                     }
961                     offset += need->vn_next;
962                 }
963             }
964             break;
965         case SHT_DYNAMIC:
966             data = NULL;
967             while ((data = elf_getdata (scn, data)) != NULL) {
968                 for (cnt = 0; cnt < (shdr->sh_size / shdr->sh_entsize); ++cnt) {
969                     dyn = gelf_getdyn (data, cnt, &dyn_mem);
970                     if (dyn == NULL)
971                         break;
972                     s = NULL;
973                     switch (dyn->d_tag) {
974                     default:
975                         continue;
976                         break;
977                     case DT_HASH:    
978                         gotHASH= 1;
979                         continue;
980                     case DT_GNU_HASH:
981                         gotGNUHASH= 1;
982                         continue;
983                     case DT_DEBUG:    
984                         gotDEBUG = 1;
985                         continue;
986                     case DT_NEEDED:
987                         /* Files with executable bit set only. */
988                         if (fc->skipReq || !(st->st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)))
989                             continue;
990                         /* Add to package requires. */
991                         depsp = &fc->requires;
992                         tagN = RPMTAG_REQUIRENAME;
993                         dsContext = RPMSENSE_FIND_REQUIRES;
994                         s = elf_strptr(elf, shdr->sh_link, dyn->d_un.d_val);
995 assert(s != NULL);
996                         break;
997                     case DT_SONAME:
998                         gotSONAME = 1;
999                         /* Add to package provides. */
1000                         if (fc->skipProv)
1001                             continue;
1002                         depsp = &fc->provides;
1003                         tagN = RPMTAG_PROVIDENAME;
1004                         dsContext = RPMSENSE_FIND_PROVIDES;
1005                         s = elf_strptr(elf, shdr->sh_link, dyn->d_un.d_val);
1006 assert(s != NULL);
1007                         break;
1008                     }
1009                     if (s == NULL)
1010                         continue;
1011
1012                     buf[0] = '\0';
1013                     t = buf;
1014                     t = stpcpy(t, s);
1015
1016 #if !defined(__alpha__)
1017                     if (isElf64)
1018                         t = stpcpy(t, "()(64bit)");
1019 #endif
1020                     t++;
1021
1022                     /* Add to package dependencies. */
1023                     ds = rpmdsSingle(tagN, buf, "", dsContext);
1024                     xx = rpmdsMerge(depsp, ds);
1025
1026                     /* Add to file dependencies. */
1027                     xx = rpmfcSaveArg(&fc->ddict,
1028                                         rpmfcFileDep(t, fc->ix, ds));
1029
1030                     ds = rpmdsFree(ds);
1031                 }
1032             }
1033             break;
1034         }
1035     }
1036
1037     /* For DSOs which use the .gnu_hash section and don't have a .hash
1038      * section, we need to ensure that we have a new enough glibc. */ 
1039     if (gotGNUHASH && !gotHASH) {
1040         ds = rpmdsSingle(RPMTAG_REQUIRENAME, "rtld(GNU_HASH)", "", 
1041                          RPMSENSE_FIND_REQUIRES);
1042         rpmdsMerge(&fc->requires, ds);
1043         buf[0] = '\0';
1044         t = buf;
1045         rpmfcSaveArg(&fc->ddict, rpmfcFileDep(t, fc->ix, ds));
1046         ds = rpmdsFree(ds);
1047     }
1048
1049     /* For DSO's, provide the basename of the file if DT_SONAME not found. */
1050     if (!fc->skipProv && isDSO && !gotDEBUG && !gotSONAME) {
1051         depsp = &fc->provides;
1052         tagN = RPMTAG_PROVIDENAME;
1053         dsContext = RPMSENSE_FIND_PROVIDES;
1054
1055         s = strrchr(fn, '/');
1056         if (s)
1057             s++;
1058         else
1059             s = fn;
1060
1061         buf[0] = '\0';
1062         t = buf;
1063 /* LCL: s is not null. */
1064         t = stpcpy(t, s);
1065
1066 #if !defined(__alpha__)
1067         if (isElf64)
1068             t = stpcpy(t, "()(64bit)");
1069 #endif
1070         t++;
1071
1072         /* Add to package dependencies. */
1073         ds = rpmdsSingle(tagN, buf, "", dsContext);
1074         xx = rpmdsMerge(depsp, ds);
1075
1076         /* Add to file dependencies. */
1077         xx = rpmfcSaveArg(&fc->ddict, rpmfcFileDep(t, fc->ix, ds));
1078
1079         ds = rpmdsFree(ds);
1080     }
1081
1082 exit:
1083     soname = _free(soname);
1084     if (elf) (void) elf_end(elf);
1085     xx = close(fdno);
1086     return 0;
1087 #else
1088     return -1;
1089 #endif
1090 }
1091
1092 typedef struct rpmfcApplyTbl_s {
1093     int (*func) (rpmfc fc);
1094     int colormask;
1095 } * rpmfcApplyTbl;
1096
1097 /**
1098  */
1099 static struct rpmfcApplyTbl_s rpmfcApplyTable[] = {
1100     { rpmfcELF,         RPMFC_ELF },
1101     { rpmfcSCRIPT,      (RPMFC_SCRIPT|RPMFC_PERL) },
1102     { rpmfcSCRIPT,      (RPMFC_SCRIPT|RPMFC_PYTHON) },
1103     { rpmfcSCRIPT,      (RPMFC_SCRIPT|RPMFC_PKGCONFIG) },
1104     { rpmfcSCRIPT,      (RPMFC_SCRIPT|RPMFC_LIBTOOL) },
1105     { rpmfcSCRIPT,      RPMFC_MONO },
1106     { NULL, 0 }
1107 };
1108
1109 int rpmfcApply(rpmfc fc)
1110 {
1111     rpmfcApplyTbl fcat;
1112     const char * s;
1113     char * se;
1114     rpmds ds;
1115     const char * N;
1116     const char * EVR;
1117     int_32 Flags;
1118     unsigned char deptype;
1119     int nddict;
1120     int previx;
1121     unsigned int val;
1122     int dix;
1123     int ix;
1124     int i;
1125     int xx;
1126
1127     /* Generate package and per-file dependencies. */
1128     for (fc->ix = 0; fc->fn[fc->ix] != NULL; fc->ix++) {
1129
1130         /* XXX Insure that /usr/lib{,64}/python files are marked RPMFC_PYTHON */
1131         /* XXX HACK: classification by path is intrinsically stupid. */
1132         {   const char *fn = strstr(fc->fn[fc->ix], "/usr/lib");
1133             if (fn) {
1134                 fn += sizeof("/usr/lib")-1;
1135                 if (fn[0] == '6' && fn[1] == '4')
1136                     fn += 2;
1137                 if (!strncmp(fn, "/python", sizeof("/python")-1))
1138                     fc->fcolor->vals[fc->ix] |= RPMFC_PYTHON;
1139             }
1140         }
1141
1142         if (fc->fcolor->vals[fc->ix])
1143         for (fcat = rpmfcApplyTable; fcat->func != NULL; fcat++) {
1144             if (!(fc->fcolor->vals[fc->ix] & fcat->colormask))
1145                 continue;
1146             xx = (*fcat->func) (fc);
1147         }
1148     }
1149
1150     /* Generate per-file indices into package dependencies. */
1151     nddict = argvCount(fc->ddict);
1152     previx = -1;
1153     for (i = 0; i < nddict; i++) {
1154         s = fc->ddict[i];
1155
1156         /* Parse out (file#,deptype,N,EVR,Flags) */
1157         ix = strtol(s, &se, 10);
1158 assert(se != NULL);
1159         deptype = *se++;
1160         se++;
1161         N = se;
1162         while (*se && *se != ' ')
1163             se++;
1164         *se++ = '\0';
1165         EVR = se;
1166         while (*se && *se != ' ')
1167             se++;
1168         *se++ = '\0';
1169         Flags = strtol(se, NULL, 16);
1170
1171         dix = -1;
1172         switch (deptype) {
1173         default:
1174             break;
1175         case 'P':       
1176             ds = rpmdsSingle(RPMTAG_PROVIDENAME, N, EVR, Flags);
1177             dix = rpmdsFind(fc->provides, ds);
1178             ds = rpmdsFree(ds);
1179             break;
1180         case 'R':
1181             ds = rpmdsSingle(RPMTAG_REQUIRENAME, N, EVR, Flags);
1182             dix = rpmdsFind(fc->requires, ds);
1183             ds = rpmdsFree(ds);
1184             break;
1185         }
1186
1187 /* XXX assertion incorrect while generating -debuginfo deps. */
1188 #if 0
1189 assert(dix >= 0);
1190 #else
1191         if (dix < 0)
1192             continue;
1193 #endif
1194
1195         val = (deptype << 24) | (dix & 0x00ffffff);
1196         xx = argiAdd(&fc->ddictx, -1, val);
1197
1198         if (previx != ix) {
1199             previx = ix;
1200             xx = argiAdd(&fc->fddictx, ix, argiCount(fc->ddictx)-1);
1201         }
1202         if (fc->fddictn && fc->fddictn->vals)
1203             fc->fddictn->vals[ix]++;
1204     }
1205
1206     return 0;
1207 }
1208
1209 int rpmfcClassify(rpmfc fc, ARGV_t argv, int_16 * fmode)
1210 {
1211     ARGV_t fcav = NULL;
1212     ARGV_t dav;
1213     const char * s, * se;
1214     size_t slen;
1215     int fcolor;
1216     int xx;
1217     int msflags = MAGIC_CHECK;  /* XXX MAGIC_COMPRESS flag? */
1218     magic_t ms = NULL;
1219
1220     if (fc == NULL || argv == NULL)
1221         return 0;
1222
1223     fc->nfiles = argvCount(argv);
1224
1225     /* Initialize the per-file dictionary indices. */
1226     xx = argiAdd(&fc->fddictx, fc->nfiles-1, 0);
1227     xx = argiAdd(&fc->fddictn, fc->nfiles-1, 0);
1228
1229     /* Build (sorted) file class dictionary. */
1230     xx = argvAdd(&fc->cdict, "");
1231     xx = argvAdd(&fc->cdict, "directory");
1232
1233     ms = magic_open(msflags);
1234     if (ms == NULL) {
1235         xx = RPMERR_EXEC;
1236         rpmlog(xx, _("magic_open(0x%x) failed: %s\n"),
1237                 msflags, strerror(errno));
1238 assert(ms != NULL);     /* XXX figger a proper return path. */
1239     }
1240
1241     xx = magic_load(ms, NULL);
1242     if (xx == -1) {
1243         xx = RPMERR_EXEC;
1244         rpmlog(xx, _("magic_load failed: %s\n"), magic_error(ms));
1245 assert(xx != -1);       /* XXX figger a proper return path. */
1246     }
1247
1248     for (fc->ix = 0; fc->ix < fc->nfiles; fc->ix++) {
1249         const char * ftype;
1250         int_16 mode = (fmode ? fmode[fc->ix] : 0);
1251
1252         s = argv[fc->ix];
1253 assert(s != NULL);
1254         slen = strlen(s);
1255
1256         switch (mode & S_IFMT) {
1257         case S_IFCHR:   ftype = "character special";    break;
1258         case S_IFBLK:   ftype = "block special";        break;
1259         case S_IFIFO:   ftype = "fifo (named pipe)";    break;
1260         case S_IFSOCK:  ftype = "socket";               break;
1261         case S_IFDIR:
1262         case S_IFLNK:
1263         case S_IFREG:
1264         default:
1265 #define _suffix(_s, _x) \
1266     (slen >= sizeof(_x) && !strcmp((_s)+slen-(sizeof(_x)-1), (_x)))
1267
1268             /* XXX all files with extension ".pm" are perl modules for now. */
1269             if (_suffix(s, ".pm"))
1270                 ftype = "Perl5 module source text";
1271
1272             /* XXX all files with extension ".la" are libtool for now. */
1273             else if (_suffix(s, ".la"))
1274                 ftype = "libtool library file";
1275
1276             /* XXX all files with extension ".pc" are pkgconfig for now. */
1277             else if (_suffix(s, ".pc"))
1278                 ftype = "pkgconfig file";
1279
1280             /* XXX skip all files in /dev/ which are (or should be) %dev dummies. */
1281             else if (slen >= fc->brlen+sizeof("/dev/") && !strncmp(s+fc->brlen, "/dev/", sizeof("/dev/")-1))
1282                 ftype = "";
1283             else
1284                 ftype = magic_file(ms, s);
1285
1286             if (ftype == NULL) {
1287                 xx = RPMERR_EXEC;
1288                 rpmlog(xx, _("magic_file(ms, \"%s\") failed: mode %06o %s\n"),
1289                         s, mode, magic_error(ms));
1290 assert(ftype != NULL);  /* XXX figger a proper return path. */
1291             }
1292         }
1293
1294         se = ftype;
1295         rpmlog(RPMLOG_DEBUG, "%s: %s\n", s, se);
1296
1297         /* Save the path. */
1298         xx = argvAdd(&fc->fn, s);
1299
1300         /* Save the file type string. */
1301         xx = argvAdd(&fcav, se);
1302
1303         /* Add (filtered) entry to sorted class dictionary. */
1304         fcolor = rpmfcColoring(se);
1305         xx = argiAdd(&fc->fcolor, fc->ix, fcolor);
1306
1307         if (fcolor != RPMFC_WHITE && (fcolor & RPMFC_INCLUDE))
1308             xx = rpmfcSaveArg(&fc->cdict, se);
1309     }
1310
1311     /* Build per-file class index array. */
1312     fc->fknown = 0;
1313     for (fc->ix = 0; fc->ix < fc->nfiles; fc->ix++) {
1314         se = fcav[fc->ix];
1315 assert(se != NULL);
1316
1317         dav = argvSearch(fc->cdict, se, NULL);
1318         if (dav) {
1319             xx = argiAdd(&fc->fcdictx, fc->ix, (dav - fc->cdict));
1320             fc->fknown++;
1321         } else {
1322             xx = argiAdd(&fc->fcdictx, fc->ix, 0);
1323             fc->fwhite++;
1324         }
1325     }
1326
1327     fcav = argvFree(fcav);
1328
1329     if (ms != NULL)
1330         magic_close(ms);
1331
1332     return 0;
1333 }
1334
1335 /**
1336  */
1337 typedef struct DepMsg_s * DepMsg_t;
1338
1339 /**
1340  */
1341 struct DepMsg_s {
1342     const char * msg;
1343     const char * argv[4];
1344     rpmTag ntag;
1345     rpmTag vtag;
1346     rpmTag ftag;
1347     int mask;
1348     int xor;
1349 };
1350
1351 /**
1352  */
1353 static struct DepMsg_s depMsgs[] = {
1354   { "Provides",         { "%{?__find_provides}", NULL, NULL, NULL },
1355         RPMTAG_PROVIDENAME, RPMTAG_PROVIDEVERSION, RPMTAG_PROVIDEFLAGS,
1356         0, -1 },
1357 #ifdef  DYING
1358   { "PreReq",           { NULL, NULL, NULL, NULL },
1359         RPMTAG_REQUIRENAME, RPMTAG_REQUIREVERSION, RPMTAG_REQUIREFLAGS,
1360         RPMSENSE_PREREQ, 0 },
1361   { "Requires(interp)", { NULL, "interp", NULL, NULL },
1362         -1, -1, RPMTAG_REQUIREFLAGS,
1363         _notpre(RPMSENSE_INTERP), 0 },
1364 #else
1365   { "Requires(interp)", { NULL, "interp", NULL, NULL },
1366         RPMTAG_REQUIRENAME, RPMTAG_REQUIREVERSION, RPMTAG_REQUIREFLAGS,
1367         _notpre(RPMSENSE_INTERP), 0 },
1368 #endif
1369   { "Requires(rpmlib)", { NULL, "rpmlib", NULL, NULL },
1370         -1, -1, RPMTAG_REQUIREFLAGS,
1371         _notpre(RPMSENSE_RPMLIB), 0 },
1372   { "Requires(verify)", { NULL, "verify", NULL, NULL },
1373         -1, -1, RPMTAG_REQUIREFLAGS,
1374         RPMSENSE_SCRIPT_VERIFY, 0 },
1375   { "Requires(pre)",    { NULL, "pre", NULL, NULL },
1376         -1, -1, RPMTAG_REQUIREFLAGS,
1377         _notpre(RPMSENSE_SCRIPT_PRE), 0 },
1378   { "Requires(post)",   { NULL, "post", NULL, NULL },
1379         -1, -1, RPMTAG_REQUIREFLAGS,
1380         _notpre(RPMSENSE_SCRIPT_POST), 0 },
1381   { "Requires(preun)",  { NULL, "preun", NULL, NULL },
1382         -1, -1, RPMTAG_REQUIREFLAGS,
1383         _notpre(RPMSENSE_SCRIPT_PREUN), 0 },
1384   { "Requires(postun)", { NULL, "postun", NULL, NULL },
1385         -1, -1, RPMTAG_REQUIREFLAGS,
1386         _notpre(RPMSENSE_SCRIPT_POSTUN), 0 },
1387   { "Requires",         { "%{?__find_requires}", NULL, NULL, NULL },
1388         -1, -1, RPMTAG_REQUIREFLAGS,    /* XXX inherit name/version arrays */
1389         RPMSENSE_PREREQ, RPMSENSE_PREREQ },
1390   { "Conflicts",        { "%{?__find_conflicts}", NULL, NULL, NULL },
1391         RPMTAG_CONFLICTNAME, RPMTAG_CONFLICTVERSION, RPMTAG_CONFLICTFLAGS,
1392         0, -1 },
1393   { "Obsoletes",        { "%{?__find_obsoletes}", NULL, NULL, NULL },
1394         RPMTAG_OBSOLETENAME, RPMTAG_OBSOLETEVERSION, RPMTAG_OBSOLETEFLAGS,
1395         0, -1 },
1396   { NULL,               { NULL, NULL, NULL, NULL },     0, 0, 0, 0, 0 }
1397 };
1398
1399 static DepMsg_t DepMsgs = depMsgs;
1400
1401 /**
1402  */
1403 static void printDeps(Header h)
1404 {
1405     DepMsg_t dm;
1406     rpmds ds = NULL;
1407     int flags = 0;      /* XXX !scareMem */
1408     const char * DNEVR;
1409     int_32 Flags;
1410     int bingo = 0;
1411
1412     for (dm = DepMsgs; dm->msg != NULL; dm++) {
1413         if (dm->ntag != -1) {
1414             ds = rpmdsFree(ds);
1415             ds = rpmdsNew(h, dm->ntag, flags);
1416         }
1417         if (dm->ftag == 0)
1418             continue;
1419
1420         ds = rpmdsInit(ds);
1421         if (ds == NULL)
1422             continue;   /* XXX can't happen */
1423
1424         bingo = 0;
1425         while (rpmdsNext(ds) >= 0) {
1426
1427             Flags = rpmdsFlags(ds);
1428         
1429             if (!((Flags & dm->mask) ^ dm->xor))
1430                 continue;
1431             if (bingo == 0) {
1432                 rpmlog(RPMLOG_NOTICE, "%s:", (dm->msg ? dm->msg : ""));
1433                 bingo = 1;
1434             }
1435             if ((DNEVR = rpmdsDNEVR(ds)) == NULL)
1436                 continue;       /* XXX can't happen */
1437             rpmlog(RPMLOG_NOTICE, " %s", DNEVR+2);
1438         }
1439         if (bingo)
1440             rpmlog(RPMLOG_NOTICE, "\n");
1441     }
1442     ds = rpmdsFree(ds);
1443 }
1444
1445 /**
1446  */
1447 static int rpmfcGenerateDependsHelper(const rpmSpec spec, Package pkg, rpmfi fi)
1448 {
1449     StringBuf sb_stdin;
1450     StringBuf sb_stdout;
1451     DepMsg_t dm;
1452     int failnonzero = 0;
1453     int rc = 0;
1454
1455     /*
1456      * Create file manifest buffer to deliver to dependency finder.
1457      */
1458     sb_stdin = newStringBuf();
1459     fi = rpmfiInit(fi, 0);
1460     if (fi != NULL)
1461     while (rpmfiNext(fi) >= 0)
1462         appendLineStringBuf(sb_stdin, rpmfiFN(fi));
1463
1464     for (dm = DepMsgs; dm->msg != NULL; dm++) {
1465         int tag, tagflags;
1466         char * s;
1467         int xx;
1468
1469         tag = (dm->ftag > 0) ? dm->ftag : dm->ntag;
1470         tagflags = 0;
1471         s = NULL;
1472
1473         switch(tag) {
1474         case RPMTAG_PROVIDEFLAGS:
1475             if (!pkg->autoProv)
1476                 continue;
1477             failnonzero = 1;
1478             tagflags = RPMSENSE_FIND_PROVIDES;
1479             break;
1480         case RPMTAG_REQUIREFLAGS:
1481             if (!pkg->autoReq)
1482                 continue;
1483             failnonzero = 0;
1484             tagflags = RPMSENSE_FIND_REQUIRES;
1485             break;
1486         default:
1487             continue;
1488             break;
1489         }
1490
1491         xx = rpmfcExec(dm->argv, sb_stdin, &sb_stdout, failnonzero);
1492         if (xx == -1)
1493             continue;
1494
1495         s = rpmExpand(dm->argv[0], NULL);
1496         rpmlog(RPMLOG_NOTICE, _("Finding  %s: %s\n"), dm->msg,
1497                 (s ? s : ""));
1498         s = _free(s);
1499
1500         if (sb_stdout == NULL) {
1501             rc = RPMERR_EXEC;
1502             rpmlog(rc, _("Failed to find %s:\n"), dm->msg);
1503             break;
1504         }
1505
1506         /* Parse dependencies into header */
1507         rc = parseRCPOT(spec, pkg, getStringBuf(sb_stdout), tag, 0, tagflags);
1508         sb_stdout = freeStringBuf(sb_stdout);
1509
1510         if (rc) {
1511             rpmlog(rc, _("Failed to find %s:\n"), dm->msg);
1512             break;
1513         }
1514     }
1515
1516     sb_stdin = freeStringBuf(sb_stdin);
1517
1518     return rc;
1519 }
1520
1521 int rpmfcGenerateDepends(const rpmSpec spec, Package pkg)
1522 {
1523     rpmfi fi = pkg->cpioList;
1524     rpmfc fc = NULL;
1525     rpmds ds;
1526     int flags = 0;      /* XXX !scareMem */
1527     ARGV_t av;
1528     int_16 * fmode;
1529     int ac = rpmfiFC(fi);
1530     const void ** p;
1531     char buf[BUFSIZ];
1532     const char * N;
1533     const char * EVR;
1534     int genConfigDeps;
1535     int c;
1536     int rc = 0;
1537     int xx;
1538
1539     /* Skip packages with no files. */
1540     if (ac <= 0)
1541         return 0;
1542
1543     /* Skip packages that have dependency generation disabled. */
1544     if (! (pkg->autoReq || pkg->autoProv))
1545         return 0;
1546
1547     /* If new-fangled dependency generation is disabled ... */
1548     if (!rpmExpandNumeric("%{?_use_internal_dependency_generator}")) {
1549         /* ... then generate dependencies using %{__find_requires} et al. */
1550         rc = rpmfcGenerateDependsHelper(spec, pkg, fi);
1551         printDeps(pkg->header);
1552         return rc;
1553     }
1554
1555     /* Extract absolute file paths in argv format. */
1556     av = xcalloc(ac+1, sizeof(*av));
1557     fmode = xcalloc(ac+1, sizeof(*fmode));
1558
1559     genConfigDeps = 0;
1560     fi = rpmfiInit(fi, 0);
1561     if (fi != NULL)
1562     while ((c = rpmfiNext(fi)) >= 0) {
1563         rpmfileAttrs fileAttrs;
1564
1565         /* Does package have any %config files? */
1566         fileAttrs = rpmfiFFlags(fi);
1567         genConfigDeps |= (fileAttrs & RPMFILE_CONFIG);
1568
1569         av[c] = xstrdup(rpmfiFN(fi));
1570         fmode[c] = rpmfiFMode(fi);
1571     }
1572     av[ac] = NULL;
1573
1574     fc = rpmfcNew();
1575     fc->skipProv = !pkg->autoProv;
1576     fc->skipReq = !pkg->autoReq;
1577     fc->tracked = 0;
1578     fc->brlen = (spec->buildRootURL ? strlen(spec->buildRootURL) : 0);
1579
1580     /* Copy (and delete) manually generated dependencies to dictionary. */
1581     if (!fc->skipProv) {
1582         ds = rpmdsNew(pkg->header, RPMTAG_PROVIDENAME, flags);
1583         xx = rpmdsMerge(&fc->provides, ds);
1584         ds = rpmdsFree(ds);
1585         xx = headerRemoveEntry(pkg->header, RPMTAG_PROVIDENAME);
1586         xx = headerRemoveEntry(pkg->header, RPMTAG_PROVIDEVERSION);
1587         xx = headerRemoveEntry(pkg->header, RPMTAG_PROVIDEFLAGS);
1588
1589         /* Add config dependency, Provides: config(N) = EVR */
1590         if (genConfigDeps) {
1591             N = rpmdsN(pkg->ds);
1592 assert(N != NULL);
1593             EVR = rpmdsEVR(pkg->ds);
1594 assert(EVR != NULL);
1595             sprintf(buf, "config(%s)", N);
1596             ds = rpmdsSingle(RPMTAG_PROVIDENAME, buf, EVR,
1597                         (RPMSENSE_EQUAL|RPMSENSE_CONFIG));
1598             xx = rpmdsMerge(&fc->provides, ds);
1599             ds = rpmdsFree(ds);
1600         }
1601     }
1602
1603     if (!fc->skipReq) {
1604         ds = rpmdsNew(pkg->header, RPMTAG_REQUIRENAME, flags);
1605         xx = rpmdsMerge(&fc->requires, ds);
1606         ds = rpmdsFree(ds);
1607         xx = headerRemoveEntry(pkg->header, RPMTAG_REQUIRENAME);
1608         xx = headerRemoveEntry(pkg->header, RPMTAG_REQUIREVERSION);
1609         xx = headerRemoveEntry(pkg->header, RPMTAG_REQUIREFLAGS);
1610
1611         /* Add config dependency,  Requires: config(N) = EVR */
1612         if (genConfigDeps) {
1613             N = rpmdsN(pkg->ds);
1614 assert(N != NULL);
1615             EVR = rpmdsEVR(pkg->ds);
1616 assert(EVR != NULL);
1617             sprintf(buf, "config(%s)", N);
1618             ds = rpmdsSingle(RPMTAG_REQUIRENAME, buf, EVR,
1619                         (RPMSENSE_EQUAL|RPMSENSE_CONFIG));
1620             xx = rpmdsMerge(&fc->requires, ds);
1621             ds = rpmdsFree(ds);
1622         }
1623     }
1624
1625     /* Build file class dictionary. */
1626     xx = rpmfcClassify(fc, av, fmode);
1627
1628     /* Build file/package dependency dictionary. */
1629     xx = rpmfcApply(fc);
1630
1631     /* Add per-file colors(#files) */
1632     p = (const void **) argiData(fc->fcolor);
1633     c = argiCount(fc->fcolor);
1634 assert(ac == c);
1635     if (p != NULL && c > 0) {
1636         int_32 * fcolors = (int_32 *)p;
1637         int i;
1638
1639         /* XXX Make sure only primary (i.e. Elf32/Elf64) colors are added. */
1640         for (i = 0; i < c; i++)
1641             fcolors[i] &= 0x0f;
1642         xx = headerAddEntry(pkg->header, RPMTAG_FILECOLORS, RPM_INT32_TYPE,
1643                         p, c);
1644     }
1645
1646     /* Add classes(#classes) */
1647     p = (const void **) argvData(fc->cdict);
1648     c = argvCount(fc->cdict);
1649     if (p != NULL && c > 0)
1650         xx = headerAddEntry(pkg->header, RPMTAG_CLASSDICT, RPM_STRING_ARRAY_TYPE,
1651                         p, c);
1652
1653     /* Add per-file classes(#files) */
1654     p = (const void **) argiData(fc->fcdictx);
1655     c = argiCount(fc->fcdictx);
1656 assert(ac == c);
1657     if (p != NULL && c > 0)
1658         xx = headerAddEntry(pkg->header, RPMTAG_FILECLASS, RPM_INT32_TYPE,
1659                         p, c);
1660
1661     /* Add Provides: */
1662     if (fc->provides != NULL && (c = rpmdsCount(fc->provides)) > 0 && !fc->skipProv) {
1663         p = (const void **) fc->provides->N;
1664         xx = headerAddEntry(pkg->header, RPMTAG_PROVIDENAME, RPM_STRING_ARRAY_TYPE,
1665                         p, c);
1666         /* XXX rpm prior to 3.0.2 did not always supply EVR and Flags. */
1667         p = (const void **) fc->provides->EVR;
1668 assert(p != NULL);
1669         xx = headerAddEntry(pkg->header, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
1670                         p, c);
1671         p = (const void **) fc->provides->Flags;
1672 assert(p != NULL);
1673         xx = headerAddEntry(pkg->header, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
1674                         p, c);
1675     }
1676
1677     /* Add Requires: */
1678     if (fc->requires != NULL && (c = rpmdsCount(fc->requires)) > 0 && !fc->skipReq) {
1679         p = (const void **) fc->requires->N;
1680         xx = headerAddEntry(pkg->header, RPMTAG_REQUIRENAME, RPM_STRING_ARRAY_TYPE,
1681                         p, c);
1682         /* XXX rpm prior to 3.0.2 did not always supply EVR and Flags. */
1683         p = (const void **) fc->requires->EVR;
1684 assert(p != NULL);
1685         xx = headerAddEntry(pkg->header, RPMTAG_REQUIREVERSION, RPM_STRING_ARRAY_TYPE,
1686                         p, c);
1687         p = (const void **) fc->requires->Flags;
1688 assert(p != NULL);
1689         xx = headerAddEntry(pkg->header, RPMTAG_REQUIREFLAGS, RPM_INT32_TYPE,
1690                         p, c);
1691     }
1692
1693     /* Add dependency dictionary(#dependencies) */
1694     p = (const void **) argiData(fc->ddictx);
1695     c = argiCount(fc->ddictx);
1696     if (p != NULL)
1697         xx = headerAddEntry(pkg->header, RPMTAG_DEPENDSDICT, RPM_INT32_TYPE,
1698                         p, c);
1699
1700     /* Add per-file dependency (start,number) pairs (#files) */
1701     p = (const void **) argiData(fc->fddictx);
1702     c = argiCount(fc->fddictx);
1703 assert(ac == c);
1704     if (p != NULL)
1705         xx = headerAddEntry(pkg->header, RPMTAG_FILEDEPENDSX, RPM_INT32_TYPE,
1706                         p, c);
1707
1708     p = (const void **) argiData(fc->fddictn);
1709     c = argiCount(fc->fddictn);
1710 assert(ac == c);
1711     if (p != NULL)
1712         xx = headerAddEntry(pkg->header, RPMTAG_FILEDEPENDSN, RPM_INT32_TYPE,
1713                         p, c);
1714
1715     printDeps(pkg->header);
1716
1717 if (fc != NULL && _rpmfc_debug) {
1718 char msg[BUFSIZ];
1719 sprintf(msg, "final: files %d cdict[%d] %d%% ddictx[%d]", fc->nfiles, argvCount(fc->cdict), ((100 * fc->fknown)/fc->nfiles), argiCount(fc->ddictx));
1720 rpmfcPrint(msg, fc, NULL);
1721 }
1722
1723     /* Clean up. */
1724     fmode = _free(fmode);
1725     fc = rpmfcFree(fc);
1726     av = argvFree(av);
1727
1728     return rc;
1729 }