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