5 #include "types/types.h"
8 #include <talloc/talloc.h>
10 #include "device-handler.h"
12 #include "parser-utils.h"
16 struct list_item list;
17 struct parser *parser;
22 static char *local_path(struct discover_context *ctx,
23 struct discover_device *dev,
26 return join_paths(ctx, dev->root_path, filename);
29 int parser_request_file(struct discover_context *ctx,
30 struct discover_device *dev, const char *filename,
37 /* we only support local files at present */
41 path = local_path(ctx, dev, filename);
43 rc = read_file(ctx, path, buf, len);
50 int parser_stat_path(struct discover_context *ctx,
51 struct discover_device *dev, const char *path,
57 /* we only support local files at present */
61 full_path = local_path(ctx, dev, path);
63 rc = stat(full_path, statbuf);
71 talloc_free(full_path);
76 int parser_replace_file(struct discover_context *ctx,
77 struct discover_device *dev, const char *filename,
87 rc = device_request_write(dev, &release);
89 pb_log("Can't write file %s: device doesn't allow write\n",
94 path = local_path(ctx, dev, filename);
96 rc = replace_file(path, buf, len);
100 device_release_write(dev, release);
105 int parser_request_url(struct discover_context *ctx, struct pb_url *url,
106 char **buf, int *len)
108 struct load_url_result *result;
111 result = load_url(ctx, url);
115 rc = read_file(ctx, result->local, buf, len);
117 pb_log("Read failed for the parser %s on file %s\n",
118 ctx->parser->name, result->local);
125 if (result->cleanup_local)
126 unlink(result->local);
131 int parser_scandir(struct discover_context *ctx, const char *dirname,
132 struct dirent ***files, int (*filter)(const struct dirent *),
133 int (*comp)(const struct dirent **, const struct dirent **))
138 path = talloc_asprintf(ctx, "%s%s", ctx->device->mount_path, dirname);
142 n = scandir(path, files, filter, comp);
147 void iterate_parsers(struct discover_context *ctx)
151 pb_log("trying parsers for %s\n", ctx->device->device->id);
153 list_for_each_entry(&parsers, i, list) {
154 pb_debug("\ttrying parser '%s'\n", i->parser->name);
155 ctx->parser = i->parser;
156 i->parser->parse(ctx);
161 static void *parsers_ctx;
163 void __register_parser(struct parser *parser)
168 parsers_ctx = talloc_new(NULL);
170 i = talloc(parsers_ctx, struct p_item);
172 list_add(&parsers, &i->list);
175 static __attribute__((destructor)) void cleanup_parsers(void)
177 talloc_free(parsers_ctx);
180 void parser_init(void)