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