7 #include "types/types.h"
10 #include <talloc/talloc.h>
12 #include "device-handler.h"
14 #include "parser-utils.h"
18 struct list_item list;
19 struct parser *parser;
24 static char *local_path(struct discover_context *ctx,
25 struct discover_device *dev,
28 return join_paths(ctx, dev->mount_path, filename);
31 int parser_request_file(struct discover_context *ctx,
32 struct discover_device *dev, const char *filename,
39 /* we only support local files at present */
43 path = local_path(ctx, dev, filename);
45 rc = read_file(ctx, path, buf, len);
52 int parser_check_dir(struct discover_context *ctx,
53 struct discover_device *dev, const char *dirname)
62 path = local_path(ctx, dev, dirname);
64 rc = stat(path, &statbuf);
68 return S_ISDIR(statbuf.st_mode) ? 0 : -1;
71 int parser_replace_file(struct discover_context *ctx,
72 struct discover_device *dev, const char *filename,
82 rc = device_request_write(dev, &release);
84 pb_log("Can't write file %s: device doesn't allow write\n",
89 path = local_path(ctx, dev, filename);
91 rc = replace_file(path, buf, len);
95 device_release_write(dev, release);
100 int parser_request_url(struct discover_context *ctx, struct pb_url *url,
101 char **buf, int *len)
103 struct load_url_result *result;
106 result = load_url(ctx, url);
110 rc = read_file(ctx, result->local, buf, len);
112 pb_log("Read failed for the parser %s on file %s\n",
113 ctx->parser->name, result->local);
120 if (result->cleanup_local)
121 unlink(result->local);
126 void iterate_parsers(struct discover_context *ctx)
130 pb_log("trying parsers for %s\n", ctx->device->device->id);
132 list_for_each_entry(&parsers, i, list) {
133 pb_debug("\ttrying parser '%s'\n", i->parser->name);
134 ctx->parser = i->parser;
135 i->parser->parse(ctx);
140 static void *parsers_ctx;
142 void __register_parser(struct parser *parser)
147 parsers_ctx = talloc_new(NULL);
149 i = talloc(parsers_ctx, struct p_item);
151 list_add(&parsers, &i->list);
154 static __attribute__((destructor)) void cleanup_parsers(void)
156 talloc_free(parsers_ctx);
159 void parser_init(void)