]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/blkid.c
merge coreutils' last GPL2 version of su.c
[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 return -1;
370
371 *res = str;
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 = NULL, *type = NULL, *version = NULL;
398 char enc[256];
399
400 blkid_probe_lookup_value(pr, "USAGE", &usage, NULL);
401 blkid_probe_lookup_value(pr, "TYPE", &type, NULL);
402 blkid_probe_lookup_value(pr, "VERSION", &version, NULL);
403
404 if (!usage || !type)
405 continue;
406
407 blkid_encode_string(usage, 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_AMBIVALEN=%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 = succes */
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 blkid_loff_t offset, blkid_loff_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);
489 if (fd < 0) {
490 fprintf(stderr, "error: %s: %m\n", 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 &&
507 !(chain & LOWPROBE_TOPOLOGY) &&
508 !(output & OUTPUT_UDEV_LIST) &&
509 !blkid_probe_has_value(pr, "TYPE") &&
510 !blkid_probe_has_value(pr, "PTTYPE"))
511 /*
512 * Ignore probing result if there is not any filesystem or
513 * partition table on the device and udev output is not
514 * requested.
515 *
516 * The udev db stores information about partitions, so
517 * PART_ENTRY_* values are alway important.
518 */
519 nvals = 0;
520
521 if (nvals && !first && output & (OUTPUT_UDEV_LIST | OUTPUT_EXPORT_LIST))
522 /* add extra line between output from devices */
523 fputc('\n', stdout);
524
525 if (nvals && (output & OUTPUT_DEVICE_ONLY)) {
526 printf("%s\n", devname);
527 goto done;
528 }
529
530 for (n = 0; n < nvals; n++) {
531 if (blkid_probe_get_value(pr, n, &name, &data, &len))
532 continue;
533 if (show[0] && !has_item(show, name))
534 continue;
535 len = strnlen((char *) data, len);
536 print_value(output, num++, devname, (char *) data, name, len);
537 }
538
539 if (first)
540 first = 0;
541 if (nvals >= 1 && !(output & (OUTPUT_VALUE_ONLY |
542 OUTPUT_UDEV_LIST | OUTPUT_EXPORT_LIST)))
543 printf("\n");
544 done:
545 if (rc == -2) {
546 if (output & OUTPUT_UDEV_LIST)
547 print_udev_ambivalent(pr);
548 else
549 fprintf(stderr,
550 "%s: ambivalent result (probably more "
551 "filesystems on the device, use wipefs(8) "
552 "to see more details)\n",
553 devname);
554 }
555 close(fd);
556
557 if (rc == -2)
558 return BLKID_EXIT_AMBIVAL; /* ambivalent probing result */
559 if (!nvals)
560 return BLKID_EXIT_NOTFOUND; /* nothing detected */
561
562 return 0; /* success */
563 }
564
565 /* converts comma separated list to BLKID_USAGE_* mask */
566 static int list_to_usage(const char *list, int *flag)
567 {
568 int mask = 0;
569 const char *word = NULL, *p = list;
570
571 if (p && strncmp(p, "no", 2) == 0) {
572 *flag = BLKID_FLTR_NOTIN;
573 p += 2;
574 }
575 if (!p || !*p)
576 goto err;
577 while(p) {
578 word = p;
579 p = strchr(p, ',');
580 if (p)
581 p++;
582 if (!strncmp(word, "filesystem", 10))
583 mask |= BLKID_USAGE_FILESYSTEM;
584 else if (!strncmp(word, "raid", 4))
585 mask |= BLKID_USAGE_RAID;
586 else if (!strncmp(word, "crypto", 6))
587 mask |= BLKID_USAGE_CRYPTO;
588 else if (!strncmp(word, "other", 5))
589 mask |= BLKID_USAGE_OTHER;
590 else
591 goto err;
592 }
593 return mask;
594 err:
595 *flag = 0;
596 fprintf(stderr, "unknown kerword in -u <list> argument: '%s'\n",
597 word ? word : list);
598 exit(BLKID_EXIT_OTHER);
599 }
600
601 /* converts comma separated list to types[] */
602 static char **list_to_types(const char *list, int *flag)
603 {
604 int i;
605 const char *p = list;
606 char **res = NULL;
607
608 if (p && strncmp(p, "no", 2) == 0) {
609 *flag = BLKID_FLTR_NOTIN;
610 p += 2;
611 }
612 if (!p || !*p) {
613 fprintf(stderr, "error: -u <list> argument is empty\n");
614 goto err;
615 }
616 for (i = 1; p && (p = strchr(p, ',')); i++, p++);
617
618 res = calloc(i + 1, sizeof(char *));
619 if (!res)
620 goto err_mem;
621 p = *flag & BLKID_FLTR_NOTIN ? list + 2 : list;
622 i = 0;
623
624 while(p) {
625 const char *word = p;
626 p = strchr(p, ',');
627 res[i] = p ? strndup(word, p - word) : strdup(word);
628 if (!res[i++])
629 goto err_mem;
630 if (p)
631 p++;
632 }
633 res[i] = NULL;
634 return res;
635 err_mem:
636 fprintf(stderr, "out of memory\n");
637 err:
638 *flag = 0;
639 free(res);
640 exit(BLKID_EXIT_OTHER);
641 }
642
643 static void free_types_list(char *list[])
644 {
645 char **n;
646
647 if (!list)
648 return;
649 for (n = list; *n; n++)
650 free(*n);
651 free(list);
652 }
653
654 int main(int argc, char **argv)
655 {
656 blkid_cache cache = NULL;
657 char **devices = NULL;
658 char *show[128] = { NULL, };
659 char *search_type = NULL, *search_value = NULL;
660 char *read = NULL;
661 int fltr_usage = 0;
662 char **fltr_type = NULL;
663 int fltr_flag = BLKID_FLTR_ONLYIN;
664 unsigned int numdev = 0, numtag = 0;
665 int version = 0;
666 int err = BLKID_EXIT_OTHER;
667 unsigned int i;
668 int output_format = 0;
669 int lookup = 0, gc = 0, lowprobe = 0, eval = 0;
670 int c;
671 uintmax_t offset = 0, size = 0;
672
673 show[0] = NULL;
674 atexit(close_stdout);
675
676 while ((c = getopt (argc, argv, "c:df:ghilL:n:ko:O:ps:S:t:u:U:w:v")) != EOF)
677 switch (c) {
678 case 'c':
679 if (optarg && !*optarg)
680 read = NULL;
681 else
682 read = optarg;
683 break;
684 case 'd':
685 raw_chars = 1;
686 break;
687 case 'L':
688 eval++;
689 search_value = strdup(optarg);
690 search_type = strdup("LABEL");
691 break;
692 case 'n':
693 if (fltr_usage) {
694 fprintf(stderr, "error: -u and -n options are mutually exclusive\n");
695 exit(BLKID_EXIT_OTHER);
696 }
697 fltr_type = list_to_types(optarg, &fltr_flag);
698 break;
699 case 'u':
700 if (fltr_type) {
701 fprintf(stderr, "error: -u and -n options are mutually exclusive\n");
702 exit(BLKID_EXIT_OTHER);
703 }
704 fltr_usage = list_to_usage(optarg, &fltr_flag);
705 break;
706 case 'U':
707 eval++;
708 search_value = strdup(optarg);
709 search_type = strdup("UUID");
710 break;
711 case 'i':
712 lowprobe |= LOWPROBE_TOPOLOGY;
713 break;
714 case 'l':
715 lookup++;
716 break;
717 case 'g':
718 gc = 1;
719 break;
720 case 'k':
721 {
722 size_t idx = 0;
723 const char *name = NULL;
724
725 while (blkid_superblocks_get_name(idx++, &name, NULL) == 0)
726 printf("%s\n", name);
727 exit(EXIT_SUCCESS);
728 }
729 case 'o':
730 if (!strcmp(optarg, "value"))
731 output_format = OUTPUT_VALUE_ONLY;
732 else if (!strcmp(optarg, "device"))
733 output_format = OUTPUT_DEVICE_ONLY;
734 else if (!strcmp(optarg, "list"))
735 output_format = OUTPUT_PRETTY_LIST; /* deprecated */
736 else if (!strcmp(optarg, "udev"))
737 output_format = OUTPUT_UDEV_LIST;
738 else if (!strcmp(optarg, "export"))
739 output_format = OUTPUT_EXPORT_LIST;
740 else if (!strcmp(optarg, "full"))
741 output_format = 0;
742 else {
743 fprintf(stderr, "Invalid output format %s. "
744 "Choose from value,\n\t"
745 "device, list, udev or full\n", optarg);
746 exit(BLKID_EXIT_OTHER);
747 }
748 break;
749 case 'O':
750 offset = strtosize_or_err(optarg, "invalid offset argument");
751 break;
752 case 'p':
753 lowprobe |= LOWPROBE_SUPERBLOCKS;
754 break;
755 case 's':
756 if (numtag + 1 >= sizeof(show) / sizeof(*show)) {
757 fprintf(stderr, "Too many tags specified\n");
758 usage(err);
759 }
760 show[numtag++] = optarg;
761 show[numtag] = NULL;
762 break;
763 case 'S':
764 size = strtosize_or_err(optarg, "invalid size argument");
765 break;
766 case 't':
767 if (search_type) {
768 fprintf(stderr, "Can only search for "
769 "one NAME=value pair\n");
770 usage(err);
771 }
772 if (blkid_parse_tag_string(optarg,
773 &search_type,
774 &search_value)) {
775 fprintf(stderr, "-t needs NAME=value pair\n");
776 usage(err);
777 }
778 break;
779 case 'v':
780 version = 1;
781 break;
782 case 'w':
783 /* ignore - backward compatibility */
784 break;
785 case 'h':
786 err = 0;
787 /* fallthrough */
788 default:
789 usage(err);
790 }
791
792
793 /* The rest of the args are device names */
794 if (optind < argc) {
795 devices = calloc(argc - optind, sizeof(char *));
796 if (!devices) {
797 fprintf(stderr, "Failed to allocate device name array\n");
798 goto exit;
799 }
800
801 while (optind < argc)
802 devices[numdev++] = argv[optind++];
803 }
804
805 if (version) {
806 print_version(stdout);
807 goto exit;
808 }
809
810 /* convert LABEL/UUID lookup to evaluate request */
811 if (lookup && output_format == OUTPUT_DEVICE_ONLY && search_type &&
812 (!strcmp(search_type, "LABEL") || !strcmp(search_type, "UUID"))) {
813 eval++;
814 lookup = 0;
815 }
816
817 if (!lowprobe && !eval && blkid_get_cache(&cache, read) < 0)
818 goto exit;
819
820 if (gc) {
821 blkid_gc_cache(cache);
822 err = 0;
823 goto exit;
824 }
825 err = BLKID_EXIT_NOTFOUND;
826
827 if (eval == 0 && (output_format & OUTPUT_PRETTY_LIST)) {
828 if (lowprobe) {
829 fprintf(stderr, "The low-level probing mode does not "
830 "support 'list' output format\n");
831 exit(BLKID_EXIT_OTHER);
832 }
833 pretty_print_dev(NULL);
834 }
835
836 if (lowprobe) {
837 /*
838 * Low-level API
839 */
840 blkid_probe pr;
841
842 if (!numdev) {
843 fprintf(stderr, "The low-level probing mode "
844 "requires a device\n");
845 exit(BLKID_EXIT_OTHER);
846 }
847
848 /* automatically enable 'export' format for I/O Limits */
849 if (!output_format && (lowprobe & LOWPROBE_TOPOLOGY))
850 output_format = OUTPUT_EXPORT_LIST;
851
852 pr = blkid_new_probe();
853 if (!pr)
854 goto exit;
855
856 if (lowprobe & LOWPROBE_SUPERBLOCKS) {
857 blkid_probe_set_superblocks_flags(pr,
858 BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID |
859 BLKID_SUBLKS_TYPE | BLKID_SUBLKS_SECTYPE |
860 BLKID_SUBLKS_USAGE | BLKID_SUBLKS_VERSION);
861
862 if (fltr_usage && blkid_probe_filter_superblocks_usage(
863 pr, fltr_flag, fltr_usage))
864 goto exit;
865
866 else if (fltr_type && blkid_probe_filter_superblocks_type(
867 pr, fltr_flag, fltr_type))
868 goto exit;
869 }
870
871 for (i = 0; i < numdev; i++) {
872 err = lowprobe_device(pr, devices[i], lowprobe, show,
873 output_format,
874 (blkid_loff_t) offset,
875 (blkid_loff_t) size);
876 if (err)
877 break;
878 }
879 blkid_free_probe(pr);
880 } else if (eval) {
881 /*
882 * Evaluate API
883 */
884 char *res = blkid_evaluate_tag(search_type, search_value, NULL);
885 if (res) {
886 err = 0;
887 printf("%s\n", res);
888 }
889 } else if (lookup) {
890 /*
891 * Classic (cache based) API
892 */
893 blkid_dev dev;
894
895 if (!search_type) {
896 fprintf(stderr, "The lookup option requires a "
897 "search type specified using -t\n");
898 exit(BLKID_EXIT_OTHER);
899 }
900 /* Load any additional devices not in the cache */
901 for (i = 0; i < numdev; i++)
902 blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL);
903
904 if ((dev = blkid_find_dev_with_tag(cache, search_type,
905 search_value))) {
906 print_tags(dev, show, output_format);
907 err = 0;
908 }
909 /* If we didn't specify a single device, show all available devices */
910 } else if (!numdev) {
911 blkid_dev_iterate iter;
912 blkid_dev dev;
913
914 blkid_probe_all(cache);
915
916 iter = blkid_dev_iterate_begin(cache);
917 blkid_dev_set_search(iter, search_type, search_value);
918 while (blkid_dev_next(iter, &dev) == 0) {
919 dev = blkid_verify(cache, dev);
920 if (!dev)
921 continue;
922 print_tags(dev, show, output_format);
923 err = 0;
924 }
925 blkid_dev_iterate_end(iter);
926 /* Add all specified devices to cache (optionally display tags) */
927 } else for (i = 0; i < numdev; i++) {
928 blkid_dev dev = blkid_get_dev(cache, devices[i],
929 BLKID_DEV_NORMAL);
930
931 if (dev) {
932 if (search_type &&
933 !blkid_dev_has_tag(dev, search_type,
934 search_value))
935 continue;
936 print_tags(dev, show, output_format);
937 err = 0;
938 }
939 }
940
941 exit:
942 free(search_type);
943 free(search_value);
944 free_types_list(fltr_type);
945 if (!lowprobe && !eval)
946 blkid_put_cache(cache);
947 free(devices);
948 return err;
949 }