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