meson: simplify setting of default compression

Follow-up for da13d2ca07. 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).
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2022-04-22 00:24:01 +02:00
parent 6ae16e01b5
commit 1d997b8114
7 changed files with 67 additions and 73 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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;

View file

@ -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 ?

View file

@ -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;
}

View file

@ -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);