rpm  4.9.1.3
rpm2cpio.c
Go to the documentation of this file.
00001 /* rpmarchive: spit out the main archive portion of a package */
00002 
00003 #include "system.h"
00004 const char *__progname;
00005 
00006 #include <rpm/rpmlib.h>         /* rpmReadPackageFile .. */
00007 #include <rpm/rpmtag.h>
00008 #include <rpm/rpmio.h>
00009 #include <rpm/rpmpgp.h>
00010 
00011 #include <rpm/rpmts.h>
00012 
00013 #include "debug.h"
00014 
00015 int main(int argc, char *argv[])
00016 {
00017     FD_t fdi, fdo;
00018     Header h;
00019     char * rpmio_flags = NULL;
00020     int rc;
00021     FD_t gzdi;
00022     
00023     setprogname(argv[0]);       /* Retrofit glibc __progname */
00024     rpmReadConfigFiles(NULL, NULL);
00025     if (argc == 1)
00026         fdi = fdDup(STDIN_FILENO);
00027     else {
00028         if (rstreq(argv[1], "-h") || rstreq(argv[1], "--help")) {
00029             fprintf(stderr, "Usage: rpm2cpio file.rpm\n");
00030             exit(EXIT_FAILURE);
00031         }
00032         fdi = Fopen(argv[1], "r.ufdio");
00033     }
00034 
00035     if (Ferror(fdi)) {
00036         fprintf(stderr, "%s: %s: %s\n", argv[0],
00037                 (argc == 1 ? "<stdin>" : argv[1]), Fstrerror(fdi));
00038         exit(EXIT_FAILURE);
00039     }
00040     fdo = fdDup(STDOUT_FILENO);
00041 
00042     {   rpmts ts = rpmtsCreate();
00043         rpmVSFlags vsflags = 0;
00044 
00045         /* XXX retain the ageless behavior of rpm2cpio */
00046         vsflags |= _RPMVSF_NODIGESTS;
00047         vsflags |= _RPMVSF_NOSIGNATURES;
00048         vsflags |= RPMVSF_NOHDRCHK;
00049         (void) rpmtsSetVSFlags(ts, vsflags);
00050 
00051         rc = rpmReadPackageFile(ts, fdi, "rpm2cpio", &h);
00052 
00053         ts = rpmtsFree(ts);
00054     }
00055 
00056     switch (rc) {
00057     case RPMRC_OK:
00058     case RPMRC_NOKEY:
00059     case RPMRC_NOTTRUSTED:
00060         break;
00061     case RPMRC_NOTFOUND:
00062         fprintf(stderr, _("argument is not an RPM package\n"));
00063         exit(EXIT_FAILURE);
00064         break;
00065     case RPMRC_FAIL:
00066     default:
00067         fprintf(stderr, _("error reading header from package\n"));
00068         exit(EXIT_FAILURE);
00069         break;
00070     }
00071 
00072     /* Retrieve type of payload compression. */
00073     {   const char *compr = headerGetString(h, RPMTAG_PAYLOADCOMPRESSOR);
00074         rpmio_flags = rstrscat(NULL, "r.", compr ? compr : "gzip", NULL);
00075     }
00076 
00077     gzdi = Fdopen(fdi, rpmio_flags);    /* XXX gzdi == fdi */
00078     free(rpmio_flags);
00079 
00080     if (gzdi == NULL) {
00081         fprintf(stderr, _("cannot re-open payload: %s\n"), Fstrerror(gzdi));
00082         exit(EXIT_FAILURE);
00083     }
00084 
00085     rc = ufdCopy(gzdi, fdo);
00086     rc = (rc <= 0) ? EXIT_FAILURE : EXIT_SUCCESS;
00087     Fclose(fdo);
00088 
00089     Fclose(gzdi);       /* XXX gzdi == fdi */
00090 
00091     return rc;
00092 }