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