]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/blkid.c
blkid: add DEVNAME= to export output format
[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>] [<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> read from <file> instead of reading from the default\n"
71 " cache file (-c /dev/null means no cache)\n"
72 " -d don't encode non-printing characters\n"
73 " -h print this usage message and exit\n"
74 " -g garbage collect the blkid cache\n"
75 " -o <format> output format; can be one of:\n"
76 " value, device, list, udev, export or full; (default: full)\n"
77 " -k list all known filesystems/RAIDs and exit\n"
78 " -s <tag> show specified tag(s) (default show all tags)\n"
79 " -t <token> find device with a specific token (NAME=value pair)\n"
80 " -l look up only first device with token specified by -t\n"
81 " -L <label> convert LABEL to device name\n"
82 " -U <uuid> convert UUID to device name\n"
83 " -v print version and exit\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 pretty_print_word(mtpt, mtpt_len, len, 0);
195
196 fputs(uuid, stdout);
197 fputc('\n', stdout);
198 }
199
200 static void pretty_print_dev(blkid_dev dev)
201 {
202 blkid_tag_iterate iter;
203 const char *type, *value, *devname;
204 const char *uuid = "", *fs_type = "", *label = "";
205 int len, mount_flags;
206 char mtpt[80];
207 int retval;
208
209 if (dev == NULL) {
210 pretty_print_line("device", "fs_type", "label",
211 "mount point", "UUID");
212 for (len=get_terminal_width()-1; len > 0; len--)
213 fputc('-', stdout);
214 fputc('\n', stdout);
215 return;
216 }
217
218 devname = blkid_dev_devname(dev);
219 if (access(devname, F_OK))
220 return;
221
222 /* Get the uuid, label, type */
223 iter = blkid_tag_iterate_begin(dev);
224 while (blkid_tag_next(iter, &type, &value) == 0) {
225 if (!strcmp(type, "UUID"))
226 uuid = value;
227 if (!strcmp(type, "TYPE"))
228 fs_type = value;
229 if (!strcmp(type, "LABEL"))
230 label = value;
231 }
232 blkid_tag_iterate_end(iter);
233
234 /* Get the mount point */
235 mtpt[0] = 0;
236 retval = check_mount_point(devname, &mount_flags, mtpt, sizeof(mtpt));
237 if (retval == 0) {
238 if (mount_flags & MF_MOUNTED) {
239 if (!mtpt[0])
240 strcpy(mtpt, "(mounted, mtpt unknown)");
241 } else if (mount_flags & MF_BUSY)
242 strcpy(mtpt, "(in use)");
243 else
244 strcpy(mtpt, "(not mounted)");
245 }
246
247 pretty_print_line(devname, fs_type, label, mtpt, uuid);
248 }
249
250 static void print_udev_format(const char *name, const char *value)
251 {
252 char enc[265], safe[256];
253 size_t namelen = strlen(name);
254
255 *safe = *enc = '\0';
256
257 if (!strcmp(name, "TYPE") || !strcmp(name, "VERSION")) {
258 blkid_encode_string(value, enc, sizeof(enc));
259 printf("ID_FS_%s=%s\n", name, enc);
260
261 } else if (!strcmp(name, "UUID") ||
262 !strcmp(name, "LABEL") ||
263 !strcmp(name, "UUID_SUB")) {
264
265 blkid_safe_string(value, safe, sizeof(safe));
266 printf("ID_FS_%s=%s\n", name, safe);
267
268 blkid_encode_string(value, enc, sizeof(enc));
269 printf("ID_FS_%s_ENC=%s\n", name, enc);
270
271 } else if (!strcmp(name, "PTTYPE")) {
272 printf("ID_PART_TABLE_TYPE=%s\n", value);
273
274 } else if (!strcmp(name, "PART_ENTRY_NAME") ||
275 !strcmp(name, "PART_ENTRY_TYPE")) {
276
277 blkid_encode_string(value, enc, sizeof(enc));
278 printf("ID_%s=%s\n", name, enc);
279
280 } else if (!strncmp(name, "PART_ENTRY_", 11))
281 printf("ID_%s=%s\n", name, value);
282
283 else if (namelen >= 15 && (
284 !strcmp(name + (namelen - 12), "_SECTOR_SIZE") ||
285 !strcmp(name + (namelen - 8), "_IO_SIZE") ||
286 !strcmp(name, "ALIGNMENT_OFFSET")))
287 printf("ID_IOLIMIT_%s=%s\n", name, value);
288 else
289 printf("ID_FS_%s=%s\n", name, value);
290 }
291
292 static int has_item(char *ary[], const char *item)
293 {
294 char **p;
295
296 for (p = ary; *p != NULL; p++)
297 if (!strcmp(item, *p))
298 return 1;
299 return 0;
300 }
301
302 static void print_value(int output, int num, const char *devname,
303 const char *value, const char *name, size_t valsz)
304 {
305 if (output & OUTPUT_VALUE_ONLY) {
306 fputs(value, stdout);
307 fputc('\n', stdout);
308
309 } else if (output & OUTPUT_UDEV_LIST) {
310 print_udev_format(name, value);
311
312 } else if (output & OUTPUT_EXPORT_LIST) {
313 if (num == 1 && devname)
314 printf("DEVNAME=%s\n", devname);
315 fputs(name, stdout);
316 fputs("=", stdout);
317 safe_print(value, valsz);
318 fputs("\n", stdout);
319
320 } else {
321 if (num == 1 && devname)
322 printf("%s: ", devname);
323 fputs(name, stdout);
324 fputs("=\"", stdout);
325 safe_print(value, valsz);
326 fputs("\" ", stdout);
327 }
328 }
329
330 static void print_tags(blkid_dev dev, char *show[], int output)
331 {
332 blkid_tag_iterate iter;
333 const char *type, *value, *devname;
334 int num = 1;
335 static int first = 1;
336
337 if (!dev)
338 return;
339
340 if (output & OUTPUT_PRETTY_LIST) {
341 pretty_print_dev(dev);
342 return;
343 }
344
345 devname = blkid_dev_devname(dev);
346
347 if (output & OUTPUT_DEVICE_ONLY) {
348 printf("%s\n", devname);
349 return;
350 }
351
352 iter = blkid_tag_iterate_begin(dev);
353 while (blkid_tag_next(iter, &type, &value) == 0) {
354 if (show[0] && !has_item(show, type))
355 continue;
356
357 if (num == 1 && !first &&
358 (output & (OUTPUT_UDEV_LIST | OUTPUT_EXPORT_LIST)))
359 /* add extra line between output from more devices */
360 fputc('\n', stdout);
361
362 print_value(output, num++, devname, value, type, strlen(value));
363 }
364 blkid_tag_iterate_end(iter);
365
366 if (num > 1) {
367 if (!(output & (OUTPUT_VALUE_ONLY | OUTPUT_UDEV_LIST |
368 OUTPUT_EXPORT_LIST)))
369 printf("\n");
370 first = 0;
371 }
372 }
373
374
375 static int append_str(char **res, size_t *sz, const char *a, const char *b)
376 {
377 char *str = *res;
378 size_t asz = a ? strlen(a) : 0;
379 size_t bsz = b ? strlen(b) : 0;
380 size_t len = *sz + asz + bsz;
381
382 if (!len)
383 return -1;
384
385 str = realloc(str, len + 1);
386 if (!str)
387 return -1;
388
389 *res = str;
390 str += *sz;
391
392 if (a) {
393 memcpy(str, a, asz);
394 str += asz;
395 }
396 if (b) {
397 memcpy(str, b, bsz);
398 str += bsz;
399 }
400 *str = '\0';
401 *sz = len;
402 return 0;
403 }
404
405 /*
406 * Compose and print ID_FS_AMBIVALENT for udev
407 */
408 static int print_udev_ambivalent(blkid_probe pr)
409 {
410 char *val = NULL;
411 size_t valsz = 0;
412 int count = 0, rc = -1;
413
414 while (!blkid_do_probe(pr)) {
415 const char *usage = NULL, *type = NULL, *version = NULL;
416 char enc[256];
417
418 blkid_probe_lookup_value(pr, "USAGE", &usage, NULL);
419 blkid_probe_lookup_value(pr, "TYPE", &type, NULL);
420 blkid_probe_lookup_value(pr, "VERSION", &version, NULL);
421
422 if (!usage || !type)
423 continue;
424
425 blkid_encode_string(usage, enc, sizeof(enc));
426 if (append_str(&val, &valsz, enc, ":"))
427 goto done;
428
429 blkid_encode_string(type, enc, sizeof(enc));
430 if (append_str(&val, &valsz, enc, version ? ":" : " "))
431 goto done;
432
433 if (version) {
434 blkid_encode_string(version, enc, sizeof(enc));
435 if (append_str(&val, &valsz, enc, " "))
436 goto done;
437 }
438 count++;
439 }
440
441 if (count > 1) {
442 *(val + valsz - 1) = '\0'; /* rem tailing whitespace */
443 printf("ID_FS_AMBIVALEN=%s\n", val);
444 rc = 0;
445 }
446 done:
447 free(val);
448 return rc;
449 }
450
451 static int lowprobe_superblocks(blkid_probe pr)
452 {
453 struct stat st;
454 int rc, fd = blkid_probe_get_fd(pr);
455
456 if (fd < 0 || fstat(fd, &st))
457 return -1;
458
459 blkid_probe_enable_partitions(pr, 1);
460
461 if (!S_ISCHR(st.st_mode) && blkid_probe_get_size(pr) <= 1024 * 1440 &&
462 blkid_probe_is_wholedisk(pr)) {
463 /*
464 * check if the small disk is partitioned, if yes then
465 * don't probe for filesystems.
466 */
467 blkid_probe_enable_superblocks(pr, 0);
468
469 rc = blkid_do_fullprobe(pr);
470 if (rc < 0)
471 return rc; /* -1 = error, 1 = nothing, 0 = succes */
472
473 if (blkid_probe_lookup_value(pr, "PTTYPE", NULL, NULL) == 0)
474 return 0; /* partition table detected */
475 }
476
477 blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS);
478 blkid_probe_enable_superblocks(pr, 1);
479
480 return blkid_do_safeprobe(pr);
481 }
482
483 static int lowprobe_topology(blkid_probe pr)
484 {
485 /* enable topology probing only */
486 blkid_probe_enable_topology(pr, 1);
487
488 blkid_probe_enable_superblocks(pr, 0);
489 blkid_probe_enable_partitions(pr, 0);
490
491 return blkid_do_fullprobe(pr);
492 }
493
494 static int lowprobe_device(blkid_probe pr, const char *devname,
495 int chain, char *show[], int output,
496 blkid_loff_t offset, blkid_loff_t size)
497 {
498 const char *data;
499 const char *name;
500 int nvals = 0, n, num = 1;
501 size_t len;
502 int fd;
503 int rc = 0;
504 static int first = 1;
505
506 fd = open(devname, O_RDONLY);
507 if (fd < 0) {
508 fprintf(stderr, "error: %s: %m\n", devname);
509 return 2;
510 }
511 if (blkid_probe_set_device(pr, fd, offset, size))
512 goto done;
513
514 if (chain & LOWPROBE_TOPOLOGY)
515 rc = lowprobe_topology(pr);
516 if (rc >= 0 && (chain & LOWPROBE_SUPERBLOCKS))
517 rc = lowprobe_superblocks(pr);
518 if (rc < 0)
519 goto done;
520
521 if (!rc)
522 nvals = blkid_probe_numof_values(pr);
523
524 if (nvals &&
525 !(chain & LOWPROBE_TOPOLOGY) &&
526 !(output & OUTPUT_UDEV_LIST) &&
527 !blkid_probe_has_value(pr, "TYPE") &&
528 !blkid_probe_has_value(pr, "PTTYPE"))
529 /*
530 * Ignore probing result if there is not any filesystem or
531 * partition table on the device and udev output is not
532 * requested.
533 *
534 * The udev db stores information about partitions, so
535 * PART_ENTRY_* values are alway important.
536 */
537 nvals = 0;
538
539 if (nvals && !first && output & (OUTPUT_UDEV_LIST | OUTPUT_EXPORT_LIST))
540 /* add extra line between output from devices */
541 fputc('\n', stdout);
542
543 if (nvals && (output & OUTPUT_DEVICE_ONLY)) {
544 printf("%s\n", devname);
545 goto done;
546 }
547
548 for (n = 0; n < nvals; n++) {
549 if (blkid_probe_get_value(pr, n, &name, &data, &len))
550 continue;
551 if (show[0] && !has_item(show, name))
552 continue;
553 len = strnlen((char *) data, len);
554 print_value(output, num++, devname, (char *) data, name, len);
555 }
556
557 if (first)
558 first = 0;
559 if (nvals >= 1 && !(output & (OUTPUT_VALUE_ONLY |
560 OUTPUT_UDEV_LIST | OUTPUT_EXPORT_LIST)))
561 printf("\n");
562 done:
563 if (rc == -2) {
564 if (output & OUTPUT_UDEV_LIST)
565 print_udev_ambivalent(pr);
566 else
567 fprintf(stderr,
568 "%s: ambivalent result (probably more "
569 "filesystems on the device, use wipefs(8) "
570 "to see more details)\n",
571 devname);
572 }
573 close(fd);
574
575 if (rc == -2)
576 return 8; /* ambivalent probing result */
577 if (!nvals)
578 return 2; /* nothing detected */
579
580 return 0; /* sucess */
581 }
582
583 /* converts comma separated list to BLKID_USAGE_* mask */
584 static int list_to_usage(const char *list, int *flag)
585 {
586 int mask = 0;
587 const char *word = NULL, *p = list;
588
589 if (p && strncmp(p, "no", 2) == 0) {
590 *flag = BLKID_FLTR_NOTIN;
591 p += 2;
592 }
593 if (!p || !*p)
594 goto err;
595 while(p) {
596 word = p;
597 p = strchr(p, ',');
598 if (p)
599 p++;
600 if (!strncmp(word, "filesystem", 10))
601 mask |= BLKID_USAGE_FILESYSTEM;
602 else if (!strncmp(word, "raid", 4))
603 mask |= BLKID_USAGE_RAID;
604 else if (!strncmp(word, "crypto", 6))
605 mask |= BLKID_USAGE_CRYPTO;
606 else if (!strncmp(word, "other", 5))
607 mask |= BLKID_USAGE_OTHER;
608 else
609 goto err;
610 }
611 return mask;
612 err:
613 *flag = 0;
614 fprintf(stderr, "unknown kerword in -u <list> argument: '%s'\n",
615 word ? word : list);
616 exit(4);
617 }
618
619 /* converts comma separated list to types[] */
620 static char **list_to_types(const char *list, int *flag)
621 {
622 int i;
623 const char *p = list;
624 char **res = NULL;
625
626 if (p && strncmp(p, "no", 2) == 0) {
627 *flag = BLKID_FLTR_NOTIN;
628 p += 2;
629 }
630 if (!p || !*p) {
631 fprintf(stderr, "error: -u <list> argument is empty\n");
632 goto err;
633 }
634 for (i = 1; p && (p = strchr(p, ',')); i++, p++);
635
636 res = calloc(i + 1, sizeof(char *));
637 if (!res)
638 goto err_mem;
639 p = *flag & BLKID_FLTR_NOTIN ? list + 2 : list;
640 i = 0;
641
642 while(p) {
643 const char *word = p;
644 p = strchr(p, ',');
645 res[i] = p ? strndup(word, p - word) : strdup(word);
646 if (!res[i++])
647 goto err_mem;
648 if (p)
649 p++;
650 }
651 res[i] = NULL;
652 return res;
653 err_mem:
654 fprintf(stderr, "out of memory\n");
655 err:
656 *flag = 0;
657 free(res);
658 exit(4);
659 }
660
661 static void free_types_list(char *list[])
662 {
663 char **n;
664
665 if (!list)
666 return;
667 for (n = list; *n; n++)
668 free(*n);
669 free(list);
670 }
671
672 int main(int argc, char **argv)
673 {
674 blkid_cache cache = NULL;
675 char **devices = NULL;
676 char *show[128] = { NULL, };
677 char *search_type = NULL, *search_value = NULL;
678 char *read = 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 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 /* ignore - backward compatibility */
807 break;
808 case 'h':
809 err = 0;
810 /* fallthrough */
811 default:
812 usage(err);
813 }
814
815
816 /* The rest of the args are device names */
817 if (optind < argc) {
818 devices = calloc(argc - optind, sizeof(char *));
819 if (!devices) {
820 fprintf(stderr, "Failed to allocate device name array\n");
821 goto exit;
822 }
823
824 while (optind < argc)
825 devices[numdev++] = argv[optind++];
826 }
827
828 if (version) {
829 print_version(stdout);
830 goto exit;
831 }
832
833 /* convert LABEL/UUID lookup to evaluate request */
834 if (lookup && output_format == OUTPUT_DEVICE_ONLY && search_type &&
835 (!strcmp(search_type, "LABEL") || !strcmp(search_type, "UUID"))) {
836 eval++;
837 lookup = 0;
838 }
839
840 if (!lowprobe && !eval && blkid_get_cache(&cache, read) < 0)
841 goto exit;
842
843 if (gc) {
844 blkid_gc_cache(cache);
845 err = 0;
846 goto exit;
847 }
848 err = 2;
849
850 if (eval == 0 && (output_format & OUTPUT_PRETTY_LIST)) {
851 if (lowprobe) {
852 fprintf(stderr, "The low-level probing mode does not "
853 "support 'list' output format\n");
854 exit(4);
855 }
856 pretty_print_dev(NULL);
857 }
858
859 if (lowprobe) {
860 /*
861 * Low-level API
862 */
863 blkid_probe pr;
864
865 if (!numdev) {
866 fprintf(stderr, "The low-level probing mode "
867 "requires a device\n");
868 exit(4);
869 }
870
871 /* automatically enable 'export' format for I/O Limits */
872 if (!output_format && (lowprobe & LOWPROBE_TOPOLOGY))
873 output_format = OUTPUT_EXPORT_LIST;
874
875 pr = blkid_new_probe();
876 if (!pr)
877 goto exit;
878
879 if (lowprobe & LOWPROBE_SUPERBLOCKS) {
880 blkid_probe_set_superblocks_flags(pr,
881 BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID |
882 BLKID_SUBLKS_TYPE | BLKID_SUBLKS_SECTYPE |
883 BLKID_SUBLKS_USAGE | BLKID_SUBLKS_VERSION);
884
885 if (fltr_usage && blkid_probe_filter_superblocks_usage(
886 pr, fltr_flag, fltr_usage))
887 goto exit;
888
889 else if (fltr_type && blkid_probe_filter_superblocks_type(
890 pr, fltr_flag, fltr_type))
891 goto exit;
892 }
893
894 for (i = 0; i < numdev; i++)
895 err = lowprobe_device(pr, devices[i], lowprobe, show,
896 output_format,
897 (blkid_loff_t) offset,
898 (blkid_loff_t) size);
899 blkid_free_probe(pr);
900 } else if (eval) {
901 /*
902 * Evaluate API
903 */
904 char *res = blkid_evaluate_tag(search_type, search_value, NULL);
905 if (res) {
906 err = 0;
907 printf("%s\n", res);
908 }
909 } else if (lookup) {
910 /*
911 * Classic (cache based) API
912 */
913 blkid_dev dev;
914
915 if (!search_type) {
916 fprintf(stderr, "The lookup option requires a "
917 "search type specified using -t\n");
918 exit(4);
919 }
920 /* Load any additional devices not in the cache */
921 for (i = 0; i < numdev; i++)
922 blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL);
923
924 if ((dev = blkid_find_dev_with_tag(cache, search_type,
925 search_value))) {
926 print_tags(dev, show, output_format);
927 err = 0;
928 }
929 /* If we didn't specify a single device, show all available devices */
930 } else if (!numdev) {
931 blkid_dev_iterate iter;
932 blkid_dev dev;
933
934 blkid_probe_all(cache);
935
936 iter = blkid_dev_iterate_begin(cache);
937 blkid_dev_set_search(iter, search_type, search_value);
938 while (blkid_dev_next(iter, &dev) == 0) {
939 dev = blkid_verify(cache, dev);
940 if (!dev)
941 continue;
942 print_tags(dev, show, output_format);
943 err = 0;
944 }
945 blkid_dev_iterate_end(iter);
946 /* Add all specified devices to cache (optionally display tags) */
947 } else for (i = 0; i < numdev; i++) {
948 blkid_dev dev = blkid_get_dev(cache, devices[i],
949 BLKID_DEV_NORMAL);
950
951 if (dev) {
952 if (search_type &&
953 !blkid_dev_has_tag(dev, search_type,
954 search_value))
955 continue;
956 print_tags(dev, show, output_format);
957 err = 0;
958 }
959 }
960
961 exit:
962 free(search_type);
963 free(search_value);
964 free_types_list(fltr_type);
965 if (!lowprobe && !eval)
966 blkid_put_cache(cache);
967 free(devices);
968 return err;
969 }