]> git.ozlabs.org Git - petitboot/blobdiff - discover/parser.c
parsers: change parser.parse to accept a buffer
[petitboot] / discover / parser.c
index 87241a9cdb2e60b98913791b81eae86f371d08f3..1f3674d3ee461cc24fac0f9ca5a8fc11d765fb1c 100644 (file)
 
+#include <fcntl.h>
 #include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
-#include "pb-protocol/pb-protocol.h"
+#include "types/types.h"
+#include <log/log.h>
+#include <talloc/talloc.h>
 
 #include "device-handler.h"
-#include "log.h"
 #include "parser.h"
 #include "parser-utils.h"
+#include "paths.h"
 
-extern struct parser __start_parsers[], __stop_parsers[];
+struct parser __grub2_parser;
+struct parser __kboot_parser;
+struct parser __native_parser;
+struct parser __yaboot_parser;
 
-void iterate_parsers(struct discover_context *ctx)
+static const struct parser *const parsers[] = {
+//     &__native_parser,
+       &__kboot_parser,
+       &__grub2_parser,
+       &__yaboot_parser,
+       NULL
+};
+
+static const int max_file_size = 1024 * 1024;
+
+static int read_file(struct discover_context *ctx,
+               const char *filename, char **bufp, int *lenp)
 {
-       struct parser *parser;
+       struct stat statbuf;
+       int rc, fd, i, len;
+       char *buf;
+
+       fd = open(filename, O_RDONLY);
+       if (fd < 0)
+               return -1;
+
+       rc = fstat(fd, &statbuf);
+       if (rc < 0)
+               goto err_close;
+
+       len = statbuf.st_size;
+       if (len > max_file_size)
+               goto err_close;
+
+       buf = talloc_array(ctx, char, len);
+       if (!buf)
+               goto err_close;
 
-       pb_log("trying parsers for %s\n", ctx->device_path);
+       for (i = 0; i < len; i += rc) {
+               rc = read(fd, buf + i, len - i);
+
+               /* unexpected EOF: trim and return */
+               if (rc == 0) {
+                       len = i;
+                       break;
+               }
+
+               if (rc < 0)
+                       goto err_free;
 
-       for (parser = __start_parsers; parser < __stop_parsers; parser++) {
-               pb_log("\ttrying parser '%s'\n", parser->name);
-               /* just use a dummy device path for now */
-               if (parser->parse(ctx))
-                       return;
        }
-       pb_log("\tno boot_options found\n");
+
+       close(fd);
+       *bufp = buf;
+       *lenp = len;
+       return 0;
+
+err_free:
+       talloc_free(buf);
+err_close:
+       close(fd);
+       return -1;
 }
 
-static int compare_parsers(const void *a, const void *b)
+static void iterate_parser_files(struct discover_context *ctx,
+               const struct parser *parser)
 {
-       const struct parser *parser_a = a, *parser_b = b;
+       const char * const *filename;
+       const char *path, *url;
+       unsigned int tempfile;
 
-       if (parser_a->priority > parser_b->priority)
-               return -1;
+       if (!parser->filenames)
+               return;
 
-       if (parser_a->priority < parser_b->priority)
-               return 1;
+       for (filename = parser->filenames; *filename; filename++) {
+               int rc, len;
+               char *buf;
 
-       return 0;
+               url = resolve_path(ctx, *filename, ctx->device->device_path);
+               if (!url)
+                       continue;
+
+               path = load_file(ctx, url, &tempfile);
+
+               if (!path)
+                       continue;
+
+               rc = read_file(ctx, path, &buf, &len);
+               if (!rc) {
+                       parser->parse(ctx, buf, len);
+                       talloc_free(buf);
+               }
+
+               if (tempfile)
+                       unlink(path);
+
+       }
+
+}
+
+void iterate_parsers(struct discover_context *ctx)
+{
+       int i;
+
+       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]);
+       }
 }
 
 void parser_init(void)
 {
-       /* sort our parsers into descending priority order */
-       qsort(__start_parsers, __stop_parsers - __start_parsers,
-                       sizeof(struct parser), compare_parsers);
 }