compress: consolidate all compression into compress.c with dlopen
Move the push-based streaming compression API from import-compress.c into compress.c and delete import-compress.c/h. This consolidates all compression code in one place and makes all compression libraries (liblzma, liblz4, libzstd, libz, libbz2) runtime-loaded via dlopen instead of directly linked. Introduce opaque Compressor/Decompressor types backed by a heap- allocated struct defined only in compress.c, keeping all third-party library headers out of compress.h. Rewrite the per-codec fd-to-fd stream functions as thin wrappers around the push API via generic compress_stream()/decompress_stream() taking a Compression type parameter. Integrate LZ4 into this framework using the LZ4 Frame API, eliminating all LZ4 special-casing. Extend the Compression enum with COMPRESSION_GZIP and COMPRESSION_BZIP2 and add the corresponding blob, startswith, and stream functions for both. Rename the ImportCompress types and functions: ImportCompressType becomes the existing Compression enum, ImportCompress becomes Compressor (with Decompressor typedef), and all import_compress_*/import_uncompress_* become compressor_*/decompressor_*. Rename dlopen_lzma() to dlopen_xz() for consistency. Make compression_to_string() return lowercase by default. Add INT_MAX/UINT_MAX overflow checks for LZ4, zlib, and bzip2 blob functions where the codec API uses narrower integer types than our uint64_t parameters. Migrate test-compress.c and test-compress-benchmark.c to the TEST() macro framework, new assertion macros, and codec-generic loops instead of per-codec duplication. Co-developed-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
committed by
Daan De Meyer
parent
44d0f273fa
commit
f08796065d
@@ -1373,6 +1373,7 @@ conf.set10('HAVE_DWFL_SET_SYSROOT',
|
||||
libz = dependency('zlib',
|
||||
required : get_option('zlib'))
|
||||
conf.set10('HAVE_ZLIB', libz.found())
|
||||
libz_cflags = libz.partial_dependency(includes: true, compile_args: true)
|
||||
|
||||
feature = get_option('bzip2')
|
||||
libbzip2 = dependency('bzip2',
|
||||
@@ -1382,6 +1383,7 @@ if not libbzip2.found()
|
||||
libbzip2 = cc.find_library('bz2', required : feature)
|
||||
endif
|
||||
conf.set10('HAVE_BZIP2', libbzip2.found())
|
||||
libbzip2_cflags = libbzip2.partial_dependency(includes: true, compile_args: true)
|
||||
|
||||
libxz = dependency('liblzma',
|
||||
required : get_option('xz'))
|
||||
|
||||
+1796
-656
File diff suppressed because it is too large
Load Diff
+53
-75
@@ -8,103 +8,81 @@ typedef enum Compression {
|
||||
COMPRESSION_XZ,
|
||||
COMPRESSION_LZ4,
|
||||
COMPRESSION_ZSTD,
|
||||
COMPRESSION_GZIP,
|
||||
COMPRESSION_BZIP2,
|
||||
_COMPRESSION_MAX,
|
||||
_COMPRESSION_INVALID = -EINVAL,
|
||||
} Compression;
|
||||
|
||||
DECLARE_STRING_TABLE_LOOKUP(compression, Compression);
|
||||
DECLARE_STRING_TABLE_LOOKUP(compression_lowercase, Compression);
|
||||
DECLARE_STRING_TABLE_LOOKUP(compression_uppercase, Compression);
|
||||
DECLARE_STRING_TABLE_LOOKUP(compression_extension, Compression);
|
||||
|
||||
/* Try the lowercase string table first, fall back to the uppercase one. Useful for parsing user input
|
||||
* where both forms (e.g. "xz" and "XZ") have historically been accepted. */
|
||||
Compression compression_from_string_harder(const char *s);
|
||||
|
||||
/* Derives the compression type from a filename's extension, defaulting to COMPRESSION_NONE if the
|
||||
* filename does not carry a recognized compression suffix. */
|
||||
Compression compression_from_filename(const char *filename);
|
||||
|
||||
bool compression_supported(Compression c);
|
||||
|
||||
int compress_blob_xz(const void *src, uint64_t src_size,
|
||||
void *dst, size_t dst_alloc_size, size_t *dst_size, int level);
|
||||
int compress_blob_lz4(const void *src, uint64_t src_size,
|
||||
void *dst, size_t dst_alloc_size, size_t *dst_size, int level);
|
||||
int compress_blob_zstd(const void *src, uint64_t src_size,
|
||||
void *dst, size_t dst_alloc_size, size_t *dst_size, int level);
|
||||
/* Buffer size used by streaming compression APIs and pipeline stages that feed into them. Sized to
|
||||
* match the typical Linux pipe buffer so that pipeline stages don't lose throughput due to small
|
||||
* intermediate buffers. */
|
||||
#define COMPRESS_PIPE_BUFFER_SIZE (128U*1024U)
|
||||
|
||||
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,
|
||||
void **dst, size_t* dst_size, size_t dst_max);
|
||||
int decompress_blob_zstd(const void *src, uint64_t src_size,
|
||||
void **dst, size_t* dst_size, size_t dst_max);
|
||||
/* Compressor / Decompressor — opaque push-based streaming compression context */
|
||||
|
||||
typedef struct Compressor Compressor;
|
||||
typedef Compressor Decompressor;
|
||||
|
||||
typedef int (*DecompressorCallback)(const void *data, size_t size, void *userdata);
|
||||
|
||||
Compressor* compressor_free(Compressor *c);
|
||||
DEFINE_TRIVIAL_CLEANUP_FUNC(Compressor*, compressor_free);
|
||||
|
||||
int compressor_new(Compressor **ret, Compression type);
|
||||
int compressor_start(Compressor *c, const void *data, size_t size, void **buffer, size_t *buffer_size, size_t *buffer_allocated);
|
||||
int compressor_finish(Compressor *c, void **buffer, size_t *buffer_size, size_t *buffer_allocated);
|
||||
|
||||
int decompressor_detect(Decompressor **ret, const void *data, size_t size);
|
||||
int decompressor_force_off(Decompressor **ret);
|
||||
int decompressor_push(Decompressor *c, const void *data, size_t size, DecompressorCallback callback, void *userdata);
|
||||
|
||||
Compression compressor_type(const Compressor *c);
|
||||
|
||||
/* Blob compression/decompression */
|
||||
|
||||
int compress_blob(Compression compression,
|
||||
const void *src, uint64_t src_size,
|
||||
void *dst, size_t dst_alloc_size, size_t *dst_size, int level);
|
||||
int decompress_blob(Compression compression,
|
||||
const void *src, uint64_t src_size,
|
||||
void **dst, size_t* dst_size, size_t dst_max);
|
||||
void **dst, size_t *dst_size, size_t dst_max);
|
||||
|
||||
int decompress_zlib_raw(const void *src, uint64_t src_size,
|
||||
void *dst, size_t dst_size, int wbits);
|
||||
|
||||
int decompress_startswith_xz(const void *src, uint64_t src_size,
|
||||
void **buffer,
|
||||
const void *prefix, size_t prefix_len,
|
||||
uint8_t extra);
|
||||
int decompress_startswith_lz4(const void *src, uint64_t src_size,
|
||||
void **buffer,
|
||||
const void *prefix, size_t prefix_len,
|
||||
uint8_t extra);
|
||||
int decompress_startswith_zstd(const void *src, uint64_t src_size,
|
||||
void **buffer,
|
||||
const void *prefix, size_t prefix_len,
|
||||
uint8_t extra);
|
||||
int decompress_startswith(Compression compression,
|
||||
const void *src, uint64_t src_size,
|
||||
void **buffer,
|
||||
const void *prefix, size_t prefix_len,
|
||||
uint8_t extra);
|
||||
|
||||
int compress_stream_xz(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_uncompressed_size);
|
||||
int compress_stream_lz4(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_uncompressed_size);
|
||||
int compress_stream_zstd(int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_uncompressed_size);
|
||||
/* Stream compression/decompression (fd-to-fd) */
|
||||
|
||||
int decompress_stream_xz(int fdf, int fdt, uint64_t max_bytes);
|
||||
int decompress_stream_lz4(int fdf, int fdt, uint64_t max_bytes);
|
||||
int decompress_stream_zstd(int fdf, int fdt, uint64_t max_bytes);
|
||||
int compress_stream(Compression type, int fdf, int fdt, uint64_t max_bytes, uint64_t *ret_uncompressed_size);
|
||||
int decompress_stream(Compression type, int fdf, int fdt, uint64_t max_bytes);
|
||||
int decompress_stream_by_filename(const char *filename, int fdf, int fdt, uint64_t max_bytes);
|
||||
|
||||
int dlopen_xz(void);
|
||||
int dlopen_lz4(void);
|
||||
int dlopen_zstd(void);
|
||||
int dlopen_lzma(void);
|
||||
|
||||
static inline int compress_blob(
|
||||
Compression compression,
|
||||
const void *src, uint64_t src_size,
|
||||
void *dst, size_t dst_alloc_size, size_t *dst_size, int level) {
|
||||
|
||||
switch (compression) {
|
||||
case COMPRESSION_ZSTD:
|
||||
return compress_blob_zstd(src, src_size, dst, dst_alloc_size, dst_size, level);
|
||||
case COMPRESSION_LZ4:
|
||||
return compress_blob_lz4(src, src_size, dst, dst_alloc_size, dst_size, level);
|
||||
case COMPRESSION_XZ:
|
||||
return compress_blob_xz(src, src_size, dst, dst_alloc_size, dst_size, level);
|
||||
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 COMPRESSION_ZSTD:
|
||||
return compress_stream_zstd(fdf, fdt, max_bytes, ret_uncompressed_size);
|
||||
case COMPRESSION_LZ4:
|
||||
return compress_stream_lz4(fdf, fdt, max_bytes, ret_uncompressed_size);
|
||||
case COMPRESSION_XZ:
|
||||
return compress_stream_xz(fdf, fdt, max_bytes, ret_uncompressed_size);
|
||||
default:
|
||||
return -EOPNOTSUPP;
|
||||
}
|
||||
}
|
||||
int dlopen_zlib(void);
|
||||
int dlopen_bzip2(void);
|
||||
|
||||
static inline const char* default_compression_extension(void) {
|
||||
switch (DEFAULT_COMPRESSION) {
|
||||
case COMPRESSION_ZSTD:
|
||||
return ".zst";
|
||||
case COMPRESSION_LZ4:
|
||||
return ".lz4";
|
||||
case COMPRESSION_XZ:
|
||||
return ".xz";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
return compression_extension_to_string(DEFAULT_COMPRESSION) ?: "";
|
||||
}
|
||||
|
||||
int decompress_stream(const char *filename, int fdf, int fdt, uint64_t max_bytes);
|
||||
|
||||
@@ -211,12 +211,14 @@ libbasic_static = static_library(
|
||||
fundamental_sources,
|
||||
include_directories : basic_includes,
|
||||
implicit_include_directories : false,
|
||||
dependencies : [libdl,
|
||||
dependencies : [libbzip2_cflags,
|
||||
libdl,
|
||||
libgcrypt_cflags,
|
||||
liblz4_cflags,
|
||||
libm,
|
||||
librt,
|
||||
libxz_cflags,
|
||||
libz_cflags,
|
||||
libzstd_cflags,
|
||||
threads,
|
||||
userspace],
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@ static void load_bcd(const char *path, void **ret_bcd, size_t *ret_bcd_len) {
|
||||
|
||||
assert_se(get_testdata_dir(path, &fn) >= 0);
|
||||
assert_se(read_full_file_full(AT_FDCWD, fn, UINT64_MAX, SIZE_MAX, 0, NULL, &compressed, &len) >= 0);
|
||||
assert_se(decompress_blob_zstd(compressed, len, ret_bcd, ret_bcd_len, SIZE_MAX) >= 0);
|
||||
assert_se(decompress_blob(COMPRESSION_ZSTD, compressed, len, ret_bcd, ret_bcd_len, SIZE_MAX) >= 0);
|
||||
}
|
||||
|
||||
static void test_get_bcd_title_one(
|
||||
|
||||
@@ -373,7 +373,7 @@ static int save_external_coredump(
|
||||
if (fd_compressed < 0)
|
||||
return log_error_errno(fd_compressed, "Failed to create temporary file for coredump %s: %m", fn_compressed);
|
||||
|
||||
r = compress_stream(fd, fd_compressed, max_size, &uncompressed_size);
|
||||
r = compress_stream(DEFAULT_COMPRESSION, fd, fd_compressed, max_size, &uncompressed_size);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to compress %s: %m", coredump_tmpfile_name(tmp_compressed));
|
||||
|
||||
@@ -386,7 +386,7 @@ static int save_external_coredump(
|
||||
tmp = unlink_and_free(tmp);
|
||||
fd = safe_close(fd);
|
||||
|
||||
r = compress_stream(context->input_fd, fd_compressed, max_size, &partial_uncompressed_size);
|
||||
r = compress_stream(DEFAULT_COMPRESSION, context->input_fd, fd_compressed, max_size, &partial_uncompressed_size);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to compress %s: %m", coredump_tmpfile_name(tmp_compressed));
|
||||
uncompressed_size += partial_uncompressed_size;
|
||||
|
||||
@@ -1103,7 +1103,7 @@ static int save_core(sd_journal *j, FILE *file, char **path, bool *unlink_temp)
|
||||
goto error;
|
||||
}
|
||||
|
||||
r = decompress_stream(filename, fdf, fd, -1);
|
||||
r = decompress_stream_by_filename(filename, fdf, fd, -1);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to decompress %s: %m", filename);
|
||||
goto error;
|
||||
|
||||
@@ -227,6 +227,12 @@ assert_cc(sizeof(long long) == sizeof(intmax_t));
|
||||
MAX(_d, a); \
|
||||
})
|
||||
|
||||
#define MAX5(x, y, z, a, b) \
|
||||
({ \
|
||||
const typeof(x) _e = MAX4(x, y, z, a); \
|
||||
MAX(_e, b); \
|
||||
})
|
||||
|
||||
#undef MIN
|
||||
#define MIN(a, b) __MIN(UNIQ, (a), UNIQ, (b))
|
||||
#define __MIN(aq, a, bq, b) \
|
||||
|
||||
+12
-13
@@ -11,7 +11,6 @@
|
||||
#include "fd-util.h"
|
||||
#include "format-util.h"
|
||||
#include "fs-util.h"
|
||||
#include "import-common.h"
|
||||
#include "log.h"
|
||||
#include "pretty-print.h"
|
||||
#include "ratelimit.h"
|
||||
@@ -32,7 +31,7 @@ typedef struct RawExport {
|
||||
int input_fd;
|
||||
int output_fd;
|
||||
|
||||
ImportCompress compress;
|
||||
Compressor *compress;
|
||||
|
||||
sd_event_source *output_event_source;
|
||||
|
||||
@@ -59,7 +58,7 @@ RawExport *raw_export_unref(RawExport *e) {
|
||||
|
||||
sd_event_source_unref(e->output_event_source);
|
||||
|
||||
import_compress_free(&e->compress);
|
||||
e->compress = compressor_free(e->compress);
|
||||
|
||||
sd_event_unref(e->event);
|
||||
|
||||
@@ -143,7 +142,7 @@ static int raw_export_process(RawExport *e) {
|
||||
|
||||
assert(e);
|
||||
|
||||
if (!e->tried_reflink && e->compress.type == IMPORT_COMPRESS_UNCOMPRESSED) {
|
||||
if (!e->tried_reflink && compressor_type(e->compress) == COMPRESSION_NONE) {
|
||||
|
||||
/* If we shall take an uncompressed snapshot we can
|
||||
* reflink source to destination directly. Let's see
|
||||
@@ -158,9 +157,9 @@ static int raw_export_process(RawExport *e) {
|
||||
e->tried_reflink = true;
|
||||
}
|
||||
|
||||
if (!e->tried_sendfile && e->compress.type == IMPORT_COMPRESS_UNCOMPRESSED) {
|
||||
if (!e->tried_sendfile && compressor_type(e->compress) == COMPRESSION_NONE) {
|
||||
|
||||
l = sendfile(e->output_fd, e->input_fd, NULL, IMPORT_BUFFER_SIZE);
|
||||
l = sendfile(e->output_fd, e->input_fd, NULL, COMPRESS_PIPE_BUFFER_SIZE);
|
||||
if (l < 0) {
|
||||
if (errno == EAGAIN)
|
||||
return 0;
|
||||
@@ -180,7 +179,7 @@ static int raw_export_process(RawExport *e) {
|
||||
}
|
||||
|
||||
while (e->buffer_size <= 0) {
|
||||
uint8_t input[IMPORT_BUFFER_SIZE];
|
||||
uint8_t input[COMPRESS_PIPE_BUFFER_SIZE];
|
||||
|
||||
if (e->eof) {
|
||||
r = 0;
|
||||
@@ -195,10 +194,10 @@ static int raw_export_process(RawExport *e) {
|
||||
|
||||
if (l == 0) {
|
||||
e->eof = true;
|
||||
r = import_compress_finish(&e->compress, &e->buffer, &e->buffer_size, &e->buffer_allocated);
|
||||
r = compressor_finish(e->compress, &e->buffer, &e->buffer_size, &e->buffer_allocated);
|
||||
} else {
|
||||
e->written_uncompressed += l;
|
||||
r = import_compress(&e->compress, input, l, &e->buffer, &e->buffer_size, &e->buffer_allocated);
|
||||
r = compressor_start(e->compress, input, l, &e->buffer, &e->buffer_size, &e->buffer_allocated);
|
||||
}
|
||||
if (r < 0) {
|
||||
r = log_error_errno(r, "Failed to encode: %m");
|
||||
@@ -280,15 +279,15 @@ static int reflink_snapshot(int fd, const char *path) {
|
||||
return new_fd;
|
||||
}
|
||||
|
||||
int raw_export_start(RawExport *e, const char *path, int fd, ImportCompressType compress) {
|
||||
int raw_export_start(RawExport *e, const char *path, int fd, Compression compress) {
|
||||
_cleanup_close_ int sfd = -EBADF, tfd = -EBADF;
|
||||
int r;
|
||||
|
||||
assert(e);
|
||||
assert(path);
|
||||
assert(fd >= 0);
|
||||
assert(compress < _IMPORT_COMPRESS_TYPE_MAX);
|
||||
assert(compress != IMPORT_COMPRESS_UNKNOWN);
|
||||
assert(compress >= 0);
|
||||
assert(compress < _COMPRESSION_MAX);
|
||||
|
||||
if (e->output_fd >= 0)
|
||||
return -EBUSY;
|
||||
@@ -318,7 +317,7 @@ int raw_export_start(RawExport *e, const char *path, int fd, ImportCompressType
|
||||
else
|
||||
e->input_fd = TAKE_FD(sfd);
|
||||
|
||||
r = import_compress_init(&e->compress, compress);
|
||||
r = compressor_new(&e->compress, compress);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include "compress.h"
|
||||
#include "shared-forward.h"
|
||||
#include "import-compress.h"
|
||||
|
||||
typedef struct RawExport RawExport;
|
||||
|
||||
@@ -13,4 +13,4 @@ RawExport* raw_export_unref(RawExport *e);
|
||||
|
||||
DEFINE_TRIVIAL_CLEANUP_FUNC(RawExport*, raw_export_unref);
|
||||
|
||||
int raw_export_start(RawExport *e, const char *path, int fd, ImportCompressType compress);
|
||||
int raw_export_start(RawExport *e, const char *path, int fd, Compression compress);
|
||||
|
||||
+12
-11
@@ -1,6 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "sd-daemon.h"
|
||||
#include "sd-event.h"
|
||||
@@ -38,7 +39,7 @@ typedef struct TarExport {
|
||||
int tree_fd; /* directory fd of the tree to set up */
|
||||
int userns_fd;
|
||||
|
||||
ImportCompress compress;
|
||||
Compressor *compress;
|
||||
|
||||
sd_event_source *output_event_source;
|
||||
|
||||
@@ -74,7 +75,7 @@ TarExport *tar_export_unref(TarExport *e) {
|
||||
free(e->temp_path);
|
||||
}
|
||||
|
||||
import_compress_free(&e->compress);
|
||||
e->compress = compressor_free(e->compress);
|
||||
|
||||
sd_event_unref(e->event);
|
||||
|
||||
@@ -188,9 +189,9 @@ static int tar_export_process(TarExport *e) {
|
||||
|
||||
assert(e);
|
||||
|
||||
if (!e->tried_splice && e->compress.type == IMPORT_COMPRESS_UNCOMPRESSED) {
|
||||
if (!e->tried_splice && compressor_type(e->compress) == COMPRESSION_NONE) {
|
||||
|
||||
l = splice(e->tar_fd, NULL, e->output_fd, NULL, IMPORT_BUFFER_SIZE, 0);
|
||||
l = splice(e->tar_fd, NULL, e->output_fd, NULL, COMPRESS_PIPE_BUFFER_SIZE, 0);
|
||||
if (l < 0) {
|
||||
if (errno == EAGAIN)
|
||||
return 0;
|
||||
@@ -210,7 +211,7 @@ static int tar_export_process(TarExport *e) {
|
||||
}
|
||||
|
||||
while (e->buffer_size <= 0) {
|
||||
uint8_t input[IMPORT_BUFFER_SIZE];
|
||||
uint8_t input[COMPRESS_PIPE_BUFFER_SIZE];
|
||||
|
||||
if (e->eof) {
|
||||
r = tar_export_finish(e);
|
||||
@@ -225,10 +226,10 @@ static int tar_export_process(TarExport *e) {
|
||||
|
||||
if (l == 0) {
|
||||
e->eof = true;
|
||||
r = import_compress_finish(&e->compress, &e->buffer, &e->buffer_size, &e->buffer_allocated);
|
||||
r = compressor_finish(e->compress, &e->buffer, &e->buffer_size, &e->buffer_allocated);
|
||||
} else {
|
||||
e->written_uncompressed += l;
|
||||
r = import_compress(&e->compress, input, l, &e->buffer, &e->buffer_size, &e->buffer_allocated);
|
||||
r = compressor_start(e->compress, input, l, &e->buffer, &e->buffer_size, &e->buffer_allocated);
|
||||
}
|
||||
if (r < 0) {
|
||||
r = log_error_errno(r, "Failed to encode: %m");
|
||||
@@ -282,7 +283,7 @@ int tar_export_start(
|
||||
TarExport *e,
|
||||
const char *path,
|
||||
int fd,
|
||||
ImportCompressType compress,
|
||||
Compression compress,
|
||||
ImportFlags flags) {
|
||||
|
||||
_cleanup_close_ int sfd = -EBADF;
|
||||
@@ -291,8 +292,8 @@ int tar_export_start(
|
||||
assert(e);
|
||||
assert(path);
|
||||
assert(fd >= 0);
|
||||
assert(compress < _IMPORT_COMPRESS_TYPE_MAX);
|
||||
assert(compress != IMPORT_COMPRESS_UNKNOWN);
|
||||
assert(compress >= 0);
|
||||
assert(compress < _COMPRESSION_MAX);
|
||||
|
||||
if (e->output_fd >= 0)
|
||||
return -EBUSY;
|
||||
@@ -336,7 +337,7 @@ int tar_export_start(
|
||||
}
|
||||
}
|
||||
|
||||
r = import_compress_init(&e->compress, compress);
|
||||
r = compressor_new(&e->compress, compress);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include "compress.h"
|
||||
#include "import-common.h"
|
||||
#include "import-compress.h"
|
||||
#include "shared-forward.h"
|
||||
|
||||
typedef struct TarExport TarExport;
|
||||
@@ -14,4 +14,4 @@ TarExport* tar_export_unref(TarExport *e);
|
||||
|
||||
DEFINE_TRIVIAL_CLEANUP_FUNC(TarExport*, tar_export_unref);
|
||||
|
||||
int tar_export_start(TarExport *e, const char *path, int fd, ImportCompressType compress, ImportFlags flags);
|
||||
int tar_export_start(TarExport *e, const char *path, int fd, Compression compress, ImportFlags flags);
|
||||
|
||||
+10
-24
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <getopt.h>
|
||||
#include <locale.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "sd-event.h"
|
||||
|
||||
@@ -22,30 +23,15 @@
|
||||
#include "verbs.h"
|
||||
|
||||
static ImportFlags arg_import_flags = 0;
|
||||
static ImportCompressType arg_compress = IMPORT_COMPRESS_UNKNOWN;
|
||||
static Compression arg_compress = _COMPRESSION_INVALID;
|
||||
static ImageClass arg_class = IMAGE_MACHINE;
|
||||
static RuntimeScope arg_runtime_scope = _RUNTIME_SCOPE_INVALID;
|
||||
|
||||
static void determine_compression_from_filename(const char *p) {
|
||||
|
||||
if (arg_compress != IMPORT_COMPRESS_UNKNOWN)
|
||||
if (arg_compress >= 0)
|
||||
return;
|
||||
|
||||
if (!p) {
|
||||
arg_compress = IMPORT_COMPRESS_UNCOMPRESSED;
|
||||
return;
|
||||
}
|
||||
|
||||
if (endswith(p, ".xz"))
|
||||
arg_compress = IMPORT_COMPRESS_XZ;
|
||||
else if (endswith(p, ".gz"))
|
||||
arg_compress = IMPORT_COMPRESS_GZIP;
|
||||
else if (endswith(p, ".bz2"))
|
||||
arg_compress = IMPORT_COMPRESS_BZIP2;
|
||||
else if (endswith(p, ".zst"))
|
||||
arg_compress = IMPORT_COMPRESS_ZSTD;
|
||||
else
|
||||
arg_compress = IMPORT_COMPRESS_UNCOMPRESSED;
|
||||
arg_compress = p ? compression_from_filename(p) : COMPRESSION_NONE;
|
||||
}
|
||||
|
||||
static void on_tar_finished(TarExport *export, int error, void *userdata) {
|
||||
@@ -91,7 +77,7 @@ static int verb_export_tar(int argc, char *argv[], uintptr_t _data, void *userda
|
||||
|
||||
fd = open_fd;
|
||||
|
||||
log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, path, import_compress_type_to_string(arg_compress));
|
||||
log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, path, compression_to_string(arg_compress));
|
||||
} else {
|
||||
_cleanup_free_ char *pretty = NULL;
|
||||
|
||||
@@ -101,7 +87,7 @@ static int verb_export_tar(int argc, char *argv[], uintptr_t _data, void *userda
|
||||
fd = STDOUT_FILENO;
|
||||
|
||||
(void) fd_get_path(fd, &pretty);
|
||||
log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, strna(pretty), import_compress_type_to_string(arg_compress));
|
||||
log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, strna(pretty), compression_to_string(arg_compress));
|
||||
}
|
||||
|
||||
r = import_allocate_event_with_signals(&event);
|
||||
@@ -172,14 +158,14 @@ static int verb_export_raw(int argc, char *argv[], uintptr_t _data, void *userda
|
||||
|
||||
fd = open_fd;
|
||||
|
||||
log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, path, import_compress_type_to_string(arg_compress));
|
||||
log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, path, compression_to_string(arg_compress));
|
||||
} else {
|
||||
_cleanup_free_ char *pretty = NULL;
|
||||
|
||||
fd = STDOUT_FILENO;
|
||||
|
||||
(void) fd_get_path(fd, &pretty);
|
||||
log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, strna(pretty), import_compress_type_to_string(arg_compress));
|
||||
log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, strna(pretty), compression_to_string(arg_compress));
|
||||
}
|
||||
|
||||
r = import_allocate_event_with_signals(&event);
|
||||
@@ -265,8 +251,8 @@ static int parse_argv(int argc, char *argv[]) {
|
||||
return version();
|
||||
|
||||
case ARG_FORMAT:
|
||||
arg_compress = import_compress_type_from_string(optarg);
|
||||
if (arg_compress < 0 || arg_compress == IMPORT_COMPRESS_UNKNOWN)
|
||||
arg_compress = compression_from_string_harder(optarg);
|
||||
if (arg_compress < 0)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
"Unknown format: %s", optarg);
|
||||
break;
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "sd-event.h"
|
||||
|
||||
#include "capability-util.h"
|
||||
#include "compress.h"
|
||||
#include "dirent-util.h"
|
||||
#include "dissect-image.h"
|
||||
#include "fd-util.h"
|
||||
@@ -43,7 +44,7 @@ int import_fork_tar_x(int tree_fd, int userns_fd, PidRef *ret_pid) {
|
||||
if (pipe2(pipefd, O_CLOEXEC) < 0)
|
||||
return log_error_errno(errno, "Failed to create pipe for tar: %m");
|
||||
|
||||
(void) fcntl(pipefd[0], F_SETPIPE_SZ, IMPORT_BUFFER_SIZE);
|
||||
(void) fcntl(pipefd[0], F_SETPIPE_SZ, COMPRESS_PIPE_BUFFER_SIZE);
|
||||
|
||||
r = pidref_safe_fork_full(
|
||||
"tar-x",
|
||||
@@ -110,7 +111,7 @@ int import_fork_tar_c(int tree_fd, int userns_fd, PidRef *ret_pid) {
|
||||
if (pipe2(pipefd, O_CLOEXEC) < 0)
|
||||
return log_error_errno(errno, "Failed to create pipe for tar: %m");
|
||||
|
||||
(void) fcntl(pipefd[0], F_SETPIPE_SZ, IMPORT_BUFFER_SIZE);
|
||||
(void) fcntl(pipefd[0], F_SETPIPE_SZ, COMPRESS_PIPE_BUFFER_SIZE);
|
||||
|
||||
r = pidref_safe_fork_full(
|
||||
"tar-c",
|
||||
|
||||
@@ -49,5 +49,3 @@ int import_allocate_event_with_signals(sd_event **ret);
|
||||
int import_make_foreign_userns(int *userns_fd);
|
||||
|
||||
int import_remove_tree(const char *path, int *userns_fd, ImportFlags flags);
|
||||
|
||||
#define IMPORT_BUFFER_SIZE (128U*1024U)
|
||||
|
||||
@@ -1,611 +0,0 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "import-common.h"
|
||||
#include "import-compress.h"
|
||||
#include "log.h"
|
||||
#include "string-table.h"
|
||||
|
||||
void import_compress_free(ImportCompress *c) {
|
||||
assert(c);
|
||||
|
||||
if (c->type == IMPORT_COMPRESS_XZ)
|
||||
lzma_end(&c->xz);
|
||||
else if (c->type == IMPORT_COMPRESS_GZIP) {
|
||||
if (c->encoding)
|
||||
deflateEnd(&c->gzip);
|
||||
else
|
||||
inflateEnd(&c->gzip);
|
||||
#if HAVE_BZIP2
|
||||
} else if (c->type == IMPORT_COMPRESS_BZIP2) {
|
||||
if (c->encoding)
|
||||
BZ2_bzCompressEnd(&c->bzip2);
|
||||
else
|
||||
BZ2_bzDecompressEnd(&c->bzip2);
|
||||
#endif
|
||||
#if HAVE_ZSTD
|
||||
} else if (c->type == IMPORT_COMPRESS_ZSTD) {
|
||||
if (c->encoding) {
|
||||
ZSTD_freeCCtx(c->c_zstd);
|
||||
c->c_zstd = NULL;
|
||||
} else {
|
||||
ZSTD_freeDCtx(c->d_zstd);
|
||||
c->d_zstd = NULL;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
c->type = IMPORT_COMPRESS_UNKNOWN;
|
||||
}
|
||||
|
||||
int import_uncompress_detect(ImportCompress *c, const void *data, size_t size) {
|
||||
static const uint8_t xz_signature[] = {
|
||||
0xfd, '7', 'z', 'X', 'Z', 0x00
|
||||
};
|
||||
static const uint8_t gzip_signature[] = {
|
||||
0x1f, 0x8b
|
||||
};
|
||||
static const uint8_t bzip2_signature[] = {
|
||||
'B', 'Z', 'h'
|
||||
};
|
||||
static const uint8_t zstd_signature[] = {
|
||||
0x28, 0xb5, 0x2f, 0xfd
|
||||
};
|
||||
|
||||
int r;
|
||||
|
||||
assert(c);
|
||||
|
||||
if (c->type != IMPORT_COMPRESS_UNKNOWN)
|
||||
return 1;
|
||||
|
||||
if (size < MAX4(sizeof(xz_signature),
|
||||
sizeof(gzip_signature),
|
||||
sizeof(zstd_signature),
|
||||
sizeof(bzip2_signature)))
|
||||
return 0;
|
||||
|
||||
assert(data);
|
||||
|
||||
if (memcmp(data, xz_signature, sizeof(xz_signature)) == 0) {
|
||||
lzma_ret xzr;
|
||||
|
||||
xzr = lzma_stream_decoder(&c->xz, UINT64_MAX, LZMA_TELL_UNSUPPORTED_CHECK | LZMA_CONCATENATED);
|
||||
if (xzr != LZMA_OK)
|
||||
return -EIO;
|
||||
|
||||
c->type = IMPORT_COMPRESS_XZ;
|
||||
|
||||
} else if (memcmp(data, gzip_signature, sizeof(gzip_signature)) == 0) {
|
||||
r = inflateInit2(&c->gzip, 15+16);
|
||||
if (r != Z_OK)
|
||||
return -EIO;
|
||||
|
||||
c->type = IMPORT_COMPRESS_GZIP;
|
||||
|
||||
#if HAVE_BZIP2
|
||||
} else if (memcmp(data, bzip2_signature, sizeof(bzip2_signature)) == 0) {
|
||||
r = BZ2_bzDecompressInit(&c->bzip2, 0, 0);
|
||||
if (r != BZ_OK)
|
||||
return -EIO;
|
||||
|
||||
c->type = IMPORT_COMPRESS_BZIP2;
|
||||
#endif
|
||||
#if HAVE_ZSTD
|
||||
} else if (memcmp(data, zstd_signature, sizeof(zstd_signature)) == 0) {
|
||||
c->d_zstd = ZSTD_createDCtx();
|
||||
if (!c->d_zstd)
|
||||
return -ENOMEM;
|
||||
|
||||
c->type = IMPORT_COMPRESS_ZSTD;
|
||||
#endif
|
||||
} else
|
||||
c->type = IMPORT_COMPRESS_UNCOMPRESSED;
|
||||
|
||||
c->encoding = false;
|
||||
|
||||
log_debug("Detected compression type: %s", import_compress_type_to_string(c->type));
|
||||
return 1;
|
||||
}
|
||||
|
||||
void import_uncompress_force_off(ImportCompress *c) {
|
||||
assert(c);
|
||||
|
||||
c->type = IMPORT_COMPRESS_UNCOMPRESSED;
|
||||
c->encoding = false;
|
||||
}
|
||||
|
||||
int import_uncompress(ImportCompress *c, const void *data, size_t size, ImportCompressCallback callback, void *userdata) {
|
||||
int r;
|
||||
|
||||
assert(c);
|
||||
assert(callback);
|
||||
|
||||
r = import_uncompress_detect(c, data, size);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
|
||||
if (c->encoding)
|
||||
return -EINVAL;
|
||||
|
||||
if (size <= 0)
|
||||
return 1;
|
||||
|
||||
assert(data);
|
||||
|
||||
switch (c->type) {
|
||||
|
||||
case IMPORT_COMPRESS_UNCOMPRESSED:
|
||||
r = callback(data, size, userdata);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
break;
|
||||
|
||||
case IMPORT_COMPRESS_XZ:
|
||||
c->xz.next_in = data;
|
||||
c->xz.avail_in = size;
|
||||
|
||||
while (c->xz.avail_in > 0) {
|
||||
uint8_t buffer[IMPORT_BUFFER_SIZE];
|
||||
lzma_ret lzr;
|
||||
|
||||
c->xz.next_out = buffer;
|
||||
c->xz.avail_out = sizeof(buffer);
|
||||
|
||||
lzr = lzma_code(&c->xz, LZMA_RUN);
|
||||
if (!IN_SET(lzr, LZMA_OK, LZMA_STREAM_END))
|
||||
return -EIO;
|
||||
|
||||
if (c->xz.avail_out < sizeof(buffer)) {
|
||||
r = callback(buffer, sizeof(buffer) - c->xz.avail_out, userdata);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case IMPORT_COMPRESS_GZIP:
|
||||
c->gzip.next_in = (void*) data;
|
||||
c->gzip.avail_in = size;
|
||||
|
||||
while (c->gzip.avail_in > 0) {
|
||||
uint8_t buffer[IMPORT_BUFFER_SIZE];
|
||||
|
||||
c->gzip.next_out = buffer;
|
||||
c->gzip.avail_out = sizeof(buffer);
|
||||
|
||||
r = inflate(&c->gzip, Z_NO_FLUSH);
|
||||
if (!IN_SET(r, Z_OK, Z_STREAM_END))
|
||||
return -EIO;
|
||||
|
||||
if (c->gzip.avail_out < sizeof(buffer)) {
|
||||
r = callback(buffer, sizeof(buffer) - c->gzip.avail_out, userdata);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
#if HAVE_BZIP2
|
||||
case IMPORT_COMPRESS_BZIP2:
|
||||
c->bzip2.next_in = (void*) data;
|
||||
c->bzip2.avail_in = size;
|
||||
|
||||
while (c->bzip2.avail_in > 0) {
|
||||
uint8_t buffer[IMPORT_BUFFER_SIZE];
|
||||
|
||||
c->bzip2.next_out = (char*) buffer;
|
||||
c->bzip2.avail_out = sizeof(buffer);
|
||||
|
||||
r = BZ2_bzDecompress(&c->bzip2);
|
||||
if (!IN_SET(r, BZ_OK, BZ_STREAM_END))
|
||||
return -EIO;
|
||||
|
||||
if (c->bzip2.avail_out < sizeof(buffer)) {
|
||||
r = callback(buffer, sizeof(buffer) - c->bzip2.avail_out, userdata);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
#endif
|
||||
#if HAVE_ZSTD
|
||||
case IMPORT_COMPRESS_ZSTD: {
|
||||
ZSTD_inBuffer input = {
|
||||
.src = (void*) data,
|
||||
.size = size,
|
||||
};
|
||||
|
||||
while (input.pos < input.size) {
|
||||
uint8_t buffer[IMPORT_BUFFER_SIZE];
|
||||
ZSTD_outBuffer output = {
|
||||
.dst = buffer,
|
||||
.size = sizeof(buffer),
|
||||
};
|
||||
size_t res;
|
||||
|
||||
res = ZSTD_decompressStream(c->d_zstd, &output, &input);
|
||||
if (ZSTD_isError(res))
|
||||
return -EIO;
|
||||
|
||||
if (output.pos > 0) {
|
||||
r = callback(output.dst, output.pos, userdata);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
default:
|
||||
assert_not_reached();
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int import_compress_init(ImportCompress *c, ImportCompressType t) {
|
||||
int r;
|
||||
|
||||
assert(c);
|
||||
|
||||
switch (t) {
|
||||
|
||||
case IMPORT_COMPRESS_XZ: {
|
||||
lzma_ret xzr;
|
||||
|
||||
xzr = lzma_easy_encoder(&c->xz, LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64);
|
||||
if (xzr != LZMA_OK)
|
||||
return -EIO;
|
||||
|
||||
c->type = IMPORT_COMPRESS_XZ;
|
||||
break;
|
||||
}
|
||||
|
||||
case IMPORT_COMPRESS_GZIP:
|
||||
r = deflateInit2(&c->gzip, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY);
|
||||
if (r != Z_OK)
|
||||
return -EIO;
|
||||
|
||||
c->type = IMPORT_COMPRESS_GZIP;
|
||||
break;
|
||||
|
||||
#if HAVE_BZIP2
|
||||
case IMPORT_COMPRESS_BZIP2:
|
||||
r = BZ2_bzCompressInit(&c->bzip2, 9, 0, 0);
|
||||
if (r != BZ_OK)
|
||||
return -EIO;
|
||||
|
||||
c->type = IMPORT_COMPRESS_BZIP2;
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if HAVE_ZSTD
|
||||
case IMPORT_COMPRESS_ZSTD:
|
||||
c->c_zstd = ZSTD_createCCtx();
|
||||
if (!c->c_zstd)
|
||||
return -ENOMEM;
|
||||
|
||||
r = ZSTD_CCtx_setParameter(c->c_zstd, ZSTD_c_compressionLevel, ZSTD_CLEVEL_DEFAULT);
|
||||
if (ZSTD_isError(r))
|
||||
return -EIO;
|
||||
|
||||
c->type = IMPORT_COMPRESS_ZSTD;
|
||||
break;
|
||||
#endif
|
||||
|
||||
case IMPORT_COMPRESS_UNCOMPRESSED:
|
||||
c->type = IMPORT_COMPRESS_UNCOMPRESSED;
|
||||
break;
|
||||
|
||||
default:
|
||||
return -EOPNOTSUPP;
|
||||
}
|
||||
|
||||
c->encoding = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int enlarge_buffer(void **buffer, size_t *buffer_size, size_t *buffer_allocated) {
|
||||
size_t l;
|
||||
void *p;
|
||||
|
||||
assert(buffer);
|
||||
assert(buffer_size);
|
||||
assert(buffer_allocated);
|
||||
|
||||
if (*buffer_allocated > *buffer_size)
|
||||
return 0;
|
||||
|
||||
l = MAX(IMPORT_BUFFER_SIZE, (*buffer_size * 2));
|
||||
p = realloc(*buffer, l);
|
||||
if (!p)
|
||||
return -ENOMEM;
|
||||
|
||||
*buffer = p;
|
||||
*buffer_allocated = l;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int import_compress(ImportCompress *c, const void *data, size_t size, void **buffer, size_t *buffer_size, size_t *buffer_allocated) {
|
||||
int r;
|
||||
|
||||
assert(c);
|
||||
assert(buffer);
|
||||
assert(buffer_size);
|
||||
assert(buffer_allocated);
|
||||
|
||||
if (!c->encoding)
|
||||
return -EINVAL;
|
||||
|
||||
if (size <= 0)
|
||||
return 0;
|
||||
|
||||
assert(data);
|
||||
|
||||
*buffer_size = 0;
|
||||
|
||||
switch (c->type) {
|
||||
|
||||
case IMPORT_COMPRESS_XZ:
|
||||
|
||||
c->xz.next_in = data;
|
||||
c->xz.avail_in = size;
|
||||
|
||||
while (c->xz.avail_in > 0) {
|
||||
lzma_ret lzr;
|
||||
|
||||
r = enlarge_buffer(buffer, buffer_size, buffer_allocated);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
c->xz.next_out = (uint8_t*) *buffer + *buffer_size;
|
||||
c->xz.avail_out = *buffer_allocated - *buffer_size;
|
||||
|
||||
lzr = lzma_code(&c->xz, LZMA_RUN);
|
||||
if (lzr != LZMA_OK)
|
||||
return -EIO;
|
||||
|
||||
*buffer_size += (*buffer_allocated - *buffer_size) - c->xz.avail_out;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case IMPORT_COMPRESS_GZIP:
|
||||
|
||||
c->gzip.next_in = (void*) data;
|
||||
c->gzip.avail_in = size;
|
||||
|
||||
while (c->gzip.avail_in > 0) {
|
||||
r = enlarge_buffer(buffer, buffer_size, buffer_allocated);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
c->gzip.next_out = (uint8_t*) *buffer + *buffer_size;
|
||||
c->gzip.avail_out = *buffer_allocated - *buffer_size;
|
||||
|
||||
r = deflate(&c->gzip, Z_NO_FLUSH);
|
||||
if (r != Z_OK)
|
||||
return -EIO;
|
||||
|
||||
*buffer_size += (*buffer_allocated - *buffer_size) - c->gzip.avail_out;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
#if HAVE_BZIP2
|
||||
case IMPORT_COMPRESS_BZIP2:
|
||||
|
||||
c->bzip2.next_in = (void*) data;
|
||||
c->bzip2.avail_in = size;
|
||||
|
||||
while (c->bzip2.avail_in > 0) {
|
||||
r = enlarge_buffer(buffer, buffer_size, buffer_allocated);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
c->bzip2.next_out = (void*) ((uint8_t*) *buffer + *buffer_size);
|
||||
c->bzip2.avail_out = *buffer_allocated - *buffer_size;
|
||||
|
||||
r = BZ2_bzCompress(&c->bzip2, BZ_RUN);
|
||||
if (r != BZ_RUN_OK)
|
||||
return -EIO;
|
||||
|
||||
*buffer_size += (*buffer_allocated - *buffer_size) - c->bzip2.avail_out;
|
||||
}
|
||||
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if HAVE_ZSTD
|
||||
case IMPORT_COMPRESS_ZSTD: {
|
||||
ZSTD_inBuffer input = {
|
||||
.src = data,
|
||||
.size = size,
|
||||
};
|
||||
|
||||
while (input.pos < input.size) {
|
||||
r = enlarge_buffer(buffer, buffer_size, buffer_allocated);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
ZSTD_outBuffer output = {
|
||||
.dst = ((uint8_t *) *buffer + *buffer_size),
|
||||
.size = *buffer_allocated - *buffer_size,
|
||||
};
|
||||
size_t res;
|
||||
|
||||
res = ZSTD_compressStream2(c->c_zstd, &output, &input, ZSTD_e_continue);
|
||||
if (ZSTD_isError(res))
|
||||
return -EIO;
|
||||
|
||||
*buffer_size += output.pos;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
case IMPORT_COMPRESS_UNCOMPRESSED:
|
||||
|
||||
if (*buffer_allocated < size) {
|
||||
void *p;
|
||||
|
||||
p = realloc(*buffer, size);
|
||||
if (!p)
|
||||
return -ENOMEM;
|
||||
|
||||
*buffer = p;
|
||||
*buffer_allocated = size;
|
||||
}
|
||||
|
||||
memcpy(*buffer, data, size);
|
||||
*buffer_size = size;
|
||||
break;
|
||||
|
||||
default:
|
||||
return -EOPNOTSUPP;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int import_compress_finish(ImportCompress *c, void **buffer, size_t *buffer_size, size_t *buffer_allocated) {
|
||||
int r;
|
||||
|
||||
assert(c);
|
||||
assert(buffer);
|
||||
assert(buffer_size);
|
||||
assert(buffer_allocated);
|
||||
|
||||
if (!c->encoding)
|
||||
return -EINVAL;
|
||||
|
||||
*buffer_size = 0;
|
||||
|
||||
switch (c->type) {
|
||||
|
||||
case IMPORT_COMPRESS_XZ: {
|
||||
lzma_ret lzr;
|
||||
|
||||
c->xz.avail_in = 0;
|
||||
|
||||
do {
|
||||
r = enlarge_buffer(buffer, buffer_size, buffer_allocated);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
c->xz.next_out = (uint8_t*) *buffer + *buffer_size;
|
||||
c->xz.avail_out = *buffer_allocated - *buffer_size;
|
||||
|
||||
lzr = lzma_code(&c->xz, LZMA_FINISH);
|
||||
if (!IN_SET(lzr, LZMA_OK, LZMA_STREAM_END))
|
||||
return -EIO;
|
||||
|
||||
*buffer_size += (*buffer_allocated - *buffer_size) - c->xz.avail_out;
|
||||
} while (lzr != LZMA_STREAM_END);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case IMPORT_COMPRESS_GZIP:
|
||||
c->gzip.avail_in = 0;
|
||||
|
||||
do {
|
||||
r = enlarge_buffer(buffer, buffer_size, buffer_allocated);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
c->gzip.next_out = (uint8_t*) *buffer + *buffer_size;
|
||||
c->gzip.avail_out = *buffer_allocated - *buffer_size;
|
||||
|
||||
r = deflate(&c->gzip, Z_FINISH);
|
||||
if (!IN_SET(r, Z_OK, Z_STREAM_END))
|
||||
return -EIO;
|
||||
|
||||
*buffer_size += (*buffer_allocated - *buffer_size) - c->gzip.avail_out;
|
||||
} while (r != Z_STREAM_END);
|
||||
|
||||
break;
|
||||
|
||||
#if HAVE_BZIP2
|
||||
case IMPORT_COMPRESS_BZIP2:
|
||||
c->bzip2.avail_in = 0;
|
||||
|
||||
do {
|
||||
r = enlarge_buffer(buffer, buffer_size, buffer_allocated);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
c->bzip2.next_out = (void*) ((uint8_t*) *buffer + *buffer_size);
|
||||
c->bzip2.avail_out = *buffer_allocated - *buffer_size;
|
||||
|
||||
r = BZ2_bzCompress(&c->bzip2, BZ_FINISH);
|
||||
if (!IN_SET(r, BZ_FINISH_OK, BZ_STREAM_END))
|
||||
return -EIO;
|
||||
|
||||
*buffer_size += (*buffer_allocated - *buffer_size) - c->bzip2.avail_out;
|
||||
} while (r != BZ_STREAM_END);
|
||||
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if HAVE_ZSTD
|
||||
case IMPORT_COMPRESS_ZSTD: {
|
||||
ZSTD_inBuffer input = {};
|
||||
size_t res;
|
||||
|
||||
do {
|
||||
r = enlarge_buffer(buffer, buffer_size, buffer_allocated);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
ZSTD_outBuffer output = {
|
||||
.dst = ((uint8_t *) *buffer + *buffer_size),
|
||||
.size = *buffer_allocated - *buffer_size,
|
||||
};
|
||||
|
||||
res = ZSTD_compressStream2(c->c_zstd, &output, &input, ZSTD_e_end);
|
||||
if (ZSTD_isError(res))
|
||||
return -EIO;
|
||||
|
||||
*buffer_size += output.pos;
|
||||
} while (res != 0);
|
||||
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
case IMPORT_COMPRESS_UNCOMPRESSED:
|
||||
break;
|
||||
|
||||
default:
|
||||
return -EOPNOTSUPP;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const char* const import_compress_type_table[_IMPORT_COMPRESS_TYPE_MAX] = {
|
||||
[IMPORT_COMPRESS_UNKNOWN] = "unknown",
|
||||
[IMPORT_COMPRESS_UNCOMPRESSED] = "uncompressed",
|
||||
[IMPORT_COMPRESS_XZ] = "xz",
|
||||
[IMPORT_COMPRESS_GZIP] = "gzip",
|
||||
#if HAVE_BZIP2
|
||||
[IMPORT_COMPRESS_BZIP2] = "bzip2",
|
||||
#endif
|
||||
#if HAVE_ZSTD
|
||||
[IMPORT_COMPRESS_ZSTD] = "zstd",
|
||||
#endif
|
||||
};
|
||||
|
||||
DEFINE_STRING_TABLE_LOOKUP(import_compress_type, ImportCompressType);
|
||||
@@ -1,54 +0,0 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#if HAVE_BZIP2
|
||||
#include <bzlib.h>
|
||||
#endif
|
||||
#include <lzma.h>
|
||||
#include <zlib.h>
|
||||
#if HAVE_ZSTD
|
||||
#include <zstd.h>
|
||||
#endif
|
||||
|
||||
#include "shared-forward.h"
|
||||
|
||||
typedef enum ImportCompressType {
|
||||
IMPORT_COMPRESS_UNKNOWN,
|
||||
IMPORT_COMPRESS_UNCOMPRESSED,
|
||||
IMPORT_COMPRESS_XZ,
|
||||
IMPORT_COMPRESS_GZIP,
|
||||
IMPORT_COMPRESS_BZIP2,
|
||||
IMPORT_COMPRESS_ZSTD,
|
||||
_IMPORT_COMPRESS_TYPE_MAX,
|
||||
_IMPORT_COMPRESS_TYPE_INVALID = -EINVAL,
|
||||
} ImportCompressType;
|
||||
|
||||
typedef struct ImportCompress {
|
||||
ImportCompressType type;
|
||||
bool encoding;
|
||||
union {
|
||||
lzma_stream xz;
|
||||
z_stream gzip;
|
||||
#if HAVE_BZIP2
|
||||
bz_stream bzip2;
|
||||
#endif
|
||||
#if HAVE_ZSTD
|
||||
ZSTD_CCtx *c_zstd;
|
||||
ZSTD_DCtx *d_zstd;
|
||||
#endif
|
||||
};
|
||||
} ImportCompress;
|
||||
|
||||
typedef int (*ImportCompressCallback)(const void *data, size_t size, void *userdata);
|
||||
|
||||
void import_compress_free(ImportCompress *c);
|
||||
|
||||
int import_uncompress_detect(ImportCompress *c, const void *data, size_t size);
|
||||
void import_uncompress_force_off(ImportCompress *c);
|
||||
int import_uncompress(ImportCompress *c, const void *data, size_t size, ImportCompressCallback callback, void *userdata);
|
||||
|
||||
int import_compress_init(ImportCompress *c, ImportCompressType t);
|
||||
int import_compress(ImportCompress *c, const void *data, size_t size, void **buffer, size_t *buffer_size, size_t *buffer_allocated);
|
||||
int import_compress_finish(ImportCompress *c, void **buffer, size_t *buffer_size, size_t *buffer_allocated);
|
||||
|
||||
DECLARE_STRING_TABLE_LOOKUP(import_compress_type, ImportCompressType);
|
||||
+10
-9
@@ -1,17 +1,18 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "sd-daemon.h"
|
||||
#include "sd-event.h"
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "compress.h"
|
||||
#include "copy.h"
|
||||
#include "fd-util.h"
|
||||
#include "format-util.h"
|
||||
#include "fs-util.h"
|
||||
#include "import-common.h"
|
||||
#include "import-compress.h"
|
||||
#include "import-raw.h"
|
||||
#include "import-util.h"
|
||||
#include "install-file.h"
|
||||
@@ -43,11 +44,11 @@ typedef struct RawImport {
|
||||
int input_fd;
|
||||
int output_fd;
|
||||
|
||||
ImportCompress compress;
|
||||
Compressor *compress;
|
||||
|
||||
sd_event_source *input_event_source;
|
||||
|
||||
uint8_t buffer[IMPORT_BUFFER_SIZE];
|
||||
uint8_t buffer[COMPRESS_PIPE_BUFFER_SIZE];
|
||||
size_t buffer_size;
|
||||
|
||||
uint64_t written_compressed;
|
||||
@@ -71,7 +72,7 @@ RawImport* raw_import_unref(RawImport *i) {
|
||||
|
||||
unlink_and_free(i->temp_path);
|
||||
|
||||
import_compress_free(&i->compress);
|
||||
i->compress = compressor_free(i->compress);
|
||||
|
||||
sd_event_unref(i->event);
|
||||
|
||||
@@ -328,7 +329,7 @@ static int raw_import_try_reflink(RawImport *i) {
|
||||
assert(i->input_fd >= 0);
|
||||
assert(i->output_fd >= 0);
|
||||
|
||||
if (i->compress.type != IMPORT_COMPRESS_UNCOMPRESSED)
|
||||
if (compressor_type(i->compress) != COMPRESSION_NONE)
|
||||
return 0;
|
||||
|
||||
if (i->offset != UINT64_MAX || i->size_max != UINT64_MAX)
|
||||
@@ -425,13 +426,13 @@ static int raw_import_process(RawImport *i) {
|
||||
|
||||
i->buffer_size += l;
|
||||
|
||||
if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
|
||||
if (!i->compress) {
|
||||
|
||||
if (l == 0) { /* EOF */
|
||||
log_debug("File too short to be compressed, as no compression signature fits in, thus assuming uncompressed.");
|
||||
import_uncompress_force_off(&i->compress);
|
||||
decompressor_force_off(&i->compress);
|
||||
} else {
|
||||
r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size);
|
||||
r = decompressor_detect(&i->compress, i->buffer, i->buffer_size);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to detect file compression: %m");
|
||||
goto finish;
|
||||
@@ -451,7 +452,7 @@ static int raw_import_process(RawImport *i) {
|
||||
goto complete;
|
||||
}
|
||||
|
||||
r = import_uncompress(&i->compress, i->buffer, i->buffer_size, raw_import_write, i);
|
||||
r = decompressor_push(i->compress, i->buffer, i->buffer_size, raw_import_write, i);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to decode and write: %m");
|
||||
goto finish;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "sd-daemon.h"
|
||||
#include "sd-event.h"
|
||||
@@ -8,12 +9,12 @@
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "btrfs-util.h"
|
||||
#include "compress.h"
|
||||
#include "dissect-image.h"
|
||||
#include "errno-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "format-util.h"
|
||||
#include "import-common.h"
|
||||
#include "import-compress.h"
|
||||
#include "import-tar.h"
|
||||
#include "import-util.h"
|
||||
#include "install-file.h"
|
||||
@@ -50,11 +51,11 @@ typedef struct TarImport {
|
||||
int tree_fd;
|
||||
int userns_fd;
|
||||
|
||||
ImportCompress compress;
|
||||
Compressor *compress;
|
||||
|
||||
sd_event_source *input_event_source;
|
||||
|
||||
uint8_t buffer[IMPORT_BUFFER_SIZE];
|
||||
uint8_t buffer[COMPRESS_PIPE_BUFFER_SIZE];
|
||||
size_t buffer_size;
|
||||
|
||||
uint64_t written_compressed;
|
||||
@@ -81,7 +82,7 @@ TarImport* tar_import_unref(TarImport *i) {
|
||||
free(i->temp_path);
|
||||
}
|
||||
|
||||
import_compress_free(&i->compress);
|
||||
i->compress = compressor_free(i->compress);
|
||||
|
||||
sd_event_unref(i->event);
|
||||
|
||||
@@ -344,13 +345,13 @@ static int tar_import_process(TarImport *i) {
|
||||
|
||||
i->buffer_size += l;
|
||||
|
||||
if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
|
||||
if (!i->compress) {
|
||||
|
||||
if (l == 0) { /* EOF */
|
||||
log_debug("File too short to be compressed, as no compression signature fits in, thus assuming uncompressed.");
|
||||
import_uncompress_force_off(&i->compress);
|
||||
decompressor_force_off(&i->compress);
|
||||
} else {
|
||||
r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size);
|
||||
r = decompressor_detect(&i->compress, i->buffer, i->buffer_size);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to detect file compression: %m");
|
||||
goto finish;
|
||||
@@ -364,7 +365,7 @@ static int tar_import_process(TarImport *i) {
|
||||
goto finish;
|
||||
}
|
||||
|
||||
r = import_uncompress(&i->compress, i->buffer, i->buffer_size, tar_import_write, i);
|
||||
r = decompressor_push(i->compress, i->buffer, i->buffer_size, tar_import_write, i);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to decode and write: %m");
|
||||
goto finish;
|
||||
|
||||
@@ -7,11 +7,7 @@ if conf.get('ENABLE_IMPORTD') != 1
|
||||
endif
|
||||
|
||||
common_deps = [
|
||||
libbzip2,
|
||||
libcurl,
|
||||
libxz,
|
||||
libz,
|
||||
libzstd,
|
||||
]
|
||||
|
||||
executables += [
|
||||
@@ -25,7 +21,6 @@ executables += [
|
||||
'extract' : files(
|
||||
'oci-util.c',
|
||||
'import-common.c',
|
||||
'import-compress.c',
|
||||
'qcow2-util.c',
|
||||
),
|
||||
'dependencies' : [common_deps, threads],
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include "sd-id128.h"
|
||||
|
||||
#include "alloc-util.h"
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/xattr.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "curl-util.h"
|
||||
@@ -53,7 +54,7 @@ PullJob* pull_job_unref(PullJob *j) {
|
||||
curl_glue_remove_and_free(j->glue, j->curl);
|
||||
curl_slist_free_all(j->request_header);
|
||||
|
||||
import_compress_free(&j->compress);
|
||||
j->compress = compressor_free(j->compress);
|
||||
|
||||
if (j->checksum_ctx)
|
||||
EVP_MD_CTX_free(j->checksum_ctx);
|
||||
@@ -134,7 +135,7 @@ int pull_job_restart(PullJob *j, const char *new_url) {
|
||||
curl_glue_remove_and_free(j->glue, j->curl);
|
||||
j->curl = NULL;
|
||||
|
||||
import_compress_free(&j->compress);
|
||||
j->compress = compressor_free(j->compress);
|
||||
|
||||
if (j->checksum_ctx) {
|
||||
EVP_MD_CTX_free(j->checksum_ctx);
|
||||
@@ -453,7 +454,7 @@ static int pull_job_write_compressed(PullJob *j, const struct iovec *data) {
|
||||
"Could not hash chunk.");
|
||||
}
|
||||
|
||||
r = import_uncompress(&j->compress, data->iov_base, data->iov_len, pull_job_write_uncompressed, j);
|
||||
r = decompressor_push(j->compress, data->iov_base, data->iov_len, pull_job_write_uncompressed, j);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
@@ -502,13 +503,13 @@ static int pull_job_detect_compression(PullJob *j) {
|
||||
|
||||
assert(j);
|
||||
|
||||
r = import_uncompress_detect(&j->compress, j->payload.iov_base, j->payload.iov_len);
|
||||
r = decompressor_detect(&j->compress, j->payload.iov_base, j->payload.iov_len);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to initialize compressor: %m");
|
||||
if (r == 0)
|
||||
return 0;
|
||||
|
||||
log_debug("Stream is compressed: %s", import_compress_type_to_string(j->compress.type));
|
||||
log_debug("Stream is compressed: %s", compression_to_string(compressor_type(j->compress)));
|
||||
|
||||
r = pull_job_open_disk(j);
|
||||
if (r < 0)
|
||||
|
||||
@@ -4,9 +4,9 @@
|
||||
#include <curl/curl.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "shared-forward.h"
|
||||
#include "import-compress.h"
|
||||
#include "compress.h"
|
||||
#include "openssl-util.h"
|
||||
#include "shared-forward.h"
|
||||
|
||||
typedef struct CurlGlue CurlGlue;
|
||||
typedef struct PullJob PullJob;
|
||||
@@ -73,7 +73,7 @@ typedef struct PullJob {
|
||||
usec_t mtime;
|
||||
char *content_type;
|
||||
|
||||
ImportCompress compress;
|
||||
Compressor *compress;
|
||||
|
||||
unsigned progress_percent;
|
||||
usec_t start_usec;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include "sd-event.h"
|
||||
#include "sd-json.h"
|
||||
#include "sd-varlink.h"
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include "sd-daemon.h"
|
||||
#include "sd-event.h"
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include "sd-daemon.h"
|
||||
#include "sd-event.h"
|
||||
#include "sd-varlink.h"
|
||||
|
||||
+5
-17
@@ -1,8 +1,9 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <zlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "compress.h"
|
||||
#include "copy.h"
|
||||
#include "qcow2-util.h"
|
||||
#include "sparse-endian.h"
|
||||
@@ -97,8 +98,6 @@ static int decompress_cluster(
|
||||
void *buffer2) {
|
||||
|
||||
_cleanup_free_ void *large_buffer = NULL;
|
||||
z_stream s = {};
|
||||
uint64_t sz;
|
||||
ssize_t l;
|
||||
int r;
|
||||
|
||||
@@ -119,20 +118,9 @@ static int decompress_cluster(
|
||||
if ((uint64_t) l != compressed_size)
|
||||
return -EIO;
|
||||
|
||||
s.next_in = buffer1;
|
||||
s.avail_in = compressed_size;
|
||||
s.next_out = buffer2;
|
||||
s.avail_out = cluster_size;
|
||||
|
||||
r = inflateInit2(&s, -12);
|
||||
if (r != Z_OK)
|
||||
return -EIO;
|
||||
|
||||
r = inflate(&s, Z_FINISH);
|
||||
sz = (uint8_t*) s.next_out - (uint8_t*) buffer2;
|
||||
inflateEnd(&s);
|
||||
if (r != Z_STREAM_END || sz != cluster_size)
|
||||
return -EIO;
|
||||
r = decompress_zlib_raw(buffer1, compressed_size, buffer2, cluster_size, /* wbits= */ -12);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
l = pwrite(dfd, buffer2, cluster_size, doffset);
|
||||
if (l < 0)
|
||||
|
||||
@@ -130,7 +130,7 @@ int config_parse_compression(
|
||||
}
|
||||
}
|
||||
|
||||
Compression c = compression_lowercase_from_string(word);
|
||||
Compression c = compression_from_string_harder(word);
|
||||
if (c <= 0 || !compression_supported(c)) {
|
||||
log_syntax(unit, LOG_WARNING, filename, line, c,
|
||||
"Compression algorithm '%s' is not supported on the system, ignoring.", word);
|
||||
|
||||
@@ -209,7 +209,7 @@ static int build_accept_encoding(char **ret) {
|
||||
|
||||
const CompressionConfig *cc;
|
||||
ORDERED_HASHMAP_FOREACH(cc, arg_compression) {
|
||||
const char *c = compression_lowercase_to_string(cc->algorithm);
|
||||
const char *c = compression_to_string(cc->algorithm);
|
||||
if (strextendf_with_separator(&buf, ",", "%s;q=%.1f", c, q) < 0)
|
||||
return -ENOMEM;
|
||||
q -= step;
|
||||
@@ -361,7 +361,7 @@ static mhd_result request_handler(
|
||||
RemoteSource *source = *connection_cls;
|
||||
header = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "Content-Encoding");
|
||||
if (header) {
|
||||
Compression c = compression_lowercase_from_string(header);
|
||||
Compression c = compression_from_string_harder(header);
|
||||
if (c <= 0 || !compression_supported(c))
|
||||
return mhd_respondf(connection, 0, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE,
|
||||
"Unsupported Content-Encoding type: %s", header);
|
||||
|
||||
@@ -354,7 +354,7 @@ static size_t journal_input_callback(void *buf, size_t size, size_t nmemb, void
|
||||
r = compress_blob(u->compression->algorithm, compression_buffer, filled, buf, size * nmemb, &compressed_size, u->compression->level);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to compress %zu bytes by %s with level %i: %m",
|
||||
filled, compression_lowercase_to_string(u->compression->algorithm), u->compression->level);
|
||||
filled, compression_to_string(u->compression->algorithm), u->compression->level);
|
||||
return CURL_READFUNC_ABORT;
|
||||
}
|
||||
|
||||
|
||||
@@ -218,7 +218,7 @@ int start_upload(Uploader *u,
|
||||
h = l;
|
||||
|
||||
if (u->compression) {
|
||||
_cleanup_free_ char *header = strjoin("Content-Encoding: ", compression_lowercase_to_string(u->compression->algorithm));
|
||||
_cleanup_free_ char *header = strjoin("Content-Encoding: ", compression_to_string(u->compression->algorithm));
|
||||
if (!header)
|
||||
return log_oom();
|
||||
|
||||
@@ -369,7 +369,7 @@ static size_t fd_input_callback(void *buf, size_t size, size_t nmemb, void *user
|
||||
r = compress_blob(u->compression->algorithm, compression_buffer, n, buf, size * nmemb, &compressed_size, u->compression->level);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to compress %zd bytes by %s with level %i: %m",
|
||||
n, compression_lowercase_to_string(u->compression->algorithm), u->compression->level);
|
||||
n, compression_to_string(u->compression->algorithm), u->compression->level);
|
||||
return CURL_READFUNC_ABORT;
|
||||
}
|
||||
assert(compressed_size <= size * nmemb);
|
||||
@@ -528,7 +528,7 @@ static int update_content_encoding_header(Uploader *u, const CompressionConfig *
|
||||
return 0; /* Already picked the algorithm. Let's shortcut. */
|
||||
|
||||
if (cc) {
|
||||
_cleanup_free_ char *header = strjoin("Content-Encoding: ", compression_lowercase_to_string(cc->algorithm));
|
||||
_cleanup_free_ char *header = strjoin("Content-Encoding: ", compression_to_string(cc->algorithm));
|
||||
if (!header)
|
||||
return log_oom();
|
||||
|
||||
@@ -572,7 +572,7 @@ static int update_content_encoding_header(Uploader *u, const CompressionConfig *
|
||||
u->compression = cc;
|
||||
|
||||
if (cc)
|
||||
log_debug("Using compression algorithm %s with compression level %i.", compression_lowercase_to_string(cc->algorithm), cc->level);
|
||||
log_debug("Using compression algorithm %s with compression level %i.", compression_to_string(cc->algorithm), cc->level);
|
||||
else
|
||||
log_debug("Disabled compression algorithm.");
|
||||
return 0;
|
||||
@@ -610,7 +610,7 @@ static int parse_accept_encoding_header(Uploader *u) {
|
||||
if (streq(word, "*"))
|
||||
return update_content_encoding_header(u, ordered_hashmap_first(arg_compression));
|
||||
|
||||
Compression c = compression_lowercase_from_string(word);
|
||||
Compression c = compression_from_string_harder(word);
|
||||
if (c <= 0 || !compression_supported(c))
|
||||
continue; /* unsupported or invalid algorithm. */
|
||||
|
||||
|
||||
@@ -370,7 +370,7 @@ static Compression getenv_compression(void) {
|
||||
if (r >= 0)
|
||||
return r ? DEFAULT_COMPRESSION : COMPRESSION_NONE;
|
||||
|
||||
c = compression_from_string(e);
|
||||
c = compression_from_string_harder(e);
|
||||
if (c < 0) {
|
||||
log_debug_errno(c, "Failed to parse SYSTEMD_JOURNAL_COMPRESS value, ignoring: %s", e);
|
||||
return DEFAULT_COMPRESSION;
|
||||
|
||||
@@ -1,27 +1,36 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "argv-util.h"
|
||||
#include "compress.h"
|
||||
#include "nulstr-util.h"
|
||||
#include "parse-util.h"
|
||||
#include "process-util.h"
|
||||
#include "random-util.h"
|
||||
#include "string-table.h"
|
||||
#include "tests.h"
|
||||
#include "time-util.h"
|
||||
|
||||
typedef int (compress_t)(const void *src, uint64_t src_size, void *dst,
|
||||
size_t dst_alloc_size, size_t *dst_size, int level);
|
||||
typedef int (decompress_t)(const void *src, uint64_t src_size,
|
||||
void **dst, size_t* dst_size, size_t dst_max);
|
||||
|
||||
#if HAVE_COMPRESSION
|
||||
|
||||
static usec_t arg_duration;
|
||||
static size_t arg_start;
|
||||
|
||||
#define MAX_SIZE (1024*1024LU)
|
||||
#define PRIME 1048571 /* A prime close enough to one megabyte that mod 4 == 3 */
|
||||
|
||||
typedef enum BenchmarkDataType {
|
||||
BENCHMARK_DATA_ZEROS,
|
||||
BENCHMARK_DATA_SIMPLE,
|
||||
BENCHMARK_DATA_RANDOM,
|
||||
_BENCHMARK_DATA_TYPE_MAX,
|
||||
} BenchmarkDataType;
|
||||
|
||||
static const char* const benchmark_data_type_table[_BENCHMARK_DATA_TYPE_MAX] = {
|
||||
[BENCHMARK_DATA_ZEROS] = "zeros",
|
||||
[BENCHMARK_DATA_SIMPLE] = "simple",
|
||||
[BENCHMARK_DATA_RANDOM] = "random",
|
||||
};
|
||||
|
||||
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(benchmark_data_type, BenchmarkDataType);
|
||||
|
||||
static size_t _permute(size_t x) {
|
||||
size_t residue;
|
||||
|
||||
@@ -39,19 +48,24 @@ static size_t permute(size_t x) {
|
||||
return _permute((_permute(x) + arg_start) % MAX_SIZE ^ 0xFF345);
|
||||
}
|
||||
|
||||
static char* make_buf(size_t count, const char *type) {
|
||||
static char* make_buf(size_t count, BenchmarkDataType type) {
|
||||
char *buf;
|
||||
size_t i;
|
||||
|
||||
buf = malloc(count);
|
||||
assert_se(buf);
|
||||
ASSERT_NOT_NULL(buf);
|
||||
|
||||
if (streq(type, "zeros"))
|
||||
switch (type) {
|
||||
|
||||
case BENCHMARK_DATA_ZEROS:
|
||||
memzero(buf, count);
|
||||
else if (streq(type, "simple"))
|
||||
for (i = 0; i < count; i++)
|
||||
break;
|
||||
|
||||
case BENCHMARK_DATA_SIMPLE:
|
||||
for (size_t i = 0; i < count; i++)
|
||||
buf[i] = 'a' + i % ('z' - 'a' + 1);
|
||||
else if (streq(type, "random")) {
|
||||
break;
|
||||
|
||||
case BENCHMARK_DATA_RANDOM: {
|
||||
size_t step = count / 10;
|
||||
|
||||
random_bytes(buf, step);
|
||||
@@ -64,110 +78,103 @@ static char* make_buf(size_t count, const char *type) {
|
||||
memzero(buf + 7*step, step);
|
||||
random_bytes(buf + 8*step, step);
|
||||
memzero(buf + 9*step, step);
|
||||
} else
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
assert_not_reached();
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
static void test_compress_decompress(const char* label, const char* type,
|
||||
compress_t compress, decompress_t decompress) {
|
||||
usec_t n, n2 = 0;
|
||||
float dt;
|
||||
TEST(benchmark) {
|
||||
for (BenchmarkDataType dt = 0; dt < _BENCHMARK_DATA_TYPE_MAX; dt++)
|
||||
for (Compression c = 0; c < _COMPRESSION_MAX; c++) {
|
||||
if (c == COMPRESSION_NONE || !compression_supported(c))
|
||||
continue;
|
||||
|
||||
_cleanup_free_ char *text = NULL, *buf = NULL;
|
||||
_cleanup_free_ void *buf2 = NULL;
|
||||
size_t skipped = 0, compressed = 0, total = 0;
|
||||
const char *label = compression_to_string(c);
|
||||
const char *type = benchmark_data_type_to_string(dt);
|
||||
usec_t n, n2 = 0;
|
||||
|
||||
text = make_buf(MAX_SIZE, type);
|
||||
buf = calloc(MAX_SIZE + 1, 1);
|
||||
assert_se(text && buf);
|
||||
_cleanup_free_ char *text = NULL, *buf = NULL;
|
||||
_cleanup_free_ void *buf2 = NULL;
|
||||
size_t skipped = 0, compressed = 0, total = 0;
|
||||
|
||||
n = now(CLOCK_MONOTONIC);
|
||||
text = make_buf(MAX_SIZE, dt);
|
||||
buf = calloc(MAX_SIZE + 1, 1);
|
||||
ASSERT_NOT_NULL(text);
|
||||
ASSERT_NOT_NULL(buf);
|
||||
|
||||
for (size_t i = 0; i <= MAX_SIZE; i++) {
|
||||
size_t j = 0, k = 0, size;
|
||||
int r;
|
||||
n = now(CLOCK_MONOTONIC);
|
||||
|
||||
size = permute(i);
|
||||
if (size == 0)
|
||||
continue;
|
||||
for (size_t i = 0; i <= MAX_SIZE; i++) {
|
||||
size_t j = 0, k = 0, size;
|
||||
int r;
|
||||
|
||||
log_debug("%s %zu %zu", type, i, size);
|
||||
size = permute(i);
|
||||
if (size == 0)
|
||||
continue;
|
||||
|
||||
memzero(buf, MIN(size + 1000, MAX_SIZE));
|
||||
log_debug("%s %zu %zu", type, i, size);
|
||||
|
||||
r = compress(text, size, buf, size, &j, /* level= */ -1);
|
||||
/* assume compression must be successful except for small or random inputs */
|
||||
assert_se(r >= 0 || (size < 2048 && r == -ENOBUFS) || streq(type, "random"));
|
||||
memzero(buf, MIN(size + 1000, MAX_SIZE));
|
||||
|
||||
/* check for overwrites */
|
||||
assert_se(buf[size] == 0);
|
||||
if (r < 0) {
|
||||
skipped += size;
|
||||
continue;
|
||||
r = compress_blob(c, text, size, buf, size, &j, /* level= */ -1);
|
||||
/* assume compression must be successful except for small or random inputs */
|
||||
ASSERT_TRUE(r >= 0 || (size < 2048 && r == -ENOBUFS) || dt == BENCHMARK_DATA_RANDOM);
|
||||
|
||||
/* check for overwrites */
|
||||
ASSERT_EQ(buf[size], 0);
|
||||
if (r < 0) {
|
||||
skipped += size;
|
||||
continue;
|
||||
}
|
||||
|
||||
ASSERT_TRUE(j > 0);
|
||||
if (j >= size)
|
||||
log_error("%s \"compressed\" %zu -> %zu", label, size, j);
|
||||
|
||||
ASSERT_OK_ZERO(decompress_blob(c, buf, j, &buf2, &k, 0));
|
||||
ASSERT_EQ(k, size);
|
||||
ASSERT_EQ(memcmp(text, buf2, size), 0);
|
||||
|
||||
total += size;
|
||||
compressed += j;
|
||||
|
||||
n2 = now(CLOCK_MONOTONIC);
|
||||
if (n2 - n > arg_duration)
|
||||
break;
|
||||
}
|
||||
|
||||
float elapsed = (n2-n) / 1e6;
|
||||
|
||||
log_info("%s/%s: compressed & decompressed %zu bytes in %.2fs (%.2fMiB/s), "
|
||||
"mean compression %.2f%%, skipped %zu bytes",
|
||||
label, type, total, elapsed,
|
||||
total / 1024. / 1024 / elapsed,
|
||||
100 - compressed * 100. / total,
|
||||
skipped);
|
||||
}
|
||||
|
||||
assert_se(j > 0);
|
||||
if (j >= size)
|
||||
log_error("%s \"compressed\" %zu -> %zu", label, size, j);
|
||||
|
||||
r = decompress(buf, j, &buf2, &k, 0);
|
||||
assert_se(r == 0);
|
||||
assert_se(k == size);
|
||||
|
||||
assert_se(memcmp(text, buf2, size) == 0);
|
||||
|
||||
total += size;
|
||||
compressed += j;
|
||||
|
||||
n2 = now(CLOCK_MONOTONIC);
|
||||
if (n2 - n > arg_duration)
|
||||
break;
|
||||
}
|
||||
|
||||
dt = (n2-n) / 1e6;
|
||||
|
||||
log_info("%s/%s: compressed & decompressed %zu bytes in %.2fs (%.2fMiB/s), "
|
||||
"mean compression %.2f%%, skipped %zu bytes",
|
||||
label, type, total, dt,
|
||||
total / 1024. / 1024 / dt,
|
||||
100 - compressed * 100. / total,
|
||||
skipped);
|
||||
}
|
||||
#endif
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
#if HAVE_COMPRESSION
|
||||
test_setup_logging(LOG_INFO);
|
||||
|
||||
if (argc >= 2) {
|
||||
static int intro(void) {
|
||||
if (saved_argc >= 2) {
|
||||
unsigned x;
|
||||
|
||||
assert_se(safe_atou(argv[1], &x) >= 0);
|
||||
ASSERT_OK(safe_atou(saved_argv[1], &x));
|
||||
arg_duration = x * USEC_PER_SEC;
|
||||
} else
|
||||
arg_duration = slow_tests_enabled() ?
|
||||
2 * USEC_PER_SEC : USEC_PER_SEC / 50;
|
||||
|
||||
if (argc == 3)
|
||||
(void) safe_atozu(argv[2], &arg_start);
|
||||
if (saved_argc == 3)
|
||||
(void) safe_atozu(saved_argv[2], &arg_start);
|
||||
else
|
||||
arg_start = getpid_cached();
|
||||
|
||||
NULSTR_FOREACH(i, "zeros\0simple\0random\0") {
|
||||
#if HAVE_XZ
|
||||
test_compress_decompress("XZ", i, compress_blob_xz, decompress_blob_xz);
|
||||
#endif
|
||||
#if HAVE_LZ4
|
||||
test_compress_decompress("LZ4", i, compress_blob_lz4, decompress_blob_lz4);
|
||||
#endif
|
||||
#if HAVE_ZSTD
|
||||
test_compress_decompress("ZSTD", i, compress_blob_zstd, decompress_blob_zstd);
|
||||
#endif
|
||||
}
|
||||
return 0;
|
||||
#else
|
||||
return log_tests_skipped("No compression feature is enabled");
|
||||
#endif
|
||||
}
|
||||
|
||||
DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);
|
||||
|
||||
+399
-464
@@ -4,13 +4,9 @@
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#if HAVE_LZ4
|
||||
#include <lz4.h>
|
||||
#endif
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "argv-util.h"
|
||||
#include "compress.h"
|
||||
#include "dlfcn-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "io-util.h"
|
||||
#include "path-util.h"
|
||||
@@ -18,516 +14,455 @@
|
||||
#include "tests.h"
|
||||
#include "tmpfile-util.h"
|
||||
|
||||
#if HAVE_XZ
|
||||
# define XZ_OK 0
|
||||
#else
|
||||
# define XZ_OK -EPROTONOSUPPORT
|
||||
#endif
|
||||
|
||||
#if HAVE_LZ4
|
||||
# define LZ4_OK 0
|
||||
#else
|
||||
# define LZ4_OK -EPROTONOSUPPORT
|
||||
#endif
|
||||
|
||||
#define HUGE_SIZE (4096*1024)
|
||||
|
||||
typedef int (compress_blob_t)(const void *src, uint64_t src_size,
|
||||
void *dst, size_t dst_alloc_size, size_t *dst_size, int level);
|
||||
typedef int (decompress_blob_t)(const void *src, uint64_t src_size,
|
||||
void **dst,
|
||||
size_t* dst_size, size_t dst_max);
|
||||
typedef int (decompress_sw_t)(const void *src, uint64_t src_size,
|
||||
void **buffer,
|
||||
const void *prefix, size_t prefix_len,
|
||||
uint8_t extra);
|
||||
static const char text[] =
|
||||
"text\0foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF"
|
||||
"foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF";
|
||||
static char data[512] = "random\0";
|
||||
static char *huge = NULL;
|
||||
static const char *srcfile;
|
||||
|
||||
typedef int (compress_stream_t)(int fdf, int fdt, uint64_t max_bytes, uint64_t *uncompressed_size);
|
||||
typedef int (decompress_stream_t)(int fdf, int fdt, uint64_t max_size);
|
||||
|
||||
#if HAVE_COMPRESSION
|
||||
_unused_ static void test_compress_decompress(
|
||||
const char *compression,
|
||||
compress_blob_t compress,
|
||||
decompress_blob_t decompress,
|
||||
const char *data,
|
||||
size_t data_len,
|
||||
bool may_fail) {
|
||||
|
||||
char compressed[512];
|
||||
size_t csize;
|
||||
_cleanup_free_ char *decompressed = NULL;
|
||||
int r;
|
||||
|
||||
log_info("/* testing %s %s blob compression/decompression */",
|
||||
compression, data);
|
||||
|
||||
r = compress(data, data_len, compressed, sizeof(compressed), &csize, /* level= */ -1);
|
||||
if (r == -ENOBUFS) {
|
||||
log_info_errno(r, "compression failed: %m");
|
||||
assert_se(may_fail);
|
||||
} else {
|
||||
assert_se(r >= 0);
|
||||
r = decompress(compressed, csize,
|
||||
(void **) &decompressed, &csize, 0);
|
||||
assert_se(r == 0);
|
||||
assert_se(decompressed);
|
||||
assert_se(memcmp(decompressed, data, data_len) == 0);
|
||||
static const char* cat_for_compression(Compression c) {
|
||||
switch (c) {
|
||||
case COMPRESSION_XZ: return "xzcat";
|
||||
case COMPRESSION_LZ4: return "lz4cat";
|
||||
case COMPRESSION_ZSTD: return "zstdcat";
|
||||
case COMPRESSION_GZIP: return "zcat";
|
||||
case COMPRESSION_BZIP2: return "bzcat";
|
||||
default: return NULL;
|
||||
}
|
||||
|
||||
r = decompress("garbage", 7,
|
||||
(void **) &decompressed, &csize, 0);
|
||||
assert_se(r < 0);
|
||||
|
||||
/* make sure to have the minimal lz4 compressed size */
|
||||
r = decompress("00000000\1g", 9,
|
||||
(void **) &decompressed, &csize, 0);
|
||||
assert_se(r < 0);
|
||||
|
||||
r = decompress("\100000000g", 9,
|
||||
(void **) &decompressed, &csize, 0);
|
||||
assert_se(r < 0);
|
||||
|
||||
explicit_bzero_safe(decompressed, MALLOC_SIZEOF_SAFE(decompressed));
|
||||
}
|
||||
|
||||
_unused_ static void test_decompress_startswith(const char *compression,
|
||||
compress_blob_t compress,
|
||||
decompress_sw_t decompress_sw,
|
||||
const char *data,
|
||||
size_t data_len,
|
||||
bool may_fail) {
|
||||
TEST(compress_decompress_blob) {
|
||||
for (Compression c = 0; c < _COMPRESSION_MAX; c++) {
|
||||
if (c == COMPRESSION_NONE || !compression_supported(c))
|
||||
continue;
|
||||
|
||||
char *compressed;
|
||||
_cleanup_free_ char *compressed1 = NULL, *compressed2 = NULL, *decompressed = NULL;
|
||||
size_t csize, len;
|
||||
int r;
|
||||
const char *label = compression_to_string(c);
|
||||
|
||||
log_info("/* testing decompress_startswith with %s on %.20s text */",
|
||||
compression, data);
|
||||
for (size_t t = 0; t < 2; t++) {
|
||||
const char *input = t == 0 ? text : data;
|
||||
size_t input_len = t == 0 ? sizeof(text) : sizeof(data);
|
||||
bool may_fail = t == 1;
|
||||
|
||||
#define BUFSIZE_1 512
|
||||
#define BUFSIZE_2 20000
|
||||
char compressed[512];
|
||||
size_t csize;
|
||||
_cleanup_free_ char *decompressed = NULL;
|
||||
int r;
|
||||
|
||||
compressed = compressed1 = malloc(BUFSIZE_1);
|
||||
assert_se(compressed1);
|
||||
r = compress(data, data_len, compressed, BUFSIZE_1, &csize, /* level= */ -1);
|
||||
if (r == -ENOBUFS) {
|
||||
log_info_errno(r, "compression failed: %m");
|
||||
assert_se(may_fail);
|
||||
log_info("/* testing %s %s blob compression/decompression */", label, input);
|
||||
|
||||
compressed = compressed2 = malloc(BUFSIZE_2);
|
||||
assert_se(compressed2);
|
||||
r = compress(data, data_len, compressed, BUFSIZE_2, &csize, /* level= */ -1);
|
||||
r = compress_blob(c, input, input_len, compressed, sizeof(compressed), &csize, -1);
|
||||
if (r == -ENOBUFS) {
|
||||
log_info_errno(r, "compression failed: %m");
|
||||
ASSERT_TRUE(may_fail);
|
||||
} else {
|
||||
ASSERT_OK(r);
|
||||
ASSERT_OK_ZERO(decompress_blob(c, compressed, csize, (void **) &decompressed, &csize, 0));
|
||||
ASSERT_NOT_NULL(decompressed);
|
||||
ASSERT_EQ(memcmp(decompressed, input, input_len), 0);
|
||||
}
|
||||
|
||||
ASSERT_FAIL(decompress_blob(c, "garbage", 7, (void **) &decompressed, &csize, 0));
|
||||
}
|
||||
}
|
||||
assert_se(r >= 0);
|
||||
|
||||
len = strlen(data);
|
||||
|
||||
r = decompress_sw(compressed, csize, (void **) &decompressed, data, len, '\0');
|
||||
assert_se(r > 0);
|
||||
r = decompress_sw(compressed, csize, (void **) &decompressed, data, len, 'w');
|
||||
assert_se(r == 0);
|
||||
r = decompress_sw(compressed, csize, (void **) &decompressed, "barbarbar", 9, ' ');
|
||||
assert_se(r == 0);
|
||||
r = decompress_sw(compressed, csize, (void **) &decompressed, data, len - 1, data[len-1]);
|
||||
assert_se(r > 0);
|
||||
r = decompress_sw(compressed, csize, (void **) &decompressed, data, len - 1, 'w');
|
||||
assert_se(r == 0);
|
||||
r = decompress_sw(compressed, csize, (void **) &decompressed, data, len, '\0');
|
||||
assert_se(r > 0);
|
||||
}
|
||||
|
||||
_unused_ static void test_decompress_startswith_short(const char *compression,
|
||||
compress_blob_t compress,
|
||||
decompress_sw_t decompress_sw) {
|
||||
TEST(decompress_startswith) {
|
||||
for (Compression c = 0; c < _COMPRESSION_MAX; c++) {
|
||||
if (c == COMPRESSION_NONE || !compression_supported(c))
|
||||
continue;
|
||||
|
||||
const char *label = compression_to_string(c);
|
||||
|
||||
struct { const char *buf; size_t len; bool may_fail; } inputs[] = {
|
||||
{ text, sizeof(text), false },
|
||||
{ data, sizeof(data), true },
|
||||
{ huge, HUGE_SIZE, true },
|
||||
};
|
||||
|
||||
for (size_t t = 0; t < ELEMENTSOF(inputs); t++) {
|
||||
char *compressed;
|
||||
_cleanup_free_ char *compressed1 = NULL, *compressed2 = NULL, *decompressed = NULL;
|
||||
size_t csize, len;
|
||||
int r;
|
||||
|
||||
log_info("/* testing decompress_startswith with %s on %.20s */", label, inputs[t].buf);
|
||||
|
||||
compressed = compressed1 = malloc(512);
|
||||
ASSERT_NOT_NULL(compressed1);
|
||||
r = compress_blob(c, inputs[t].buf, inputs[t].len, compressed, 512, &csize, -1);
|
||||
if (r == -ENOBUFS) {
|
||||
log_info_errno(r, "compression failed: %m");
|
||||
ASSERT_TRUE(inputs[t].may_fail);
|
||||
|
||||
compressed = compressed2 = malloc(20000);
|
||||
ASSERT_NOT_NULL(compressed2);
|
||||
r = compress_blob(c, inputs[t].buf, inputs[t].len, compressed, 20000, &csize, -1);
|
||||
}
|
||||
if (r == -ENOBUFS) {
|
||||
log_info_errno(r, "compression failed again: %m");
|
||||
ASSERT_TRUE(inputs[t].may_fail);
|
||||
continue;
|
||||
}
|
||||
ASSERT_OK(r);
|
||||
|
||||
len = strlen(inputs[t].buf);
|
||||
|
||||
ASSERT_OK_POSITIVE(decompress_startswith(c, compressed, csize, (void **) &decompressed, inputs[t].buf, len, '\0'));
|
||||
ASSERT_OK_ZERO(decompress_startswith(c, compressed, csize, (void **) &decompressed, inputs[t].buf, len, 'w'));
|
||||
ASSERT_OK_POSITIVE(decompress_startswith(c, compressed, csize, (void **) &decompressed, inputs[t].buf, len - 1, inputs[t].buf[len-1]));
|
||||
ASSERT_OK_ZERO(decompress_startswith(c, compressed, csize, (void **) &decompressed, inputs[t].buf, len - 1, 'w'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(decompress_startswith_large) {
|
||||
/* Test decompress_startswith with large data to exercise the buffer growth path. */
|
||||
|
||||
_cleanup_free_ char *large = NULL;
|
||||
size_t large_size = 8 * 1024;
|
||||
|
||||
ASSERT_NOT_NULL(large = malloc(large_size));
|
||||
for (size_t i = 0; i < large_size; i++)
|
||||
large[i] = 'A' + (i % 26);
|
||||
|
||||
for (Compression c = 0; c < _COMPRESSION_MAX; c++) {
|
||||
if (c == COMPRESSION_NONE || !compression_supported(c))
|
||||
continue;
|
||||
|
||||
_cleanup_free_ char *compressed = NULL;
|
||||
size_t csize;
|
||||
|
||||
log_info("/* decompress_startswith_large with %s */", compression_to_string(c));
|
||||
|
||||
ASSERT_NOT_NULL(compressed = malloc(large_size));
|
||||
int r = compress_blob(c, large, large_size, compressed, large_size, &csize, -1);
|
||||
if (r == -ENOBUFS) {
|
||||
log_info_errno(r, "compression failed: %m");
|
||||
continue;
|
||||
}
|
||||
ASSERT_OK(r);
|
||||
|
||||
_cleanup_free_ void *buf = NULL;
|
||||
|
||||
ASSERT_OK_POSITIVE(decompress_startswith(c, compressed, csize, &buf, large, 1, large[1]));
|
||||
ASSERT_OK_ZERO(decompress_startswith(c, compressed, csize, &buf, large, 1, 0xff));
|
||||
ASSERT_OK_POSITIVE(decompress_startswith(c, compressed, csize, &buf, large, 512, large[512]));
|
||||
ASSERT_OK_ZERO(decompress_startswith(c, compressed, csize, &buf, large, 512, 0xff));
|
||||
ASSERT_OK_POSITIVE(decompress_startswith(c, compressed, csize, &buf, large, 4096, large[4096]));
|
||||
ASSERT_OK_ZERO(decompress_startswith(c, compressed, csize, &buf, large, 4096, 0xff));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(decompress_startswith_short) {
|
||||
#define TEXT "HUGE=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||
|
||||
char buf[1024];
|
||||
size_t csize;
|
||||
int r;
|
||||
for (Compression c = 0; c < _COMPRESSION_MAX; c++) {
|
||||
if (c == COMPRESSION_NONE || !compression_supported(c))
|
||||
continue;
|
||||
|
||||
log_info("/* %s with %s */", __func__, compression);
|
||||
char buf[1024];
|
||||
size_t csize;
|
||||
|
||||
r = compress(TEXT, sizeof TEXT, buf, sizeof buf, &csize, /* level= */ -1);
|
||||
assert_se(r >= 0);
|
||||
log_info("/* decompress_startswith_short with %s */", compression_to_string(c));
|
||||
|
||||
for (size_t i = 1; i < strlen(TEXT); i++) {
|
||||
_cleanup_free_ void *buf2 = NULL;
|
||||
ASSERT_OK(compress_blob(c, TEXT, sizeof TEXT, buf, sizeof buf, &csize, -1));
|
||||
|
||||
assert_se(buf2 = malloc(i));
|
||||
for (size_t i = 1; i < strlen(TEXT); i++) {
|
||||
_cleanup_free_ void *buf2 = NULL;
|
||||
|
||||
assert_se(decompress_sw(buf, csize, &buf2, TEXT, i, TEXT[i]) == 1);
|
||||
assert_se(decompress_sw(buf, csize, &buf2, TEXT, i, 'y') == 0);
|
||||
ASSERT_NOT_NULL(buf2 = malloc(i));
|
||||
|
||||
ASSERT_OK_POSITIVE(decompress_startswith(c, buf, csize, &buf2, TEXT, i, TEXT[i]));
|
||||
ASSERT_OK_ZERO(decompress_startswith(c, buf, csize, &buf2, TEXT, i, 'y'));
|
||||
}
|
||||
}
|
||||
#undef TEXT
|
||||
}
|
||||
|
||||
_unused_ static void test_compress_stream(const char *compression,
|
||||
const char *cat,
|
||||
compress_stream_t compress,
|
||||
decompress_stream_t decompress,
|
||||
const char *srcfile) {
|
||||
TEST(compress_decompress_stream) {
|
||||
for (Compression c = 0; c < _COMPRESSION_MAX; c++) {
|
||||
if (c == COMPRESSION_NONE || !compression_supported(c))
|
||||
continue;
|
||||
|
||||
_cleanup_close_ int src = -EBADF, dst = -EBADF, dst2 = -EBADF;
|
||||
_cleanup_(unlink_tempfilep) char
|
||||
pattern[] = "/tmp/systemd-test.compressed.XXXXXX",
|
||||
pattern2[] = "/tmp/systemd-test.compressed.XXXXXX";
|
||||
int r;
|
||||
_cleanup_free_ char *cmd = NULL, *cmd2 = NULL;
|
||||
struct stat st = {};
|
||||
uint64_t uncompressed_size;
|
||||
const char *cat = cat_for_compression(c);
|
||||
if (!cat)
|
||||
continue;
|
||||
|
||||
r = find_executable(cat, NULL);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Skipping %s, could not find %s binary: %m", __func__, cat);
|
||||
return;
|
||||
}
|
||||
int r = find_executable(cat, NULL);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Skipping %s, could not find %s binary: %m",
|
||||
compression_to_string(c), cat);
|
||||
continue;
|
||||
}
|
||||
|
||||
log_debug("/* testing %s compression */", compression);
|
||||
|
||||
log_debug("/* create source from %s */", srcfile);
|
||||
|
||||
ASSERT_OK(src = open(srcfile, O_RDONLY|O_CLOEXEC));
|
||||
|
||||
log_debug("/* test compression */");
|
||||
|
||||
assert_se((dst = mkostemp_safe(pattern)) >= 0);
|
||||
|
||||
ASSERT_OK(compress(src, dst, -1, &uncompressed_size));
|
||||
|
||||
if (cat) {
|
||||
assert_se(asprintf(&cmd, "%s %s | diff '%s' -", cat, pattern, srcfile) > 0);
|
||||
assert_se(system(cmd) == 0);
|
||||
}
|
||||
|
||||
log_debug("/* test decompression */");
|
||||
|
||||
assert_se((dst2 = mkostemp_safe(pattern2)) >= 0);
|
||||
|
||||
assert_se(stat(srcfile, &st) == 0);
|
||||
assert_se((uint64_t)st.st_size == uncompressed_size);
|
||||
|
||||
assert_se(lseek(dst, 0, SEEK_SET) == 0);
|
||||
r = decompress(dst, dst2, st.st_size);
|
||||
assert_se(r == 0);
|
||||
|
||||
assert_se(asprintf(&cmd2, "diff '%s' %s", srcfile, pattern2) > 0);
|
||||
assert_se(system(cmd2) == 0);
|
||||
|
||||
log_debug("/* test faulty decompression */");
|
||||
|
||||
assert_se(lseek(dst, 1, SEEK_SET) == 1);
|
||||
r = decompress(dst, dst2, st.st_size);
|
||||
assert_se(IN_SET(r, 0, -EBADMSG));
|
||||
|
||||
assert_se(lseek(dst, 0, SEEK_SET) == 0);
|
||||
assert_se(lseek(dst2, 0, SEEK_SET) == 0);
|
||||
r = decompress(dst, dst2, st.st_size - 1);
|
||||
assert_se(r == -EFBIG);
|
||||
}
|
||||
|
||||
_unused_ static void test_decompress_stream_sparse(const char *compression,
|
||||
compress_stream_t compress,
|
||||
decompress_stream_t decompress) {
|
||||
|
||||
_cleanup_close_ int src = -EBADF, compressed = -EBADF, decompressed = -EBADF;
|
||||
_cleanup_(unlink_tempfilep) char
|
||||
pattern_src[] = "/tmp/systemd-test.sparse-src.XXXXXX",
|
||||
pattern_compressed[] = "/tmp/systemd-test.sparse-compressed.XXXXXX",
|
||||
pattern_decompressed[] = "/tmp/systemd-test.sparse-decompressed.XXXXXX";
|
||||
/* Create a sparse-like input: 4K of data, 64K of zeros, 4K of data, 64K trailing zeros.
|
||||
* Total apparent size: 136K, but most of it is zeros. */
|
||||
uint8_t data_block[4096];
|
||||
struct stat st_src, st_decompressed;
|
||||
uint64_t uncompressed_size;
|
||||
int r;
|
||||
|
||||
assert(compression);
|
||||
|
||||
log_debug("/* testing %s sparse decompression */", compression);
|
||||
|
||||
random_bytes(data_block, sizeof(data_block));
|
||||
|
||||
assert_se((src = mkostemp_safe(pattern_src)) >= 0);
|
||||
|
||||
/* Write: 4K data, 64K zeros, 4K data, 64K zeros */
|
||||
assert_se(loop_write(src, data_block, sizeof(data_block)) >= 0);
|
||||
assert_se(ftruncate(src, sizeof(data_block) + 65536) >= 0);
|
||||
assert_se(lseek(src, sizeof(data_block) + 65536, SEEK_SET) >= 0);
|
||||
assert_se(loop_write(src, data_block, sizeof(data_block)) >= 0);
|
||||
assert_se(ftruncate(src, 2 * sizeof(data_block) + 2 * 65536) >= 0);
|
||||
assert_se(lseek(src, 0, SEEK_SET) == 0);
|
||||
|
||||
assert_se(fstat(src, &st_src) >= 0);
|
||||
assert_se(st_src.st_size == 2 * (off_t) sizeof(data_block) + 2 * 65536);
|
||||
|
||||
/* Compress */
|
||||
assert_se((compressed = mkostemp_safe(pattern_compressed)) >= 0);
|
||||
ASSERT_OK(compress(src, compressed, -1, &uncompressed_size));
|
||||
assert_se((uint64_t) st_src.st_size == uncompressed_size);
|
||||
|
||||
/* Decompress to a regular file (sparse writes auto-detected) */
|
||||
assert_se((decompressed = mkostemp_safe(pattern_decompressed)) >= 0);
|
||||
assert_se(lseek(compressed, 0, SEEK_SET) == 0);
|
||||
r = decompress(compressed, decompressed, st_src.st_size);
|
||||
assert_se(r == 0);
|
||||
|
||||
/* Verify apparent size matches */
|
||||
assert_se(fstat(decompressed, &st_decompressed) >= 0);
|
||||
assert_se(st_decompressed.st_size == st_src.st_size);
|
||||
|
||||
/* Verify content matches by comparing bytes */
|
||||
assert_se(lseek(src, 0, SEEK_SET) == 0);
|
||||
assert_se(lseek(decompressed, 0, SEEK_SET) == 0);
|
||||
|
||||
for (off_t offset = 0; offset < st_src.st_size;) {
|
||||
uint8_t buf_src[4096], buf_dst[4096];
|
||||
size_t to_read = MIN((size_t) (st_src.st_size - offset), sizeof(buf_src));
|
||||
ssize_t n;
|
||||
|
||||
n = loop_read(src, buf_src, to_read, true);
|
||||
assert_se(n == (ssize_t) to_read);
|
||||
n = loop_read(decompressed, buf_dst, to_read, true);
|
||||
assert_se(n == (ssize_t) to_read);
|
||||
assert_se(memcmp(buf_src, buf_dst, to_read) == 0);
|
||||
offset += to_read;
|
||||
}
|
||||
|
||||
/* Verify the decompressed file is actually sparse (uses less disk than apparent size).
|
||||
* st_blocks is in 512-byte units. The file has 128K of zeros, so disk usage should be
|
||||
* noticeably less than the apparent size if sparse writes worked.
|
||||
* Only assert if the filesystem supports holes (SEEK_HOLE). */
|
||||
log_debug("%s sparse decompression: apparent=%jd disk=%jd",
|
||||
compression,
|
||||
(intmax_t) st_decompressed.st_size,
|
||||
(intmax_t) st_decompressed.st_blocks * 512);
|
||||
if (lseek(decompressed, 0, SEEK_HOLE) < st_decompressed.st_size)
|
||||
assert_se(st_decompressed.st_blocks * 512 < st_decompressed.st_size);
|
||||
else
|
||||
log_debug("Filesystem does not support holes, skipping sparsity check");
|
||||
|
||||
/* Test all-zeros input: entire output should be a hole */
|
||||
log_debug("/* testing %s sparse decompression of all-zeros */", compression);
|
||||
{
|
||||
_cleanup_close_ int zsrc = -EBADF, zcompressed = -EBADF, zdecompressed = -EBADF;
|
||||
_cleanup_close_ int src = -EBADF, dst = -EBADF, dst2 = -EBADF;
|
||||
_cleanup_(unlink_tempfilep) char
|
||||
zp_src[] = "/tmp/systemd-test.sparse-zero-src.XXXXXX",
|
||||
zp_compressed[] = "/tmp/systemd-test.sparse-zero-compressed.XXXXXX",
|
||||
zp_decompressed[] = "/tmp/systemd-test.sparse-zero-decompressed.XXXXXX";
|
||||
struct stat zst;
|
||||
uint64_t zsize;
|
||||
uint8_t zeros[65536] = {};
|
||||
pattern[] = "/tmp/systemd-test.compressed.XXXXXX",
|
||||
pattern2[] = "/tmp/systemd-test.compressed.XXXXXX";
|
||||
_cleanup_free_ char *cmd = NULL, *cmd2 = NULL;
|
||||
struct stat st = {};
|
||||
uint64_t uncompressed_size;
|
||||
|
||||
assert_se((zsrc = mkostemp_safe(zp_src)) >= 0);
|
||||
assert_se(loop_write(zsrc, zeros, sizeof(zeros)) >= 0);
|
||||
assert_se(lseek(zsrc, 0, SEEK_SET) == 0);
|
||||
log_debug("/* testing %s stream compression */", compression_to_string(c));
|
||||
|
||||
assert_se((zcompressed = mkostemp_safe(zp_compressed)) >= 0);
|
||||
ASSERT_OK(compress(zsrc, zcompressed, -1, &zsize));
|
||||
assert_se(zsize == sizeof(zeros));
|
||||
ASSERT_OK(src = open(srcfile, O_RDONLY|O_CLOEXEC));
|
||||
ASSERT_OK(dst = mkostemp_safe(pattern));
|
||||
|
||||
assert_se((zdecompressed = mkostemp_safe(zp_decompressed)) >= 0);
|
||||
assert_se(lseek(zcompressed, 0, SEEK_SET) == 0);
|
||||
assert_se(decompress(zcompressed, zdecompressed, sizeof(zeros)) == 0);
|
||||
ASSERT_OK(compress_stream(c, src, dst, -1, &uncompressed_size));
|
||||
|
||||
assert_se(fstat(zdecompressed, &zst) >= 0);
|
||||
assert_se(zst.st_size == (off_t) sizeof(zeros));
|
||||
/* All zeros — disk usage should be minimal */
|
||||
log_debug("%s all-zeros sparse: apparent=%jd disk=%jd",
|
||||
compression, (intmax_t) zst.st_size, (intmax_t) zst.st_blocks * 512);
|
||||
if (lseek(zdecompressed, 0, SEEK_HOLE) < zst.st_size)
|
||||
assert_se(zst.st_blocks * 512 < zst.st_size);
|
||||
ASSERT_OK_POSITIVE(asprintf(&cmd, "%s %s | diff '%s' -", cat, pattern, srcfile));
|
||||
ASSERT_OK_ZERO(system(cmd));
|
||||
|
||||
ASSERT_OK(dst2 = mkostemp_safe(pattern2));
|
||||
|
||||
ASSERT_OK_ZERO_ERRNO(stat(srcfile, &st));
|
||||
ASSERT_EQ((uint64_t) st.st_size, uncompressed_size);
|
||||
|
||||
ASSERT_OK_ERRNO(lseek(dst, 0, SEEK_SET));
|
||||
ASSERT_OK_ZERO(decompress_stream(c, dst, dst2, st.st_size));
|
||||
|
||||
ASSERT_OK_POSITIVE(asprintf(&cmd2, "diff '%s' %s", srcfile, pattern2));
|
||||
ASSERT_OK_ZERO(system(cmd2));
|
||||
|
||||
log_debug("/* test faulty decompression */");
|
||||
|
||||
ASSERT_OK_ERRNO(lseek(dst, 1, SEEK_SET));
|
||||
r = decompress_stream(c, dst, dst2, st.st_size);
|
||||
ASSERT_TRUE(IN_SET(r, 0, -EBADMSG));
|
||||
|
||||
ASSERT_OK_ERRNO(lseek(dst, 0, SEEK_SET));
|
||||
ASSERT_OK_ERRNO(lseek(dst2, 0, SEEK_SET));
|
||||
ASSERT_ERROR(decompress_stream(c, dst, dst2, st.st_size - 1), EFBIG);
|
||||
}
|
||||
}
|
||||
|
||||
struct decompressor_test_data {
|
||||
uint8_t *buf;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
static int test_decompressor_callback(const void *p, size_t size, void *userdata) {
|
||||
struct decompressor_test_data *d = ASSERT_PTR(userdata);
|
||||
|
||||
if (!GREEDY_REALLOC(d->buf, d->size + size))
|
||||
return -ENOMEM;
|
||||
|
||||
memcpy(d->buf + d->size, p, size);
|
||||
d->size += size;
|
||||
return 0;
|
||||
}
|
||||
|
||||
TEST(decompress_stream_sparse) {
|
||||
for (Compression c = 0; c < _COMPRESSION_MAX; c++) {
|
||||
if (c == COMPRESSION_NONE || !compression_supported(c))
|
||||
continue;
|
||||
|
||||
_cleanup_close_ int src = -EBADF, compressed = -EBADF, decompressed = -EBADF;
|
||||
_cleanup_(unlink_tempfilep) char
|
||||
pattern_src[] = "/tmp/systemd-test.sparse-src.XXXXXX",
|
||||
pattern_compressed[] = "/tmp/systemd-test.sparse-compressed.XXXXXX",
|
||||
pattern_decompressed[] = "/tmp/systemd-test.sparse-decompressed.XXXXXX";
|
||||
/* Create a sparse-like input: 4K of data, 64K of zeros, 4K of data, 64K trailing zeros.
|
||||
* Total apparent size: 136K, but most of it is zeros. */
|
||||
uint8_t data_block[4096];
|
||||
struct stat st_src, st_decompressed;
|
||||
uint64_t uncompressed_size;
|
||||
|
||||
log_debug("/* testing %s sparse decompression */", compression_to_string(c));
|
||||
|
||||
random_bytes(data_block, sizeof(data_block));
|
||||
|
||||
ASSERT_OK(src = mkostemp_safe(pattern_src));
|
||||
|
||||
/* Write: 4K data, 64K zeros, 4K data, 64K zeros */
|
||||
ASSERT_OK(loop_write(src, data_block, sizeof(data_block)));
|
||||
ASSERT_OK_ERRNO(ftruncate(src, sizeof(data_block) + 65536));
|
||||
ASSERT_OK_ERRNO(lseek(src, sizeof(data_block) + 65536, SEEK_SET));
|
||||
ASSERT_OK(loop_write(src, data_block, sizeof(data_block)));
|
||||
ASSERT_OK_ERRNO(ftruncate(src, 2 * sizeof(data_block) + 2 * 65536));
|
||||
ASSERT_EQ(lseek(src, 0, SEEK_SET), (off_t) 0);
|
||||
|
||||
ASSERT_OK_ERRNO(fstat(src, &st_src));
|
||||
ASSERT_EQ(st_src.st_size, 2 * (off_t) sizeof(data_block) + 2 * 65536);
|
||||
|
||||
/* Compress */
|
||||
ASSERT_OK(compressed = mkostemp_safe(pattern_compressed));
|
||||
ASSERT_OK(compress_stream(c, src, compressed, -1, &uncompressed_size));
|
||||
ASSERT_EQ((uint64_t) st_src.st_size, uncompressed_size);
|
||||
|
||||
/* Decompress to a regular file (sparse writes auto-detected) */
|
||||
ASSERT_OK(decompressed = mkostemp_safe(pattern_decompressed));
|
||||
ASSERT_EQ(lseek(compressed, 0, SEEK_SET), (off_t) 0);
|
||||
ASSERT_OK_ZERO(decompress_stream(c, compressed, decompressed, st_src.st_size));
|
||||
|
||||
/* Verify apparent size matches */
|
||||
ASSERT_OK_ERRNO(fstat(decompressed, &st_decompressed));
|
||||
ASSERT_EQ(st_decompressed.st_size, st_src.st_size);
|
||||
|
||||
/* Verify content matches by comparing bytes */
|
||||
ASSERT_EQ(lseek(src, 0, SEEK_SET), (off_t) 0);
|
||||
ASSERT_EQ(lseek(decompressed, 0, SEEK_SET), (off_t) 0);
|
||||
|
||||
for (off_t offset = 0; offset < st_src.st_size;) {
|
||||
uint8_t buf_src[4096], buf_dst[4096];
|
||||
size_t to_read = MIN((size_t) (st_src.st_size - offset), sizeof(buf_src));
|
||||
|
||||
ASSERT_EQ(loop_read(src, buf_src, to_read, true), (ssize_t) to_read);
|
||||
ASSERT_EQ(loop_read(decompressed, buf_dst, to_read, true), (ssize_t) to_read);
|
||||
ASSERT_EQ(memcmp(buf_src, buf_dst, to_read), 0);
|
||||
offset += to_read;
|
||||
}
|
||||
|
||||
/* Verify the decompressed file is actually sparse (uses less disk than apparent size).
|
||||
* st_blocks is in 512-byte units. The file has 128K of zeros, so disk usage should be
|
||||
* noticeably less than the apparent size if sparse writes worked.
|
||||
* Only assert if the filesystem supports holes (SEEK_HOLE). */
|
||||
log_debug("%s sparse decompression: apparent=%jd disk=%jd",
|
||||
compression_to_string(c),
|
||||
(intmax_t) st_decompressed.st_size,
|
||||
(intmax_t) st_decompressed.st_blocks * 512);
|
||||
if (lseek(decompressed, 0, SEEK_HOLE) < st_decompressed.st_size)
|
||||
ASSERT_LT(st_decompressed.st_blocks * 512, st_decompressed.st_size);
|
||||
else
|
||||
log_debug("Filesystem does not support holes, skipping sparsity check");
|
||||
}
|
||||
|
||||
/* Test data ending with non-zero bytes: ftruncate should be a no-op */
|
||||
log_debug("/* testing %s sparse decompression ending with data */", compression);
|
||||
{
|
||||
_cleanup_close_ int dsrc = -EBADF, dcompressed = -EBADF, ddecompressed = -EBADF;
|
||||
_cleanup_(unlink_tempfilep) char
|
||||
dp_src[] = "/tmp/systemd-test.sparse-end-src.XXXXXX",
|
||||
dp_compressed[] = "/tmp/systemd-test.sparse-end-compressed.XXXXXX",
|
||||
dp_decompressed[] = "/tmp/systemd-test.sparse-end-decompressed.XXXXXX";
|
||||
struct stat dst;
|
||||
uint64_t dsize;
|
||||
uint8_t zeros[65536] = {};
|
||||
/* Test all-zeros input: entire output should be a hole */
|
||||
log_debug("/* testing %s sparse decompression of all-zeros */", compression_to_string(c));
|
||||
{
|
||||
_cleanup_close_ int zsrc = -EBADF, zcompressed = -EBADF, zdecompressed = -EBADF;
|
||||
_cleanup_(unlink_tempfilep) char
|
||||
zp_src[] = "/tmp/systemd-test.sparse-zero-src.XXXXXX",
|
||||
zp_compressed[] = "/tmp/systemd-test.sparse-zero-compressed.XXXXXX",
|
||||
zp_decompressed[] = "/tmp/systemd-test.sparse-zero-decompressed.XXXXXX";
|
||||
struct stat zst;
|
||||
uint64_t zsize;
|
||||
uint8_t zeros[65536] = {};
|
||||
|
||||
/* 64K zeros followed by 4K random data */
|
||||
assert_se((dsrc = mkostemp_safe(dp_src)) >= 0);
|
||||
assert_se(loop_write(dsrc, zeros, sizeof(zeros)) >= 0);
|
||||
assert_se(loop_write(dsrc, data_block, sizeof(data_block)) >= 0);
|
||||
assert_se(lseek(dsrc, 0, SEEK_SET) == 0);
|
||||
ASSERT_OK(zsrc = mkostemp_safe(zp_src));
|
||||
ASSERT_OK(loop_write(zsrc, zeros, sizeof(zeros)));
|
||||
ASSERT_EQ(lseek(zsrc, 0, SEEK_SET), (off_t) 0);
|
||||
|
||||
assert_se((dcompressed = mkostemp_safe(dp_compressed)) >= 0);
|
||||
ASSERT_OK(compress(dsrc, dcompressed, -1, &dsize));
|
||||
assert_se(dsize == sizeof(zeros) + sizeof(data_block));
|
||||
ASSERT_OK(zcompressed = mkostemp_safe(zp_compressed));
|
||||
ASSERT_OK(compress_stream(c, zsrc, zcompressed, -1, &zsize));
|
||||
ASSERT_EQ(zsize, (uint64_t) sizeof(zeros));
|
||||
|
||||
assert_se((ddecompressed = mkostemp_safe(dp_decompressed)) >= 0);
|
||||
assert_se(lseek(dcompressed, 0, SEEK_SET) == 0);
|
||||
assert_se(decompress(dcompressed, ddecompressed, dsize) == 0);
|
||||
ASSERT_OK(zdecompressed = mkostemp_safe(zp_decompressed));
|
||||
ASSERT_EQ(lseek(zcompressed, 0, SEEK_SET), (off_t) 0);
|
||||
ASSERT_OK_ZERO(decompress_stream(c, zcompressed, zdecompressed, sizeof(zeros)));
|
||||
|
||||
assert_se(fstat(ddecompressed, &dst) >= 0);
|
||||
assert_se(dst.st_size == (off_t)(sizeof(zeros) + sizeof(data_block)));
|
||||
ASSERT_OK_ERRNO(fstat(zdecompressed, &zst));
|
||||
ASSERT_EQ(zst.st_size, (off_t) sizeof(zeros));
|
||||
/* All zeros — disk usage should be minimal */
|
||||
log_debug("%s all-zeros sparse: apparent=%jd disk=%jd",
|
||||
compression_to_string(c), (intmax_t) zst.st_size, (intmax_t) zst.st_blocks * 512);
|
||||
if (lseek(zdecompressed, 0, SEEK_HOLE) < zst.st_size)
|
||||
ASSERT_LT(zst.st_blocks * 512, zst.st_size);
|
||||
else
|
||||
log_debug("Filesystem does not support holes, skipping sparsity check");
|
||||
}
|
||||
|
||||
/* Test data ending with non-zero bytes: ftruncate should be a no-op */
|
||||
log_debug("/* testing %s sparse decompression ending with data */", compression_to_string(c));
|
||||
{
|
||||
_cleanup_close_ int dsrc = -EBADF, dcompressed = -EBADF, ddecompressed = -EBADF;
|
||||
_cleanup_(unlink_tempfilep) char
|
||||
dp_src[] = "/tmp/systemd-test.sparse-end-src.XXXXXX",
|
||||
dp_compressed[] = "/tmp/systemd-test.sparse-end-compressed.XXXXXX",
|
||||
dp_decompressed[] = "/tmp/systemd-test.sparse-end-decompressed.XXXXXX";
|
||||
struct stat dst;
|
||||
uint64_t dsize;
|
||||
uint8_t zeros[65536] = {};
|
||||
|
||||
/* 64K zeros followed by 4K random data */
|
||||
ASSERT_OK(dsrc = mkostemp_safe(dp_src));
|
||||
ASSERT_OK(loop_write(dsrc, zeros, sizeof(zeros)));
|
||||
ASSERT_OK(loop_write(dsrc, data_block, sizeof(data_block)));
|
||||
ASSERT_EQ(lseek(dsrc, 0, SEEK_SET), (off_t) 0);
|
||||
|
||||
ASSERT_OK(dcompressed = mkostemp_safe(dp_compressed));
|
||||
ASSERT_OK(compress_stream(c, dsrc, dcompressed, -1, &dsize));
|
||||
ASSERT_EQ(dsize, (uint64_t)(sizeof(zeros) + sizeof(data_block)));
|
||||
|
||||
ASSERT_OK(ddecompressed = mkostemp_safe(dp_decompressed));
|
||||
ASSERT_EQ(lseek(dcompressed, 0, SEEK_SET), (off_t) 0);
|
||||
ASSERT_OK_ZERO(decompress_stream(c, dcompressed, ddecompressed, dsize));
|
||||
|
||||
ASSERT_OK_ERRNO(fstat(ddecompressed, &dst));
|
||||
ASSERT_EQ(dst.st_size, (off_t)(sizeof(zeros) + sizeof(data_block)));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if HAVE_LZ4
|
||||
extern DLSYM_PROTOTYPE(LZ4_compress_default);
|
||||
extern DLSYM_PROTOTYPE(LZ4_decompress_safe);
|
||||
extern DLSYM_PROTOTYPE(LZ4_decompress_safe_partial);
|
||||
extern DLSYM_PROTOTYPE(LZ4_versionNumber);
|
||||
TEST(compressor_decompressor_push_api) {
|
||||
for (Compression c = 0; c < _COMPRESSION_MAX; c++) {
|
||||
if (c == COMPRESSION_NONE || !compression_supported(c))
|
||||
continue;
|
||||
|
||||
static void test_lz4_decompress_partial(void) {
|
||||
char buf[20000], buf2[100];
|
||||
size_t buf_size = sizeof(buf), compressed;
|
||||
int r;
|
||||
_cleanup_free_ char *huge = NULL;
|
||||
log_info("/* testing %s Compressor/Decompressor push API */", compression_to_string(c));
|
||||
|
||||
log_debug("/* %s */", __func__);
|
||||
_cleanup_(compressor_freep) Compressor *compressor = NULL;
|
||||
_cleanup_(compressor_freep) Decompressor *decompressor = NULL;
|
||||
_cleanup_free_ void *compressed = NULL, *finish_buf = NULL;
|
||||
size_t compressed_size = 0, compressed_alloc = 0;
|
||||
size_t finish_size = 0, finish_alloc = 0;
|
||||
|
||||
assert_se(huge = malloc(HUGE_SIZE));
|
||||
/* Compress */
|
||||
ASSERT_OK(compressor_new(&compressor, c));
|
||||
ASSERT_EQ(compressor_type(compressor), c);
|
||||
|
||||
ASSERT_OK(compressor_start(compressor, text, sizeof(text), &compressed, &compressed_size, &compressed_alloc));
|
||||
ASSERT_OK(compressor_finish(compressor, &finish_buf, &finish_size, &finish_alloc));
|
||||
|
||||
size_t total_compressed = compressed_size + finish_size;
|
||||
_cleanup_free_ void *full_compressed = malloc(total_compressed);
|
||||
ASSERT_NOT_NULL(full_compressed);
|
||||
memcpy(full_compressed, compressed, compressed_size);
|
||||
if (finish_size > 0)
|
||||
memcpy((uint8_t*) full_compressed + compressed_size, finish_buf, finish_size);
|
||||
|
||||
compressor = compressor_free(compressor);
|
||||
|
||||
/* Decompress via detect + push and verify content */
|
||||
ASSERT_OK_POSITIVE(decompressor_detect(&decompressor, full_compressed, total_compressed));
|
||||
ASSERT_EQ(compressor_type(decompressor), c);
|
||||
|
||||
struct decompressor_test_data result = {};
|
||||
ASSERT_OK(decompressor_push(decompressor, full_compressed, total_compressed, test_decompressor_callback, &result));
|
||||
ASSERT_EQ(result.size, sizeof(text));
|
||||
ASSERT_EQ(memcmp(result.buf, text, sizeof(text)), 0);
|
||||
free(result.buf);
|
||||
|
||||
decompressor = compressor_free(decompressor);
|
||||
}
|
||||
|
||||
/* Test compressor_type on NULL */
|
||||
ASSERT_EQ(compressor_type(NULL), _COMPRESSION_INVALID);
|
||||
|
||||
/* Test decompressor_force_off */
|
||||
_cleanup_(compressor_freep) Decompressor *d = NULL;
|
||||
ASSERT_OK(decompressor_force_off(&d));
|
||||
ASSERT_EQ(compressor_type(d), COMPRESSION_NONE);
|
||||
d = compressor_free(d);
|
||||
|
||||
/* Test decompressor_detect returning 0 on too-small input */
|
||||
ASSERT_OK_ZERO(decompressor_detect(&d, "x", 1));
|
||||
ASSERT_NULL(d);
|
||||
}
|
||||
|
||||
static int intro(void) {
|
||||
srcfile = saved_argc > 1 ? saved_argv[1] : saved_argv[0];
|
||||
|
||||
ASSERT_NOT_NULL(huge = malloc(HUGE_SIZE));
|
||||
memcpy(huge, "HUGE=", STRLEN("HUGE="));
|
||||
memset(&huge[STRLEN("HUGE=")], 'x', HUGE_SIZE - STRLEN("HUGE=") - 1);
|
||||
huge[HUGE_SIZE - 1] = '\0';
|
||||
|
||||
r = sym_LZ4_compress_default(huge, buf, HUGE_SIZE, buf_size);
|
||||
assert_se(r >= 0);
|
||||
compressed = r;
|
||||
log_info("Compressed %i → %zu", HUGE_SIZE, compressed);
|
||||
|
||||
r = sym_LZ4_decompress_safe(buf, huge, r, HUGE_SIZE);
|
||||
assert_se(r >= 0);
|
||||
log_info("Decompressed → %i", r);
|
||||
|
||||
r = sym_LZ4_decompress_safe_partial(buf, huge,
|
||||
compressed,
|
||||
12, HUGE_SIZE);
|
||||
assert_se(r >= 0);
|
||||
log_info("Decompressed partial %i/%i → %i", 12, HUGE_SIZE, r);
|
||||
|
||||
for (size_t size = 1; size < sizeof(buf2); size++) {
|
||||
/* This failed in older lz4s but works in newer ones. */
|
||||
r = sym_LZ4_decompress_safe_partial(buf, buf2, compressed, size, size);
|
||||
log_info("Decompressed partial %zu/%zu → %i (%s)", size, size, r,
|
||||
r < 0 ? "bad" : "good");
|
||||
if (r >= 0 && sym_LZ4_versionNumber() >= 10803)
|
||||
/* lz4 <= 1.8.2 should fail that test, let's only check for newer ones */
|
||||
assert_se(memcmp(buf2, huge, r) == 0);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
#if HAVE_COMPRESSION
|
||||
_unused_ const char text[] =
|
||||
"text\0foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF"
|
||||
"foofoofoofoo AAAA aaaaaaaaa ghost busters barbarbar FFF";
|
||||
|
||||
/* The file to test compression on can be specified as the first argument */
|
||||
const char *srcfile = argc > 1 ? argv[1] : argv[0];
|
||||
|
||||
char data[512] = "random\0";
|
||||
|
||||
_cleanup_free_ char *huge = NULL;
|
||||
|
||||
assert_se(huge = malloc(HUGE_SIZE));
|
||||
memcpy(huge, "HUGE=", STRLEN("HUGE="));
|
||||
memset(&huge[STRLEN("HUGE=")], 'x', HUGE_SIZE - STRLEN("HUGE=") - 1);
|
||||
huge[HUGE_SIZE - 1] = '\0';
|
||||
|
||||
test_setup_logging(LOG_DEBUG);
|
||||
|
||||
random_bytes(data + 7, sizeof(data) - 7);
|
||||
|
||||
#if HAVE_XZ
|
||||
test_compress_decompress("XZ", compress_blob_xz, decompress_blob_xz,
|
||||
text, sizeof(text), false);
|
||||
test_compress_decompress("XZ", compress_blob_xz, decompress_blob_xz,
|
||||
data, sizeof(data), true);
|
||||
|
||||
test_decompress_startswith("XZ",
|
||||
compress_blob_xz, decompress_startswith_xz,
|
||||
text, sizeof(text), false);
|
||||
test_decompress_startswith("XZ",
|
||||
compress_blob_xz, decompress_startswith_xz,
|
||||
data, sizeof(data), true);
|
||||
test_decompress_startswith("XZ",
|
||||
compress_blob_xz, decompress_startswith_xz,
|
||||
huge, HUGE_SIZE, true);
|
||||
|
||||
test_compress_stream("XZ", "xzcat",
|
||||
compress_stream_xz, decompress_stream_xz, srcfile);
|
||||
|
||||
test_decompress_stream_sparse("XZ", compress_stream_xz, decompress_stream_xz);
|
||||
|
||||
test_decompress_startswith_short("XZ", compress_blob_xz, decompress_startswith_xz);
|
||||
|
||||
#else
|
||||
log_info("/* XZ test skipped */");
|
||||
#endif
|
||||
|
||||
#if HAVE_LZ4
|
||||
if (dlopen_lz4() >= 0) {
|
||||
test_compress_decompress("LZ4", compress_blob_lz4, decompress_blob_lz4,
|
||||
text, sizeof(text), false);
|
||||
test_compress_decompress("LZ4", compress_blob_lz4, decompress_blob_lz4,
|
||||
data, sizeof(data), true);
|
||||
|
||||
test_decompress_startswith("LZ4",
|
||||
compress_blob_lz4, decompress_startswith_lz4,
|
||||
text, sizeof(text), false);
|
||||
test_decompress_startswith("LZ4",
|
||||
compress_blob_lz4, decompress_startswith_lz4,
|
||||
data, sizeof(data), true);
|
||||
test_decompress_startswith("LZ4",
|
||||
compress_blob_lz4, decompress_startswith_lz4,
|
||||
huge, HUGE_SIZE, true);
|
||||
|
||||
test_compress_stream("LZ4", "lz4cat",
|
||||
compress_stream_lz4, decompress_stream_lz4, srcfile);
|
||||
|
||||
test_decompress_stream_sparse("LZ4", compress_stream_lz4, decompress_stream_lz4);
|
||||
|
||||
test_lz4_decompress_partial();
|
||||
|
||||
test_decompress_startswith_short("LZ4", compress_blob_lz4, decompress_startswith_lz4);
|
||||
} else
|
||||
log_error("/* Can't load liblz4 */");
|
||||
#else
|
||||
log_info("/* LZ4 test skipped */");
|
||||
#endif
|
||||
|
||||
#if HAVE_ZSTD
|
||||
test_compress_decompress("ZSTD", compress_blob_zstd, decompress_blob_zstd,
|
||||
text, sizeof(text), false);
|
||||
test_compress_decompress("ZSTD", compress_blob_zstd, decompress_blob_zstd,
|
||||
data, sizeof(data), true);
|
||||
|
||||
test_decompress_startswith("ZSTD",
|
||||
compress_blob_zstd, decompress_startswith_zstd,
|
||||
text, sizeof(text), false);
|
||||
test_decompress_startswith("ZSTD",
|
||||
compress_blob_zstd, decompress_startswith_zstd,
|
||||
data, sizeof(data), true);
|
||||
test_decompress_startswith("ZSTD",
|
||||
compress_blob_zstd, decompress_startswith_zstd,
|
||||
huge, HUGE_SIZE, true);
|
||||
|
||||
test_compress_stream("ZSTD", "zstdcat",
|
||||
compress_stream_zstd, decompress_stream_zstd, srcfile);
|
||||
|
||||
test_decompress_stream_sparse("ZSTD", compress_stream_zstd, decompress_stream_zstd);
|
||||
|
||||
test_decompress_startswith_short("ZSTD", compress_blob_zstd, decompress_startswith_zstd);
|
||||
#else
|
||||
log_info("/* ZSTD test skipped */");
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
#else
|
||||
return log_tests_skipped("no compression algorithm supported");
|
||||
#endif
|
||||
}
|
||||
|
||||
DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);
|
||||
|
||||
@@ -42,6 +42,7 @@ static int run(int argc, char **argv) {
|
||||
* where .so versions change and distributions update, but systemd doesn't have the new so names
|
||||
* around yet. */
|
||||
|
||||
ASSERT_DLOPEN(dlopen_bzip2, HAVE_BZIP2);
|
||||
ASSERT_DLOPEN(dlopen_bpf, HAVE_LIBBPF);
|
||||
ASSERT_DLOPEN(dlopen_cryptsetup, HAVE_LIBCRYPTSETUP);
|
||||
ASSERT_DLOPEN(dlopen_dw, HAVE_ELFUTILS);
|
||||
@@ -60,14 +61,15 @@ static int run(int argc, char **argv) {
|
||||
ASSERT_DLOPEN(dlopen_libpam, HAVE_PAM);
|
||||
ASSERT_DLOPEN(dlopen_libseccomp, HAVE_SECCOMP);
|
||||
ASSERT_DLOPEN(dlopen_libselinux, HAVE_SELINUX);
|
||||
ASSERT_DLOPEN(dlopen_xz, HAVE_XZ);
|
||||
ASSERT_DLOPEN(dlopen_lz4, HAVE_LZ4);
|
||||
ASSERT_DLOPEN(dlopen_lzma, HAVE_XZ);
|
||||
ASSERT_DLOPEN(dlopen_p11kit, HAVE_P11KIT);
|
||||
ASSERT_DLOPEN(dlopen_passwdqc, HAVE_PASSWDQC);
|
||||
ASSERT_DLOPEN(dlopen_pcre2, HAVE_PCRE2);
|
||||
ASSERT_DLOPEN(dlopen_pwquality, HAVE_PWQUALITY);
|
||||
ASSERT_DLOPEN(dlopen_qrencode, HAVE_QRENCODE);
|
||||
ASSERT_DLOPEN(dlopen_tpm2, HAVE_TPM2);
|
||||
ASSERT_DLOPEN(dlopen_zlib, HAVE_ZLIB);
|
||||
ASSERT_DLOPEN(dlopen_zstd, HAVE_ZSTD);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -17,6 +17,13 @@ EOF
|
||||
systemctl reset-failed systemd-journald.service
|
||||
|
||||
for c in NONE XZ LZ4 ZSTD; do
|
||||
# compression_to_string() returns "uncompressed" for COMPRESSION_NONE
|
||||
if [[ "${c}" == NONE ]]; then
|
||||
log_name="uncompressed"
|
||||
else
|
||||
log_name="${c,,}"
|
||||
fi
|
||||
|
||||
cat >/run/systemd/system/systemd-journald.service.d/compress.conf <<EOF
|
||||
[Service]
|
||||
Environment=SYSTEMD_JOURNAL_COMPRESS=${c}
|
||||
@@ -28,14 +35,20 @@ EOF
|
||||
ID="$(systemd-id128 new)"
|
||||
systemd-cat -t "$ID" bash -c "for ((i=0;i<100;i++)); do echo -n hoge with ${c}; done; echo"
|
||||
journalctl --sync
|
||||
timeout 10 bash -c "until SYSTEMD_LOG_LEVEL=debug journalctl --verify --quiet --file /var/log/journal/$MACHINE_ID/system.journal 2>&1 | grep -F 'compress=${c}' >/dev/null; do sleep .5; done"
|
||||
timeout 10 bash -c "until SYSTEMD_LOG_LEVEL=debug journalctl --verify --quiet --file /var/log/journal/$MACHINE_ID/system.journal 2>&1 | grep -F 'compress=${log_name}' >/dev/null; do sleep .5; done"
|
||||
|
||||
# $SYSTEMD_JOURNAL_COMPRESS= also works for journal-remote
|
||||
if [[ -x /usr/lib/systemd/systemd-journal-remote ]]; then
|
||||
for cc in NONE XZ LZ4 ZSTD; do
|
||||
if [[ "${cc}" == NONE ]]; then
|
||||
cc_log_name="uncompressed"
|
||||
else
|
||||
cc_log_name="${cc,,}"
|
||||
fi
|
||||
|
||||
rm -f /tmp/foo.journal
|
||||
SYSTEMD_JOURNAL_COMPRESS="${cc}" /usr/lib/systemd/systemd-journal-remote --split-mode=none -o /tmp/foo.journal --getter="journalctl -b -o export -t $ID"
|
||||
SYSTEMD_LOG_LEVEL=debug journalctl --verify --quiet --file /tmp/foo.journal 2>&1 | grep -F "compress=${cc}" >/dev/null
|
||||
SYSTEMD_LOG_LEVEL=debug journalctl --verify --quiet --file /tmp/foo.journal 2>&1 | grep -F "compress=${cc_log_name}" >/dev/null
|
||||
journalctl -t "$ID" -o cat --file /tmp/foo.journal | grep -F "hoge with ${c}" >/dev/null
|
||||
done
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user