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