Don't use stack allocations in rpm2cpio when not needed
[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
36     {   rpmts ts = rpmtsCreate();
37         rpmVSFlags vsflags = 0;
38
39         /* XXX retain the ageless behavior of rpm2cpio */
40         vsflags |= _RPMVSF_NODIGESTS;
41         vsflags |= _RPMVSF_NOSIGNATURES;
42         vsflags |= RPMVSF_NOHDRCHK;
43         (void) rpmtsSetVSFlags(ts, vsflags);
44
45              /* LCL: segfault */
46         rc = rpmReadPackageFile(ts, fdi, "rpm2cpio", &h);
47
48         ts = rpmtsFree(ts);
49     }
50
51     switch (rc) {
52     case RPMRC_OK:
53     case RPMRC_NOKEY:
54     case RPMRC_NOTTRUSTED:
55         break;
56     case RPMRC_NOTFOUND:
57         fprintf(stderr, _("argument is not an RPM package\n"));
58         exit(EXIT_FAILURE);
59         break;
60     case RPMRC_FAIL:
61     default:
62         fprintf(stderr, _("error reading header from package\n"));
63         exit(EXIT_FAILURE);
64         break;
65     }
66
67     /* Retrieve type of payload compression. */
68     {   const char * payload_compressor = NULL;
69
70         if (!headerGetEntry(h, RPMTAG_PAYLOADCOMPRESSOR, NULL,
71                             (rpm_data_t *) &payload_compressor, NULL))
72             payload_compressor = "gzip";
73         if (!strcmp(payload_compressor, "gzip"))
74             rpmio_flags = "r.gzdio";
75         if (!strcmp(payload_compressor, "bzip2"))
76             rpmio_flags = "r.bzdio";
77     }
78
79     gzdi = Fdopen(fdi, rpmio_flags);    /* XXX gzdi == fdi */
80     if (gzdi == NULL) {
81         fprintf(stderr, _("cannot re-open payload: %s\n"), Fstrerror(gzdi));
82         exit(EXIT_FAILURE);
83     }
84
85     rc = ufdCopy(gzdi, fdo);
86     rc = (rc <= 0) ? EXIT_FAILURE : EXIT_SUCCESS;
87     Fclose(fdo);
88
89     Fclose(gzdi);       /* XXX gzdi == fdi */
90
91     return rc;
92 }