]> git.ozlabs.org Git - petitboot/blob - discover/parser.c
utils/pb-udhcpc: Recognise DHCPv6 parameters
[petitboot] / discover / parser.c
1
2 #include <fcntl.h>
3 #include <stdlib.h>
4
5 #include "types/types.h"
6 #include <file/file.h>
7 #include <log/log.h>
8 #include <talloc/talloc.h>
9
10 #include "device-handler.h"
11 #include "parser.h"
12 #include "parser-utils.h"
13 #include "paths.h"
14
15 struct p_item {
16         struct list_item list;
17         struct parser *parser;
18 };
19
20 STATIC_LIST(parsers);
21
22 static char *local_path(struct discover_context *ctx,
23                 struct discover_device *dev,
24                 const char *filename)
25 {
26         return join_paths(ctx, dev->root_path, filename);
27 }
28
29 int parser_request_file(struct discover_context *ctx,
30                 struct discover_device *dev, const char *filename,
31                 char **buf, int *len)
32
33 {
34         char *path;
35         int rc;
36
37         /* we only support local files at present */
38         if (!dev->mount_path)
39                 return -1;
40
41         path = local_path(ctx, dev, filename);
42
43         rc = read_file(ctx, path, buf, len);
44
45         talloc_free(path);
46
47         return rc;
48 }
49
50 int parser_stat_path(struct discover_context *ctx,
51                 struct discover_device *dev, const char *path,
52                 struct stat *statbuf)
53 {
54         int rc = -1;
55         char *full_path;
56
57         /* we only support local files at present */
58         if (!dev->mount_path)
59                 return -1;
60
61         full_path = local_path(ctx, dev, path);
62
63         rc = stat(full_path, statbuf);
64         if (rc) {
65                 rc = -1;
66                 goto out;
67         }
68
69         rc = 0;
70 out:
71         talloc_free(full_path);
72
73         return rc;
74 }
75
76 int parser_replace_file(struct discover_context *ctx,
77                 struct discover_device *dev, const char *filename,
78                 char *buf, int len)
79 {
80         bool release;
81         char *path;
82         int rc;
83
84         if (!dev->mounted)
85                 return -1;
86
87         rc = device_request_write(dev, &release);
88         if (rc) {
89                 pb_log("Can't write file %s: device doesn't allow write\n",
90                                 dev->device_path);
91                 return -1;
92         }
93
94         path = local_path(ctx, dev, filename);
95
96         rc = replace_file(path, buf, len);
97
98         talloc_free(path);
99
100         device_release_write(dev, release);
101
102         return rc;
103 }
104
105 int parser_request_url(struct discover_context *ctx, struct pb_url *url,
106                 char **buf, int *len)
107 {
108         struct load_url_result *result;
109         int rc;
110
111         result = load_url(ctx, url);
112         if (!result)
113                 goto out;
114
115         rc = read_file(ctx, result->local, buf, len);
116         if (rc) {
117                 pb_log("Read failed for the parser %s on file %s\n",
118                                 ctx->parser->name, result->local);
119                 goto out_clean;
120         }
121
122         return 0;
123
124 out_clean:
125         if (result->cleanup_local)
126                 unlink(result->local);
127 out:
128         return -1;
129 }
130
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 **))
134 {
135         char *path;
136         int n;
137
138         path = talloc_asprintf(ctx, "%s%s", ctx->device->mount_path, dirname);
139         if (!path)
140                 return -1;
141
142         n = scandir(path, files, filter, comp);
143         talloc_free(path);
144         return n;
145 }
146
147 void iterate_parsers(struct discover_context *ctx)
148 {
149         struct p_item* i;
150
151         pb_log("trying parsers for %s\n", ctx->device->device->id);
152
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);
157         }
158         ctx->parser = NULL;
159 }
160
161 static void *parsers_ctx;
162
163 void __register_parser(struct parser *parser)
164 {
165         struct p_item *i;
166
167         if (!parsers_ctx)
168                 parsers_ctx = talloc_new(NULL);
169
170         i = talloc(parsers_ctx, struct p_item);
171         i->parser = parser;
172         list_add(&parsers, &i->list);
173 }
174
175 static __attribute__((destructor)) void cleanup_parsers(void)
176 {
177         talloc_free(parsers_ctx);
178 }
179
180 void parser_init(void)
181 {
182 }