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