7 #include <talloc/talloc.h>
11 #define DEVICE_MOUNT_BASE (LOCAL_STATE_DIR "/petitboot/mnt")
17 static struct mount_map *mount_map;
18 static int mount_map_size;
20 static int is_prefix(const char *str, const char *prefix)
22 return !strncmp(str, prefix, strlen(prefix));
25 static int is_prefix_ignorecase(const char *str, const char *prefix)
27 return !strncasecmp(str, prefix, strlen(prefix));
30 const char *mount_base(void)
32 return DEVICE_MOUNT_BASE;
35 char *encode_label(void *alloc_ctx, const char *label)
40 /* the label can be expanded by up to four times */
41 str = talloc_size(alloc_ctx, strlen(label) * 4 + 1);
44 for (i = 0; i < strlen(label); i++) {
46 if (label[i] == '/' || label[i] == '\\') {
47 sprintf(c, "\\x%02x", label[i]);
60 char *parse_device_path(void *alloc_ctx,
61 const char *dev_str, const char *cur_dev)
63 char *dev, tmp[256], *enc;
65 if (is_prefix_ignorecase(dev_str, "uuid=")) {
66 dev = talloc_asprintf(alloc_ctx, "/dev/disk/by-uuid/%s",
67 dev_str + strlen("uuid="));
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);
78 /* normalise '/dev/foo' to 'foo' for easy comparisons, we'll expand
79 * back before returning.
81 if (is_prefix(dev_str, "/dev/"))
82 dev_str += strlen("/dev/");
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);
92 return join_paths(alloc_ctx, "/dev", dev_str);
95 const char *mountpoint_for_device(const char *dev)
99 if (is_prefix(dev, "/dev/"))
100 dev += strlen("/dev/");
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;
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);
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;
117 char *resolve_path(void *alloc_ctx, const char *path, const char *current_dev)
119 static const char s_file[] = "file://";
121 const char *devpath, *sep;
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);
130 sep = strchr(path, ':');
132 devpath = mountpoint_for_device(current_dev);
133 ret = join_paths(alloc_ctx, devpath, path);
135 /* parse just the device name into dev */
137 tmp = talloc_strndup(NULL, path, sep - path);
138 dev = parse_device_path(NULL, tmp, current_dev);
140 devpath = mountpoint_for_device(dev);
141 ret = join_paths(alloc_ctx, devpath, sep + 1);
150 char *join_paths(void *alloc_ctx, const char *a, const char *b)
154 full_path = talloc_array(alloc_ctx, char, strlen(a) + strlen(b) + 2);
156 strcpy(full_path, a);
157 if (b[0] != '/' && a[strlen(a) - 1] != '/')
158 strcat(full_path, "/");
159 strcat(full_path, b);