7 #include "types/types.h"
9 #include <talloc/talloc.h>
11 #include "device-handler.h"
13 #include "parser-utils.h"
17 static struct parser **parsers;
19 static const int max_file_size = 1024 * 1024;
21 static int read_file(struct discover_context *ctx,
22 const char *filename, char **bufp, int *lenp)
28 fd = open(filename, O_RDONLY);
32 rc = fstat(fd, &statbuf);
36 len = statbuf.st_size;
37 if (len > max_file_size)
40 buf = talloc_array(ctx, char, len);
44 for (i = 0; i < len; i += rc) {
45 rc = read(fd, buf + i, len - i);
47 /* unexpected EOF: trim and return */
70 static char *local_path(struct discover_context *ctx,
73 return join_paths(ctx, ctx->device->mount_path, filename);
76 static void iterate_parser_files(struct discover_context *ctx,
77 const struct parser *parser)
79 const char * const *filename;
82 if (!parser->filenames)
85 for (filename = parser->filenames; *filename; filename++) {
89 path = local_path(ctx, *filename);
93 rc = read_file(ctx, path, &buf, &len);
95 parser->parse(ctx, buf, len);
101 void iterate_parsers(struct discover_context *ctx)
105 pb_log("trying parsers for %s\n", ctx->device->device->id);
107 for (i = 0; i < n_parsers; i++) {
108 pb_log("\ttrying parser '%s'\n", parsers[i]->name);
109 ctx->parser = parsers[i];
110 iterate_parser_files(ctx, parsers[i]);
115 void __register_parser(struct parser *parser)
117 parsers = talloc_realloc(NULL, parsers, struct parser *, n_parsers + 1);
118 parsers[n_parsers] = parser;
122 void parser_init(void)