File speedup1.patch of Package rpm

From 721a660a507d6d062e7aecafad886c643970a5d5 Mon Sep 17 00:00:00 2001
From: Alexander Kanavin <alex.kanavin@gmail.com>
Date: Thu, 25 May 2017 18:15:27 +0300
Subject: [PATCH 1/4] Split binary package building into a separate function

So that it can be run as a thread pool task.

Upstream-Status: Submitted [https://github.com/rpm-software-management/rpm/pull/226]
Signed-off-by: Alexander Kanavin <alex.kanavin@gmail.com>

---
 build/pack.c | 33 +++++++++++++++++++++------------
 1 file changed, 21 insertions(+), 12 deletions(-)

Index: rpm-4.14.1/build/pack.c
===================================================================
--- rpm-4.14.1.orig/build/pack.c
+++ rpm-4.14.1/build/pack.c
@@ -228,7 +228,7 @@ static rpmRC processScriptFiles(rpmSpec
 				RPMTAG_FILETRIGGERPRIORITIES,
 				RPMTAG_TRANSFILETRIGGERPRIORITIES};
     int i;
-    
+
     if (addFileToTag(spec, pkg->preInFile, h, RPMTAG_PREIN, 1) ||
 	addFileToTag(spec, pkg->preUnFile, h, RPMTAG_PREUN, 1) ||
 	addFileToTag(spec, pkg->preTransFile, h, RPMTAG_PRETRANS, 1) ||
@@ -316,7 +316,7 @@ static char *getIOFlags(Package pkg)
     /* Save payload information */
     if (headerIsSource(pkg->header))
 	rpmio_flags = rpmExpand("%{?_source_payload}", NULL);
-    else 
+    else
 	rpmio_flags = rpmExpand("%{?_binary_payload}", NULL);
 
     /* If not configured or bogus, fall back to gz */
@@ -509,7 +509,7 @@ static rpmRC writeRPM(Package pkg, unsig
     headerPutUint32(pkg->header, RPMTAG_PAYLOADDIGESTALGO, &pld_algo, 1);
     headerPutString(pkg->header, RPMTAG_PAYLOADDIGEST, pld);
     pld = _free(pld);
-    
+
     /* Check for UTF-8 encoding of string tags, add encoding tag if all good */
     if (checkForEncoding(pkg->header, 1))
 	goto exit;
@@ -626,7 +626,7 @@ static rpmRC checkPackages(char *pkgchec
 {
     int fail = rpmExpandNumeric("%{?_nonzero_exit_pkgcheck_terminate_build}");
     int xx;
-    
+
     rpmlog(RPMLOG_NOTICE, _("Executing \"%s\":\n"), pkgcheck);
     xx = system(pkgcheck);
     if (WEXITSTATUS(xx) == -1 || WEXITSTATUS(xx) == 127) {
@@ -637,7 +637,7 @@ static rpmRC checkPackages(char *pkgchec
 	rpmlog(RPMLOG_ERR, _("Package check \"%s\" failed.\n"), pkgcheck);
 	if (fail) return RPMRC_FAIL;
     }
-    
+
     return RPMRC_OK;
 }
 
@@ -706,30 +706,22 @@ static void trimChangelog(Header h)
     rpmtdFreeData(&timestd);
 }
 
-rpmRC packageBinaries(rpmSpec spec, const char *cookie, int cheating)
+static rpmRC packageBinary(rpmSpec spec, Package pkg, const char *cookie, int cheating, char** filename)
 {
-    rpmRC rc;
-    const char *errorString;
-    Package pkg;
-    char *pkglist = NULL;
-
-    trimChangelog(spec->packages->header);
-    for (pkg = spec->packages; pkg != NULL; pkg = pkg->next) {
-	char *fn;
 
 	if (pkg->fileList == NULL)
-	    continue;
+	    return rc;
 
 	if ((rc = processScriptFiles(spec, pkg)))
 	    return rc;
-	
+
 	if (cookie) {
 	    headerPutString(pkg->header, RPMTAG_COOKIE, cookie);
 	}
 
 	/* Copy changelog from src rpm */
 	headerCopyTags(spec->packages->header, pkg->header, copyTags);
-	
+
 	headerPutString(pkg->header, RPMTAG_RPMVERSION, VERSION);
 	headerPutString(pkg->header, RPMTAG_BUILDHOST, buildHost());
 	headerPutUint32(pkg->header, RPMTAG_BUILDTIME, getBuildTime(), 1);
@@ -741,18 +733,18 @@ rpmRC packageBinaries(rpmSpec spec, cons
 	if (cheating) {
 	    (void) rpmlibNeedsFeature(pkg, "ShortCircuited", "4.9.0-1");
 	}
-	
+
 	{   char *binFormat = rpmGetPath("%{_rpmfilename}", NULL);
 	    char *binRpm, *binDir;
 	    binRpm = headerFormat(pkg->header, binFormat, &errorString);
 	    free(binFormat);
 	    if (binRpm == NULL) {
 		rpmlog(RPMLOG_ERR, _("Could not generate output "
-		     "filename for package %s: %s\n"), 
+		     "filename for package %s: %s\n"),
 		     headerGetString(pkg->header, RPMTAG_NAME), errorString);
 		return RPMRC_FAIL;
 	    }
-	    fn = rpmGetPath("%{_rpmdir}/", binRpm, NULL);
+	    *filename = rpmGetPath("%{_rpmdir}/", binRpm, NULL);
 	    if ((binDir = strchr(binRpm, '/')) != NULL) {
 		struct stat st;
 		char *dn;
@@ -774,14 +766,28 @@ rpmRC packageBinaries(rpmSpec spec, cons
 	    free(binRpm);
 	}
 
-	rc = writeRPM(pkg, NULL, fn, NULL);
+	rc = writeRPM(pkg, NULL, *filename, NULL);
 	if (rc == RPMRC_OK) {
 	    /* Do check each written package if enabled */
-	    char *pkgcheck = rpmExpand("%{?_build_pkgcheck} ", fn, NULL);
+	    char *pkgcheck = rpmExpand("%{?_build_pkgcheck} ", *filename, NULL);
 	    if (pkgcheck[0] != ' ') {
 		rc = checkPackages(pkgcheck);
 	    }
 	    free(pkgcheck);
+	}
+	return rc;
+}
+
+rpmRC packageBinaries(rpmSpec spec, const char *cookie, int cheating)
+{
+    rpmRC rc;
+    Package pkg;
+    char *pkglist = NULL;
+
+    for (pkg = spec->packages; pkg != NULL; pkg = pkg->next) {
+	char *fn = NULL;
+	rc = packageBinary(spec, pkg, cookie, cheating, &fn);
+	if (rc == RPMRC_OK) {
 	    rstrcat(&pkglist, fn);
 	    rstrcat(&pkglist, " ");
 	}
@@ -801,7 +807,7 @@ rpmRC packageBinaries(rpmSpec spec, cons
 	free(pkgcheck_set);
 	pkglist = _free(pkglist);
     }
-    
+
     return RPMRC_OK;
 }