Resolve device paths in kernel and initrd locations.
[petitboot] / devices / parser-test.c
1 #define _GNU_SOURCE
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <stdarg.h>
6 #include <unistd.h>
7 #include <string.h>
8
9 #include "parser.h"
10
11 void pb_log(const char *fmt, ...)
12 {
13         va_list ap;
14
15         va_start(ap, fmt);
16         vfprintf(stderr, fmt, ap);
17         va_end(ap);
18 }
19
20
21 int mount_device(const char *dev_path)
22 {
23         pb_log("attempt to mount device (%s) not supported\n", dev_path);
24         return 0;
25 }
26
27 int add_device(const struct device *dev)
28 {
29         printf("device added:\n");
30         printf("\tid: %s\n", dev->id);
31         printf("\tname: %s\n", dev->name);
32         printf("\tdescription: %s\n", dev->description);
33         printf("\tboot_image: %s\n", dev->icon_file);
34         return 0;
35 }
36
37 int add_boot_option(const struct boot_option *opt)
38 {
39         printf("option added:\n");
40         printf("\tname: %s\n", opt->name);
41         printf("\tdescription: %s\n", opt->description);
42         printf("\tboot_image: %s\n", opt->boot_image_file);
43         printf("\tinitrd: %s\n", opt->initrd_file);
44         printf("\tboot_args: %s\n", opt->boot_args);
45         return 0;
46 }
47
48 enum generic_icon_type guess_device_type(void)
49 {
50         return ICON_TYPE_UNKNOWN;
51 }
52
53 static char *mountpoint;
54
55 const char *mountpoint_for_device(const char *dev_path)
56 {
57         char *tmp, *dev;
58         dev = strrchr(dev_path, '/');
59         if (dev)
60                 dev_path = dev + 1;
61         asprintf(&tmp, "%s/%s", mountpoint, dev_path);
62         return tmp;
63 }
64
65 char *resolve_path(const char *path, const char *default_mountpoint)
66 {
67         char *sep, *ret;
68         const char *devpath;
69
70         sep = strchr(path, ':');
71         if (!sep) {
72                 devpath = default_mountpoint;
73                 asprintf(&ret, "%s/%s", devpath, path);
74         } else {
75                 char *tmp = strndup(path, sep - path);
76                 devpath = mountpoint_for_device(path);
77                 asprintf(&ret, "%s/%s", devpath, sep + 1);
78                 free(tmp);
79         }
80
81         return ret;
82 }
83
84 int main(int argc, char **argv)
85 {
86         const char *dev = "sda1";
87
88         if (argc != 2) {
89                 fprintf(stderr, "usage: %s <fake-mountpoint>\n", argv[0]);
90                 return EXIT_FAILURE;
91         }
92
93         mountpoint = argv[1];
94
95         iterate_parsers(dev, mountpoint);
96
97         return EXIT_SUCCESS;
98 }