From ee00684c50a47fd1b56edc3ea06da42e3848a2b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 22 Apr 2022 13:10:07 +0200 Subject: [PATCH] meson: use a single constant for default compression setting Suggested by Daniele Nicolodi: https://github.com/systemd/systemd/pull/23160#discussion_r855853716 This is possible only if the macro is never used in #if, but only in C code. This means that all places that use #if have to be refactored into C, but we reduce the duplication a bit, and C is nicer to read than preprocessor conditionals. --- meson.build | 6 +-- src/coredump/coredump.c | 2 +- src/libsystemd/sd-journal/compress.h | 59 +++++++++++++----------- src/libsystemd/sd-journal/journal-file.c | 14 +++--- 4 files changed, 43 insertions(+), 38 deletions(-) diff --git a/meson.build b/meson.build index 9720ae590d1..596baf9fe87 100644 --- a/meson.build +++ b/meson.build @@ -1470,9 +1470,9 @@ elif compression == 'lz4' and not have_lz4 elif compression == 'xz' and not have_xz error('default-compression=xz requires xz') endif -conf.set10('DEFAULT_COMPRESSION_ZSTD', compression == 'zstd') -conf.set10('DEFAULT_COMPRESSION_LZ4', compression == 'lz4') -conf.set10('DEFAULT_COMPRESSION_XZ', compression == 'xz') +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.h b/src/libsystemd/sd-journal/compress.h index a9f1994d797..6cc04e88284 100644 --- a/src/libsystemd/sd-journal/compress.h +++ b/src/libsystemd/sd-journal/compress.h @@ -53,37 +53,42 @@ int decompress_stream_zstd(int fdf, int fdt, uint64_t max_size); 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 + 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) { -#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 + 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; + } } -#if DEFAULT_COMPRESSION_ZSTD -# define COMPRESSED_EXT ".zst" -#elif DEFAULT_COMPRESSION_LZ4 -# define COMPRESSED_EXT ".lz4" -#elif DEFAULT_COMPRESSION_XZ -# define COMPRESSED_EXT ".xz" -#else -# define COMPRESSED_EXT "" -#endif +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 ""; + } +} int decompress_stream(const char *filename, int fdf, int fdt, uint64_t max_bytes); diff --git a/src/libsystemd/sd-journal/journal-file.c b/src/libsystemd/sd-journal/journal-file.c index a9d4e7c6ffc..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_ZSTD - .compress_zstd = FLAGS_SET(file_flags, JOURNAL_COMPRESS), -#elif DEFAULT_COMPRESSION_LZ4 - .compress_lz4 = FLAGS_SET(file_flags, JOURNAL_COMPRESS), -#elif DEFAULT_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");