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>
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)
/* 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);
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);
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;
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);