[4.0] Use strip (instead of eu-strip) to support --strip-debug of *.so at build time
[platform/upstream/rpm.git] / rpm2cpio.c
index 2b70caa..89ebdfa 100644 (file)
@@ -1,64 +1,94 @@
 /* rpmarchive: spit out the main archive portion of a package */
 
-#include <stdio.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <zlib.h>
-
-#include "rpmlib.h"
-
-char *zlib_err [] = {
-   "No",
-   "Unix",
-   "Data",
-   "Memory",
-   "Buffer",
-   "Version"
-};
-
-int main(int argc, char **argv)
+#include "system.h"
+const char *__progname;
+
+#include <rpm/rpmlib.h>                /* rpmReadPackageFile .. */
+#include <rpm/rpmtag.h>
+#include <rpm/rpmio.h>
+#include <rpm/rpmpgp.h>
+
+#include <rpm/rpmts.h>
+
+#include "debug.h"
+
+int main(int argc, char *argv[])
 {
-    int fd;
-    Header hd;
-    int rc, isSource;
-    char buffer[1024];
-    int ct;
-    gzFile stream;
+    FD_t fdi, fdo;
+    Header h;
+    char * rpmio_flags = NULL;
+    int rc;
+    off_t payload_size;
+    FD_t gzdi;
     
-    if (argc == 1) {
-       fd = 0;
-    } else {
-       fd = open(argv[1], O_RDONLY, 0644);
+    setprogname(argv[0]);      /* Retrofit glibc __progname */
+    rpmReadConfigFiles(NULL, NULL);
+    if (argc == 1)
+       fdi = fdDup(STDIN_FILENO);
+    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 (fd < 0) {
-       perror("cannot open package");
-       exit(1);
+    if (Ferror(fdi)) {
+       fprintf(stderr, "%s: %s: %s\n", argv[0],
+               (argc == 1 ? "<stdin>" : argv[1]), Fstrerror(fdi));
+       exit(EXIT_FAILURE);
     }
+    fdo = fdDup(STDOUT_FILENO);
+
+    {  rpmts ts = rpmtsCreate();
+       rpmVSFlags vsflags = 0;
+
+       /* XXX retain the ageless behavior of rpm2cpio */
+        vsflags |= _RPMVSF_NODIGESTS;
+        vsflags |= _RPMVSF_NOSIGNATURES;
+        vsflags |= RPMVSF_NOHDRCHK;
+       (void) rpmtsSetVSFlags(ts, vsflags);
+
+       rc = rpmReadPackageFile(ts, fdi, "rpm2cpio", &h);
 
-    rc = pkgReadHeader(fd, &hd, &isSource, NULL, NULL);
-    if (rc == 1) {
-       fprintf(stderr, "argument is not an RPM package\n");
-       exit(1);
-    } else if (rc) {
-       fprintf(stderr, "error reading header from package\n");
-       exit(1);
+       ts = rpmtsFree(ts);
     }
 
-    stream = gzdopen(fd, "r");
+    switch (rc) {
+    case RPMRC_OK:
+    case RPMRC_NOKEY:
+    case RPMRC_NOTTRUSTED:
+       break;
+    case RPMRC_NOTFOUND:
+       fprintf(stderr, _("argument is not an RPM package\n"));
+       exit(EXIT_FAILURE);
+       break;
+    case RPMRC_FAIL:
+    default:
+       fprintf(stderr, _("error reading header from package\n"));
+       exit(EXIT_FAILURE);
+       break;
+    }
 
-    while ((ct = gzread(stream, &buffer, 1024)) > 0) {
-       write(1, &buffer, ct);
+    /* 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);
     }
-    if (ct < 0){
-        int zerror;
-       
-        gzerror (stream, &zerror);
-        if (zerror == Z_ERRNO){
-           perror ("While uncompressing");
-           return 1;
-       }
-        fprintf (stderr, "rpm2cpio: zlib: %s error\n", zlib_err [-zerror]);
+
+    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);
     }
-    return 0;
+
+    rc = (ufdCopy(gzdi, fdo) == payload_size) ? EXIT_SUCCESS : EXIT_FAILURE;
+
+    Fclose(fdo);
+
+    Fclose(gzdi);      /* XXX gzdi == fdi */
+
+    return rc;
 }