]> git.ozlabs.org Git - petitboot/blob - discover/parser.c
c04a0afca610b31bfb07468824770445cfc94d92
[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
16 static int n_parsers;
17 static struct parser **parsers;
18
19 static const int max_file_size = 1024 * 1024;
20
21 static int read_file(struct discover_context *ctx,
22                 const char *filename, char **bufp, int *lenp)
23 {
24         struct stat statbuf;
25         int rc, fd, i, len;
26         char *buf;
27
28         fd = open(filename, O_RDONLY);
29         if (fd < 0)
30                 return -1;
31
32         rc = fstat(fd, &statbuf);
33         if (rc < 0)
34                 goto err_close;
35
36         len = statbuf.st_size;
37         if (len > max_file_size)
38                 goto err_close;
39
40         buf = talloc_array(ctx, char, len);
41         if (!buf)
42                 goto err_close;
43
44         for (i = 0; i < len; i += rc) {
45                 rc = read(fd, buf + i, len - i);
46
47                 /* unexpected EOF: trim and return */
48                 if (rc == 0) {
49                         len = i;
50                         break;
51                 }
52
53                 if (rc < 0)
54                         goto err_free;
55
56         }
57
58         close(fd);
59         *bufp = buf;
60         *lenp = len;
61         return 0;
62
63 err_free:
64         talloc_free(buf);
65 err_close:
66         close(fd);
67         return -1;
68 }
69
70 static char *local_path(struct discover_context *ctx,
71                 const char *filename)
72 {
73         return join_paths(ctx, ctx->device->mount_path, filename);
74 }
75
76 static void iterate_parser_files(struct discover_context *ctx,
77                 const struct parser *parser)
78 {
79         const char * const *filename;
80         const char *path;
81
82         if (!parser->filenames)
83                 return;
84
85         for (filename = parser->filenames; *filename; filename++) {
86                 int rc, len;
87                 char *buf;
88
89                 path = local_path(ctx, *filename);
90                 if (!path)
91                         continue;
92
93                 rc = read_file(ctx, path, &buf, &len);
94                 if (!rc) {
95                         parser->parse(ctx, buf, len);
96                         talloc_free(buf);
97                 }
98         }
99 }
100
101 void iterate_parsers(struct discover_context *ctx, enum conf_method method)
102 {
103         int i;
104
105         pb_log("trying parsers for %s\n", ctx->device->device->id);
106
107         if (method == CONF_METHOD_LOCAL_FILE) {
108                 for (i = 0; i < n_parsers; i++) {
109                         if (parsers[i]->method != CONF_METHOD_LOCAL_FILE)
110                                 continue;
111
112                         pb_log("\ttrying parser '%s'\n", parsers[i]->name);
113                         ctx->parser = parsers[i];
114                         iterate_parser_files(ctx, ctx->parser);
115                 }
116                 ctx->parser = NULL;
117         }
118 }
119
120 void __register_parser(struct parser *parser)
121 {
122         parsers = talloc_realloc(NULL, parsers, struct parser *, n_parsers + 1);
123         parsers[n_parsers] = parser;
124         n_parsers++;
125 }
126
127 void parser_init(void)
128 {
129 }