compression: add separate pre-processor definitions

Follow-up for cd3c6322db

journal-def.h should be self-contained too, as it represents the journal object ABI.
Duplicate the enums, as they also need to be in config.h for it to be self-contained,
and enums are not available to the preprocessor. Use an assert to ensure they don't
diverge.
This commit is contained in:
Luca Boccassi 2022-04-19 16:23:53 +02:00 committed by Luca Boccassi
parent 3603f15171
commit da13d2ca07
4 changed files with 30 additions and 18 deletions

View file

@ -1470,11 +1470,14 @@ elif compression == 'lz4' and not have_lz4
elif compression == 'xz' and not have_xz
error('default-compression=xz requires xz')
endif
conf.set('OBJECT_COMPRESSED_NONE', 0)
conf.set('OBJECT_COMPRESSED_XZ', 1)
conf.set('OBJECT_COMPRESSED_LZ4', 2)
conf.set('OBJECT_COMPRESSED_ZSTD', 4)
conf.set('DEFAULT_COMPRESSION', 'OBJECT_COMPRESSED_@0@'.format(compression.to_upper()))
# 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()))
want_xkbcommon = get_option('xkbcommon')
if want_xkbcommon != 'false' and not skip_deps

View file

@ -18,18 +18,18 @@ int compress_blob_zstd(const void *src, uint64_t src_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 == OBJECT_COMPRESSED_ZSTD
#if DEFAULT_COMPRESSION == COMPRESSION_ZSTD
r = compress_blob_zstd(src, src_size, dst, dst_alloc_size, dst_size);
if (r == 0)
return OBJECT_COMPRESSED_ZSTD;
#elif DEFAULT_COMPRESSION == OBJECT_COMPRESSED_LZ4
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 OBJECT_COMPRESSED_LZ4;
#elif DEFAULT_COMPRESSION == OBJECT_COMPRESSED_XZ
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 OBJECT_COMPRESSED_XZ;
return COMPRESSION_XZ;
#else
r = -EOPNOTSUPP;
#endif
@ -72,13 +72,13 @@ 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 == OBJECT_COMPRESSED_ZSTD
#if DEFAULT_COMPRESSION == COMPRESSION_ZSTD
# define compress_stream compress_stream_zstd
# define COMPRESSED_EXT ".zst"
#elif DEFAULT_COMPRESSION == OBJECT_COMPRESSED_LZ4
#elif DEFAULT_COMPRESSION == COMPRESSION_LZ4
# define compress_stream compress_stream_lz4
# define COMPRESSED_EXT ".lz4"
#elif DEFAULT_COMPRESSION == OBJECT_COMPRESSED_XZ
#elif DEFAULT_COMPRESSION == COMPRESSION_XZ
# define compress_stream compress_stream_xz
# define COMPRESSED_EXT ".xz"
#else

View file

@ -43,12 +43,21 @@ typedef enum ObjectType {
} ObjectType;
/* Object flags
* The per-compression enums are defined in meson.build so that config.h is self-contained */
* The per-compression enums are also redefined in meson.build so that config.h is self-contained */
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_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;

View file

@ -3359,11 +3359,11 @@ int journal_file_open(
.open_flags = open_flags,
.writable = (open_flags & O_ACCMODE) != O_RDONLY,
#if DEFAULT_COMPRESSION == OBJECT_COMPRESSED_ZSTD
#if DEFAULT_COMPRESSION == COMPRESSION_ZSTD
.compress_zstd = FLAGS_SET(file_flags, JOURNAL_COMPRESS),
#elif DEFAULT_COMPRESSION == OBJECT_COMPRESSED_LZ4
#elif DEFAULT_COMPRESSION == COMPRESSION_LZ4
.compress_lz4 = FLAGS_SET(file_flags, JOURNAL_COMPRESS),
#elif DEFAULT_COMPRESSION == OBJECT_COMPRESSED_XZ
#elif DEFAULT_COMPRESSION == COMPRESSION_XZ
.compress_xz = FLAGS_SET(file_flags, JOURNAL_COMPRESS),
#endif
.compress_threshold_bytes = compress_threshold_bytes == UINT64_MAX ?