]> git.ozlabs.org Git - petitboot/blob - discover/paths.c
Add URL test to resolve_path
[petitboot] / discover / paths.c
1 #define _GNU_SOURCE
2
3 #include <string.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 #include <talloc/talloc.h>
8
9 #include "paths.h"
10
11 #define DEVICE_MOUNT_BASE (LOCAL_STATE_DIR "/petitboot/mnt")
12
13 struct mount_map {
14         char *dev, *mnt;
15 };
16
17 static struct mount_map *mount_map;
18 static int mount_map_size;
19
20 static int is_prefix(const char *str, const char *prefix)
21 {
22         return !strncmp(str, prefix, strlen(prefix));
23 }
24
25 static int is_prefix_ignorecase(const char *str, const char *prefix)
26 {
27         return !strncasecmp(str, prefix, strlen(prefix));
28 }
29
30 const char *mount_base(void)
31 {
32         return DEVICE_MOUNT_BASE;
33 }
34
35 char *encode_label(void *alloc_ctx, const char *label)
36 {
37         char *str, *c;
38         unsigned int i;
39
40         /* the label can be expanded by up to four times */
41         str = talloc_size(alloc_ctx, strlen(label) * 4 + 1);
42         c = str;
43
44         for (i = 0; i < strlen(label); i++) {
45
46                 if (label[i] == '/' || label[i] == '\\') {
47                         sprintf(c, "\\x%02x", label[i]);
48                         c += 4;
49                         continue;
50                 }
51
52                 *(c++) = label[i];
53         }
54
55         *c = '\0';
56
57         return str;
58 }
59
60 char *parse_device_path(void *alloc_ctx,
61                 const char *dev_str, const char *cur_dev)
62 {
63         char *dev, tmp[256], *enc;
64
65         if (is_prefix_ignorecase(dev_str, "uuid=")) {
66                 dev = talloc_asprintf(alloc_ctx, "/dev/disk/by-uuid/%s",
67                                 dev_str + strlen("uuid="));
68                 return dev;
69         }
70
71         if (is_prefix_ignorecase(dev_str, "label=")) {
72                 enc = encode_label(NULL, dev_str + strlen("label="));
73                 dev = talloc_asprintf(alloc_ctx, "/dev/disk/by-label/%s", enc);
74                 talloc_free(enc);
75                 return dev;
76         }
77
78         /* normalise '/dev/foo' to 'foo' for easy comparisons, we'll expand
79          * back before returning.
80          */
81         if (is_prefix(dev_str, "/dev/"))
82                 dev_str += strlen("/dev/");
83
84         /* PS3 hack: if we're reading from a ps3dx device, and we refer to
85          * a sdx device, remap to ps3dx */
86         if (cur_dev && is_prefix(cur_dev, "/dev/ps3d")
87                         && is_prefix(dev_str, "sd")) {
88                 snprintf(tmp, 255, "ps3d%s", dev_str + 2);
89                 dev_str = tmp;
90         }
91
92         return join_paths(alloc_ctx, "/dev", dev_str);
93 }
94
95 const char *mountpoint_for_device(const char *dev)
96 {
97         int i;
98
99         if (is_prefix(dev, "/dev/"))
100                 dev += strlen("/dev/");
101
102         /* check existing entries in the map */
103         for (i = 0; i < mount_map_size; i++)
104                 if (!strcmp(mount_map[i].dev, dev))
105                         return mount_map[i].mnt;
106
107         /* no existing entry, create a new one */
108         i = mount_map_size++;
109         mount_map = talloc_realloc(NULL, mount_map,
110                         struct mount_map, mount_map_size);
111
112         mount_map[i].dev = talloc_strdup(mount_map, dev);
113         mount_map[i].mnt = join_paths(mount_map, DEVICE_MOUNT_BASE, dev);
114         return mount_map[i].mnt;
115 }
116
117 char *resolve_path(void *alloc_ctx, const char *path, const char *current_dev)
118 {
119         static const char s_file[] = "file://";
120         char *ret;
121         const char *devpath, *sep;
122
123         /* test for urls */
124
125         if (!strncasecmp(path, s_file, sizeof(s_file) - 1))
126                 path += sizeof(s_file) - 1;
127         else if (strstr(path, "://"))
128                 return talloc_strdup(alloc_ctx, path);
129
130         sep = strchr(path, ':');
131         if (!sep) {
132                 devpath = mountpoint_for_device(current_dev);
133                 ret = join_paths(alloc_ctx, devpath, path);
134         } else {
135                 /* parse just the device name into dev */
136                 char *tmp, *dev;
137                 tmp = talloc_strndup(NULL, path, sep - path);
138                 dev = parse_device_path(NULL, tmp, current_dev);
139
140                 devpath = mountpoint_for_device(dev);
141                 ret = join_paths(alloc_ctx, devpath, sep + 1);
142
143                 talloc_free(dev);
144                 talloc_free(tmp);
145         }
146
147         return ret;
148 }
149
150 char *join_paths(void *alloc_ctx, const char *a, const char *b)
151 {
152         char *full_path;
153
154         full_path = talloc_array(alloc_ctx, char, strlen(a) + strlen(b) + 2);
155
156         strcpy(full_path, a);
157         if (b[0] != '/' && a[strlen(a) - 1] != '/')
158                 strcat(full_path, "/");
159         strcat(full_path, b);
160
161         return full_path;
162 }
163