]> git.ozlabs.org Git - petitboot/blob - discover/parser.c
discover: Support DHCP "pathprefix" configuration option
[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 int parser_replace_file(struct discover_context *ctx,
53                 struct discover_device *dev, const char *filename,
54                 char *buf, int len)
55 {
56         bool release;
57         char *path;
58         int rc;
59
60         if (!dev->mounted)
61                 return -1;
62
63         rc = device_request_write(dev, &release);
64         if (rc) {
65                 pb_log("Can't write file %s: device doesn't allow write\n",
66                                 dev->device_path);
67                 return -1;
68         }
69
70         path = local_path(ctx, dev, filename);
71
72         rc = replace_file(path, buf, len);
73
74         talloc_free(path);
75
76         device_release_write(dev, release);
77
78         return rc;
79 }
80
81 int parser_request_url(struct discover_context *ctx, struct pb_url *url,
82                 char **buf, int *len)
83 {
84         struct load_url_result *result;
85         int rc;
86
87         result = load_url(ctx, url);
88         if (!result)
89                 goto out;
90
91         rc = read_file(ctx, result->local, buf, len);
92         if (rc) {
93                 pb_log("Read failed for the parser %s on file %s\n",
94                                 ctx->parser->name, result->local);
95                 goto out_clean;
96         }
97
98         return 0;
99
100 out_clean:
101         if (result->cleanup_local)
102                 unlink(result->local);
103 out:
104         return -1;
105 }
106
107 void iterate_parsers(struct discover_context *ctx)
108 {
109         struct p_item* i;
110
111         pb_log("trying parsers for %s\n", ctx->device->device->id);
112
113         list_for_each_entry(&parsers, i, list) {
114                 pb_debug("\ttrying parser '%s'\n", i->parser->name);
115                 ctx->parser = i->parser;
116                 i->parser->parse(ctx);
117         }
118         ctx->parser = NULL;
119 }
120
121 static void *parsers_ctx;
122
123 void __register_parser(struct parser *parser)
124 {
125         struct p_item *i;
126
127         if (!parsers_ctx)
128                 parsers_ctx = talloc_new(NULL);
129
130         i = talloc(parsers_ctx, struct p_item);
131         i->parser = parser;
132         list_add(&parsers, &i->list);
133 }
134
135 static __attribute__((destructor)) void cleanup_parsers(void)
136 {
137         talloc_free(parsers_ctx);
138 }
139
140 void parser_init(void)
141 {
142 }