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