]> git.ozlabs.org Git - petitboot/commitdiff
discover: Add parser_request_file
authorJeremy Kerr <jk@ozlabs.org>
Fri, 27 Sep 2013 06:19:20 +0000 (14:19 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Tue, 1 Oct 2013 04:52:00 +0000 (12:52 +0800)
Add a function to allow parsers to access files on a local device.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
discover/parser.c
discover/parser.h
test/parser/parser-test.h
test/parser/utils.c

index a304f0c989e32f4e124d1f62647547b067d952b5..d34600d18f1526856945689f4fbb0723cbdefc28 100644 (file)
@@ -22,9 +22,31 @@ struct p_item {
 STATIC_LIST(parsers);
 
 static char *local_path(struct discover_context *ctx,
+               struct discover_device *dev,
                const char *filename)
 {
-       return join_paths(ctx, ctx->device->mount_path, filename);
+       return join_paths(ctx, dev->mount_path, filename);
+}
+
+int parser_request_file(struct discover_context *ctx,
+               struct discover_device *dev, const char *filename,
+               char **buf, int *len)
+
+{
+       char *path;
+       int rc;
+
+       /* we only support local files at present */
+       if (!dev->mount_path)
+               return -1;
+
+       path = local_path(ctx, dev, filename);
+
+       rc = read_file(ctx, path, buf, len);
+
+       talloc_free(path);
+
+       return rc;
 }
 
 static int download_config(struct discover_context *ctx, char **buf, int *len)
@@ -61,7 +83,7 @@ static void iterate_parser_files(struct discover_context *ctx,
                int rc, len;
                char *buf;
 
-               path = local_path(ctx, *filename);
+               path = local_path(ctx, ctx->device, *filename);
                if (!path)
                        continue;
 
index b738577ff3d9d4a17a41bdecb3a5247774d51ad9..c1b4012d67cb29764fbb499cb27224555137b4d8 100644 (file)
@@ -51,4 +51,8 @@ void parser_init(void);
 void iterate_parsers(struct discover_context *ctx, enum conf_method method);
 int parse_user_event(struct discover_context *ctx, struct event *event);
 
+int parser_request_file(struct discover_context *ctx,
+               struct discover_device *dev, const char *filename,
+               char **buf, int *len);
+
 #endif /* _PARSER_H */
index 7e43a6843b8386eb0ad3586251292c80fefec41a..eb43c25c7c128eb57c24d4516432f4334ee3bcf2 100644 (file)
@@ -9,6 +9,7 @@
 struct parser_test {
        struct device_handler *handler;
        struct discover_context *ctx;
+       struct list files;
        struct config *config;
        struct {
                void    *buf;
@@ -35,6 +36,12 @@ int test_run_parser(struct parser_test *test, const char *parser_name);
 
 void test_hotplug_device(struct parser_test *test, struct discover_device *dev);
 
+void test_add_file_data(struct parser_test *test, struct discover_device *dev,
+               const char *filename, void *data, int size);
+
+#define test_add_file_string(test, dev, filename, str) \
+       test_add_file_data(test, dev, filename, str, sizeof(str))
+
 struct discover_boot_option *get_boot_option(struct discover_context *ctx,
                int idx);
 
index 40737c4f17c8ef6dd0ca00b8ace225f60c7caf3d..d1ced73a6365fff837902fd027811a1d0db500e4 100644 (file)
@@ -22,6 +22,14 @@ struct p_item {
        struct parser *parser;
 };
 
+struct test_file {
+       struct discover_device  *dev;
+       const char              *name;
+       void                    *data;
+       int                     size;
+       struct list_item        list;
+};
+
 STATIC_LIST(parsers);
 
 void __register_parser(struct parser *parser)
@@ -91,6 +99,7 @@ struct parser_test *test_init(void)
        test->config = test_config_init(test);
        test->handler = device_handler_init(NULL, NULL, 0);
        test->ctx = test_create_context(test);
+       list_init(&test->files);
 
        return test;
 }
@@ -142,6 +151,41 @@ void test_set_conf_source(struct parser_test *test, const char *url)
        assert(test->ctx->conf_url);
 }
 
+void test_add_file_data(struct parser_test *test, struct discover_device *dev,
+               const char *filename, void *data, int size)
+{
+       struct test_file *file;
+
+       file = talloc_zero(test, struct test_file);
+       file->dev = dev;
+       file->name = filename;
+       file->data = data;
+       file->size = size;
+       list_add(&test->files, &file->list);
+}
+
+
+int parser_request_file(struct discover_context *ctx,
+               struct discover_device *dev, const char *filename,
+               char **buf, int *len)
+{
+       struct parser_test *test = ctx->test_data;
+       struct test_file *file;
+
+       list_for_each_entry(&test->files, file, list) {
+               if (file->dev != dev)
+                       continue;
+               if (strcmp(file->name, filename))
+                       continue;
+
+               *buf = talloc_memdup(test, file->data, file->size);
+               *len = file->size;
+               return 0;
+       }
+
+       return -1;
+}
+
 int test_run_parser(struct parser_test *test, const char *parser_name)
 {
        struct p_item* i;