Add macro %isu_package to generate ISU Package
[platform/upstream/rpm.git] / rpm2cpio.c
index ac88528..8bee3c7 100644 (file)
@@ -1,12 +1,14 @@
 /* rpmarchive: spit out the main archive portion of a package */
 
 #include "system.h"
-const char *__progname;
 
-#include "rpmlib.h"
-#include "rpmpgp.h"
+#include <rpm/rpmlib.h>                /* rpmReadPackageFile .. */
+#include <rpm/rpmtag.h>
+#include <rpm/rpmio.h>
+#include <rpm/rpmpgp.h>
 
-#include <rpmts.h>
+#include <rpm/rpmts.h>
+#include <unistd.h>
 
 #include "debug.h"
 
@@ -14,21 +16,33 @@ int main(int argc, char *argv[])
 {
     FD_t fdi, fdo;
     Header h;
-    char * rpmio_flags;
-    rpmRC rc;
+    char * rpmio_flags = NULL;
+    int rc;
+    off_t payload_size;
     FD_t gzdi;
     
-    setprogname(argv[0]);      /* Retrofit glibc __progname */
+    xsetprogname(argv[0]); /* Portability call -- see system.h */
+
+    rpmReadConfigFiles(NULL, NULL);
     if (argc == 1)
        fdi = fdDup(STDIN_FILENO);
-    else
+    else {
+       if (rstreq(argv[1], "-h") || rstreq(argv[1], "--help")) {
+           fprintf(stderr, "Usage: rpm2cpio file.rpm\n");
+           exit(EXIT_FAILURE);
+       }
        fdi = Fopen(argv[1], "r.ufdio");
+    }
 
     if (Ferror(fdi)) {
        fprintf(stderr, "%s: %s: %s\n", argv[0],
                (argc == 1 ? "<stdin>" : argv[1]), Fstrerror(fdi));
        exit(EXIT_FAILURE);
     }
+    if (isatty(STDOUT_FILENO)) {
+       fprintf(stderr, "Error: refusing to output cpio data to a terminal.\n");
+       exit(EXIT_FAILURE);
+    }
     fdo = fdDup(STDOUT_FILENO);
 
     {  rpmts ts = rpmtsCreate();
@@ -40,7 +54,6 @@ int main(int argc, char *argv[])
         vsflags |= RPMVSF_NOHDRCHK;
        (void) rpmtsSetVSFlags(ts, vsflags);
 
-            /* LCL: segfault */
        rc = rpmReadPackageFile(ts, fdi, "rpm2cpio", &h);
 
        ts = rpmtsFree(ts);
@@ -62,29 +75,22 @@ int main(int argc, char *argv[])
        break;
     }
 
-    /* Retrieve type of payload compression. */
-    {  const char * payload_compressor = NULL;
-       char * t;
-
-       if (!headerGetEntry(h, RPMTAG_PAYLOADCOMPRESSOR, NULL,
-                           (void **) &payload_compressor, NULL))
-           payload_compressor = "gzip";
-       rpmio_flags = t = alloca(sizeof("r.gzdio"));
-       *t++ = 'r';
-       if (!strcmp(payload_compressor, "gzip"))
-           t = stpcpy(t, ".gzdio");
-       if (!strcmp(payload_compressor, "bzip2"))
-           t = stpcpy(t, ".bzdio");
+    /* Retrieve payload size and compression type. */
+    {  const char *compr = headerGetString(h, RPMTAG_PAYLOADCOMPRESSOR);
+       rpmio_flags = rstrscat(NULL, "r.", compr ? compr : "gzip", NULL);
+       payload_size = headerGetNumber(h, RPMTAG_LONGARCHIVESIZE);
     }
 
     gzdi = Fdopen(fdi, rpmio_flags);   /* XXX gzdi == fdi */
+    free(rpmio_flags);
+
     if (gzdi == NULL) {
        fprintf(stderr, _("cannot re-open payload: %s\n"), Fstrerror(gzdi));
        exit(EXIT_FAILURE);
     }
 
-    rc = ufdCopy(gzdi, fdo);
-    rc = (rc <= 0) ? EXIT_FAILURE : EXIT_SUCCESS;
+    rc = (ufdCopy(gzdi, fdo) == payload_size) ? EXIT_SUCCESS : EXIT_FAILURE;
+
     Fclose(fdo);
 
     Fclose(gzdi);      /* XXX gzdi == fdi */