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