7 #include "types/types.h"
9 #include <talloc/talloc.h>
11 #include "device-handler.h"
13 #include "parser-utils.h"
16 struct parser __grub2_parser;
17 struct parser __kboot_parser;
18 struct parser __native_parser;
19 struct parser __yaboot_parser;
21 static struct parser *const parsers[] = {
29 static const int max_file_size = 1024 * 1024;
31 static int read_file(struct discover_context *ctx,
32 const char *filename, char **bufp, int *lenp)
38 fd = open(filename, O_RDONLY);
42 rc = fstat(fd, &statbuf);
46 len = statbuf.st_size;
47 if (len > max_file_size)
50 buf = talloc_array(ctx, char, len);
54 for (i = 0; i < len; i += rc) {
55 rc = read(fd, buf + i, len - i);
57 /* unexpected EOF: trim and return */
80 static char *local_path(struct discover_context *ctx,
83 return join_paths(ctx, ctx->device->mount_path, filename);
86 static void iterate_parser_files(struct discover_context *ctx,
87 const struct parser *parser)
89 const char * const *filename;
92 if (!parser->filenames)
95 for (filename = parser->filenames; *filename; filename++) {
99 path = local_path(ctx, *filename);
103 rc = read_file(ctx, path, &buf, &len);
105 parser->parse(ctx, buf, len);
111 void iterate_parsers(struct discover_context *ctx)
115 pb_log("trying parsers for %s\n", ctx->device->device->id);
117 for (i = 0; parsers[i]; i++) {
118 pb_log("\ttrying parser '%s'\n", parsers[i]->name);
119 ctx->parser = parsers[i];
120 iterate_parser_files(ctx, parsers[i]);
125 void parser_init(void)