]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/blkid.c
blkid: add -n <list> option
[thirdparty/util-linux.git] / misc-utils / blkid.c
1 /*
2 * blkid.c - User command-line interface for libblkid
3 *
4 * Copyright (C) 2001 Andreas Dilger
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the
8 * GNU Lesser General Public License.
9 * %End-Header%
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <termios.h>
20 #include <errno.h>
21 #ifdef HAVE_SYS_IOCTL_H
22 #include <sys/ioctl.h>
23 #endif
24 #ifdef HAVE_GETOPT_H
25 #include <getopt.h>
26 #else
27 extern int getopt(int argc, char * const argv[], const char *optstring);
28 extern char *optarg;
29 extern int optind;
30 #endif
31
32 #define OUTPUT_VALUE_ONLY 0x0001
33 #define OUTPUT_DEVICE_ONLY 0x0002
34 #define OUTPUT_PRETTY_LIST 0x0004
35 #define OUTPUT_UDEV_LIST 0x0008
36
37 #include <blkid.h>
38
39 #include "ismounted.h"
40
41 const char *progname = "blkid";
42
43 static void print_version(FILE *out)
44 {
45 fprintf(out, "%s from %s (libblkid %s, %s)\n",
46 progname, PACKAGE_STRING, LIBBLKID_VERSION, LIBBLKID_DATE);
47 }
48
49 static void usage(int error)
50 {
51 FILE *out = error ? stderr : stdout;
52
53 print_version(out);
54 fprintf(out,
55 "Usage:\n"
56 " %1$s -L <label> | -U <uuid>\n\n"
57 " %1$s [-c <file>] [-ghlLv] [-o format] [-s <tag>] \n"
58 " [-t <token>] [-w <file>] [dev ...]\n\n"
59 " %1$s -p [-O <offset>] [-S <size>] [-o format] <dev> [dev ...]\n\n"
60 "Options:\n"
61 " -c <file> cache file (default: /etc/blkid.tab, /dev/null = none)\n"
62 " -h print this usage message and exit\n"
63 " -g garbage collect the blkid cache\n"
64 " -o <format> output format; can be one of:\n"
65 " value, device, list, udev or full; (default: full)\n"
66 " -s <tag> show specified tag(s) (default show all tags)\n"
67 " -t <token> find device with a specific token (NAME=value pair)\n"
68 " -l lookup the the first device with arguments specified by -t\n"
69 " -L <label> convert LABEL to device name\n"
70 " -U <uuid> convert UUID to device name\n"
71 " -v print version and exit\n"
72 " -w <file> write cache to different file (/dev/null = no write)\n"
73 " <dev> specify device(s) to probe (default: all devices)\n\n"
74 "Low-level probing options:\n"
75 " -p switch to low-level mode (bypass cache)\n"
76 " -S <bytes> overwrite device size\n"
77 " -O <bytes> probe at the given offset\n"
78 " -u <list> filter by \"usage\" (e.g. -u filesystem,raid)\n"
79 " -n <list> filter by filesystem type (e.g. -n vfat,ext3)\n"
80 "\n",
81 progname);
82
83 exit(error);
84 }
85
86 /*
87 * This function does "safe" printing. It will convert non-printable
88 * ASCII characters using '^' and M- notation.
89 */
90 static void safe_print(const char *cp, int len)
91 {
92 unsigned char ch;
93
94 if (len < 0)
95 len = strlen(cp);
96
97 while (len--) {
98 ch = *cp++;
99 if (ch > 128) {
100 fputs("M-", stdout);
101 ch -= 128;
102 }
103 if ((ch < 32) || (ch == 0x7f)) {
104 fputc('^', stdout);
105 ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
106 }
107 fputc(ch, stdout);
108 }
109 }
110
111 static int get_terminal_width(void)
112 {
113 #ifdef TIOCGSIZE
114 struct ttysize t_win;
115 #endif
116 #ifdef TIOCGWINSZ
117 struct winsize w_win;
118 #endif
119 const char *cp;
120
121 #ifdef TIOCGSIZE
122 if (ioctl (0, TIOCGSIZE, &t_win) == 0)
123 return (t_win.ts_cols);
124 #endif
125 #ifdef TIOCGWINSZ
126 if (ioctl (0, TIOCGWINSZ, &w_win) == 0)
127 return (w_win.ws_col);
128 #endif
129 cp = getenv("COLUMNS");
130 if (cp)
131 return strtol(cp, NULL, 10);
132 return 80;
133 }
134
135 static int pretty_print_word(const char *str, int max_len,
136 int left_len, int overflow_nl)
137 {
138 int len = strlen(str) + left_len;
139 int ret = 0;
140
141 fputs(str, stdout);
142 if (overflow_nl && len > max_len) {
143 fputc('\n', stdout);
144 len = 0;
145 } else if (len > max_len)
146 ret = len - max_len;
147 do
148 fputc(' ', stdout);
149 while (len++ < max_len);
150 return ret;
151 }
152
153 static void pretty_print_line(const char *device, const char *fs_type,
154 const char *label, const char *mtpt,
155 const char *uuid)
156 {
157 static int device_len = 10, fs_type_len = 7;
158 static int label_len = 8, mtpt_len = 14;
159 static int term_width = -1;
160 int len, w;
161
162 if (term_width < 0)
163 term_width = get_terminal_width();
164
165 if (term_width > 80) {
166 term_width -= 80;
167 w = term_width / 10;
168 if (w > 8)
169 w = 8;
170 term_width -= 2*w;
171 label_len += w;
172 fs_type_len += w;
173 w = term_width/2;
174 device_len += w;
175 mtpt_len +=w;
176 }
177
178 len = pretty_print_word(device, device_len, 0, 1);
179 len = pretty_print_word(fs_type, fs_type_len, len, 0);
180 len = pretty_print_word(label, label_len, len, 0);
181 len = pretty_print_word(mtpt, mtpt_len, len, 0);
182 fputs(uuid, stdout);
183 fputc('\n', stdout);
184 }
185
186 static void pretty_print_dev(blkid_dev dev)
187 {
188 blkid_tag_iterate iter;
189 const char *type, *value, *devname;
190 const char *uuid = "", *fs_type = "", *label = "";
191 int len, mount_flags;
192 char mtpt[80];
193 int retval;
194
195 if (dev == NULL) {
196 pretty_print_line("device", "fs_type", "label",
197 "mount point", "UUID");
198 for (len=get_terminal_width()-1; len > 0; len--)
199 fputc('-', stdout);
200 fputc('\n', stdout);
201 return;
202 }
203
204 devname = blkid_dev_devname(dev);
205 if (access(devname, F_OK))
206 return;
207
208 /* Get the uuid, label, type */
209 iter = blkid_tag_iterate_begin(dev);
210 while (blkid_tag_next(iter, &type, &value) == 0) {
211 if (!strcmp(type, "UUID"))
212 uuid = value;
213 if (!strcmp(type, "TYPE"))
214 fs_type = value;
215 if (!strcmp(type, "LABEL"))
216 label = value;
217 }
218 blkid_tag_iterate_end(iter);
219
220 /* Get the mount point */
221 mtpt[0] = 0;
222 retval = check_mount_point(devname, &mount_flags, mtpt, sizeof(mtpt));
223 if (retval == 0) {
224 if (mount_flags & MF_MOUNTED) {
225 if (!mtpt[0])
226 strcpy(mtpt, "(mounted, mtpt unknown)");
227 } else if (mount_flags & MF_BUSY)
228 strcpy(mtpt, "(in use)");
229 else
230 strcpy(mtpt, "(not mounted)");
231 }
232
233 pretty_print_line(devname, fs_type, label, mtpt, uuid);
234 }
235
236 static void print_udev_format(const char *name, const char *value, size_t sz)
237 {
238 char enc[265], safe[256];
239
240 *safe = *enc = '\0';
241
242 if (!strcmp(name, "TYPE") || !strcmp(name, "VERSION")) {
243 blkid_encode_string(value, enc, sizeof(enc));
244 printf("ID_FS_%s=%s\n", name, enc);
245
246 } else if (!strcmp(name, "UUID") ||
247 !strcmp(name, "LABEL") ||
248 !strcmp(name, "UUID_SUB")) {
249
250 blkid_safe_string(value, safe, sizeof(safe));
251 printf("ID_FS_%s=%s\n", name, safe);
252
253 blkid_encode_string(value, enc, sizeof(enc));
254 printf("ID_FS_%s_ENC=%s\n", name, enc);
255
256 } else if (!strcmp(name, "PTTYPE")) {
257 printf("ID_PART_TABLE_TYPE=%s\n", value);
258
259 } else if (!strcmp(name, "PART_ENTRY_NAME") ||
260 !strcmp(name, "PART_ENTRY_TYPE")) {
261
262 blkid_safe_string(value, safe, sizeof(safe));
263 printf("ID_%s=%s\n", name, safe);
264
265 blkid_encode_string(value, enc, sizeof(enc));
266 printf("ID_%s_ENC=%s\n", name, enc);
267
268 } else if (!strncmp(name, "PART_ENTRY_", 11))
269 printf("ID_%s=%s\n", name, value);
270
271 else
272 printf("ID_FS_%s=%s\n", name, value);
273 }
274
275 static int has_item(char *ary[], const char *item)
276 {
277 char **p;
278
279 for (p = ary; *p != NULL; p++)
280 if (!strcmp(item, *p))
281 return 1;
282 return 0;
283 }
284
285 static void print_value(int output, int num, const char *devname,
286 const char *value, const char *name, size_t valsz)
287 {
288 if (output & OUTPUT_VALUE_ONLY) {
289 fputs(value, stdout);
290 fputc('\n', stdout);
291
292 } else if (output & OUTPUT_UDEV_LIST) {
293 print_udev_format(name, value, valsz);
294
295 } else {
296 if (num == 1 && devname)
297 printf("%s: ", devname);
298 fputs(name, stdout);
299 fputs("=\"", stdout);
300 safe_print(value, valsz);
301 fputs("\" ", stdout);
302 }
303 }
304
305 static void print_tags(blkid_dev dev, char *show[], int output)
306 {
307 blkid_tag_iterate iter;
308 const char *type, *value, *devname;
309 int num = 1;
310
311 if (!dev)
312 return;
313
314 if (output & OUTPUT_PRETTY_LIST) {
315 pretty_print_dev(dev);
316 return;
317 }
318
319 devname = blkid_dev_devname(dev);
320
321 if (output & OUTPUT_DEVICE_ONLY) {
322 printf("%s\n", devname);
323 return;
324 }
325
326 iter = blkid_tag_iterate_begin(dev);
327 while (blkid_tag_next(iter, &type, &value) == 0) {
328 if (show[0] && !has_item(show, type))
329 continue;
330 print_value(output, num++, devname, value, type, strlen(value));
331 }
332 blkid_tag_iterate_end(iter);
333
334 if (num > 1 && !(output & (OUTPUT_VALUE_ONLY | OUTPUT_UDEV_LIST)))
335 printf("\n");
336 }
337
338
339 static int append_str(char **res, size_t *sz, const char *a, const char *b)
340 {
341 char *str = *res;
342 size_t asz = a ? strlen(a) : 0;
343 size_t bsz = b ? strlen(b) : 0;
344 size_t len = *sz + asz + bsz;
345
346 if (!len)
347 return -1;
348
349 str = realloc(str, len + 1);
350 if (!str) {
351 free(*res);
352 return -1;
353 }
354 *res = str;
355 str += *sz;
356
357 if (a) {
358 memcpy(str, a, asz);
359 str += asz;
360 }
361 if (b) {
362 memcpy(str, b, bsz);
363 str += bsz;
364 }
365 *str = '\0';
366 *sz = len;
367 return 0;
368 }
369
370 /*
371 * Compose and print ID_FS_AMBIVALENT for udev
372 */
373 static int print_udev_ambivalent(blkid_probe pr)
374 {
375 char *val = NULL;
376 size_t valsz = 0;
377 int count = 0, rc = -1;
378
379 while (!blkid_do_probe(pr)) {
380 const char *usage = NULL, *type = NULL, *version = NULL;
381 char enc[256];
382
383 blkid_probe_lookup_value(pr, "USAGE", &usage, NULL);
384 blkid_probe_lookup_value(pr, "TYPE", &type, NULL);
385 blkid_probe_lookup_value(pr, "VERSION", &version, NULL);
386
387 if (!usage || !type)
388 continue;
389
390 blkid_encode_string(usage, enc, sizeof(enc));
391 if (append_str(&val, &valsz, enc, ":"))
392 goto done;
393
394 blkid_encode_string(type, enc, sizeof(enc));
395 if (append_str(&val, &valsz, enc, version ? ":" : " "))
396 goto done;
397
398 if (version) {
399 blkid_encode_string(version, enc, sizeof(enc));
400 if (append_str(&val, &valsz, enc, " "))
401 goto done;
402 }
403 count++;
404 }
405
406 if (count > 1) {
407 *(val + valsz - 1) = '\0'; /* rem tailing whitespace */
408 printf("ID_FS_AMBIVALEN=%s\n", val);
409 rc = 0;
410 }
411 done:
412 free(val);
413 return rc;
414 }
415
416 static int lowprobe_device(blkid_probe pr, const char *devname, char *show[],
417 int output, blkid_loff_t offset, blkid_loff_t size)
418 {
419 const char *data;
420 const char *name;
421 int nvals = 0, n, num = 1, has_pt = 0;
422 size_t len;
423 int fd;
424 int rc = 0;
425 struct stat st;
426
427 fd = open(devname, O_RDONLY);
428 if (fd < 0) {
429 fprintf(stderr, "error: %s: %s\n", devname, strerror(errno));
430 return 2;
431 }
432 if (blkid_probe_set_device(pr, fd, offset, size))
433 goto done;
434
435 if (fstat(fd, &st))
436 goto done;
437 /*
438 * partitions probing
439 */
440 blkid_probe_enable_superblocks(pr, 0); /* enabled by default ;-( */
441
442 blkid_probe_enable_partitions(pr, 1);
443
444 /* This is for PART_ENTRY_{UUID,NAME,TYPE,...} values and it will
445 * open() whole-disk device.
446 */
447 blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS);
448
449 rc = blkid_do_fullprobe(pr);
450 blkid_probe_enable_partitions(pr, 0);
451
452 if (rc < 0)
453 goto done; /* -1 = error, 1 = nothing, 0 = succes */
454
455 if (blkid_probe_lookup_value(pr, "PTTYPE", NULL, NULL) == 0)
456 /* partition table detected */
457 has_pt = 1;
458
459 /*
460 * filesystems/raids probing
461 *
462 * Q: Why do we try to probe for FS/RAID on device with partiton table?
463 *
464 * A: For some devices the kernel creates partitions because it is too
465 * dumb to find out, that the device contains a RAID signature or
466 * other metadata that signifies, that this device should not be
467 * handled.
468 *
469 * Many RAID signatures are at the end of the device, or at least
470 * still allow that the volume contains a partition table at the
471 * beginning of the device, which the kernel will find. Udev needs
472 * to look at the disk, if there are such RAID signatures, and then
473 * it needs to handle it, and in many cases just kill all the
474 * wrongly kernel-created partitions.
475 *
476 * -- Kay Sievers
477 * http://thread.gmane.org/gmane.linux.utilities.util-linux-ng/2888/focus=2890
478 *
479 * This all is unncecessary for very small devices (<= 1.44MiB).
480 */
481 if (has_pt == 0 || S_ISCHR(st.st_mode) ||
482 blkid_probe_get_size(pr) > 1024 * 1440) {
483 /*
484 * filesystems/RAIDs probing
485 */
486 blkid_probe_enable_superblocks(pr, 1);
487
488 rc = blkid_do_safeprobe(pr);
489 if (rc < 0)
490 goto done;
491 }
492
493 nvals = blkid_probe_numof_values(pr);
494
495 if (output & OUTPUT_DEVICE_ONLY) {
496 printf("%s\n", devname);
497 goto done;
498 }
499
500 for (n = 0; n < nvals; n++) {
501 if (blkid_probe_get_value(pr, n, &name, &data, &len))
502 continue;
503 if (show[0] && !has_item(show, name))
504 continue;
505 len = strnlen((char *) data, len);
506 print_value(output, num++, devname, (char *) data, name, len);
507 }
508
509 if (nvals >= 1 && !(output & (OUTPUT_VALUE_ONLY | OUTPUT_UDEV_LIST)))
510 printf("\n");
511 done:
512 if (rc == -2) {
513 if (output & OUTPUT_UDEV_LIST)
514 print_udev_ambivalent(pr);
515 else
516 fprintf(stderr,
517 "%s: ambivalent result (probably more "
518 "filesystems on the device, use wipefs(8) "
519 "to see more details)\n",
520 devname);
521 }
522 close(fd);
523 return !nvals ? 2 : 0;
524 }
525
526 /* converts comma separated list to BLKID_USAGE_* mask */
527 static int list_to_usage(const char *list, int *flag)
528 {
529 int mask = 0;
530 const char *word = NULL, *p = list;
531
532 if (p && strncmp(p, "no", 2) == 0) {
533 *flag = BLKID_FLTR_NOTIN;
534 p += 2;
535 }
536 if (!p || !*p)
537 goto err;
538 while(p) {
539 word = p;
540 p = strchr(p, ',');
541 if (p)
542 p++;
543 if (!strncmp(word, "filesystem", 10))
544 mask |= BLKID_USAGE_FILESYSTEM;
545 else if (!strncmp(word, "raid", 4))
546 mask |= BLKID_USAGE_RAID;
547 else if (!strncmp(word, "crypto", 6))
548 mask |= BLKID_USAGE_CRYPTO;
549 else if (!strncmp(word, "other", 5))
550 mask |= BLKID_USAGE_OTHER;
551 else
552 goto err;
553 }
554 return mask;
555 err:
556 *flag = 0;
557 fprintf(stderr, "unknown kerword in -u <list> argument: '%s'\n",
558 word ? word : list);
559 exit(4);
560 }
561
562 /* converts comma separated list to types[] */
563 static char **list_to_types(const char *list, int *flag)
564 {
565 int i;
566 const char *p = list;
567 char **res;
568
569 if (p && strncmp(p, "no", 2) == 0) {
570 *flag = BLKID_FLTR_NOTIN;
571 p += 2;
572 }
573 if (!p || !*p) {
574 fprintf(stderr, "error: -u <list> argument is empty\n");
575 goto err;
576 }
577 for (i = 1; p && (p = strchr(p, ',')); i++, p++);
578
579 res = calloc(i + 1, sizeof(char *));
580 if (!res)
581 goto err_mem;
582 p = *flag & BLKID_FLTR_NOTIN ? list + 2 : list;
583 i = 0;
584
585 while(p) {
586 const char *word = p;
587 p = strchr(p, ',');
588 res[i] = p ? strndup(word, p - word) : strdup(word);
589 if (!res[i++])
590 goto err_mem;
591 if (p)
592 p++;
593 }
594 res[i] = NULL;
595 return res;
596 err_mem:
597 fprintf(stderr, "out of memory\n");
598 err:
599 *flag = 0;
600 free(res);
601 exit(4);
602 }
603
604 static void free_types_list(char *list[])
605 {
606 char **n;
607
608 if (!list)
609 return;
610 for (n = list; *n; n++)
611 free(*n);
612 free(list);
613 }
614
615 int main(int argc, char **argv)
616 {
617 blkid_cache cache = NULL;
618 char *devices[128] = { NULL, };
619 char *show[128] = { NULL, };
620 char *search_type = NULL, *search_value = NULL;
621 char *read = NULL;
622 char *write = NULL;
623 int fltr_usage = 0;
624 char **fltr_type = NULL;
625 int fltr_flag = BLKID_FLTR_ONLYIN;
626 unsigned int numdev = 0, numtag = 0;
627 int version = 0;
628 int err = 4;
629 unsigned int i;
630 int output_format = 0;
631 int lookup = 0, gc = 0, lowprobe = 0, eval = 0;
632 int c;
633 blkid_loff_t offset = 0, size = 0;
634
635 show[0] = NULL;
636
637 while ((c = getopt (argc, argv, "c:f:ghlL:n:o:O:ps:S:t:u:U:w:v")) != EOF)
638 switch (c) {
639 case 'c':
640 if (optarg && !*optarg)
641 read = NULL;
642 else
643 read = optarg;
644 if (!write)
645 write = read;
646 break;
647 case 'L':
648 eval++;
649 search_value = strdup(optarg);
650 search_type = strdup("LABEL");
651 break;
652 case 'n':
653 if (fltr_usage) {
654 fprintf(stderr, "error: -u and -n options are mutually exclusive\n");
655 exit(4);
656 }
657 fltr_type = list_to_types(optarg, &fltr_flag);
658 break;
659 case 'u':
660 if (fltr_type) {
661 fprintf(stderr, "error: -u and -n options are mutually exclusive\n");
662 exit(4);
663 }
664 fltr_usage = list_to_usage(optarg, &fltr_flag);
665 break;
666 case 'U':
667 eval++;
668 search_value = strdup(optarg);
669 search_type = strdup("UUID");
670 break;
671 case 'l':
672 lookup++;
673 break;
674 case 'g':
675 gc = 1;
676 break;
677 case 'o':
678 if (!strcmp(optarg, "value"))
679 output_format = OUTPUT_VALUE_ONLY;
680 else if (!strcmp(optarg, "device"))
681 output_format = OUTPUT_DEVICE_ONLY;
682 else if (!strcmp(optarg, "list"))
683 output_format = OUTPUT_PRETTY_LIST;
684 else if (!strcmp(optarg, "udev"))
685 output_format = OUTPUT_UDEV_LIST;
686 else if (!strcmp(optarg, "full"))
687 output_format = 0;
688 else {
689 fprintf(stderr, "Invalid output format %s. "
690 "Choose from value,\n\t"
691 "device, list, udev or full\n", optarg);
692 exit(4);
693 }
694 break;
695 case 'O':
696 offset = strtoll(optarg, NULL, 10);
697 break;
698 case 'p':
699 lowprobe++;
700 break;
701 case 's':
702 if (numtag + 1 >= sizeof(show) / sizeof(*show)) {
703 fprintf(stderr, "Too many tags specified\n");
704 usage(err);
705 }
706 show[numtag++] = optarg;
707 show[numtag] = NULL;
708 break;
709 case 'S':
710 size = strtoll(optarg, NULL, 10);
711 break;
712 case 't':
713 if (search_type) {
714 fprintf(stderr, "Can only search for "
715 "one NAME=value pair\n");
716 usage(err);
717 }
718 if (blkid_parse_tag_string(optarg,
719 &search_type,
720 &search_value)) {
721 fprintf(stderr, "-t needs NAME=value pair\n");
722 usage(err);
723 }
724 break;
725 case 'v':
726 version = 1;
727 break;
728 case 'w':
729 if (optarg && !*optarg)
730 write = NULL;
731 else
732 write = optarg;
733 break;
734 case 'h':
735 err = 0;
736 default:
737 usage(err);
738 }
739
740 while (optind < argc)
741 devices[numdev++] = argv[optind++];
742
743 if (version) {
744 print_version(stdout);
745 goto exit;
746 }
747
748 /* convert LABEL/UUID lookup to evaluate request */
749 if (lookup && output_format == OUTPUT_DEVICE_ONLY && search_type &&
750 (!strcmp(search_type, "LABEL") || !strcmp(search_type, "UUID"))) {
751 eval++;
752 lookup = 0;
753 }
754
755 if (!lowprobe && !eval && blkid_get_cache(&cache, read) < 0)
756 goto exit;
757
758 if (gc) {
759 blkid_gc_cache(cache);
760 err = 0;
761 goto exit;
762 }
763 err = 2;
764
765 if (eval == 0 && output_format & OUTPUT_PRETTY_LIST) {
766 if (lowprobe) {
767 fprintf(stderr, "The low-level probing mode does not "
768 "support 'list' output format\n");
769 exit(4);
770 }
771 pretty_print_dev(NULL);
772 }
773
774 if (lowprobe) {
775 /*
776 * Low-level API
777 */
778 blkid_probe pr;
779
780 if (!numdev) {
781 fprintf(stderr, "The low-level probing mode "
782 "requires a device\n");
783 exit(4);
784 }
785 pr = blkid_new_probe();
786 if (!pr)
787 goto exit;
788
789 blkid_probe_set_superblocks_flags(pr,
790 BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID |
791 BLKID_SUBLKS_TYPE | BLKID_SUBLKS_SECTYPE |
792 BLKID_SUBLKS_USAGE | BLKID_SUBLKS_VERSION);
793
794 if (fltr_usage &&
795 blkid_probe_filter_superblocks_usage(pr, fltr_flag, fltr_usage))
796 goto exit;
797 else if (fltr_type &&
798 blkid_probe_filter_superblocks_type(pr, fltr_flag, fltr_type))
799 goto exit;
800
801 for (i = 0; i < numdev; i++)
802 err = lowprobe_device(pr, devices[i], show,
803 output_format, offset, size);
804 blkid_free_probe(pr);
805 } else if (eval) {
806 /*
807 * Evaluate API
808 */
809 char *res = blkid_evaluate_tag(search_type, search_value, NULL);
810 if (res) {
811 err = 0;
812 printf("%s\n", res);
813 }
814 } else if (lookup) {
815 /*
816 * Classic (cache based) API
817 */
818 blkid_dev dev;
819
820 if (!search_type) {
821 fprintf(stderr, "The lookup option requires a "
822 "search type specified using -t\n");
823 exit(4);
824 }
825 /* Load any additional devices not in the cache */
826 for (i = 0; i < numdev; i++)
827 blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL);
828
829 if ((dev = blkid_find_dev_with_tag(cache, search_type,
830 search_value))) {
831 print_tags(dev, show, output_format);
832 err = 0;
833 }
834 /* If we didn't specify a single device, show all available devices */
835 } else if (!numdev) {
836 blkid_dev_iterate iter;
837 blkid_dev dev;
838
839 blkid_probe_all(cache);
840
841 iter = blkid_dev_iterate_begin(cache);
842 blkid_dev_set_search(iter, search_type, search_value);
843 while (blkid_dev_next(iter, &dev) == 0) {
844 dev = blkid_verify(cache, dev);
845 if (!dev)
846 continue;
847 print_tags(dev, show, output_format);
848 err = 0;
849 }
850 blkid_dev_iterate_end(iter);
851 /* Add all specified devices to cache (optionally display tags) */
852 } else for (i = 0; i < numdev; i++) {
853 blkid_dev dev = blkid_get_dev(cache, devices[i],
854 BLKID_DEV_NORMAL);
855
856 if (dev) {
857 if (search_type &&
858 !blkid_dev_has_tag(dev, search_type,
859 search_value))
860 continue;
861 print_tags(dev, show, output_format);
862 err = 0;
863 }
864 }
865
866 exit:
867 free(search_type);
868 free(search_value);
869 free_types_list(fltr_type);
870 if (!lowprobe && !eval)
871 blkid_put_cache(cache);
872 return err;
873 }