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