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