Skip to content

Commit fd447e3

Browse files
committed
zstd filter writer: add threads option
1 parent b9f4955 commit fd447e3

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

libarchive/archive_write_add_filter_zstd.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ __FBSDID("$FreeBSD$");
5050

5151
struct private_data {
5252
int compression_level;
53+
int threads;
5354
#if HAVE_ZSTD_H && HAVE_LIBZSTD
5455
ZSTD_CStream *cstream;
5556
int64_t total_in;
@@ -107,6 +108,7 @@ archive_write_add_filter_zstd(struct archive *_a)
107108
f->code = ARCHIVE_FILTER_ZSTD;
108109
f->name = "zstd";
109110
data->compression_level = CLEVEL_DEFAULT;
111+
data->threads = 0;
110112
#if HAVE_ZSTD_H && HAVE_LIBZSTD
111113
data->cstream = ZSTD_createCStream();
112114
if (data->cstream == NULL) {
@@ -204,6 +206,20 @@ archive_compressor_zstd_options(struct archive_write_filter *f, const char *key,
204206
}
205207
data->compression_level = level;
206208
return (ARCHIVE_OK);
209+
} else if (strcmp(key, "threads") == 0) {
210+
int threads = atoi(value);
211+
if (string_is_numeric(value) != ARCHIVE_OK) {
212+
return (ARCHIVE_WARN);
213+
}
214+
215+
int minimum = 0;
216+
217+
if (threads < minimum) {
218+
return (ARCHIVE_WARN);
219+
}
220+
221+
data->threads = threads;
222+
return (ARCHIVE_OK);
207223
}
208224

209225
/* Note: The "warn" return is just to inform the options
@@ -252,6 +268,8 @@ archive_compressor_zstd_open(struct archive_write_filter *f)
252268
return (ARCHIVE_FATAL);
253269
}
254270

271+
ZSTD_CCtx_setParameter(data->cstream, ZSTD_c_nbWorkers, data->threads);
272+
255273
return (ARCHIVE_OK);
256274
}
257275

@@ -366,6 +384,14 @@ archive_compressor_zstd_open(struct archive_write_filter *f)
366384
archive_strcat(&as, " --ultra");
367385
}
368386

387+
if (data->threads != 0) {
388+
struct archive_string as2;
389+
archive_string_init(&as2);
390+
archive_string_sprintf(&as, " --threads=%d", data->threads);
391+
archive_string_concat(&as, &as2);
392+
archive_string_free(&as2);
393+
}
394+
369395
f->write = archive_compressor_zstd_write;
370396
r = __archive_write_program_open(f, data->pdata, as.s);
371397
archive_string_free(&as);

libarchive/test/test_write_filter_zstd.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ DEFINE_TEST(test_write_filter_zstd)
129129
archive_write_set_filter_option(a, NULL, "compression-level", "-1")); */
130130
assertEqualIntA(a, ARCHIVE_OK,
131131
archive_write_set_filter_option(a, NULL, "compression-level", "7"));
132+
assertEqualIntA(a, ARCHIVE_FAILED,
133+
archive_write_set_filter_option(a, NULL, "threads", "-1")); /* negative */
134+
assertEqualIntA(a, ARCHIVE_OK,
135+
archive_write_set_filter_option(a, NULL, "threads", "4"));
132136
assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, buffsize, &used2));
133137
for (i = 0; i < 100; i++) {
134138
sprintf(path, "file%03d", i);

0 commit comments

Comments
 (0)