07acf4
From d8a169164cf40fc1cf6448792c1fa991f19bb375 Mon Sep 17 00:00:00 2001
07acf4
From: Florian Festi <ffesti@redhat.com>
07acf4
Date: Thu, 22 Apr 2021 14:50:34 +0200
07acf4
Subject: [PATCH] Add --nocompression option to rpm2archive
07acf4
07acf4
Also use popt for the command line handling. As we are using librpm
07acf4
anyway there is no reason to keep the dependencies low (as with
07acf4
rpm2cpio).
07acf4
07acf4
Resolves: #1530
07acf4
---
07acf4
 doc/rpm2archive.8 | 16 ++++++++++---
07acf4
 rpm2archive.c     | 60 ++++++++++++++++++++++++++++++++++-------------
07acf4
 2 files changed, 57 insertions(+), 19 deletions(-)
07acf4
07acf4
diff --git a/rpm2archive.c b/rpm2archive.c
07acf4
index d96db006ea..cb39c7a712 100644
07acf4
--- a/rpm2archive.c
07acf4
+++ b/rpm2archive.c
07acf4
@@ -10,6 +10,8 @@
07acf4
 
07acf4
 #include <rpm/rpmts.h>
07acf4
 
07acf4
+#include <popt.h>
07acf4
+
07acf4
 #include <archive.h>
07acf4
 #include <archive_entry.h>
07acf4
 #include <unistd.h>
07acf4
@@ -18,6 +20,16 @@
07acf4
 
07acf4
 #define BUFSIZE (128*1024)
07acf4
 
07acf4
+int compress = 1;
07acf4
+
07acf4
+static struct poptOption optionsTable[] = {
07acf4
+    { "nocompression", 'n', POPT_ARG_VAL, &compress, 0,
07acf4
+        N_("create uncompressed tar file"),
07acf4
+        NULL },
07acf4
+    POPT_AUTOHELP
07acf4
+    POPT_TABLEEND
07acf4
+};
07acf4
+
07acf4
 static void fill_archive_entry(struct archive * a, struct archive_entry * entry, rpmfi fi)
07acf4
 {
07acf4
     archive_entry_clear(entry);
07acf4
@@ -60,7 +72,7 @@ static void write_file_content(struct archive * a, char * buf, rpmfi fi)
07acf4
     }
07acf4
 }
07acf4
 
07acf4
-static int process_package(rpmts ts, char * filename)
07acf4
+static int process_package(rpmts ts, const char * filename)
07acf4
 {
07acf4
     FD_t fdi;
07acf4
     FD_t gzdi;
07acf4
@@ -119,9 +131,11 @@ static int process_package(rpmts ts, char * filename)
07acf4
 
07acf4
     /* create archive */
07acf4
     a = archive_write_new();
07acf4
-    if (archive_write_add_filter_gzip(a) != ARCHIVE_OK) {
07acf4
-	fprintf(stderr, "Error: Could not create gzip output filter\n");
07acf4
-	exit(EXIT_FAILURE);
07acf4
+    if (compress) {
07acf4
+	if (archive_write_add_filter_gzip(a) != ARCHIVE_OK) {
07acf4
+	    fprintf(stderr, "%s\n", archive_error_string(a));
07acf4
+	    exit(EXIT_FAILURE);
07acf4
+	}
07acf4
     }
07acf4
     if (archive_write_set_format_pax_restricted(a) != ARCHIVE_OK) {
07acf4
 	fprintf(stderr, "Error: Format pax restricted is not supported\n");
07acf4
@@ -142,7 +156,12 @@ static int process_package(rpmts ts, char * filename)
07acf4
 	}
07acf4
 	archive_write_open_fd(a, STDOUT_FILENO);
07acf4
     } else {
07acf4
-	char * outname = rstrscat(NULL, filename, ".tgz", NULL);
07acf4
+	char * outname = rstrscat(NULL, filename, NULL);
07acf4
+	if (compress) {
07acf4
+	    outname = rstrscat(&outname, ".tgz", NULL);
07acf4
+	} else {
07acf4
+	    outname = rstrscat(&outname, ".tar", NULL);
07acf4
+	}
07acf4
 	if (archive_write_open_filename(a, outname) != ARCHIVE_OK) {
07acf4
 	    fprintf(stderr, "Error: Can't open output file: %s\n", outname);
07acf4
 	    exit(EXIT_FAILURE);
07acf4
@@ -203,21 +222,22 @@ static int process_package(rpmts ts, char * filename)
07acf4
     return rc;
07acf4
 }
07acf4
 
07acf4
-int main(int argc, char *argv[])
07acf4
+int main(int argc, const char *argv[])
07acf4
 {
07acf4
-    int rc = 0, i;
07acf4
+    int rc = 0;
07acf4
+    poptContext optCon;
07acf4
+    const char *fn;
07acf4
 
07acf4
     xsetprogname(argv[0]);	/* Portability call -- see system.h */
07acf4
     rpmReadConfigFiles(NULL, NULL);
07acf4
 
07acf4
-    if (argc > 1 && (rstreq(argv[1], "-h") || rstreq(argv[1], "--help"))) {
07acf4
-	fprintf(stderr, "Usage: %s [file.rpm ...]\n", argv[0]);
07acf4
+    optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
07acf4
+    poptSetOtherOptionHelp(optCon, "[OPTIONS]* <FILES>");
07acf4
+    if (argc < 2 || poptGetNextOpt(optCon) == 0) {
07acf4
+	poptPrintUsage(optCon, stderr, 0);
07acf4
 	exit(EXIT_FAILURE);
07acf4
     }
07acf4
 
07acf4
-    if (argc == 1)
07acf4
-	argv[argc++] = "-";	/* abuse NULL pointer at the end of argv */
07acf4
-
07acf4
     rpmts ts = rpmtsCreate();
07acf4
     rpmVSFlags vsflags = 0;
07acf4
 
07acf4
@@ -227,13 +247,21 @@ int main(int argc, char *argv[])
07acf4
     vsflags |= RPMVSF_NOHDRCHK;
07acf4
     (void) rpmtsSetVSFlags(ts, vsflags);
07acf4
 
07acf4
-    for (i = 1; i < argc; i++) {
07acf4
+    /* if no file name is given use stdin/stdout */
07acf4
+    if (!poptPeekArg(optCon)) {
07acf4
+	rc = process_package(ts, "-");
07acf4
+	if (rc != 0)
07acf4
+	    goto exit;
07acf4
+    }
07acf4
 
07acf4
-	rc = process_package(ts, argv[i]);
07acf4
+    while ((fn = poptGetArg(optCon)) != NULL) {
07acf4
+	rc = process_package(ts, fn);
07acf4
 	if (rc != 0)
07acf4
-	    return rc;
07acf4
+	    goto exit;
07acf4
     }
07acf4
 
07acf4
+ exit:
07acf4
+    poptFreeContext(optCon);
07acf4
     (void) rpmtsFree(ts);
07acf4
     return rc;
07acf4
 }