]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
zstd: Implement core detection (#2083)
authorDag-Erling Smørgrav <des@des.no>
Mon, 4 Mar 2024 10:37:14 +0000 (11:37 +0100)
committerMartin Matuska <martin@matuska.de>
Tue, 23 Apr 2024 13:11:40 +0000 (15:11 +0200)
The bsdtar manual page claims that setting zstd:threads to 0 tells zstd
to use as many threads as there are cores in the system, but it actually
disables multi-threading.  Replace 0 with the number of configured
processors.

While here, add a previously missing overflow check.

Co-authored-by: Martin Matuska <martin@matuska.de>
CMakeLists.txt
build/cmake/config.h.in
configure.ac
libarchive/archive_write_add_filter_zstd.c
tar/bsdtar.1

index a953bcea1d7ee9039f74d5bb4fddae441a602a6d..822056b9102b943d6b6b4de287d6d9b537122b3c 100644 (file)
@@ -1497,6 +1497,7 @@ CHECK_FUNCTION_EXISTS_GLIBC(strncpy_s HAVE_STRNCPY_S)
 CHECK_FUNCTION_EXISTS_GLIBC(strnlen HAVE_STRNLEN)
 CHECK_FUNCTION_EXISTS_GLIBC(strrchr HAVE_STRRCHR)
 CHECK_FUNCTION_EXISTS_GLIBC(symlink HAVE_SYMLINK)
+CHECK_FUNCTION_EXISTS_GLIBC(sysconf HAVE_SYSCONF)
 CHECK_FUNCTION_EXISTS_GLIBC(timegm HAVE_TIMEGM)
 CHECK_FUNCTION_EXISTS_GLIBC(tzset HAVE_TZSET)
 CHECK_FUNCTION_EXISTS_GLIBC(unlinkat HAVE_UNLINKAT)
index 045a6b41657ed5d24e3159edff641b6533799db2..d47694c0c1f1a3e2c25d205f903b398ae0a065d4 100644 (file)
@@ -1094,6 +1094,9 @@ typedef uint64_t uintmax_t;
 /* Define to 1 if you have the `symlink' function. */
 #cmakedefine HAVE_SYMLINK 1
 
+/* Define to 1 if you have the `sysconf' function. */
+#cmakedefine HAVE_SYSCONF 1
+
 /* Define to 1 if you have the <sys/acl.h> header file. */
 #cmakedefine HAVE_SYS_ACL_H 1
 
index 3cf5f50e781f2278716ce23e5e2410bf7ba50a25..c778c043f0a2cc4ec26aafd80d78b419243ae79b 100644 (file)
@@ -804,6 +804,7 @@ AC_CHECK_FUNCS([nl_langinfo openat pipe poll posix_spawnp readlink readlinkat])
 AC_CHECK_FUNCS([readpassphrase])
 AC_CHECK_FUNCS([select setenv setlocale sigaction statfs statvfs])
 AC_CHECK_FUNCS([strchr strdup strerror strncpy_s strnlen strrchr symlink])
+AC_CHECK_FUNCS([sysconf])
 AC_CHECK_FUNCS([timegm tzset unlinkat unsetenv utime utimensat utimes vfork])
 AC_CHECK_FUNCS([wcrtomb wcscmp wcscpy wcslen wctomb wmemcmp wmemcpy wmemmove])
 AC_CHECK_FUNCS([_fseeki64 _get_timezone])
index df539d326e2a51a8db8afd6bbc03ea0aee2da191..b49f4531027e8a27acd8aa8e4b9e94b9dacb281d 100644 (file)
@@ -29,6 +29,9 @@
 #ifdef HAVE_ERRNO_H
 #include <errno.h>
 #endif
+#ifdef HAVE_LIMITS_H
+#include <limits.h>
+#endif
 #ifdef HAVE_STDINT_H
 #include <stdint.h>
 #endif
@@ -38,6 +41,9 @@
 #ifdef HAVE_STRING_H
 #include <string.h>
 #endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
 #ifdef HAVE_ZSTD_H
 #include <zstd.h>
 #endif
@@ -266,7 +272,13 @@ archive_compressor_zstd_options(struct archive_write_filter *f, const char *key,
                if (string_to_number(value, &threads) != ARCHIVE_OK) {
                        return (ARCHIVE_WARN);
                }
-               if (threads < 0) {
+
+#if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
+               if (threads == 0) {
+                       threads = sysconf(_SC_NPROCESSORS_ONLN);
+               }
+#endif
+               if (threads < 0 || threads > INT_MAX) {
                        return (ARCHIVE_WARN);
                }
                data->threads = (int)threads;
index e570d2a48a017931e58c2b7d6de782177292e503..fe9ec95046743aa88306566941e88c3bf0d3ba7c 100644 (file)
@@ -23,7 +23,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd March 1, 2024
+.Dd April 23, 2024
 .Dt TAR 1
 .Os
 .Sh NAME
@@ -644,14 +644,13 @@ A decimal integer from 4 to 7 specifying the lz4 compression block size
 .It Cm lz4:block-dependence
 Use the previous block of the block being compressed for
 a compression dictionary to improve compression ratio.
-.It Cm zstd:compression-level
-A decimal integer specifying the zstd compression level. Supported values depend
+.It Cm zstd:compression-level Ns = Ns Ar N
+A decimal integer specifying the zstd compression level.
+Supported values depend
 on the library version, common values are from 1 to 22.
-.It Cm zstd:threads
-Specify the number of worker threads to use.
-Setting threads to a special value 0 makes
-.Xr zstd 1
-use as many threads as there are CPU cores on the system.
+.It Cm zstd:threads Ns = Ns Ar N
+Specify the number of worker threads to use, or 0 to use as many
+threads as there are CPU cores in the system.
 .It Cm zstd:frame-per-file
 Start a new compression frame at the beginning of each file in the
 archive.