]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/blkid.c
blkid: print ID_PART_ENTRY_* for small devices
[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
458 if (!S_ISCHR(st.st_mode) && blkid_probe_get_size(pr) <= 1024 * 1440 &&
459 blkid_probe_is_wholedisk(pr)) {
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
474 blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS);
475 blkid_probe_enable_superblocks(pr, 1);
476
477 return blkid_do_safeprobe(pr);
478 }
479
480 static int lowprobe_topology(blkid_probe pr)
481 {
482 /* enable topology probing only */
483 blkid_probe_enable_topology(pr, 1);
484
485 blkid_probe_enable_superblocks(pr, 0);
486 blkid_probe_enable_partitions(pr, 0);
487
488 return blkid_do_fullprobe(pr);
489 }
490
491 static int lowprobe_device(blkid_probe pr, const char *devname,
492 int chain, char *show[], int output,
493 blkid_loff_t offset, blkid_loff_t size)
494 {
495 const char *data;
496 const char *name;
497 int nvals = 0, n, num = 1;
498 size_t len;
499 int fd;
500 int rc = 0;
501 static int first = 1;
502
503 fd = open(devname, O_RDONLY);
504 if (fd < 0) {
505 fprintf(stderr, "error: %s: %m\n", devname);
506 return 2;
507 }
508 if (blkid_probe_set_device(pr, fd, offset, size))
509 goto done;
510
511 if (chain & LOWPROBE_TOPOLOGY)
512 rc = lowprobe_topology(pr);
513 if (rc >= 0 && (chain & LOWPROBE_SUPERBLOCKS))
514 rc = lowprobe_superblocks(pr);
515 if (rc < 0)
516 goto done;
517
518 if (!rc)
519 nvals = blkid_probe_numof_values(pr);
520
521 if (nvals &&
522 !(chain & LOWPROBE_TOPOLOGY) &&
523 !(output & OUTPUT_UDEV_LIST) &&
524 !blkid_probe_has_value(pr, "TYPE") &&
525 !blkid_probe_has_value(pr, "PTTYPE"))
526 /*
527 * Ignore probing result if there is not any filesystem or
528 * partition table on the device and udev output is not
529 * requested.
530 *
531 * The udev db stores information about partitions, so
532 * PART_ENTRY_* values are alway important.
533 */
534 nvals = 0;
535
536 if (nvals && !first && output & (OUTPUT_UDEV_LIST | OUTPUT_EXPORT_LIST))
537 /* add extra line between output from devices */
538 fputc('\n', stdout);
539
540 if (nvals && (output & OUTPUT_DEVICE_ONLY)) {
541 printf("%s\n", devname);
542 goto done;
543 }
544
545 for (n = 0; n < nvals; n++) {
546 if (blkid_probe_get_value(pr, n, &name, &data, &len))
547 continue;
548 if (show[0] && !has_item(show, name))
549 continue;
550 len = strnlen((char *) data, len);
551 print_value(output, num++, devname, (char *) data, name, len);
552 }
553
554 if (first)
555 first = 0;
556 if (nvals >= 1 && !(output & (OUTPUT_VALUE_ONLY |
557 OUTPUT_UDEV_LIST | OUTPUT_EXPORT_LIST)))
558 printf("\n");
559 done:
560 if (rc == -2) {
561 if (output & OUTPUT_UDEV_LIST)
562 print_udev_ambivalent(pr);
563 else
564 fprintf(stderr,
565 "%s: ambivalent result (probably more "
566 "filesystems on the device, use wipefs(8) "
567 "to see more details)\n",
568 devname);
569 }
570 close(fd);
571
572 if (rc == -2)
573 return 8; /* ambivalent probing result */
574 if (!nvals)
575 return 2; /* nothing detected */
576
577 return 0; /* sucess */
578 }
579
580 /* converts comma separated list to BLKID_USAGE_* mask */
581 static int list_to_usage(const char *list, int *flag)
582 {
583 int mask = 0;
584 const char *word = NULL, *p = list;
585
586 if (p && strncmp(p, "no", 2) == 0) {
587 *flag = BLKID_FLTR_NOTIN;
588 p += 2;
589 }
590 if (!p || !*p)
591 goto err;
592 while(p) {
593 word = p;
594 p = strchr(p, ',');
595 if (p)
596 p++;
597 if (!strncmp(word, "filesystem", 10))
598 mask |= BLKID_USAGE_FILESYSTEM;
599 else if (!strncmp(word, "raid", 4))
600 mask |= BLKID_USAGE_RAID;
601 else if (!strncmp(word, "crypto", 6))
602 mask |= BLKID_USAGE_CRYPTO;
603 else if (!strncmp(word, "other", 5))
604 mask |= BLKID_USAGE_OTHER;
605 else
606 goto err;
607 }
608 return mask;
609 err:
610 *flag = 0;
611 fprintf(stderr, "unknown kerword in -u <list> argument: '%s'\n",
612 word ? word : list);
613 exit(4);
614 }
615
616 /* converts comma separated list to types[] */
617 static char **list_to_types(const char *list, int *flag)
618 {
619 int i;
620 const char *p = list;
621 char **res = NULL;
622
623 if (p && strncmp(p, "no", 2) == 0) {
624 *flag = BLKID_FLTR_NOTIN;
625 p += 2;
626 }
627 if (!p || !*p) {
628 fprintf(stderr, "error: -u <list> argument is empty\n");
629 goto err;
630 }
631 for (i = 1; p && (p = strchr(p, ',')); i++, p++);
632
633 res = calloc(i + 1, sizeof(char *));
634 if (!res)
635 goto err_mem;
636 p = *flag & BLKID_FLTR_NOTIN ? list + 2 : list;
637 i = 0;
638
639 while(p) {
640 const char *word = p;
641 p = strchr(p, ',');
642 res[i] = p ? strndup(word, p - word) : strdup(word);
643 if (!res[i++])
644 goto err_mem;
645 if (p)
646 p++;
647 }
648 res[i] = NULL;
649 return res;
650 err_mem:
651 fprintf(stderr, "out of memory\n");
652 err:
653 *flag = 0;
654 free(res);
655 exit(4);
656 }
657
658 static void free_types_list(char *list[])
659 {
660 char **n;
661
662 if (!list)
663 return;
664 for (n = list; *n; n++)
665 free(*n);
666 free(list);
667 }
668
669 int main(int argc, char **argv)
670 {
671 blkid_cache cache = NULL;
672 char **devices = NULL;
673 char *show[128] = { NULL, };
674 char *search_type = NULL, *search_value = NULL;
675 char *read = NULL;
676 char *write = NULL;
677 int fltr_usage = 0;
678 char **fltr_type = NULL;
679 int fltr_flag = BLKID_FLTR_ONLYIN;
680 unsigned int numdev = 0, numtag = 0;
681 int version = 0;
682 int err = 4;
683 unsigned int i;
684 int output_format = 0;
685 int lookup = 0, gc = 0, lowprobe = 0, eval = 0;
686 int c;
687 uintmax_t offset = 0, size = 0;
688
689 show[0] = NULL;
690
691 while ((c = getopt (argc, argv, "c:df:ghilL:n:ko:O:ps:S:t:u:U:w:v")) != EOF)
692 switch (c) {
693 case 'c':
694 if (optarg && !*optarg)
695 read = NULL;
696 else
697 read = optarg;
698 if (!write)
699 write = read;
700 break;
701 case 'd':
702 raw_chars = 1;
703 break;
704 case 'L':
705 eval++;
706 search_value = strdup(optarg);
707 search_type = strdup("LABEL");
708 break;
709 case 'n':
710 if (fltr_usage) {
711 fprintf(stderr, "error: -u and -n options are mutually exclusive\n");
712 exit(4);
713 }
714 fltr_type = list_to_types(optarg, &fltr_flag);
715 break;
716 case 'u':
717 if (fltr_type) {
718 fprintf(stderr, "error: -u and -n options are mutually exclusive\n");
719 exit(4);
720 }
721 fltr_usage = list_to_usage(optarg, &fltr_flag);
722 break;
723 case 'U':
724 eval++;
725 search_value = strdup(optarg);
726 search_type = strdup("UUID");
727 break;
728 case 'i':
729 lowprobe |= LOWPROBE_TOPOLOGY;
730 break;
731 case 'l':
732 lookup++;
733 break;
734 case 'g':
735 gc = 1;
736 break;
737 case 'k':
738 {
739 size_t idx = 0;
740 const char *name = NULL;
741
742 while (blkid_superblocks_get_name(idx++, &name, NULL) == 0)
743 printf("%s\n", name);
744 exit(0);
745 }
746 case 'o':
747 if (!strcmp(optarg, "value"))
748 output_format = OUTPUT_VALUE_ONLY;
749 else if (!strcmp(optarg, "device"))
750 output_format = OUTPUT_DEVICE_ONLY;
751 else if (!strcmp(optarg, "list"))
752 output_format = OUTPUT_PRETTY_LIST;
753 else if (!strcmp(optarg, "udev"))
754 output_format = OUTPUT_UDEV_LIST;
755 else if (!strcmp(optarg, "export"))
756 output_format = OUTPUT_EXPORT_LIST;
757 else if (!strcmp(optarg, "full"))
758 output_format = 0;
759 else {
760 fprintf(stderr, "Invalid output format %s. "
761 "Choose from value,\n\t"
762 "device, list, udev or full\n", optarg);
763 exit(4);
764 }
765 break;
766 case 'O':
767 if (strtosize(optarg, &offset))
768 fprintf(stderr,
769 "Invalid offset '%s' specified\n",
770 optarg);
771 break;
772 case 'p':
773 lowprobe |= LOWPROBE_SUPERBLOCKS;
774 break;
775 case 's':
776 if (numtag + 1 >= sizeof(show) / sizeof(*show)) {
777 fprintf(stderr, "Too many tags specified\n");
778 usage(err);
779 }
780 show[numtag++] = optarg;
781 show[numtag] = NULL;
782 break;
783 case 'S':
784 if (strtosize(optarg, &size))
785 fprintf(stderr,
786 "Invalid size '%s' specified\n",
787 optarg);
788 break;
789 case 't':
790 if (search_type) {
791 fprintf(stderr, "Can only search for "
792 "one NAME=value pair\n");
793 usage(err);
794 }
795 if (blkid_parse_tag_string(optarg,
796 &search_type,
797 &search_value)) {
798 fprintf(stderr, "-t needs NAME=value pair\n");
799 usage(err);
800 }
801 break;
802 case 'v':
803 version = 1;
804 break;
805 case 'w':
806 if (optarg && !*optarg)
807 write = NULL;
808 else
809 write = optarg;
810 break;
811 case 'h':
812 err = 0;
813 default:
814 usage(err);
815 }
816
817
818 /* The rest of the args are device names */
819 if (optind < argc) {
820 devices = calloc(argc - optind, sizeof(char *));
821 if (!devices) {
822 fprintf(stderr, "Failed to allocate device name array\n");
823 goto exit;
824 }
825
826 while (optind < argc)
827 devices[numdev++] = argv[optind++];
828 }
829
830 if (version) {
831 print_version(stdout);
832 goto exit;
833 }
834
835 /* convert LABEL/UUID lookup to evaluate request */
836 if (lookup && output_format == OUTPUT_DEVICE_ONLY && search_type &&
837 (!strcmp(search_type, "LABEL") || !strcmp(search_type, "UUID"))) {
838 eval++;
839 lookup = 0;
840 }
841
842 if (!lowprobe && !eval && blkid_get_cache(&cache, read) < 0)
843 goto exit;
844
845 if (gc) {
846 blkid_gc_cache(cache);
847 err = 0;
848 goto exit;
849 }
850 err = 2;
851
852 if (eval == 0 && (output_format & OUTPUT_PRETTY_LIST)) {
853 if (lowprobe) {
854 fprintf(stderr, "The low-level probing mode does not "
855 "support 'list' output format\n");
856 exit(4);
857 }
858 pretty_print_dev(NULL);
859 }
860
861 if (lowprobe) {
862 /*
863 * Low-level API
864 */
865 blkid_probe pr;
866
867 if (!numdev) {
868 fprintf(stderr, "The low-level probing mode "
869 "requires a device\n");
870 exit(4);
871 }
872
873 /* automatically enable 'export' format for I/O Limits */
874 if (!output_format && (lowprobe & LOWPROBE_TOPOLOGY))
875 output_format = OUTPUT_EXPORT_LIST;
876
877 pr = blkid_new_probe();
878 if (!pr)
879 goto exit;
880
881 if (lowprobe & LOWPROBE_SUPERBLOCKS) {
882 blkid_probe_set_superblocks_flags(pr,
883 BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID |
884 BLKID_SUBLKS_TYPE | BLKID_SUBLKS_SECTYPE |
885 BLKID_SUBLKS_USAGE | BLKID_SUBLKS_VERSION);
886
887 if (fltr_usage && blkid_probe_filter_superblocks_usage(
888 pr, fltr_flag, fltr_usage))
889 goto exit;
890
891 else if (fltr_type && blkid_probe_filter_superblocks_type(
892 pr, fltr_flag, fltr_type))
893 goto exit;
894 }
895
896 for (i = 0; i < numdev; i++)
897 err = lowprobe_device(pr, devices[i], lowprobe, show,
898 output_format,
899 (blkid_loff_t) offset,
900 (blkid_loff_t) size);
901 blkid_free_probe(pr);
902 } else if (eval) {
903 /*
904 * Evaluate API
905 */
906 char *res = blkid_evaluate_tag(search_type, search_value, NULL);
907 if (res) {
908 err = 0;
909 printf("%s\n", res);
910 }
911 } else if (lookup) {
912 /*
913 * Classic (cache based) API
914 */
915 blkid_dev dev;
916
917 if (!search_type) {
918 fprintf(stderr, "The lookup option requires a "
919 "search type specified using -t\n");
920 exit(4);
921 }
922 /* Load any additional devices not in the cache */
923 for (i = 0; i < numdev; i++)
924 blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL);
925
926 if ((dev = blkid_find_dev_with_tag(cache, search_type,
927 search_value))) {
928 print_tags(dev, show, output_format);
929 err = 0;
930 }
931 /* If we didn't specify a single device, show all available devices */
932 } else if (!numdev) {
933 blkid_dev_iterate iter;
934 blkid_dev dev;
935
936 blkid_probe_all(cache);
937
938 iter = blkid_dev_iterate_begin(cache);
939 blkid_dev_set_search(iter, search_type, search_value);
940 while (blkid_dev_next(iter, &dev) == 0) {
941 dev = blkid_verify(cache, dev);
942 if (!dev)
943 continue;
944 print_tags(dev, show, output_format);
945 err = 0;
946 }
947 blkid_dev_iterate_end(iter);
948 /* Add all specified devices to cache (optionally display tags) */
949 } else for (i = 0; i < numdev; i++) {
950 blkid_dev dev = blkid_get_dev(cache, devices[i],
951 BLKID_DEV_NORMAL);
952
953 if (dev) {
954 if (search_type &&
955 !blkid_dev_has_tag(dev, search_type,
956 search_value))
957 continue;
958 print_tags(dev, show, output_format);
959 err = 0;
960 }
961 }
962
963 exit:
964 free(search_type);
965 free(search_value);
966 free_types_list(fltr_type);
967 if (!lowprobe && !eval)
968 blkid_put_cache(cache);
969 free(devices);
970 return err;
971 }