- support for separate source/binary compression policy.
[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
7 int main(int argc, char **argv)
8 {
9     FD_t fdi, fdo;
10     Header h;
11     char * rpmio_flags;
12     int rc, isSource;
13     FD_t gzdi;
14     
15     setprogname(argv[0]);       /* Retrofit glibc __progname */
16     if (argc == 1) {
17         fdi = fdDup(STDIN_FILENO);
18     } else {
19         fdi = Fopen(argv[1], "r.ufdio");
20     }
21
22     if (fdi == NULL || Ferror(fdi)) {
23         fprintf(stderr, _("cannot open package: %s\n"), Fstrerror(fdi));
24         exit(EXIT_FAILURE);
25     }
26     fdo = fdDup(STDOUT_FILENO);
27
28     rc = rpmReadPackageHeader(fdi, &h, &isSource, NULL, NULL);
29     switch (rc) {
30     case 0:
31         break;
32     case 1:
33         fprintf(stderr, _("argument is not an RPM package\n"));
34         exit(EXIT_FAILURE);
35         break;
36     default:
37         fprintf(stderr, _("error reading header from package\n"));
38         exit(EXIT_FAILURE);
39         break;
40     }
41
42     /* Retrieve type of payload compression. */
43     {   const char * payload_compressor = NULL;
44         char * t;
45
46         if (!headerGetEntry(h, RPMTAG_PAYLOADCOMPRESSOR, NULL,
47                             (void **) &payload_compressor, NULL))
48             payload_compressor = "gzip";
49         rpmio_flags = t = alloca(sizeof("r.gzdio"));
50         *t++ = 'r';
51         if (!strcmp(payload_compressor, "gzip"))
52             t = stpcpy(t, ".gzdio");
53         if (!strcmp(payload_compressor, "bzip2"))
54             t = stpcpy(t, ".bzdio");
55     }
56
57     gzdi = Fdopen(fdi, rpmio_flags);    /* XXX gzdi == fdi */
58     if (gzdi == NULL || Ferror(gzdi)) {
59         fprintf(stderr, _("cannot re-open payload: %s\n"), Fstrerror(gzdi));
60         exit(EXIT_FAILURE);
61     }
62
63 #ifdef  DYING
64   { char buffer[BUFSIZ];
65     int ct;
66     while ((ct = Fread(buffer, sizeof(buffer[0]), sizeof(buffer), gzdi)) > 0) {
67         Fwrite(buffer, sizeof(buffer[0]), ct, fdo);
68     }
69
70     if (ct < 0) {
71         fprintf (stderr, "rpm2cpio: zlib: %s\n", Fstrerror(gzdi));
72         rc = EXIT_FAILURE;
73     } else {
74         rc = EXIT_SUCCESS;
75     }
76   }
77 #else
78     rc = ufdCopy(gzdi, fdo);
79     rc = (rc <= 0) ? EXIT_FAILURE : EXIT_SUCCESS;
80     Fclose(fdo);
81 #endif
82
83     Fclose(gzdi);       /* XXX gzdi == fdi */
84
85     return rc;
86 }