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