]> git.ozlabs.org Git - petitboot/blob - discover/parser.c
discover/paths: Parse Busybox progress information
[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 void iterate_parsers(struct discover_context *ctx)
132 {
133         struct p_item* i;
134
135         pb_log("trying parsers for %s\n", ctx->device->device->id);
136
137         list_for_each_entry(&parsers, i, list) {
138                 pb_debug("\ttrying parser '%s'\n", i->parser->name);
139                 ctx->parser = i->parser;
140                 i->parser->parse(ctx);
141         }
142         ctx->parser = NULL;
143 }
144
145 static void *parsers_ctx;
146
147 void __register_parser(struct parser *parser)
148 {
149         struct p_item *i;
150
151         if (!parsers_ctx)
152                 parsers_ctx = talloc_new(NULL);
153
154         i = talloc(parsers_ctx, struct p_item);
155         i->parser = parser;
156         list_add(&parsers, &i->list);
157 }
158
159 static __attribute__((destructor)) void cleanup_parsers(void)
160 {
161         talloc_free(parsers_ctx);
162 }
163
164 void parser_init(void)
165 {
166 }