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