]> git.ozlabs.org Git - petitboot/blob - discover/parser.c
discover/devmapper: Retry dm-device remove if busy
[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 bool parser_is_unique(struct discover_context *ctx, struct discover_device *dev,
77         const char *filename, struct list *found_list)
78 {
79         struct stat stat;
80         struct parser_found_file *found_file;
81         const struct parser_found_file *entry;
82
83         if (parser_stat_path(ctx, dev, filename, &stat)) {
84                 pb_debug("%s: Not found: '%s'\n", __func__, filename);
85                 return false;
86         }
87
88         list_for_each_entry(found_list, entry, list) {
89                 if (entry->ino == stat.st_ino) {
90                         pb_log("%s: Duplicate: '%s' = '%s'\n",
91                                 __func__, filename, entry->filename);
92                         return false;
93                 }
94         }
95
96         pb_debug("%s: Found:     '%s'\n", __func__, filename);
97
98         found_file = talloc_zero(found_list, struct parser_found_file);
99         found_file->filename = talloc_strdup(found_file, filename);
100         found_file->ino = stat.st_ino;
101         list_add(found_list, &found_file->list);
102
103         return true;
104 }
105
106 int parser_replace_file(struct discover_context *ctx,
107                 struct discover_device *dev, const char *filename,
108                 char *buf, int len)
109 {
110         bool release;
111         char *path;
112         int rc;
113
114         if (!dev->mounted)
115                 return -1;
116
117         rc = device_request_write(dev, &release);
118         if (rc) {
119                 pb_log("Can't write file %s: device doesn't allow write\n",
120                                 dev->device_path);
121                 return -1;
122         }
123
124         path = local_path(ctx, dev, filename);
125
126         rc = replace_file(path, buf, len);
127
128         talloc_free(path);
129
130         device_release_write(dev, release);
131
132         return rc;
133 }
134
135 int parser_request_url(struct discover_context *ctx, struct pb_url *url,
136                 char **buf, int *len)
137 {
138         struct load_url_result *result;
139         int rc;
140
141         result = load_url(ctx, url);
142         if (!result)
143                 goto out;
144
145         rc = read_file(ctx, result->local, buf, len);
146         if (rc) {
147                 pb_log("Read failed for the parser %s on file %s\n",
148                                 ctx->parser->name, result->local);
149                 goto out_clean;
150         }
151
152         return 0;
153
154 out_clean:
155         if (result->cleanup_local)
156                 unlink(result->local);
157 out:
158         return -1;
159 }
160
161 int parser_scandir(struct discover_context *ctx, const char *dirname,
162                    struct dirent ***files, int (*filter)(const struct dirent *),
163                    int (*comp)(const struct dirent **, const struct dirent **))
164 {
165         char *path;
166         int n;
167
168         path = talloc_asprintf(ctx, "%s%s", ctx->device->mount_path, dirname);
169         if (!path)
170                 return -1;
171
172         n = scandir(path, files, filter, comp);
173         talloc_free(path);
174         return n;
175 }
176
177 void iterate_parsers(struct discover_context *ctx)
178 {
179         struct p_item* i;
180
181         pb_log("trying parsers for %s\n", ctx->device->device->id);
182
183         list_for_each_entry(&parsers, i, list) {
184                 pb_debug("\ttrying parser '%s'\n", i->parser->name);
185                 ctx->parser = i->parser;
186                 i->parser->parse(ctx);
187         }
188         ctx->parser = NULL;
189 }
190
191 static void *parsers_ctx;
192
193 void __register_parser(struct parser *parser)
194 {
195         struct p_item *i;
196
197         if (!parsers_ctx)
198                 parsers_ctx = talloc_new(NULL);
199
200         i = talloc(parsers_ctx, struct p_item);
201         i->parser = parser;
202         list_add(&parsers, &i->list);
203 }
204
205 static __attribute__((destructor)) void cleanup_parsers(void)
206 {
207         talloc_free(parsers_ctx);
208 }
209
210 void parser_init(void)
211 {
212 }