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