d34600d18f1526856945689f4fbb0723cbdefc28
[petitboot] / discover / parser.c
1
2 #include <fcntl.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6
7 #include "types/types.h"
8 #include <log/log.h>
9 #include <talloc/talloc.h>
10
11 #include "device-handler.h"
12 #include "parser.h"
13 #include "parser-utils.h"
14 #include "paths.h"
15 #include "file.h"
16
17 struct p_item {
18         struct list_item list;
19         struct parser *parser;
20 };
21
22 STATIC_LIST(parsers);
23
24 static char *local_path(struct discover_context *ctx,
25                 struct discover_device *dev,
26                 const char *filename)
27 {
28         return join_paths(ctx, dev->mount_path, filename);
29 }
30
31 int parser_request_file(struct discover_context *ctx,
32                 struct discover_device *dev, const char *filename,
33                 char **buf, int *len)
34
35 {
36         char *path;
37         int rc;
38
39         /* we only support local files at present */
40         if (!dev->mount_path)
41                 return -1;
42
43         path = local_path(ctx, dev, filename);
44
45         rc = read_file(ctx, path, buf, len);
46
47         talloc_free(path);
48
49         return rc;
50 }
51
52 static int download_config(struct discover_context *ctx, char **buf, int *len)
53 {
54         struct load_url_result *result;
55         int rc;
56
57         result = load_url(ctx, ctx->conf_url);
58         if (!result)
59                 return -1;
60
61         rc = read_file(ctx, result->local, buf, len);
62         if (rc)
63                 goto out_clean;
64
65         return 0;
66
67 out_clean:
68         if (result->cleanup_local)
69                 unlink(result->local);
70         return -1;
71 }
72
73 static void iterate_parser_files(struct discover_context *ctx,
74                 const struct parser *parser)
75 {
76         const char * const *filename;
77         const char *path;
78
79         if (!parser->filenames)
80                 return;
81
82         for (filename = parser->filenames; *filename; filename++) {
83                 int rc, len;
84                 char *buf;
85
86                 path = local_path(ctx, ctx->device, *filename);
87                 if (!path)
88                         continue;
89
90                 rc = read_file(ctx, path, &buf, &len);
91                 if (!rc) {
92                         pb_log("Running parser %s on file %s\n",
93                                         parser->name, *filename);
94                         parser->parse(ctx, buf, len);
95                         talloc_free(buf);
96                 }
97         }
98 }
99
100 void iterate_parsers(struct discover_context *ctx, enum conf_method method)
101 {
102         struct p_item* i;
103         int rc, len;
104         char *buf;
105
106         pb_log("trying parsers for %s\n", ctx->device->device->id);
107
108         switch (method) {
109         case CONF_METHOD_LOCAL_FILE:
110                 list_for_each_entry(&parsers, i, list) {
111                         if (i->parser->method != CONF_METHOD_LOCAL_FILE)
112                                 continue;
113
114                         pb_log("\ttrying parser '%s'\n", i->parser->name);
115                         ctx->parser = i->parser;
116                         iterate_parser_files(ctx, ctx->parser);
117                 }
118                 ctx->parser = NULL;
119                 break;
120
121         case CONF_METHOD_DHCP:
122                 rc = download_config(ctx, &buf, &len);
123                 if (rc) {
124                         pb_log("\tdownload failed, aborting\n");
125                         return;
126                 }
127
128                 list_for_each_entry(&parsers, i, list) {
129                         if (i->parser->method != method)
130                                 continue;
131
132                         pb_log("\ttrying parser '%s'\n", i->parser->name);
133                         ctx->parser = i->parser;
134                         i->parser->parse(ctx, buf, len);
135                 }
136
137                 break;
138
139         case CONF_METHOD_UNKNOWN:
140                 break;
141
142         }
143 }
144
145 void __register_parser(struct parser *parser)
146 {
147         struct p_item* i = talloc(NULL, struct p_item);
148
149         printf("%s: %s\n", __func__, parser->name);
150
151         i->parser = parser;
152         list_add(&parsers, &i->list);
153 }
154
155 void parser_init(void)
156 {
157 }