diff options
author | Lucas De Marchi <lucas.de.marchi@gmail.com> | 2023-06-01 15:39:59 -0700 |
---|---|---|
committer | Lucas De Marchi <lucas.de.marchi@gmail.com> | 2023-06-09 10:45:55 -0700 |
commit | e539827635db486ecc70801250f9f73f652cd8d0 (patch) | |
tree | 7b11cde5c5897d6b78e8e0fc11ac3dcf3ed91b58 | |
parent | e1f0e169de5413139c378c188028ae147d0b73ae (diff) | |
download | kmod-e539827635db486ecc70801250f9f73f652cd8d0.tar.gz kmod-e539827635db486ecc70801250f9f73f652cd8d0.tar.bz2 kmod-e539827635db486ecc70801250f9f73f652cd8d0.zip |
libkmod: Keep track of compression type
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.
Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
-rw-r--r-- | libkmod/libkmod-file.c | 27 | ||||
-rw-r--r-- | 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 08adea9..ca9d06f 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; @@ -516,7 +519,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 3275bc5..d7ec1e4 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_; |