From e9ef603255972bcde6867cdfac4a8ff059d20d0d Mon Sep 17 00:00:00 2001 From: Emil Velikov Date: Mon, 4 Nov 2024 14:32:39 +0000 Subject: [PATCH] libkmod: pass bool alias_prefix to index_{,mm_}dump() As seen with previous commit, the way we pass an empty string to index_{,mm_}dump() is very error prone. Swap that with a bool, which makes things a lot more obvious. Signed-off-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/224 Signed-off-by: Lucas De Marchi --- libkmod/libkmod-index.c | 8 ++++---- libkmod/libkmod-index.h | 4 ++-- libkmod/libkmod.c | 16 ++++++++-------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/libkmod/libkmod-index.c b/libkmod/libkmod-index.c index 9e1ba43d..46a40793 100644 --- a/libkmod/libkmod-index.c +++ b/libkmod/libkmod-index.c @@ -391,7 +391,7 @@ static void index_dump_node(struct index_node_f *node, struct strbuf *buf, int f index_close(node); } -void index_dump(struct index_file *in, int fd, const char *prefix) +void index_dump(struct index_file *in, int fd, bool alias_prefix) { struct index_node_f *root; struct strbuf buf; @@ -401,7 +401,7 @@ void index_dump(struct index_file *in, int fd, const char *prefix) return; strbuf_init(&buf); - if (prefix[0] == '\0' || strbuf_pushchars(&buf, prefix)) + if (!alias_prefix || strbuf_pushchars(&buf, "alias ")) index_dump_node(root, &buf, fd); strbuf_release(&buf); } @@ -856,7 +856,7 @@ static void index_mm_dump_node(struct index_mm_node *node, struct strbuf *buf, i strbuf_popchars(buf, pushed); } -void index_mm_dump(struct index_mm *idx, int fd, const char *prefix) +void index_mm_dump(struct index_mm *idx, int fd, bool alias_prefix) { struct index_mm_node nbuf, *root; struct strbuf buf; @@ -866,7 +866,7 @@ void index_mm_dump(struct index_mm *idx, int fd, const char *prefix) return; strbuf_init(&buf); - if (prefix[0] == '\0' || strbuf_pushchars(&buf, prefix)) + if (!alias_prefix || strbuf_pushchars(&buf, "alias ")) index_mm_dump_node(root, &buf, fd); strbuf_release(&buf); } diff --git a/libkmod/libkmod-index.h b/libkmod/libkmod-index.h index b33a7bbf..25b1b74b 100644 --- a/libkmod/libkmod-index.h +++ b/libkmod/libkmod-index.h @@ -19,7 +19,7 @@ struct index_file; struct index_file *index_file_open(const char *filename); void index_file_close(struct index_file *idx); char *index_search(struct index_file *idx, const char *key); -void index_dump(struct index_file *in, int fd, const char *prefix); +void index_dump(struct index_file *in, int fd, bool alias_prefix); struct index_value *index_searchwild(struct index_file *idx, const char *key); void index_values_free(struct index_value *values); @@ -31,4 +31,4 @@ int index_mm_open(const struct kmod_ctx *ctx, const char *filename, void index_mm_close(struct index_mm *index); char *index_mm_search(struct index_mm *idx, const char *key); struct index_value *index_mm_searchwild(struct index_mm *idx, const char *key); -void index_mm_dump(struct index_mm *idx, int fd, const char *prefix); +void index_mm_dump(struct index_mm *idx, int fd, bool alias_prefix); diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c index 51a10b48..a8685c1a 100644 --- a/libkmod/libkmod.c +++ b/libkmod/libkmod.c @@ -30,14 +30,14 @@ static const struct { const char *fn; - const char *prefix; + bool alias_prefix; } index_files[] = { // clang-format off - [KMOD_INDEX_MODULES_DEP] = { .fn = "modules.dep", .prefix = "" }, - [KMOD_INDEX_MODULES_ALIAS] = { .fn = "modules.alias", .prefix = "alias " }, - [KMOD_INDEX_MODULES_SYMBOL] = { .fn = "modules.symbols", .prefix = "alias " }, - [KMOD_INDEX_MODULES_BUILTIN_ALIAS] = { .fn = "modules.builtin.alias", .prefix = "" }, - [KMOD_INDEX_MODULES_BUILTIN] = { .fn = "modules.builtin", .prefix = "" }, + [KMOD_INDEX_MODULES_DEP] = { .fn = "modules.dep" }, + [KMOD_INDEX_MODULES_ALIAS] = { .fn = "modules.alias", .alias_prefix = true }, + [KMOD_INDEX_MODULES_SYMBOL] = { .fn = "modules.symbols", .alias_prefix = true }, + [KMOD_INDEX_MODULES_BUILTIN_ALIAS] = { .fn = "modules.builtin.alias" }, + [KMOD_INDEX_MODULES_BUILTIN] = { .fn = "modules.builtin" }, // clang-format on }; @@ -813,7 +813,7 @@ KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type, int if (ctx->indexes[type] != NULL) { DBG(ctx, "use mmapped index '%s'\n", index_files[type].fn); - index_mm_dump(ctx->indexes[type], fd, index_files[type].prefix); + index_mm_dump(ctx->indexes[type], fd, index_files[type].alias_prefix); } else { char fn[PATH_MAX]; struct index_file *idx; @@ -826,7 +826,7 @@ KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type, int if (idx == NULL) return -ENOSYS; - index_dump(idx, fd, index_files[type].prefix); + index_dump(idx, fd, index_files[type].alias_prefix); index_file_close(idx); } -- 2.47.2