]> git.ozlabs.org Git - petitboot/blob - devices/paths.c
Move path maniuplation functions to devices/paths.c
[petitboot] / devices / paths.c
1 #define _GNU_SOURCE
2
3 #include <string.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 #include "paths.h"
8
9 static char *mount_base;
10
11 struct device_map {
12         char *dev, *mnt;
13 };
14
15 #define DEVICE_MAP_SIZE 32
16 static struct device_map device_map[DEVICE_MAP_SIZE];
17
18 const char *mountpoint_for_device(const char *dev_path)
19 {
20         int i;
21         const char *basename;
22
23         /* shorten '/dev/foo' to 'foo' */
24         basename = strrchr(dev_path, '/');
25         if (basename)
26                 basename++;
27         else
28                 basename = dev_path;
29
30         /* check existing entries in the map */
31         for (i = 0; (i < DEVICE_MAP_SIZE) && device_map[i].dev; i++)
32                 if (!strcmp(device_map[i].dev, basename))
33                         return device_map[i].mnt;
34
35         if (i == DEVICE_MAP_SIZE)
36                 return NULL;
37
38         device_map[i].dev = strdup(dev_path);
39         asprintf(&device_map[i].mnt, "%s/%s", mount_base, basename);
40         return device_map[i].mnt;
41 }
42
43 char *resolve_path(const char *path, const char *current_mountpoint)
44 {
45         char *ret;
46         const char *devpath, *sep;
47
48         sep = strchr(path, ':');
49         if (!sep) {
50                 devpath = current_mountpoint;
51                 asprintf(&ret, "%s/%s", devpath, path);
52         } else {
53                 /* copy just the device name into tmp */
54                 char *dev = strndup(path, sep - path);
55                 devpath = mountpoint_for_device(dev);
56                 asprintf(&ret, "%s/%s", devpath, sep + 1);
57                 free(dev);
58         }
59
60         return ret;
61 }
62
63 void set_mount_base(const char *path)
64 {
65         if (mount_base)
66                 free(mount_base);
67         mount_base = strdup(path);
68 }
69