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