]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: Keep track of compression type
authorLucas De Marchi <lucas.de.marchi@gmail.com>
Thu, 1 Jun 2023 22:39:59 +0000 (15:39 -0700)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Fri, 9 Jun 2023 17:45:55 +0000 (10:45 -0700)
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>
libkmod/libkmod-file.c
libkmod/libkmod-internal.h

index 08adea9bd35e58bb88d72ce1eb5a12a766f23537..ca9d06f09d3fedbd288aee9878a844dd02906076 100644 (file)
@@ -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 = &reg_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)
index 3275bc5fd677550ce777cd13a214142b98e50a6e..d7ec1e430be9c889eaa3c9b95f080f20d875344f 100644 (file)
@@ -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_;