1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <sys/mount.h>
16#if !defined(BLKGETSIZE64)
17# define BLKGETSIZE64 _IOR(0x12,114,size_t)
18#endif
19#include "volume_id_internal.h"
20
21static struct uuidCache_s {
22 struct uuidCache_s *next;
23
24 char *device;
25 char *label;
26 char *uc_uuid;
27 IF_FEATURE_BLKID_TYPE(const char *type;)
28} *uuidCache;
29
30#if !ENABLE_FEATURE_BLKID_TYPE
31#define get_label_uuid(fd, label, uuid, type) \
32 get_label_uuid(fd, label, uuid)
33#define uuidcache_addentry(device, label, uuid, type) \
34 uuidcache_addentry(device, label, uuid)
35#endif
36
37
38
39
40
41static int
42get_label_uuid(int fd, char **label, char **uuid, const char **type)
43{
44 int rv = 1;
45 uint64_t size;
46 struct volume_id *vid;
47
48
49 vid = volume_id_open_node(fd);
50
51 if (ioctl(fd, BLKGETSIZE64, &size) != 0)
52 size = 0;
53
54 if (volume_id_probe_all(vid, size) != 0)
55 goto ret;
56
57 if (vid->label[0] != '\0' || vid->uuid[0] != '\0'
58#if ENABLE_FEATURE_BLKID_TYPE
59 || vid->type != NULL
60#endif
61 ) {
62 *label = xstrndup(vid->label, sizeof(vid->label));
63 *uuid = xstrndup(vid->uuid, sizeof(vid->uuid));
64#if ENABLE_FEATURE_BLKID_TYPE
65 *type = vid->type;
66 dbg("found label '%s', uuid '%s', type '%s'", *label, *uuid, *type);
67#else
68 dbg("found label '%s', uuid '%s'", *label, *uuid);
69#endif
70 rv = 0;
71 }
72 ret:
73 free_volume_id(vid);
74 return rv;
75}
76
77
78static void
79uuidcache_addentry(char *device, char *label, char *uuid, const char *type)
80{
81 struct uuidCache_s *last;
82
83 if (!uuidCache) {
84 last = uuidCache = xzalloc(sizeof(*uuidCache));
85 } else {
86 for (last = uuidCache; last->next; last = last->next)
87 continue;
88 last->next = xzalloc(sizeof(*uuidCache));
89 last = last->next;
90 }
91
92
93
94 last->device = device;
95 last->label = label;
96 last->uc_uuid = uuid;
97 IF_FEATURE_BLKID_TYPE(last->type = type;)
98}
99
100
101
102
103static int FAST_FUNC
104uuidcache_check_device(const char *device,
105 struct stat *statbuf,
106 void *userData UNUSED_PARAM,
107 int depth UNUSED_PARAM)
108{
109
110 if (!S_ISBLK(statbuf->st_mode))
111 return TRUE;
112
113
114
115
116
117
118 if (major(statbuf->st_rdev) == 2)
119 return TRUE;
120
121 add_to_uuid_cache(device);
122
123 return TRUE;
124}
125
126static struct uuidCache_s*
127uuidcache_init(int scan_devices)
128{
129 dbg("DBG: uuidCache=%x, uuidCache");
130 if (uuidCache)
131 return uuidCache;
132
133
134
135
136
137
138
139
140
141
142
143 if (scan_devices)
144 recursive_action("/dev", ACTION_RECURSE,
145 uuidcache_check_device,
146 NULL,
147 NULL,
148 0 );
149
150 return uuidCache;
151}
152
153#define UUID 1
154#define VOL 2
155
156#ifdef UNUSED
157static char *
158get_spec_by_x(int n, const char *t, int *majorPtr, int *minorPtr)
159{
160 struct uuidCache_s *uc;
161
162 uc = uuidcache_init( 1);
163 while (uc) {
164 switch (n) {
165 case UUID:
166 if (strcmp(t, uc->uc_uuid) == 0) {
167 *majorPtr = uc->major;
168 *minorPtr = uc->minor;
169 return uc->device;
170 }
171 break;
172 case VOL:
173 if (strcmp(t, uc->label) == 0) {
174 *majorPtr = uc->major;
175 *minorPtr = uc->minor;
176 return uc->device;
177 }
178 break;
179 }
180 uc = uc->next;
181 }
182 return NULL;
183}
184
185static unsigned char
186fromhex(char c)
187{
188 if (isdigit(c))
189 return (c - '0');
190 return ((c|0x20) - 'a' + 10);
191}
192
193static char *
194get_spec_by_uuid(const char *s, int *major, int *minor)
195{
196 unsigned char uuid[16];
197 int i;
198
199 if (strlen(s) != 36 || s[8] != '-' || s[13] != '-'
200 || s[18] != '-' || s[23] != '-'
201 ) {
202 goto bad_uuid;
203 }
204 for (i = 0; i < 16; i++) {
205 if (*s == '-')
206 s++;
207 if (!isxdigit(s[0]) || !isxdigit(s[1]))
208 goto bad_uuid;
209 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
210 s += 2;
211 }
212 return get_spec_by_x(UUID, (char *)uuid, major, minor);
213
214 bad_uuid:
215 fprintf(stderr, _("mount: bad UUID"));
216 return 0;
217}
218
219static char *
220get_spec_by_volume_label(const char *s, int *major, int *minor)
221{
222 return get_spec_by_x(VOL, s, major, minor);
223}
224#endif
225
226
227void display_uuid_cache(int scan_devices)
228{
229 struct uuidCache_s *uc;
230
231 uc = uuidcache_init(scan_devices);
232 while (uc) {
233 printf("%s:", uc->device);
234 if (uc->label[0])
235 printf(" LABEL=\"%s\"", uc->label);
236 if (uc->uc_uuid[0])
237 printf(" UUID=\"%s\"", uc->uc_uuid);
238#if ENABLE_FEATURE_BLKID_TYPE
239 if (uc->type)
240 printf(" TYPE=\"%s\"", uc->type);
241#endif
242 bb_putchar('\n');
243 uc = uc->next;
244 }
245}
246
247int add_to_uuid_cache(const char *device)
248{
249 char *uuid = uuid;
250 char *label = label;
251#if ENABLE_FEATURE_BLKID_TYPE
252 const char *type = type;
253#endif
254 int fd;
255
256 fd = open(device, O_RDONLY);
257 if (fd < 0)
258 return 0;
259
260
261 if (get_label_uuid(fd, &label, &uuid, &type) == 0) {
262
263 uuidcache_addentry(xstrdup(device), label, uuid, type);
264 return 1;
265 }
266 return 0;
267}
268
269
270
271
272char *get_devname_from_label(const char *spec)
273{
274 struct uuidCache_s *uc;
275
276 uc = uuidcache_init( 1);
277 while (uc) {
278 if (uc->label[0] && strcmp(spec, uc->label) == 0) {
279 return xstrdup(uc->device);
280 }
281 uc = uc->next;
282 }
283 return NULL;
284}
285
286char *get_devname_from_uuid(const char *spec)
287{
288 struct uuidCache_s *uc;
289
290 uc = uuidcache_init( 1);
291 while (uc) {
292
293 if (strcasecmp(spec, uc->uc_uuid) == 0) {
294 return xstrdup(uc->device);
295 }
296 uc = uc->next;
297 }
298 return NULL;
299}
300
301int resolve_mount_spec(char **fsname)
302{
303 char *tmp = *fsname;
304
305 if (is_prefixed_with(*fsname, "UUID="))
306 tmp = get_devname_from_uuid(*fsname + 5);
307 else if (is_prefixed_with(*fsname, "LABEL="))
308 tmp = get_devname_from_label(*fsname + 6);
309
310 if (tmp == *fsname)
311 return 0;
312
313 if (tmp)
314 *fsname = tmp;
315 return 1;
316}
317