From 98565f9ed227f5d7d8741c5b16d434e72685f0a1 Mon Sep 17 00:00:00 2001
From: Florian Festi <ffesti@redhat.com>
Date: Mon, 18 Jan 2021 15:04:47 +0100
Subject: [PATCH] rpm2archive: Use last part of URL as file name

when getting a file from an URL. This prevents trying to write the file
at the location of the full URL which fails most of the time.

Related: #1091
---
 rpm2archive.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/rpm2archive.c b/rpm2archive.c
index 15c5da016..d96db006e 100644
--- a/rpm2archive.c
+++ b/rpm2archive.c
@@ -7,7 +7,7 @@
 #include <rpm/rpmtag.h>
 #include <rpm/rpmio.h>
 #include <rpm/rpmpgp.h>
-
+#include <rpm/rpmurl.h>
 #include <rpm/rpmts.h>
 
 #include <archive.h>
@@ -134,7 +134,18 @@ static int process_package(rpmts ts, char * filename)
 	}
 	archive_write_open_fd(a, STDOUT_FILENO);
     } else {
-	char * outname = rstrscat(NULL, filename, ".tgz", NULL);
+	char * outname;
+	if (urlIsURL(filename)) {
+	    const char * fname = strrchr(filename, '/');
+	    if (fname != NULL) {
+		fname++;
+	    } else {
+		fname = filename;
+	    }
+	    outname = rstrscat(NULL, fname, ".tgz", NULL);
+	} else {
+	    outname = rstrscat(NULL, filename, ".tgz", NULL);
+	}
 	if (archive_write_open_filename(a, outname) != ARCHIVE_OK) {
 	    fprintf(stderr, "Error: Can't open output file: %s\n", outname);
 	    exit(EXIT_FAILURE);
-- 
2.27.0

