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 bool parser_is_unique(struct discover_context *ctx, struct discover_device *dev,
77 const char *filename, struct list *found_list)
80 struct parser_found_file *found_file;
81 const struct parser_found_file *entry;
83 if (parser_stat_path(ctx, dev, filename, &stat)) {
84 pb_debug("%s: Not found: '%s'\n", __func__, filename);
88 list_for_each_entry(found_list, entry, list) {
89 if (entry->ino == stat.st_ino) {
90 pb_log("%s: Duplicate: '%s' = '%s'\n",
91 __func__, filename, entry->filename);
96 pb_debug("%s: Found: '%s'\n", __func__, filename);
98 found_file = talloc_zero(found_list, struct parser_found_file);
99 found_file->filename = talloc_strdup(found_file, filename);
100 found_file->ino = stat.st_ino;
101 list_add(found_list, &found_file->list);
106 int parser_replace_file(struct discover_context *ctx,
107 struct discover_device *dev, const char *filename,
117 rc = device_request_write(dev, &release);
119 pb_log("Can't write file %s: device doesn't allow write\n",
124 path = local_path(ctx, dev, filename);
126 rc = replace_file(path, buf, len);
130 device_release_write(dev, release);
135 int parser_request_url(struct discover_context *ctx, struct pb_url *url,
136 char **buf, int *len)
138 struct load_url_result *result;
141 result = load_url(ctx, url);
145 rc = read_file(ctx, result->local, buf, len);
147 pb_log("Read failed for the parser %s on file %s\n",
148 ctx->parser->name, result->local);
155 if (result->cleanup_local)
156 unlink(result->local);
161 int parser_scandir(struct discover_context *ctx, const char *dirname,
162 struct dirent ***files, int (*filter)(const struct dirent *),
163 int (*comp)(const struct dirent **, const struct dirent **))
168 path = talloc_asprintf(ctx, "%s%s", ctx->device->mount_path, dirname);
172 n = scandir(path, files, filter, comp);
177 void iterate_parsers(struct discover_context *ctx)
181 pb_log("trying parsers for %s\n", ctx->device->device->id);
183 list_for_each_entry(&parsers, i, list) {
184 pb_debug("\ttrying parser '%s'\n", i->parser->name);
185 ctx->parser = i->parser;
186 i->parser->parse(ctx);
191 static void *parsers_ctx;
193 void __register_parser(struct parser *parser)
198 parsers_ctx = talloc_new(NULL);
200 i = talloc(parsers_ctx, struct p_item);
202 list_add(&parsers, &i->list);
205 static __attribute__((destructor)) void cleanup_parsers(void)
207 talloc_free(parsers_ctx);
210 void parser_init(void)