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