dcavalca / rpms / rpm

Forked from rpms/rpm 2 years ago
Clone
5523e9
diff --git a/rpmio/rpmio.c b/rpmio/rpmio.c
5523e9
index cd223e8..f23fc11 100644
5523e9
--- a/rpmio/rpmio.c
5523e9
+++ b/rpmio/rpmio.c
5523e9
@@ -5,6 +5,7 @@
5523e9
 #include "system.h"
5523e9
 #include <stdarg.h>
5523e9
 #include <errno.h>
5523e9
+#include <ctype.h>
5523e9
 
5523e9
 #include <rpm/rpmlog.h>
5523e9
 #include <rpm/rpmmacro.h>
5523e9
@@ -873,7 +874,12 @@ static const char * getFdErrstr (FD_t fd)
5523e9
 
5523e9
 #include <sys/types.h>
5523e9
 #include <inttypes.h>
5523e9
+#define LZMA_UNSTABLE
5523e9
 #include <lzma.h>
5523e9
+/* Multithreading support in stable API since xz 5.2.0 */
5523e9
+#if LZMA_VERSION >= 50010020
5523e9
+#define HAVE_LZMA_MT
5523e9
+#endif
5523e9
 
5523e9
 #define kBufferSize (1 << 15)
5523e9
 
5523e9
@@ -897,7 +902,10 @@ static LZFILE *lzopen_internal(const char *path, const char *mode, int fd, int x
5523e9
     LZFILE *lzfile;
5523e9
     lzma_ret ret;
5523e9
     lzma_stream init_strm = LZMA_STREAM_INIT;
5523e9
-
5523e9
+    uint64_t mem_limit = rpmExpandNumeric("%{_xz_memlimit}");
5523e9
+#ifdef HAVE_LZMA_MT
5523e9
+    int threads = 0;
5523e9
+#endif
5523e9
     for (; *mode; mode++) {
5523e9
 	if (*mode == 'w')
5523e9
 	    encoding = 1;
5523e9
@@ -905,6 +913,21 @@ static LZFILE *lzopen_internal(const char *path, const char *mode, int fd, int x
5523e9
 	    encoding = 0;
5523e9
 	else if (*mode >= '1' && *mode <= '9')
5523e9
 	    level = *mode - '0';
5523e9
+	else if (*mode == 'T') {
5523e9
+	    if (isdigit(*(mode+1))) {
5523e9
+#ifdef HAVE_LZMA_MT
5523e9
+		threads = atoi(++mode);
5523e9
+#endif
5523e9
+		/* skip past rest of digits in string that atoi()
5523e9
+		 * should've processed
5523e9
+		 * */
5523e9
+		while(isdigit(*++mode));
5523e9
+	    }
5523e9
+#ifdef HAVE_LZMA_MT
5523e9
+	    else
5523e9
+		threads = -1;
5523e9
+#endif
5523e9
+	}
5523e9
     }
5523e9
     if (fd != -1)
5523e9
 	fp = fdopen(fd, encoding ? "w" : "r");
5523e9
@@ -924,16 +947,48 @@ static LZFILE *lzopen_internal(const char *path, const char *mode, int fd, int x
5523e9
     lzfile->strm = init_strm;
5523e9
     if (encoding) {
5523e9
 	if (xz) {
5523e9
-	    ret = lzma_easy_encoder(&lzfile->strm, level, LZMA_CHECK_SHA256);
5523e9
+#ifdef HAVE_LZMA_MT
5523e9
+	    if (!threads) {
5523e9
+#endif
5523e9
+		ret = lzma_easy_encoder(&lzfile->strm, level, LZMA_CHECK_SHA256);
5523e9
+#ifdef HAVE_LZMA_MT
5523e9
+	    } else {
5523e9
+		if (threads == -1)
5523e9
+		    threads = sysconf(_SC_NPROCESSORS_ONLN);
5523e9
+		lzma_mt mt_options = {
5523e9
+		    .flags = 0,
5523e9
+		    .threads = threads,
5523e9
+		    .block_size = 0,
5523e9
+		    .timeout = 0,
5523e9
+		    .preset = level,
5523e9
+		    .filters = NULL,
5523e9
+		    .check = LZMA_CHECK_SHA256 };
5523e9
+
5523e9
+		ret = lzma_stream_encoder_mt(&lzfile->strm, &mt_options);
5523e9
+	    }
5523e9
+#endif
5523e9
 	} else {
5523e9
 	    lzma_options_lzma options;
5523e9
 	    lzma_lzma_preset(&options, level);
5523e9
 	    ret = lzma_alone_encoder(&lzfile->strm, &options);
5523e9
 	}
5523e9
-    } else {	/* lzma_easy_decoder_memusage(level) is not ready yet, use hardcoded limit for now */
5523e9
-	ret = lzma_auto_decoder(&lzfile->strm, 100<<20, 0);
5523e9
+    } else {   /* lzma_easy_decoder_memusage(level) is not ready yet, use hardcoded limit for now */
5523e9
+	ret = lzma_auto_decoder(&lzfile->strm, mem_limit ? mem_limit : 100<<20, 0);
5523e9
     }
5523e9
     if (ret != LZMA_OK) {
5523e9
+	switch (ret) {
5523e9
+	    case LZMA_MEM_ERROR:
5523e9
+		rpmlog(RPMLOG_ERR, "liblzma: Memory allocation failed");
5523e9
+		break;
5523e9
+
5523e9
+	    case LZMA_DATA_ERROR:
5523e9
+		rpmlog(RPMLOG_ERR, "liblzma: File size limits exceeded");
5523e9
+		break;
5523e9
+
5523e9
+	    default:
5523e9
+		rpmlog(RPMLOG_ERR, "liblzma: 
5523e9
+		break;
5523e9
+	}
5523e9
 	fclose(fp);
5523e9
 	free(lzfile);
5523e9
 	return 0;