aboutsummaryrefslogtreecommitdiff
path: root/util-linux/volume_id/get_devname.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-03-17 09:21:26 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-03-17 09:21:26 +0000
commitc5b737231debe51edb769a0b5667ee9719e560e0 (patch)
tree9abeaec24486a888ef3bbfe2c179180dbecafd35 /util-linux/volume_id/get_devname.c
parent592d4fe7722b094b18194cdf6a692a71a116e4ca (diff)
downloadbusybox-w32-c5b737231debe51edb769a0b5667ee9719e560e0.tar.gz
busybox-w32-c5b737231debe51edb769a0b5667ee9719e560e0.tar.bz2
busybox-w32-c5b737231debe51edb769a0b5667ee9719e560e0.zip
volude_id: remove unused fields and functions which were setting them
function old new delta uuidcache_init 567 704 +137 findfs_main - 125 +125 packed_usage 23662 23694 +32 static.drive_name_string - 14 +14 applet_names 1815 1822 +7 applet_main 1104 1108 +4 volume_id_open_node 35 38 +3 applet_nameofs 552 554 +2 get_devname_from_label 78 79 +1 applet_install_loc 138 139 +1 volume_id_set_label_unicode16 31 27 -4 volume_id_set_label_string 74 70 -4 get_devname_from_uuid 63 59 -4 fs1 12 8 -4 volume_id_set_unicode16 209 202 -7 volume_id_free_buffer 71 56 -15 free_volume_id 52 37 -15 volume_id_probe_luks 97 79 -18 volume_id_set_usage_part 21 - -21 volume_id_probe_ufs 148 126 -22 volume_id_probe_lsi_mega_raid 131 108 -23 volume_id_probe_highpoint_45x_raid 127 104 -23 volume_id_probe_highpoint_37x_raid 87 63 -24 volume_id_set_usage 28 - -28 volume_id_probe_romfs 121 93 -28 volume_id_set_label_raw 30 - -30 volume_id_probe_promise_fasttrack_raid 174 144 -30 volume_id_set_uuid 373 342 -31 volume_id_probe_xfs 122 91 -31 volume_id_probe_jfs 138 107 -31 volume_id_probe_ocfs2 139 107 -32 volume_id_probe_lvm1 90 58 -32 volume_id_probe_cramfs 106 63 -43 volume_id_probe_via_raid 145 101 -44 volume_id_probe_intel_software_raid 153 108 -45 volume_id_probe_minix 121 74 -47 volume_id_probe_udf 682 632 -50 volume_id_probe_nvidia_raid 158 108 -50 volume_id_probe_lvm2 128 78 -50 volume_id_probe_hpfs 117 67 -50 volume_id_probe_ext 143 93 -50 volume_id_probe_linux_raid 191 139 -52 volume_id_probe_sysv 244 187 -57 volume_id_probe_silicon_medley_raid 158 97 -61 usage_to_string 64 - -64 volume_id_probe_linux_swap 263 198 -65 volume_id_get_buffer 372 307 -65 volume_id_probe_iso9660 412 332 -80 volume_id_probe_hfs_hfsplus 888 806 -82 volume_id_probe_ntfs 396 294 -102 volume_id_probe_vfat 980 872 -108 volume_id_probe_reiserfs 417 268 -149 dev_get_major_minor 175 - -175 uuidcache_check_device 692 499 -193 volume_id_probe_mac_partition_map 697 - -697 ------------------------------------------------------------------------------ (add/remove: 2/6 grow/shrink: 8/39 up/down: 326/-2836) Total: -2510 bytes text data bss dec hex filename 797220 662 7420 805302 c49b6 busybox_old 794244 662 7420 802326 c3e16 busybox_unstripped
Diffstat (limited to 'util-linux/volume_id/get_devname.c')
-rw-r--r--util-linux/volume_id/get_devname.c216
1 files changed, 118 insertions, 98 deletions
diff --git a/util-linux/volume_id/get_devname.c b/util-linux/volume_id/get_devname.c
index af03d07a3..bb3043cd6 100644
--- a/util-linux/volume_id/get_devname.c
+++ b/util-linux/volume_id/get_devname.c
@@ -10,22 +10,19 @@
10 10
11#include "volume_id_internal.h" 11#include "volume_id_internal.h"
12 12
13#define BLKGETSIZE64 _IOR(0x12,114,size_t) 13//#define BLKGETSIZE64 _IOR(0x12,114,size_t)
14
15#define PROC_PARTITIONS "/proc/partitions"
16#define PROC_CDROMS "/proc/sys/dev/cdrom/info"
17#define DEVLABELDIR "/dev"
18#define SYS_BLOCK "/sys/block"
19 14
20static struct uuidCache_s { 15static struct uuidCache_s {
21 struct uuidCache_s *next; 16 struct uuidCache_s *next;
22 char uuid[16]; 17// int major, minor;
23 char *device; 18 char *device;
24 char *label; 19 char *label;
25 int major, minor; 20 char *uc_uuid; /* prefix makes it easier to grep for */
26} *uuidCache; 21} *uuidCache;
27 22
28/* for now, only ext2, ext3 and xfs are supported */ 23/* Returns !0 on error.
24 * Otherwise, returns malloc'ed strings for label and uuid
25 * (and they can't be NULL, although they can be "") */
29#if !ENABLE_FEATURE_VOLUMEID_ISO9660 26#if !ENABLE_FEATURE_VOLUMEID_ISO9660
30#define get_label_uuid(device, label, uuid, iso_only) \ 27#define get_label_uuid(device, label, uuid, iso_only) \
31 get_label_uuid(device, label, uuid) 28 get_label_uuid(device, label, uuid)
@@ -38,15 +35,18 @@ get_label_uuid(const char *device, char **label, char **uuid, int iso_only)
38 struct volume_id *vid; 35 struct volume_id *vid;
39 36
40 vid = volume_id_open_node(device); 37 vid = volume_id_open_node(device);
38 if (!vid)
39 return rv;
41 40
42 if (ioctl(vid->fd, BLKGETSIZE64, &size) != 0) { 41 if (ioctl(vid->fd, BLKGETSIZE64, &size) != 0)
43 size = 0; 42 size = 0;
44 }
45 43
46#if ENABLE_FEATURE_VOLUMEID_ISO9660 44#if ENABLE_FEATURE_VOLUMEID_ISO9660
47 if (iso_only ? 45 if ((iso_only ?
48 volume_id_probe_iso9660(vid, 0) != 0 : 46 volume_id_probe_iso9660(vid, 0) :
49 volume_id_probe_all(vid, 0, size) != 0) { 47 volume_id_probe_all(vid, 0, size)
48 ) != 0
49 ) {
50 goto ret; 50 goto ret;
51 } 51 }
52#else 52#else
@@ -55,10 +55,10 @@ get_label_uuid(const char *device, char **label, char **uuid, int iso_only)
55 } 55 }
56#endif 56#endif
57 57
58 if (vid->label[0] != '\0') { 58 if (vid->label[0] != '\0' || vid->uuid[0] != '\0') {
59 *label = xstrndup(vid->label, sizeof(vid->label)); 59 *label = xstrndup(vid->label, sizeof(vid->label));
60 *uuid = xstrndup(vid->uuid, sizeof(vid->uuid)); 60 *uuid = xstrndup(vid->uuid, sizeof(vid->uuid));
61 printf("Found label %s on %s (uuid:%s)\n", *label, device, *uuid); 61 dbg("found label '%s', uuid '%s' on %s", *label, *uuid, device);
62 rv = 0; 62 rv = 0;
63 } 63 }
64 ret: 64 ret:
@@ -66,8 +66,9 @@ get_label_uuid(const char *device, char **label, char **uuid, int iso_only)
66 return rv; 66 return rv;
67} 67}
68 68
69/* NB: we take ownership of (malloc'ed) label and uuid */
69static void 70static void
70uuidcache_addentry(char * device, int major, int minor, char *label, char *uuid) 71uuidcache_addentry(char *device, /*int major, int minor,*/ char *label, char *uuid)
71{ 72{
72 struct uuidCache_s *last; 73 struct uuidCache_s *last;
73 74
@@ -80,13 +81,16 @@ uuidcache_addentry(char * device, int major, int minor, char *label, char *uuid)
80 last = last->next; 81 last = last->next;
81 } 82 }
82 /*last->next = NULL; - xzalloc did it*/ 83 /*last->next = NULL; - xzalloc did it*/
83 last->label = label; 84// last->major = major;
85// last->minor = minor;
84 last->device = device; 86 last->device = device;
85 last->major = major; 87 last->label = label;
86 last->minor = minor; 88 last->uc_uuid = uuid;
87 memcpy(last->uuid, uuid, sizeof(last->uuid));
88} 89}
89 90
91/* If get_label_uuid() on device_name returns success,
92 * add a cache entry for this device.
93 * If device node does not exist, it will be temporarily created. */
90#if !ENABLE_FEATURE_VOLUMEID_ISO9660 94#if !ENABLE_FEATURE_VOLUMEID_ISO9660
91#define uuidcache_check_device(device_name, ma, mi, iso_only) \ 95#define uuidcache_check_device(device_name, ma, mi, iso_only) \
92 uuidcache_check_device(device_name, ma, mi) 96 uuidcache_check_device(device_name, ma, mi)
@@ -94,45 +98,62 @@ uuidcache_addentry(char * device, int major, int minor, char *label, char *uuid)
94static void 98static void
95uuidcache_check_device(const char *device_name, int ma, int mi, int iso_only) 99uuidcache_check_device(const char *device_name, int ma, int mi, int iso_only)
96{ 100{
97 char device[110]; 101 char *device, *last_slash;
98 char *uuid = NULL, *label = NULL; 102 char *uuid, *label;
99 char *ptr; 103 char *ptr;
100 char *deviceDir = NULL; 104 int must_remove = 0;
101 int mustRemove = 0; 105 int added = 0;
102 int mustRemoveDir = 0; 106
103 int i; 107 last_slash = NULL;
108 device = xasprintf("/dev/%s", device_name);
109 if (access(device, F_OK) != 0) {
110 /* device does not exist, temporarily create */
111 int slash_cnt = 0;
112
113 if ((ma | mi) < 0)
114 goto ret; /* we don't know major:minor! */
104 115
105 sprintf(device, "%s/%s", DEVLABELDIR, device_name);
106 if (access(device, F_OK)) {
107 ptr = device; 116 ptr = device;
108 i = 0;
109 while (*ptr) 117 while (*ptr)
110 if (*ptr++ == '/') 118 if (*ptr++ == '/')
111 i++; 119 slash_cnt++;
112 if (i > 2) { 120 if (slash_cnt > 2) {
113 deviceDir = alloca(strlen(device) + 1); 121// BUG: handles only slash_cnt == 3 case
114 strcpy(deviceDir, device); 122 last_slash = strrchr(device, '/');
115 ptr = deviceDir + (strlen(device) - 1); 123 *last_slash = '\0';
116 while (*ptr != '/') 124 if (mkdir(device, 0644)) {
117 *ptr-- = '\0'; 125 bb_perror_msg("can't create directory %s", device);
118 if (mkdir(deviceDir, 0644)) { 126 *last_slash = '/';
119 printf("mkdir: cannot create directory %s: %d\n", deviceDir, errno); 127 last_slash = NULL; /* prevents rmdir */
120 } else { 128 } else {
121 mustRemoveDir = 1; 129 *last_slash = '/';
122 } 130 }
123 } 131 }
124
125 mknod(device, S_IFBLK | 0600, makedev(ma, mi)); 132 mknod(device, S_IFBLK | 0600, makedev(ma, mi));
126 mustRemove = 1; 133 must_remove = 1;
134 }
135
136 uuid = NULL;
137 label = NULL;
138 if (get_label_uuid(device, &label, &uuid, iso_only) == 0) {
139 uuidcache_addentry(device, /*ma, mi,*/ label, uuid);
140 /* "device" is owned by cache now, don't free */
141 added = 1;
127 } 142 }
128 if (!get_label_uuid(device, &label, &uuid, iso_only))
129 uuidcache_addentry(strdup(device), ma, mi,
130 label, uuid);
131 143
132 if (mustRemove) unlink(device); 144 if (must_remove)
133 if (mustRemoveDir) rmdir(deviceDir); 145 unlink(device);
146 if (last_slash) {
147 *last_slash = '\0';
148 rmdir(device);
149 }
150 ret:
151 if (!added)
152 free(device);
134} 153}
135 154
155/* Run uuidcache_check_device() for every device mentioned
156 * in /proc/partitions */
136static void 157static void
137uuidcache_init_partitions(void) 158uuidcache_init_partitions(void)
138{ 159{
@@ -144,7 +165,7 @@ uuidcache_init_partitions(void)
144 int handleOnFirst; 165 int handleOnFirst;
145 char *chptr; 166 char *chptr;
146 167
147 procpt = xfopen(PROC_PARTITIONS, "r"); 168 procpt = xfopen("/proc/partitions", "r");
148/* 169/*
149# cat /proc/partitions 170# cat /proc/partitions
150major minor #blocks name 171major minor #blocks name
@@ -165,7 +186,7 @@ major minor #blocks name
165 diet's sscanf is quite limited */ 186 diet's sscanf is quite limited */
166 chptr = line; 187 chptr = line;
167 if (*chptr != ' ') continue; 188 if (*chptr != ' ') continue;
168 chptr++; 189 chptr = skip_whitespace(chptr);
169 190
170 ma = bb_strtou(chptr, &chptr, 0); 191 ma = bb_strtou(chptr, &chptr, 0);
171 if (ma < 0) continue; 192 if (ma < 0) continue;
@@ -185,6 +206,8 @@ major minor #blocks name
185 206
186 *strchrnul(chptr, '\n') = '\0'; 207 *strchrnul(chptr, '\n') = '\0';
187 /* now chptr => device name */ 208 /* now chptr => device name */
209 dbg("/proc/partitions: maj:%d min:%d sz:%llu name:'%s'",
210 ma, mi, sz, chptr);
188 if (!chptr[0]) 211 if (!chptr[0])
189 continue; 212 continue;
190 213
@@ -203,36 +226,35 @@ major minor #blocks name
203 fclose(procpt); 226 fclose(procpt);
204} 227}
205 228
206static int 229static void
207dev_get_major_minor(char *device_name, int *major, int *minor) 230dev_get_major_minor(char *device_name, int *major, int *minor)
208{ 231{
209 char * dev_path; 232 char dev[16];
210 int fd; 233 char *dev_path;
211 char dev[7]; 234 char *colon;
212 char *major_ptr, *minor_ptr; 235 int sz;
213 236
214 dev_path = alloca(strlen(SYS_BLOCK) + strlen(device_name) + 6); 237 dev_path = xasprintf("/sys/block/%s/dev", device_name);
215 sprintf(dev_path, "%s/%s/dev", SYS_BLOCK, device_name); 238 sz = open_read_close(dev_path, dev, sizeof(dev) - 1);
216 239 if (sz < 0)
217 fd = open(dev_path, O_RDONLY); 240 goto ret;
218 if (fd < 0) return 1; 241 dev[sz] = '\0';
219 full_read(fd, dev, sizeof(dev));
220 close(fd);
221
222 major_ptr = dev;
223 minor_ptr = strchr(dev, ':');
224 if (!minor_ptr) return 1;
225 *minor_ptr++ = '\0';
226 242
227 *major = strtol(major_ptr, NULL, 10); 243 colon = strchr(dev, ':');
228 *minor = strtol(minor_ptr, NULL, 10); 244 if (!colon)
245 goto ret;
246 *major = strtol(dev, NULL, 10);
247 *minor = strtol(colon + 1, NULL, 10);
229 248
230 return 0; 249 ret:
250 free(dev_path);
251 return;
231} 252}
232 253
233static void 254static void
234uuidcache_init_cdroms(void) 255uuidcache_init_cdroms(void)
235{ 256{
257#define PROC_CDROMS "/proc/sys/dev/cdrom/info"
236 char line[100]; 258 char line[100];
237 int ma, mi; 259 int ma, mi;
238 FILE *proccd; 260 FILE *proccd;
@@ -250,11 +272,13 @@ uuidcache_init_cdroms(void)
250 } 272 }
251 273
252 while (fgets(line, sizeof(line), proccd)) { 274 while (fgets(line, sizeof(line), proccd)) {
253 const char *drive_name_string = "drive name:\t\t"; 275 static const char drive_name_string[] ALIGN1 = "drive name:\t\t";
254 if (!strncmp(line, drive_name_string, strlen(drive_name_string))) { 276
277 if (strncmp(line, drive_name_string, sizeof(drive_name_string) - 1) == 0) {
255 char *device_name; 278 char *device_name;
256 device_name = strtok(line + strlen(drive_name_string), "\t\n"); 279 device_name = strtok(line + sizeof(drive_name_string) - 1, "\t\n");
257 while (device_name) { 280 while (device_name) {
281 ma = mi = -1;
258 dev_get_major_minor(device_name, &ma, &mi); 282 dev_get_major_minor(device_name, &ma, &mi);
259 uuidcache_check_device(device_name, ma, mi, 1); 283 uuidcache_check_device(device_name, ma, mi, 1);
260 device_name = strtok(NULL, "\t\n"); 284 device_name = strtok(NULL, "\t\n");
@@ -281,24 +305,24 @@ uuidcache_init(void)
281 305
282#ifdef UNUSED 306#ifdef UNUSED
283static char * 307static char *
284get_spec_by_x(int n, const char *t, int * majorPtr, int * minorPtr) 308get_spec_by_x(int n, const char *t, int *majorPtr, int *minorPtr)
285{ 309{
286 struct uuidCache_s *uc; 310 struct uuidCache_s *uc;
287 311
288 uuidcache_init(); 312 uuidcache_init();
289 uc = uuidCache; 313 uc = uuidCache;
290 314
291 while(uc) { 315 while (uc) {
292 switch (n) { 316 switch (n) {
293 case UUID: 317 case UUID:
294 if (!memcmp(t, uc->uuid, sizeof(uc->uuid))) { 318 if (strcmp(t, uc->uc_uuid) == 0) {
295 *majorPtr = uc->major; 319 *majorPtr = uc->major;
296 *minorPtr = uc->minor; 320 *minorPtr = uc->minor;
297 return uc->device; 321 return uc->device;
298 } 322 }
299 break; 323 break;
300 case VOL: 324 case VOL:
301 if (!strcmp(t, uc->label)) { 325 if (strcmp(t, uc->label) == 0) {
302 *majorPtr = uc->major; 326 *majorPtr = uc->major;
303 *minorPtr = uc->minor; 327 *minorPtr = uc->minor;
304 return uc->device; 328 return uc->device;
@@ -315,26 +339,27 @@ fromhex(char c)
315{ 339{
316 if (isdigit(c)) 340 if (isdigit(c))
317 return (c - '0'); 341 return (c - '0');
318 if (islower(c)) 342 return ((c|0x20) - 'a' + 10);
319 return (c - 'a' + 10);
320 return (c - 'A' + 10);
321} 343}
322 344
323static char * 345static char *
324get_spec_by_uuid(const char *s, int * major, int * minor) 346get_spec_by_uuid(const char *s, int *major, int *minor)
325{ 347{
326 unsigned char uuid[16]; 348 unsigned char uuid[16];
327 int i; 349 int i;
328 350
329 if (strlen(s) != 36 || 351 if (strlen(s) != 36 || s[8] != '-' || s[13] != '-'
330 s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-') 352 || s[18] != '-' || s[23] != '-'
353 ) {
331 goto bad_uuid; 354 goto bad_uuid;
332 for (i=0; i<16; i++) { 355 }
333 if (*s == '-') s++; 356 for (i = 0; i < 16; i++) {
334 if (!isxdigit(s[0]) || !isxdigit(s[1])) 357 if (*s == '-')
335 goto bad_uuid; 358 s++;
336 uuid[i] = ((fromhex(s[0])<<4) | fromhex(s[1])); 359 if (!isxdigit(s[0]) || !isxdigit(s[1]))
337 s += 2; 360 goto bad_uuid;
361 uuid[i] = ((fromhex(s[0]) << 4) | fromhex(s[1]));
362 s += 2;
338 } 363 }
339 return get_spec_by_x(UUID, (char *)uuid, major, minor); 364 return get_spec_by_x(UUID, (char *)uuid, major, minor);
340 365
@@ -358,13 +383,7 @@ static int display_uuid_cache(void)
358 383
359 u = uuidCache; 384 u = uuidCache;
360 while (u) { 385 while (u) {
361 printf("%s %s ", u->device, u->label); 386 printf("%s %s %s\n", u->device, u->label, u->uc_uuid);
362 for (i = 0; i < sizeof(u->uuid); i++) {
363 if (i == 4 || i == 6 || i == 8 || i == 10)
364 printf("-");
365 printf("%x", u->uuid[i] & 0xff);
366 }
367 printf("\n");
368 u = u->next; 387 u = u->next;
369 } 388 }
370 389
@@ -383,7 +402,8 @@ char *get_devname_from_label(const char *spec)
383 uuidcache_init(); 402 uuidcache_init();
384 uc = uuidCache; 403 uc = uuidCache;
385 while (uc) { 404 while (uc) {
386 if (uc->label && !strncmp(spec, uc->label, spec_len)) { 405// FIXME: empty label ("LABEL=") matches anything??!
406 if (uc->label[0] && strncmp(spec, uc->label, spec_len) == 0) {
387 return xstrdup(uc->device); 407 return xstrdup(uc->device);
388 } 408 }
389 uc = uc->next; 409 uc = uc->next;
@@ -398,7 +418,7 @@ char *get_devname_from_uuid(const char *spec)
398 uuidcache_init(); 418 uuidcache_init();
399 uc = uuidCache; 419 uc = uuidCache;
400 while (uc) { 420 while (uc) {
401 if (!memcmp(spec, uc->uuid, sizeof(uc->uuid))) { 421 if (strcmp(spec, uc->uc_uuid) == 0) {
402 return xstrdup(uc->device); 422 return xstrdup(uc->device);
403 } 423 }
404 uc = uc->next; 424 uc = uc->next;