diff --git a/meson.build b/meson.build index 8c79c78e4ed..596baf9fe87 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.set('DEFAULT_COMPRESSION', + compression == 'none' ? 0 : + 'OBJECT_COMPRESSED_@0@'.format(compression.to_upper())) want_xkbcommon = get_option('xkbcommon') if want_xkbcommon != 'false' and not skip_deps diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index 6055b91acbd..003d4f74d16 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -462,7 +462,7 @@ static int save_external_coredump( if (lseek(fd, 0, SEEK_SET) == (off_t) -1) return log_error_errno(errno, "Failed to seek on coredump %s: %m", fn); - fn_compressed = strjoin(fn, COMPRESSED_EXT); + fn_compressed = strjoin(fn, default_compression_extension()); if (!fn_compressed) return log_oom(); 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..6cc04e88284 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,20 +51,44 @@ 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 -# define COMPRESSED_EXT ".zst" -#elif DEFAULT_COMPRESSION == COMPRESSION_LZ4 -# define compress_stream compress_stream_lz4 -# define COMPRESSED_EXT ".lz4" -#elif DEFAULT_COMPRESSION == COMPRESSION_XZ -# define compress_stream compress_stream_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; +static inline int compress_blob(const void *src, uint64_t src_size, + void *dst, size_t dst_alloc_size, size_t *dst_size) { + switch (DEFAULT_COMPRESSION) { + case OBJECT_COMPRESSED_ZSTD: + return compress_blob_zstd(src, src_size, dst, dst_alloc_size, dst_size); + case OBJECT_COMPRESSED_LZ4: + return compress_blob_lz4(src, src_size, dst, dst_alloc_size, dst_size); + case OBJECT_COMPRESSED_XZ: + return compress_blob_xz(src, src_size, dst, dst_alloc_size, dst_size); + default: + return -EOPNOTSUPP; + } +} + +static inline int compress_stream(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_uncompressed_size) { + switch (DEFAULT_COMPRESSION) { + case OBJECT_COMPRESSED_ZSTD: + return compress_stream_zstd(fdf, fdt, max_bytes, ret_uncompressed_size); + case OBJECT_COMPRESSED_LZ4: + return compress_stream_lz4(fdf, fdt, max_bytes, ret_uncompressed_size); + case OBJECT_COMPRESSED_XZ: + return compress_stream_xz(fdf, fdt, max_bytes, ret_uncompressed_size); + default: + return -EOPNOTSUPP; + } +} + +static inline const char* default_compression_extension(void) { + switch (DEFAULT_COMPRESSION) { + case OBJECT_COMPRESSED_ZSTD: + return ".zst"; + case OBJECT_COMPRESSED_LZ4: + return ".lz4"; + case OBJECT_COMPRESSED_XZ: + return ".xz"; + default: + return ""; + } } -# define COMPRESSED_EXT "" -#endif int decompress_stream(const char *filename, int fdf, int fdt, uint64_t max_bytes); 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..ca3ffc6a039 100644 --- a/src/libsystemd/sd-journal/journal-file.c +++ b/src/libsystemd/sd-journal/journal-file.c @@ -3359,13 +3359,6 @@ int journal_file_open( .open_flags = open_flags, .writable = (open_flags & O_ACCMODE) != O_RDONLY, -#if DEFAULT_COMPRESSION == COMPRESSION_ZSTD - .compress_zstd = FLAGS_SET(file_flags, JOURNAL_COMPRESS), -#elif DEFAULT_COMPRESSION == COMPRESSION_LZ4 - .compress_lz4 = FLAGS_SET(file_flags, JOURNAL_COMPRESS), -#elif DEFAULT_COMPRESSION == COMPRESSION_XZ - .compress_xz = FLAGS_SET(file_flags, JOURNAL_COMPRESS), -#endif .compress_threshold_bytes = compress_threshold_bytes == UINT64_MAX ? DEFAULT_COMPRESS_THRESHOLD : MAX(MIN_COMPRESS_THRESHOLD, compress_threshold_bytes), @@ -3374,6 +3367,13 @@ int journal_file_open( #endif }; + if (DEFAULT_COMPRESSION == OBJECT_COMPRESSED_ZSTD) + f->compress_zstd = FLAGS_SET(file_flags, JOURNAL_COMPRESS); + else if (DEFAULT_COMPRESSION == OBJECT_COMPRESSED_LZ4) + f->compress_lz4 = FLAGS_SET(file_flags, JOURNAL_COMPRESS); + else if (DEFAULT_COMPRESSION == OBJECT_COMPRESSED_XZ) + f->compress_xz = FLAGS_SET(file_flags, JOURNAL_COMPRESS); + /* We turn on keyed hashes by default, but provide an environment variable to turn them off, if * people really want that */ r = getenv_bool("SYSTEMD_JOURNAL_KEYED_HASH"); 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);