]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: pass bool alias_prefix to index_{,mm_}dump()
authorEmil Velikov <emil.l.velikov@gmail.com>
Mon, 4 Nov 2024 14:32:39 +0000 (14:32 +0000)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Thu, 7 Nov 2024 19:43:49 +0000 (13:43 -0600)
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 <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/224
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
libkmod/libkmod-index.c
libkmod/libkmod-index.h
libkmod/libkmod.c

index 9e1ba43d9eb0518b26ab2add840efb97436e2efd..46a407939c8c7e3e2b8da5445d4be2d41a257631 100644 (file)
@@ -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);
 }
index b33a7bbf33c81a1d6e15b008d9fb71144fe1b9db..25b1b74bbc001a8543fed3e06858b2f6dad8e247 100644 (file)
@@ -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);
index 51a10b482397d6423137139317f2c21ff987373e..a8685c1ad42e67321f57c3cc912cc353c4dcdf3e 100644 (file)
 
 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);
        }