aboutsummaryrefslogtreecommitdiff
path: root/util-linux/volume_id/get_devname.c
diff options
context:
space:
mode:
Diffstat (limited to 'util-linux/volume_id/get_devname.c')
-rw-r--r--util-linux/volume_id/get_devname.c59
1 files changed, 45 insertions, 14 deletions
diff --git a/util-linux/volume_id/get_devname.c b/util-linux/volume_id/get_devname.c
index bf32e6a8c..7c9930543 100644
--- a/util-linux/volume_id/get_devname.c
+++ b/util-linux/volume_id/get_devname.c
@@ -19,14 +19,22 @@ static struct uuidCache_s {
19 char *device; 19 char *device;
20 char *label; 20 char *label;
21 char *uc_uuid; /* prefix makes it easier to grep for */ 21 char *uc_uuid; /* prefix makes it easier to grep for */
22 IF_FEATURE_BLKID_TYPE(const char *type;)
22} *uuidCache; 23} *uuidCache;
23 24
25#if !ENABLE_FEATURE_BLKID_TYPE
26#define get_label_uuid(fd, label, uuid, type) \
27 get_label_uuid(fd, label, uuid)
28#define uuidcache_addentry(device, label, uuid, type) \
29 uuidcache_addentry(device, label, uuid)
30#endif
31
24/* Returns !0 on error. 32/* Returns !0 on error.
25 * Otherwise, returns malloc'ed strings for label and uuid 33 * Otherwise, returns malloc'ed strings for label and uuid
26 * (and they can't be NULL, although they can be ""). 34 * (and they can't be NULL, although they can be "").
27 * NB: closes fd. */ 35 * NB: closes fd. */
28static int 36static int
29get_label_uuid(int fd, char **label, char **uuid) 37get_label_uuid(int fd, char **label, char **uuid, const char **type)
30{ 38{
31 int rv = 1; 39 int rv = 1;
32 uint64_t size; 40 uint64_t size;
@@ -44,7 +52,12 @@ get_label_uuid(int fd, char **label, char **uuid)
44 if (vid->label[0] != '\0' || vid->uuid[0] != '\0') { 52 if (vid->label[0] != '\0' || vid->uuid[0] != '\0') {
45 *label = xstrndup(vid->label, sizeof(vid->label)); 53 *label = xstrndup(vid->label, sizeof(vid->label));
46 *uuid = xstrndup(vid->uuid, sizeof(vid->uuid)); 54 *uuid = xstrndup(vid->uuid, sizeof(vid->uuid));
55#if ENABLE_FEATURE_BLKID_TYPE
56 *type = vid->type;
57 dbg("found label '%s', uuid '%s', type '%s'", *label, *uuid, *type);
58#else
47 dbg("found label '%s', uuid '%s'", *label, *uuid); 59 dbg("found label '%s', uuid '%s'", *label, *uuid);
60#endif
48 rv = 0; 61 rv = 0;
49 } 62 }
50 ret: 63 ret:
@@ -54,7 +67,7 @@ get_label_uuid(int fd, char **label, char **uuid)
54 67
55/* NB: we take ownership of (malloc'ed) label and uuid */ 68/* NB: we take ownership of (malloc'ed) label and uuid */
56static void 69static void
57uuidcache_addentry(char *device, /*int major, int minor,*/ char *label, char *uuid) 70uuidcache_addentry(char *device, /*int major, int minor,*/ char *label, char *uuid, const char *type)
58{ 71{
59 struct uuidCache_s *last; 72 struct uuidCache_s *last;
60 73
@@ -72,6 +85,7 @@ uuidcache_addentry(char *device, /*int major, int minor,*/ char *label, char *uu
72 last->device = device; 85 last->device = device;
73 last->label = label; 86 last->label = label;
74 last->uc_uuid = uuid; 87 last->uc_uuid = uuid;
88 IF_FEATURE_BLKID_TYPE(last->type = type;)
75} 89}
76 90
77/* If get_label_uuid() on device_name returns success, 91/* If get_label_uuid() on device_name returns success,
@@ -83,10 +97,6 @@ uuidcache_check_device(const char *device,
83 void *userData UNUSED_PARAM, 97 void *userData UNUSED_PARAM,
84 int depth UNUSED_PARAM) 98 int depth UNUSED_PARAM)
85{ 99{
86 char *uuid = uuid; /* for compiler */
87 char *label = label;
88 int fd;
89
90 /* note: this check rejects links to devices, among other nodes */ 100 /* note: this check rejects links to devices, among other nodes */
91 if (!S_ISBLK(statbuf->st_mode)) 101 if (!S_ISBLK(statbuf->st_mode))
92 return TRUE; 102 return TRUE;
@@ -99,21 +109,15 @@ uuidcache_check_device(const char *device,
99 if (major(statbuf->st_rdev) == 2) 109 if (major(statbuf->st_rdev) == 2)
100 return TRUE; 110 return TRUE;
101 111
102 fd = open(device, O_RDONLY); 112 add_to_uuid_cache(device);
103 if (fd < 0)
104 return TRUE;
105 113
106 /* get_label_uuid() closes fd in all cases (success & failure) */
107 if (get_label_uuid(fd, &label, &uuid) == 0) {
108 /* uuidcache_addentry() takes ownership of all three params */
109 uuidcache_addentry(xstrdup(device), /*ma, mi,*/ label, uuid);
110 }
111 return TRUE; 114 return TRUE;
112} 115}
113 116
114static void 117static void
115uuidcache_init(void) 118uuidcache_init(void)
116{ 119{
120 dbg("DBG: uuidCache=%x, uuidCache");
117 if (uuidCache) 121 if (uuidCache)
118 return; 122 return;
119 123
@@ -223,11 +227,38 @@ void display_uuid_cache(void)
223 printf(" LABEL=\"%s\"", u->label); 227 printf(" LABEL=\"%s\"", u->label);
224 if (u->uc_uuid[0]) 228 if (u->uc_uuid[0])
225 printf(" UUID=\"%s\"", u->uc_uuid); 229 printf(" UUID=\"%s\"", u->uc_uuid);
230#if ENABLE_FEATURE_BLKID_TYPE
231 if (u->type)
232 printf(" TYPE=\"%s\"", u->type);
233#endif
226 bb_putchar('\n'); 234 bb_putchar('\n');
227 u = u->next; 235 u = u->next;
228 } 236 }
229} 237}
230 238
239int add_to_uuid_cache(const char *device)
240{
241 char *uuid = uuid; /* for compiler */
242 char *label = label;
243#if ENABLE_FEATURE_BLKID_TYPE
244 const char *type = type;
245#endif
246 int fd;
247
248 fd = open(device, O_RDONLY);
249 if (fd < 0)
250 return 0;
251
252 /* get_label_uuid() closes fd in all cases (success & failure) */
253 if (get_label_uuid(fd, &label, &uuid, &type) == 0) {
254 /* uuidcache_addentry() takes ownership of all four params */
255 uuidcache_addentry(xstrdup(device), /*ma, mi,*/ label, uuid, type);
256 return 1;
257 }
258 return 0;
259}
260
261
231/* Used by mount and findfs */ 262/* Used by mount and findfs */
232 263
233char *get_devname_from_label(const char *spec) 264char *get_devname_from_label(const char *spec)