Do not only set the type as direct, but also keep track of the compression being used. This will allow using the in-kernel compression in future. Signed-off-by: Lucas De Marchi <lucas.de.marchi@xxxxxxxxx> --- libkmod/libkmod-file.c | 27 +++++++++++++++------------ libkmod/libkmod-internal.h | 7 +++++++ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/libkmod/libkmod-file.c b/libkmod/libkmod-file.c index 1449c41..705770a 100644 --- a/libkmod/libkmod-file.c +++ b/libkmod/libkmod-file.c @@ -58,7 +58,7 @@ struct kmod_file { gzFile gzf; #endif int fd; - bool direct; + enum kmod_file_compression_type compression; off_t size; void *memory; const struct file_ops *ops; @@ -376,19 +376,20 @@ static const char magic_zlib[] = {0x1f, 0x8b}; static const struct comp_type { size_t magic_size; + enum kmod_file_compression_type compression; const char *magic_bytes; const struct file_ops ops; } comp_types[] = { #ifdef ENABLE_ZSTD - {sizeof(magic_zstd), magic_zstd, {load_zstd, unload_zstd}}, + {sizeof(magic_zstd), KMOD_FILE_COMPRESSION_ZSTD, magic_zstd, {load_zstd, unload_zstd}}, #endif #ifdef ENABLE_XZ - {sizeof(magic_xz), magic_xz, {load_xz, unload_xz}}, + {sizeof(magic_xz), KMOD_FILE_COMPRESSION_XZ, magic_xz, {load_xz, unload_xz}}, #endif #ifdef ENABLE_ZLIB - {sizeof(magic_zlib), magic_zlib, {load_zlib, unload_zlib}}, + {sizeof(magic_zlib), KMOD_FILE_COMPRESSION_ZLIB, magic_zlib, {load_zlib, unload_zlib}}, #endif - {0, NULL, {NULL, NULL}} + {0, KMOD_FILE_COMPRESSION_NONE, NULL, {NULL, NULL}} }; static int load_reg(struct kmod_file *file) @@ -403,7 +404,7 @@ static int load_reg(struct kmod_file *file) file->fd, 0); if (file->memory == MAP_FAILED) return -errno; - file->direct = true; + return 0; } @@ -448,7 +449,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx, magic_size_max = itr->magic_size; } - file->direct = false; if (magic_size_max > 0) { char *buf = alloca(magic_size_max + 1); ssize_t sz; @@ -468,15 +468,18 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx, } for (itr = comp_types; itr->ops.load != NULL; itr++) { - if (memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) + if (memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) { + file->ops = &itr->ops; + file->compression = itr->compression; break; + } } - if (itr->ops.load != NULL) - file->ops = &itr->ops; } - if (file->ops == NULL) + if (file->ops == NULL) { file->ops = ®_ops; + file->compression = KMOD_FILE_COMPRESSION_NONE; + } file->ctx = ctx; @@ -512,7 +515,7 @@ off_t kmod_file_get_size(const struct kmod_file *file) bool kmod_file_get_direct(const struct kmod_file *file) { - return file->direct; + return file->compression == KMOD_FILE_COMPRESSION_NONE; } int kmod_file_get_fd(const struct kmod_file *file) diff --git a/libkmod/libkmod-internal.h b/libkmod/libkmod-internal.h index 4799ed5..7b8a158 100644 --- a/libkmod/libkmod-internal.h +++ b/libkmod/libkmod-internal.h @@ -61,6 +61,13 @@ struct kmod_list { void *data; }; +enum kmod_file_compression_type { + KMOD_FILE_COMPRESSION_NONE = 0, + KMOD_FILE_COMPRESSION_ZSTD, + KMOD_FILE_COMPRESSION_XZ, + KMOD_FILE_COMPRESSION_ZLIB, +}; + struct kmod_list *kmod_list_append(struct kmod_list *list, const void *data) _must_check_ __attribute__((nonnull(2))); struct kmod_list *kmod_list_prepend(struct kmod_list *list, const void *data) _must_check_ __attribute__((nonnull(2))); struct kmod_list *kmod_list_remove(struct kmod_list *list) _must_check_; -- 2.40.1