]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: fold kmod_file_{load_content,get_{content,size}}
authorEmil Velikov <emil.l.velikov@gmail.com>
Fri, 6 Feb 2026 19:35:15 +0000 (19:35 +0000)
committerLucas De Marchi <demarchi@kernel.org>
Wed, 22 Apr 2026 13:21:30 +0000 (08:21 -0500)
Currently, the kmod_file data fits in two groups:
 - populated on initialization (aka kmod_file_open)- fd, compression
 - populated on kmod_file_load_contents - contents, size

Currently, the caller has to track/remember kmod_file_load_contents
prior to calling the contents/size getters...

At the same time, we don't need the fine grained API since all call
sites need both contents and size. So let's just fold the three
together.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
---
Not a huge fan of the casts, although we already use similar approach in
kmod_module_load_elf and others.

Don't mind going either way - keep the casts or drop the const.

Link: https://github.com/kmod-project/kmod/pull/418
Signed-off-by: Lucas De Marchi <demarchi@kernel.org>
libkmod/libkmod-file.c
libkmod/libkmod-internal.h
libkmod/libkmod-module.c
libkmod/libkmod-signature.c

index e287a03c3ae04727d4b91f760d006ec661f9dd12..b44b95a83edeaa4780b1da50e6b7aa2a8775f189 100644 (file)
@@ -116,26 +116,18 @@ int kmod_file_open(const struct kmod_ctx *ctx, const char *filename,
        return 0;
 }
 
-/*
- *  Callers should just check file->memory got updated
- */
-int kmod_file_load_contents(struct kmod_file *file)
-{
-       if (file->memory)
-               return 0;
-
-       /*  The load functions already log possible errors. */
-       return file->load(file);
-}
-
-const void *kmod_file_get_contents(const struct kmod_file *file)
+int kmod_file_get_contents(const struct kmod_file *file, const void **contents,
+                          off_t *size)
 {
-       return file->memory;
-}
-
-off_t kmod_file_get_size(const struct kmod_file *file)
-{
-       return file->size;
+       if (!file->memory) {
+               int ret = file->load((struct kmod_file *)file);
+               /*  The load functions already log possible errors. */
+               if (ret)
+                       return ret;
+       }
+       *contents = file->memory;
+       *size = file->size;
+       return 0;
 }
 
 enum kmod_file_compression_type kmod_file_get_compression(const struct kmod_file *file)
index 67c0f3b1352d4e0ccbf4aa81efe163bf922ebfa1..014b34ae9058512c9fec7e0e457dad07c64080c4 100644 (file)
@@ -137,9 +137,7 @@ _nonnull_all_ bool kmod_module_is_builtin(struct kmod_module *mod);
 /* libkmod-file.c */
 struct kmod_file;
 _must_check_ _nonnull_all_ int kmod_file_open(const struct kmod_ctx *ctx, const char *filename, struct kmod_file **file);
-_nonnull_all_ int kmod_file_load_contents(struct kmod_file *file);
-_must_check_ _nonnull_all_ const void *kmod_file_get_contents(const struct kmod_file *file);
-_must_check_ _nonnull_all_ off_t kmod_file_get_size(const struct kmod_file *file);
+_must_check_ _nonnull_all_ int kmod_file_get_contents(const struct kmod_file *file, const void **contents, off_t *size);
 _must_check_ _nonnull_all_ enum kmod_file_compression_type kmod_file_get_compression(const struct kmod_file *file);
 _must_check_ _nonnull_all_ int kmod_file_get_fd(const struct kmod_file *file);
 _nonnull_all_ void kmod_file_unref(struct kmod_file *file);
index cd6d8fc3b6732d5d4333bd734d4605c036e9b5c5..72beec14f7bde9f234ead941e929d72fe879df58 100644 (file)
@@ -660,13 +660,10 @@ static int do_init_module(struct kmod_module *mod, unsigned int flags, const cha
        off_t size;
        int err;
 
-       err = kmod_file_load_contents(mod->file);
+       err = kmod_file_get_contents(mod->file, &mem, &size);
        if (err)
                return err;
 
-       mem = kmod_file_get_contents(mod->file);
-       size = kmod_file_get_size(mod->file);
-
        if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
                if (mod->elf == NULL) {
                        err = kmod_elf_new(mem, size, &mod->elf);
@@ -1757,13 +1754,10 @@ static int kmod_module_load_elf(const struct kmod_module *mod)
        if (mod->elf == NULL) {
                const void *mem;
                off_t size;
-               int err = kmod_file_load_contents(mod->file);
+               int err = kmod_file_get_contents(mod->file, &mem, &size);
                if (err)
                        return err;
 
-               mem = kmod_file_get_contents(mod->file);
-               size = kmod_file_get_size(mod->file);
-
                err = kmod_elf_new(mem, size, &((struct kmod_module *)mod)->elf);
                if (err)
                        return err;
index 2cb67f6efbbfb300580dfea134330f5dd32de52a..fbaf3c3be7df57c80676c56b2dd11e97f548e92a 100644 (file)
@@ -305,12 +305,18 @@ bool kmod_module_signature_info(const struct kmod_file *file,
                                struct kmod_signature_info *sig_info)
 {
        const char *mem;
+       const void *contents;
        off_t size;
        struct module_signature modsig;
        size_t sig_len;
+       int ret;
+
+       ret = kmod_file_get_contents(file, &contents, &size);
+       if (ret)
+               return false;
+
+       mem = contents;
 
-       size = kmod_file_get_size(file);
-       mem = kmod_file_get_contents(file);
        if (size < (off_t)strlen(SIG_MAGIC))
                return false;
        size -= strlen(SIG_MAGIC);