From: Zbigniew Jędrzejewski-Szmek Date: Thu, 21 Apr 2022 22:24:01 +0000 (+0200) Subject: meson: simplify setting of default compression X-Git-Tag: v251-rc2~63^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1d997b811411c1572e8771d95eb1597bfbb0f444;p=thirdparty%2Fsystemd.git meson: simplify setting of default compression Follow-up for da13d2ca0731b413841663052f2cc6832a855334. Instead of having separate definitions of the bitmask flags, just define DEFAULT_COMPRESSION_FOO=0|1 directly. (It *should* be possible to do this more simply, but the problem is that anything that is used in #if cannot refer to C constants or enums. This is the simplest I could come up with that preserves the property that we don't use #ifdef.) The return value from compress_blob() is changed to propagate the error instead of always returning -EOPNOTSUPP. The callers don't care about the specific error value. compress_blob_*() are changed to return the compression method on success, so that compress_blob() can be simplified. compress_stream_*() and compress_stream() are changed in the same way for consistency, even though the callers do not currently use this information (outside of tests). --- diff --git a/meson.build b/meson.build index 8c79c78e4ed..9720ae590d1 100644 --- a/meson.build +++ b/meson.build @@ -1470,14 +1470,9 @@ elif compression == 'lz4' and not have_lz4 elif compression == 'xz' and not have_xz error('default-compression=xz requires xz') endif -# These values are defined here so that config.h is self-contained, but they are also -# re-defined (with different names to avoid conflits) in src/libsystemd/sd-journal/journal-def.h -# as that source file represents the journal object ABI, and we want that to be self-contained too. -conf.set('COMPRESSION_NONE', 0) -conf.set('COMPRESSION_XZ', 1) -conf.set('COMPRESSION_LZ4', 2) -conf.set('COMPRESSION_ZSTD', 4) -conf.set('DEFAULT_COMPRESSION', 'COMPRESSION_@0@'.format(compression.to_upper())) +conf.set10('DEFAULT_COMPRESSION_ZSTD', compression == 'zstd') +conf.set10('DEFAULT_COMPRESSION_LZ4', compression == 'lz4') +conf.set10('DEFAULT_COMPRESSION_XZ', compression == 'xz') want_xkbcommon = get_option('xkbcommon') if want_xkbcommon != 'false' and not skip_deps diff --git a/src/libsystemd/sd-journal/compress.c b/src/libsystemd/sd-journal/compress.c index cb2e82667f7..54a50ef0356 100644 --- a/src/libsystemd/sd-journal/compress.c +++ b/src/libsystemd/sd-journal/compress.c @@ -100,7 +100,7 @@ int compress_blob_xz(const void *src, uint64_t src_size, return -ENOBUFS; *dst_size = out_pos; - return 0; + return OBJECT_COMPRESSED_XZ; #else return -EPROTONOSUPPORT; #endif @@ -130,7 +130,7 @@ int compress_blob_lz4(const void *src, uint64_t src_size, unaligned_write_le64(dst, src_size); *dst_size = r + 8; - return 0; + return OBJECT_COMPRESSED_LZ4; #else return -EPROTONOSUPPORT; #endif @@ -153,7 +153,7 @@ int compress_blob_zstd( return zstd_ret_to_errno(k); *dst_size = k; - return 0; + return OBJECT_COMPRESSED_ZSTD; #else return -EPROTONOSUPPORT; #endif @@ -619,7 +619,7 @@ int compress_stream_xz(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_uncom s.total_in, s.total_out, (double) s.total_out / s.total_in * 100); - return 0; + return OBJECT_COMPRESSED_XZ; } } } @@ -710,7 +710,7 @@ int compress_stream_lz4(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_unco total_in, total_out, (double) total_out / total_in * 100); - return 0; + return OBJECT_COMPRESSED_LZ4; #else return -EPROTONOSUPPORT; #endif @@ -954,7 +954,7 @@ int compress_stream_zstd(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_unc log_debug("ZSTD compression finished (%" PRIu64 " -> %" PRIu64 " bytes)", in_bytes, max_bytes - left); - return 0; + return OBJECT_COMPRESSED_ZSTD; #else return -EPROTONOSUPPORT; #endif diff --git a/src/libsystemd/sd-journal/compress.h b/src/libsystemd/sd-journal/compress.h index 4f2701d1fa1..a9f1994d797 100644 --- a/src/libsystemd/sd-journal/compress.h +++ b/src/libsystemd/sd-journal/compress.h @@ -15,27 +15,6 @@ int compress_blob_lz4(const void *src, uint64_t src_size, int compress_blob_zstd(const void *src, uint64_t src_size, void *dst, size_t dst_alloc_size, size_t *dst_size); -static inline int compress_blob(const void *src, uint64_t src_size, - void *dst, size_t dst_alloc_size, size_t *dst_size) { - int r; -#if DEFAULT_COMPRESSION == COMPRESSION_ZSTD - r = compress_blob_zstd(src, src_size, dst, dst_alloc_size, dst_size); - if (r == 0) - return COMPRESSION_ZSTD; -#elif DEFAULT_COMPRESSION == COMPRESSION_LZ4 - r = compress_blob_lz4(src, src_size, dst, dst_alloc_size, dst_size); - if (r == 0) - return COMPRESSION_LZ4; -#elif DEFAULT_COMPRESSION == COMPRESSION_XZ - r = compress_blob_xz(src, src_size, dst, dst_alloc_size, dst_size); - if (r == 0) - return COMPRESSION_XZ; -#else - r = -EOPNOTSUPP; -#endif - return r; -} - int decompress_blob_xz(const void *src, uint64_t src_size, void **dst, size_t* dst_size, size_t dst_max); int decompress_blob_lz4(const void *src, uint64_t src_size, @@ -72,19 +51,38 @@ int decompress_stream_xz(int fdf, int fdt, uint64_t max_size); int decompress_stream_lz4(int fdf, int fdt, uint64_t max_size); int decompress_stream_zstd(int fdf, int fdt, uint64_t max_size); -#if DEFAULT_COMPRESSION == COMPRESSION_ZSTD -# define compress_stream compress_stream_zstd +static inline int compress_blob(const void *src, uint64_t src_size, + void *dst, size_t dst_alloc_size, size_t *dst_size) { +#if DEFAULT_COMPRESSION_ZSTD + return compress_blob_zstd(src, src_size, dst, dst_alloc_size, dst_size); +#elif DEFAULT_COMPRESSION_LZ4 + return compress_blob_lz4(src, src_size, dst, dst_alloc_size, dst_size); +#elif DEFAULT_COMPRESSION_XZ + return compress_blob_xz(src, src_size, dst, dst_alloc_size, dst_size); +#else + return -EOPNOTSUPP; +#endif +} + +static inline int compress_stream(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_uncompressed_size) { +#if DEFAULT_COMPRESSION_ZSTD + return compress_stream_zstd(fdf, fdt, max_bytes, ret_uncompressed_size); +#elif DEFAULT_COMPRESSION_LZ4 + return compress_stream_lz4(fdf, fdt, max_bytes, ret_uncompressed_size); +#elif DEFAULT_COMPRESSION_XZ + return compress_stream_xz(fdf, fdt, max_bytes, ret_uncompressed_size); +#else + return -EOPNOTSUPP; +#endif +} + +#if DEFAULT_COMPRESSION_ZSTD # define COMPRESSED_EXT ".zst" -#elif DEFAULT_COMPRESSION == COMPRESSION_LZ4 -# define compress_stream compress_stream_lz4 +#elif DEFAULT_COMPRESSION_LZ4 # define COMPRESSED_EXT ".lz4" -#elif DEFAULT_COMPRESSION == COMPRESSION_XZ -# define compress_stream compress_stream_xz +#elif DEFAULT_COMPRESSION_XZ # define COMPRESSED_EXT ".xz" #else -static inline int compress_stream(int fdf, int fdt, uint64_t max_size, uint64_t *ret_uncompressed_size) { - return -EOPNOTSUPP; -} # define COMPRESSED_EXT "" #endif diff --git a/src/libsystemd/sd-journal/journal-def.h b/src/libsystemd/sd-journal/journal-def.h index 829fc65cff1..d9a65298cef 100644 --- a/src/libsystemd/sd-journal/journal-def.h +++ b/src/libsystemd/sd-journal/journal-def.h @@ -48,16 +48,10 @@ enum { OBJECT_COMPRESSED_XZ = 1 << 0, OBJECT_COMPRESSED_LZ4 = 1 << 1, OBJECT_COMPRESSED_ZSTD = 1 << 2, - OBJECT_COMPRESSION_MASK = (OBJECT_COMPRESSED_XZ | OBJECT_COMPRESSED_LZ4 | OBJECT_COMPRESSED_ZSTD), + OBJECT_COMPRESSION_MASK = OBJECT_COMPRESSED_XZ | OBJECT_COMPRESSED_LZ4 | OBJECT_COMPRESSED_ZSTD, _OBJECT_COMPRESSED_MAX = OBJECT_COMPRESSION_MASK, }; -#ifdef COMPRESSION_XZ -assert_cc(OBJECT_COMPRESSED_XZ == COMPRESSION_XZ); -assert_cc(OBJECT_COMPRESSED_LZ4 == COMPRESSION_LZ4); -assert_cc(OBJECT_COMPRESSED_ZSTD == COMPRESSION_ZSTD); -#endif - struct ObjectHeader { uint8_t type; uint8_t flags; diff --git a/src/libsystemd/sd-journal/journal-file.c b/src/libsystemd/sd-journal/journal-file.c index 6ee5697bc95..a9d4e7c6ffc 100644 --- a/src/libsystemd/sd-journal/journal-file.c +++ b/src/libsystemd/sd-journal/journal-file.c @@ -3359,11 +3359,11 @@ int journal_file_open( .open_flags = open_flags, .writable = (open_flags & O_ACCMODE) != O_RDONLY, -#if DEFAULT_COMPRESSION == COMPRESSION_ZSTD +#if DEFAULT_COMPRESSION_ZSTD .compress_zstd = FLAGS_SET(file_flags, JOURNAL_COMPRESS), -#elif DEFAULT_COMPRESSION == COMPRESSION_LZ4 +#elif DEFAULT_COMPRESSION_LZ4 .compress_lz4 = FLAGS_SET(file_flags, JOURNAL_COMPRESS), -#elif DEFAULT_COMPRESSION == COMPRESSION_XZ +#elif DEFAULT_COMPRESSION_XZ .compress_xz = FLAGS_SET(file_flags, JOURNAL_COMPRESS), #endif .compress_threshold_bytes = compress_threshold_bytes == UINT64_MAX ? diff --git a/src/libsystemd/sd-journal/test-compress-benchmark.c b/src/libsystemd/sd-journal/test-compress-benchmark.c index f222bf30212..3f8225cbcc1 100644 --- a/src/libsystemd/sd-journal/test-compress-benchmark.c +++ b/src/libsystemd/sd-journal/test-compress-benchmark.c @@ -102,11 +102,11 @@ static void test_compress_decompress(const char* label, const char* type, r = compress(text, size, buf, size, &j); /* assume compression must be successful except for small or random inputs */ - assert_se(r == 0 || (size < 2048 && r == -ENOBUFS) || streq(type, "random")); + assert_se(r > 0 || (size < 2048 && r == -ENOBUFS) || streq(type, "random")); /* check for overwrites */ assert_se(buf[size] == 0); - if (r != 0) { + if (r < 0) { skipped += size; continue; } diff --git a/src/libsystemd/sd-journal/test-compress.c b/src/libsystemd/sd-journal/test-compress.c index 615bbb29346..49812e3ebf5 100644 --- a/src/libsystemd/sd-journal/test-compress.c +++ b/src/libsystemd/sd-journal/test-compress.c @@ -46,6 +46,7 @@ typedef int (decompress_stream_t)(int fdf, int fdt, uint64_t max_size); #if HAVE_COMPRESSION _unused_ static void test_compress_decompress( + int flag, const char *compression, compress_blob_t compress, decompress_blob_t decompress, @@ -66,7 +67,7 @@ _unused_ static void test_compress_decompress( log_info_errno(r, "compression failed: %m"); assert_se(may_fail); } else { - assert_se(r == 0); + assert_se(r == flag); r = decompress(compressed, csize, (void **) &decompressed, &csize, 0); assert_se(r == 0); @@ -118,9 +119,8 @@ _unused_ static void test_decompress_startswith(const char *compression, compressed = compressed2 = malloc(BUFSIZE_2); assert_se(compressed2); r = compress(data, data_len, compressed, BUFSIZE_2, &csize); - assert_se(r == 0); } - assert_se(r == 0); + assert_se(r > 0); len = strlen(data); @@ -145,15 +145,15 @@ _unused_ static void test_decompress_startswith_short(const char *compression, #define TEXT "HUGE=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" char buf[1024]; - size_t i, csize; + size_t csize; int r; log_info("/* %s with %s */", __func__, compression); r = compress(TEXT, sizeof TEXT, buf, sizeof buf, &csize); - assert_se(r == 0); + assert_se(r > 0); - for (i = 1; i < strlen(TEXT); i++) { + for (size_t i = 1; i < strlen(TEXT); i++) { _cleanup_free_ void *buf2 = NULL; assert_se(buf2 = malloc(i)); @@ -163,7 +163,8 @@ _unused_ static void test_decompress_startswith_short(const char *compression, } } -_unused_ static void test_compress_stream(const char *compression, +_unused_ static void test_compress_stream(int flag, + const char *compression, const char *cat, compress_stream_t compress, decompress_stream_t decompress, @@ -194,7 +195,7 @@ _unused_ static void test_compress_stream(const char *compression, assert_se((dst = mkostemp_safe(pattern)) >= 0); - assert_se(compress(src, dst, -1, &uncompressed_size) == 0); + assert_se(compress(src, dst, -1, &uncompressed_size) == flag); if (cat) { assert_se(asprintf(&cmd, "%s %s | diff %s -", cat, pattern, srcfile) > 0); @@ -292,9 +293,11 @@ int main(int argc, char *argv[]) { random_bytes(data + 7, sizeof(data) - 7); #if HAVE_XZ - test_compress_decompress("XZ", compress_blob_xz, decompress_blob_xz, + test_compress_decompress(OBJECT_COMPRESSED_XZ, "XZ", + compress_blob_xz, decompress_blob_xz, text, sizeof(text), false); - test_compress_decompress("XZ", compress_blob_xz, decompress_blob_xz, + test_compress_decompress(OBJECT_COMPRESSED_XZ, "XZ", + compress_blob_xz, decompress_blob_xz, data, sizeof(data), true); test_decompress_startswith("XZ", @@ -307,7 +310,7 @@ int main(int argc, char *argv[]) { compress_blob_xz, decompress_startswith_xz, huge, HUGE_SIZE, true); - test_compress_stream("XZ", "xzcat", + test_compress_stream(OBJECT_COMPRESSED_XZ, "XZ", "xzcat", compress_stream_xz, decompress_stream_xz, srcfile); test_decompress_startswith_short("XZ", compress_blob_xz, decompress_startswith_xz); @@ -317,9 +320,11 @@ int main(int argc, char *argv[]) { #endif #if HAVE_LZ4 - test_compress_decompress("LZ4", compress_blob_lz4, decompress_blob_lz4, + test_compress_decompress(OBJECT_COMPRESSED_LZ4, "LZ4", + compress_blob_lz4, decompress_blob_lz4, text, sizeof(text), false); - test_compress_decompress("LZ4", compress_blob_lz4, decompress_blob_lz4, + test_compress_decompress(OBJECT_COMPRESSED_LZ4, "LZ4", + compress_blob_lz4, decompress_blob_lz4, data, sizeof(data), true); test_decompress_startswith("LZ4", @@ -332,7 +337,7 @@ int main(int argc, char *argv[]) { compress_blob_lz4, decompress_startswith_lz4, huge, HUGE_SIZE, true); - test_compress_stream("LZ4", "lz4cat", + test_compress_stream(OBJECT_COMPRESSED_LZ4, "LZ4", "lz4cat", compress_stream_lz4, decompress_stream_lz4, srcfile); test_lz4_decompress_partial(); @@ -344,9 +349,11 @@ int main(int argc, char *argv[]) { #endif #if HAVE_ZSTD - test_compress_decompress("ZSTD", compress_blob_zstd, decompress_blob_zstd, + test_compress_decompress(OBJECT_COMPRESSED_ZSTD, "ZSTD", + compress_blob_zstd, decompress_blob_zstd, text, sizeof(text), false); - test_compress_decompress("ZSTD", compress_blob_zstd, decompress_blob_zstd, + test_compress_decompress(OBJECT_COMPRESSED_ZSTD, "ZSTD", + compress_blob_zstd, decompress_blob_zstd, data, sizeof(data), true); test_decompress_startswith("ZSTD", @@ -359,7 +366,7 @@ int main(int argc, char *argv[]) { compress_blob_zstd, decompress_startswith_zstd, huge, HUGE_SIZE, true); - test_compress_stream("ZSTD", "zstdcat", + test_compress_stream(OBJECT_COMPRESSED_ZSTD, "ZSTD", "zstdcat", compress_stream_zstd, decompress_stream_zstd, srcfile); test_decompress_startswith_short("ZSTD", compress_blob_zstd, decompress_startswith_zstd);