From: Emil Velikov Date: Fri, 6 Feb 2026 19:35:15 +0000 (+0000) Subject: libkmod: fold kmod_file_{load_content,get_{content,size}} X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5f60ea3d7fe2339561689b73e5a7d3804d402cbf;p=thirdparty%2Fkmod.git libkmod: fold kmod_file_{load_content,get_{content,size}} 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 --- 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 --- diff --git a/libkmod/libkmod-file.c b/libkmod/libkmod-file.c index e287a03c..b44b95a8 100644 --- a/libkmod/libkmod-file.c +++ b/libkmod/libkmod-file.c @@ -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) diff --git a/libkmod/libkmod-internal.h b/libkmod/libkmod-internal.h index 67c0f3b1..014b34ae 100644 --- a/libkmod/libkmod-internal.h +++ b/libkmod/libkmod-internal.h @@ -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); diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c index cd6d8fc3..72beec14 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -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; diff --git a/libkmod/libkmod-signature.c b/libkmod/libkmod-signature.c index 2cb67f6e..fbaf3c3b 100644 --- a/libkmod/libkmod-signature.c +++ b/libkmod/libkmod-signature.c @@ -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);