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