X-Git-Url: http://git.ozlabs.org/?p=petitboot;a=blobdiff_plain;f=discover%2Fparser.c;h=11728b779ce66f1af15900857e4d33168a1cc3a8;hp=1f3674d3ee461cc24fac0f9ca5a8fc11d765fb1c;hb=bb19338a08bd4449c38d596e66b92028c5855b0d;hpb=06a49ebdfc795a70b938f5aee29f3c488ef9fc21 diff --git a/discover/parser.c b/discover/parser.c index 1f3674d..11728b7 100644 --- a/discover/parser.c +++ b/discover/parser.c @@ -13,18 +13,8 @@ #include "parser-utils.h" #include "paths.h" -struct parser __grub2_parser; -struct parser __kboot_parser; -struct parser __native_parser; -struct parser __yaboot_parser; - -static const struct parser *const parsers[] = { -// &__native_parser, - &__kboot_parser, - &__grub2_parser, - &__yaboot_parser, - NULL -}; +static int n_parsers; +static struct parser **parsers; static const int max_file_size = 1024 * 1024; @@ -77,12 +67,39 @@ err_close: return -1; } +static char *local_path(struct discover_context *ctx, + const char *filename) +{ + return join_paths(ctx, ctx->device->mount_path, filename); +} + +static int download_config(struct discover_context *ctx, char **buf, int *len) +{ + unsigned tempfile; + const char *file; + int rc; + + file = load_url(ctx, ctx->conf_url, &tempfile); + if (!file) + return -1; + + rc = read_file(ctx, file, buf, len); + if (rc) + goto out_clean; + + return 0; + +out_clean: + if (tempfile) + unlink(file); + return -1; +} + static void iterate_parser_files(struct discover_context *ctx, const struct parser *parser) { const char * const *filename; - const char *path, *url; - unsigned int tempfile; + const char *path; if (!parser->filenames) return; @@ -91,12 +108,7 @@ static void iterate_parser_files(struct discover_context *ctx, int rc, len; char *buf; - url = resolve_path(ctx, *filename, ctx->device->device_path); - if (!url) - continue; - - path = load_file(ctx, url, &tempfile); - + path = local_path(ctx, *filename); if (!path) continue; @@ -105,26 +117,60 @@ static void iterate_parser_files(struct discover_context *ctx, parser->parse(ctx, buf, len); talloc_free(buf); } - - if (tempfile) - unlink(path); - } - } -void iterate_parsers(struct discover_context *ctx) +void iterate_parsers(struct discover_context *ctx, enum conf_method method) { - int i; + int rc, i, len; + char *buf; pb_log("trying parsers for %s\n", ctx->device->device->id); - for (i = 0; parsers[i]; i++) { - pb_log("\ttrying parser '%s'\n", parsers[i]->name); - iterate_parser_files(ctx, parsers[i]); + switch (method) { + case CONF_METHOD_LOCAL_FILE: + for (i = 0; i < n_parsers; i++) { + if (parsers[i]->method != CONF_METHOD_LOCAL_FILE) + continue; + + pb_log("\ttrying parser '%s'\n", parsers[i]->name); + ctx->parser = parsers[i]; + iterate_parser_files(ctx, ctx->parser); + } + ctx->parser = NULL; + break; + + case CONF_METHOD_DHCP: + rc = download_config(ctx, &buf, &len); + if (rc) { + pb_log("\tdownload failed, aborting\n"); + return; + } + + for (i = 0; i < n_parsers; i++) { + if (parsers[i]->method != method) + continue; + + pb_log("\ttrying parser '%s'\n", parsers[i]->name); + ctx->parser = parsers[i]; + parsers[i]->parse(ctx, buf, len); + } + + break; + + case CONF_METHOD_UNKNOWN: + break; + } } +void __register_parser(struct parser *parser) +{ + parsers = talloc_realloc(NULL, parsers, struct parser *, n_parsers + 1); + parsers[n_parsers] = parser; + n_parsers++; +} + void parser_init(void) { }