dcavalca / rpms / rpm

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