]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
meson: use a single constant for default compression setting 23160/head
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 22 Apr 2022 11:10:07 +0000 (13:10 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 22 Apr 2022 13:08:28 +0000 (15:08 +0200)
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
src/coredump/coredump.c
src/libsystemd/sd-journal/compress.h
src/libsystemd/sd-journal/journal-file.c

index 9720ae590d1d6994fe1c2f288485db33d900096b..596baf9fe87a8edc0c8ad79ce83fdd6c6fc2585d 100644 (file)
@@ -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
index 6055b91acbd7a0cf4a3772365eeeabef43fcae38..003d4f74d16cbe15c61e887e03cb3c3761db1a6f 100644 (file)
@@ -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();
 
index a9f1994d7977aecf562c23e005d6f5be47c1924a..6cc04e88284bb8d5c0b8c9abd5bf36ef0bb238f2 100644 (file)
@@ -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);
index a9d4e7c6ffc859effd1e4a93a8d088588a9c1431..ca3ffc6a03931fe05b1b53fe8108df1f20378309 100644 (file)
@@ -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");