Call rpmReadConfigFiles() in rpm2cpio so that rpm2cpio won't complain like:
[platform/upstream/rpm.git] / rpm2cpio.c
1 /* rpmarchive: spit out the main archive portion of a package */
2
3 #include "system.h"
4 const char *__progname;
5
6 #include <rpm/rpmlib.h>         /* rpmReadPackageFile .. */
7 #include <rpm/rpmtag.h>
8 #include <rpm/rpmio.h>
9 #include <rpm/rpmpgp.h>
10
11 #include <rpm/rpmts.h>
12
13 #include "debug.h"
14
15 int main(int argc, char *argv[])
16 {
17     FD_t fdi, fdo;
18     Header h;
19     char * rpmio_flags;
20     rpmRC rc;
21     FD_t gzdi;
22     
23     setprogname(argv[0]);       /* Retrofit glibc __progname */
24     if (argc == 1)
25         fdi = fdDup(STDIN_FILENO);
26     else
27         fdi = Fopen(argv[1], "r.ufdio");
28
29     if (Ferror(fdi)) {
30         fprintf(stderr, "%s: %s: %s\n", argv[0],
31                 (argc == 1 ? "<stdin>" : argv[1]), Fstrerror(fdi));
32         exit(EXIT_FAILURE);
33     }
34     fdo = fdDup(STDOUT_FILENO);
35     rpmReadConfigFiles(NULL, NULL);
36
37     {   rpmts ts = rpmtsCreate();
38         rpmVSFlags vsflags = 0;
39
40         /* XXX retain the ageless behavior of rpm2cpio */
41         vsflags |= _RPMVSF_NODIGESTS;
42         vsflags |= _RPMVSF_NOSIGNATURES;
43         vsflags |= RPMVSF_NOHDRCHK;
44         (void) rpmtsSetVSFlags(ts, vsflags);
45
46              /* LCL: segfault */
47         rc = rpmReadPackageFile(ts, fdi, "rpm2cpio", &h);
48
49         ts = rpmtsFree(ts);
50     }
51
52     switch (rc) {
53     case RPMRC_OK:
54     case RPMRC_NOKEY:
55     case RPMRC_NOTTRUSTED:
56         break;
57     case RPMRC_NOTFOUND:
58         fprintf(stderr, _("argument is not an RPM package\n"));
59         exit(EXIT_FAILURE);
60         break;
61     case RPMRC_FAIL:
62     default:
63         fprintf(stderr, _("error reading header from package\n"));
64         exit(EXIT_FAILURE);
65         break;
66     }
67
68     /* Retrieve type of payload compression. */
69     {   const char * payload_compressor = NULL;
70
71         if (!headerGetEntry(h, RPMTAG_PAYLOADCOMPRESSOR, NULL,
72                             (rpm_data_t *) &payload_compressor, NULL))
73             payload_compressor = "gzip";
74         if (!strcmp(payload_compressor, "gzip"))
75             rpmio_flags = "r.gzdio";
76         if (!strcmp(payload_compressor, "bzip2"))
77             rpmio_flags = "r.bzdio";
78     }
79
80     gzdi = Fdopen(fdi, rpmio_flags);    /* XXX gzdi == fdi */
81     if (gzdi == NULL) {
82         fprintf(stderr, _("cannot re-open payload: %s\n"), Fstrerror(gzdi));
83         exit(EXIT_FAILURE);
84     }
85
86     rc = ufdCopy(gzdi, fdo);
87     rc = (rc <= 0) ? EXIT_FAILURE : EXIT_SUCCESS;
88     Fclose(fdo);
89
90     Fclose(gzdi);       /* XXX gzdi == fdi */
91
92     return rc;
93 }