]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/blkid.c
lscpu: add --bytes
[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 print_version(FILE *out)
66 {
67 fprintf(out, _("%s from %s (libblkid %s, %s)\n"),
68 program_invocation_short_name, PACKAGE_STRING,
69 LIBBLKID_VERSION, LIBBLKID_DATE);
70 }
71
72 static void __attribute__((__noreturn__)) usage(void)
73 {
74 FILE *out = stdout;
75
76 fputs(USAGE_HEADER, out);
77 fprintf(out, _( " %s --label <label> | --uuid <uuid>\n\n"), program_invocation_short_name);
78 fprintf(out, _( " %s [--cache-file <file>] [-ghlLv] [--output <format>] [--match-tag <tag>] \n"
79 " [--match-token <token>] [<dev> ...]\n\n"), program_invocation_short_name);
80 fprintf(out, _( " %s -p [--match-tag <tag>] [--offset <offset>] [--size <size>] \n"
81 " [--output <format>] <dev> ...\n\n"), program_invocation_short_name);
82 fprintf(out, _( " %s -i [--match-tag <tag>] [--output <format>] <dev> ...\n"), program_invocation_short_name);
83 fputs(USAGE_OPTIONS, out);
84 fputs(_( " -c, --cache-file <file> read from <file> instead of reading from the default\n"
85 " cache file (-c /dev/null means no cache)\n"), out);
86 fputs(_( " -d, --no-encoding don't encode non-printing characters\n"), out);
87 fputs(_( " -g, --garbage-collect garbage collect the blkid cache\n"), out);
88 fputs(_( " -o, --output <format> output format; can be one of:\n"
89 " value, device, export or full; (default: full)\n"), out);
90 fputs(_( " -k, --list-filesystems list all known filesystems/RAIDs and exit\n"), out);
91 fputs(_( " -s, --match-tag <tag> show specified tag(s) (default show all tags)\n"), out);
92 fputs(_( " -t, --match-token <token> find device with a specific token (NAME=value pair)\n"), out);
93 fputs(_( " -l, --list-one look up only first device with token specified by -t\n"), out);
94 fputs(_( " -L, --label <label> convert LABEL to device name\n"), out);
95 fputs(_( " -U, --uuid <uuid> convert UUID to device name\n"), out);
96 fputs(_( " <dev> specify device(s) to probe (default: all devices)\n"), out);
97 fputs( "\n", out);
98 fputs(_( "Low-level probing options:\n"), out);
99 fputs(_( " -p, --probe low-level superblocks probing (bypass cache)\n"), out);
100 fputs(_( " -i, --info gather information about I/O limits\n"), out);
101 fputs(_( " -S, --size <size> overwrite device size\n"), out);
102 fputs(_( " -O, --offset <offset> probe at the given offset\n"), out);
103 fputs(_( " -u, --usages <list> filter by \"usage\" (e.g. -u filesystem,raid)\n"), out);
104 fputs(_( " -n, --match-types <list> filter by filesystem type (e.g. -n vfat,ext3)\n"), out);
105 fputs(_( " -D, --no-part-details don't print info from partition table\n"), out);
106
107 fputs(USAGE_SEPARATOR, out);
108 printf(USAGE_HELP_OPTIONS(28));
109 printf(USAGE_MAN_TAIL("blkid(8)"));
110 exit(EXIT_SUCCESS);
111 }
112
113 /*
114 * This function does "safe" printing. It will convert non-printable
115 * ASCII characters using '^' and M- notation.
116 *
117 * If 'esc' is defined then escape all chars from esc by \.
118 */
119 static void safe_print(const struct blkid_control *ctl, const char *cp, int len,
120 const char *esc)
121 {
122 unsigned char ch;
123
124 if (len < 0)
125 len = strlen(cp);
126
127 while (len--) {
128 ch = *cp++;
129 if (!ctl->raw_chars) {
130 if (ch >= 128) {
131 fputs("M-", stdout);
132 ch -= 128;
133 }
134 if ((ch < 32) || (ch == 0x7f)) {
135 fputc('^', stdout);
136 ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
137
138 } else if (esc && strchr(esc, ch))
139 fputc('\\', stdout);
140 }
141 fputc(ch, stdout);
142 }
143 }
144
145 static int pretty_print_word(const char *str, int max_len,
146 int left_len, int overflow_nl)
147 {
148 int len = strlen(str) + left_len;
149 int ret = 0;
150
151 fputs(str, stdout);
152 if (overflow_nl && len > max_len) {
153 fputc('\n', stdout);
154 len = 0;
155 } else if (len > max_len)
156 ret = len - max_len;
157 do {
158 fputc(' ', stdout);
159 } while (len++ < max_len);
160 return ret;
161 }
162
163 static void pretty_print_line(const char *device, const char *fs_type,
164 const char *label, const char *mtpt,
165 const char *uuid)
166 {
167 static int device_len = 10, fs_type_len = 7;
168 static int label_len = 8, mtpt_len = 14;
169 static int term_width = -1;
170 int len, w;
171
172 if (term_width < 0) {
173 term_width = get_terminal_width(80);
174 }
175 if (term_width > 80) {
176 term_width -= 80;
177 w = term_width / 10;
178 if (w > 8)
179 w = 8;
180 term_width -= 2*w;
181 label_len += w;
182 fs_type_len += w;
183 w = term_width/2;
184 device_len += w;
185 mtpt_len +=w;
186 }
187
188 len = pretty_print_word(device, device_len, 0, 1);
189 len = pretty_print_word(fs_type, fs_type_len, len, 0);
190 len = pretty_print_word(label, label_len, len, 0);
191 pretty_print_word(mtpt, mtpt_len, len, 0);
192
193 fputs(uuid, stdout);
194 fputc('\n', stdout);
195 }
196
197 static void pretty_print_dev(blkid_dev dev)
198 {
199 blkid_tag_iterate iter;
200 const char *type, *value, *devname;
201 const char *uuid = "", *fs_type = "", *label = "";
202 int len, mount_flags;
203 char mtpt[80];
204 int retval;
205
206 if (dev == NULL) {
207 pretty_print_line("device", "fs_type", "label",
208 "mount point", "UUID");
209 for (len=get_terminal_width(0)-1; len > 0; len--)
210 fputc('-', stdout);
211 fputc('\n', stdout);
212 return;
213 }
214
215 devname = blkid_dev_devname(dev);
216 if (access(devname, F_OK))
217 return;
218
219 /* Get the uuid, label, type */
220 iter = blkid_tag_iterate_begin(dev);
221 while (blkid_tag_next(iter, &type, &value) == 0) {
222 if (!strcmp(type, "UUID"))
223 uuid = value;
224 if (!strcmp(type, "TYPE"))
225 fs_type = value;
226 if (!strcmp(type, "LABEL"))
227 label = value;
228 }
229 blkid_tag_iterate_end(iter);
230
231 /* Get the mount point */
232 mtpt[0] = 0;
233 retval = check_mount_point(devname, &mount_flags, mtpt, sizeof(mtpt));
234 if (retval == 0) {
235 if (mount_flags & MF_MOUNTED) {
236 if (!mtpt[0])
237 strcpy(mtpt, _("(mounted, mtpt unknown)"));
238 } else if (mount_flags & MF_BUSY)
239 strcpy(mtpt, _("(in use)"));
240 else
241 strcpy(mtpt, _("(not mounted)"));
242 }
243
244 pretty_print_line(devname, fs_type, label, mtpt, uuid);
245 }
246
247 static void print_udev_format(const char *name, const char *value)
248 {
249 char enc[265], safe[256];
250 size_t namelen = strlen(name);
251
252 *safe = *enc = '\0';
253
254 if (!strcmp(name, "TYPE") || !strcmp(name, "VERSION")) {
255 blkid_encode_string(value, enc, sizeof(enc));
256 printf("ID_FS_%s=%s\n", name, enc);
257
258 } else if (!strcmp(name, "UUID") ||
259 !strncmp(name, "LABEL", 5) ||
260 !strcmp(name, "UUID_SUB")) {
261
262 blkid_safe_string(value, safe, sizeof(safe));
263 printf("ID_FS_%s=%s\n", name, safe);
264
265 blkid_encode_string(value, enc, sizeof(enc));
266 printf("ID_FS_%s_ENC=%s\n", name, enc);
267
268 } else if (!strcmp(name, "PTUUID")) {
269 printf("ID_PART_TABLE_UUID=%s\n", value);
270
271 } else if (!strcmp(name, "PTTYPE")) {
272 printf("ID_PART_TABLE_TYPE=%s\n", value);
273
274 } else if (!strcmp(name, "PART_ENTRY_NAME") ||
275 !strcmp(name, "PART_ENTRY_TYPE")) {
276
277 blkid_encode_string(value, enc, sizeof(enc));
278 printf("ID_%s=%s\n", name, enc);
279
280 } else if (!strncmp(name, "PART_ENTRY_", 11))
281 printf("ID_%s=%s\n", name, value);
282
283 else if (namelen >= 15 && (
284 !strcmp(name + (namelen - 12), "_SECTOR_SIZE") ||
285 !strcmp(name + (namelen - 8), "_IO_SIZE") ||
286 !strcmp(name, "ALIGNMENT_OFFSET")))
287 printf("ID_IOLIMIT_%s=%s\n", name, value);
288 else
289 printf("ID_FS_%s=%s\n", name, value);
290 }
291
292 static int has_item(const struct blkid_control *ctl, const char *item)
293 {
294 char * const *p;
295
296 for (p = ctl->show; *p != NULL; p++)
297 if (!strcmp(item, *p))
298 return 1;
299 return 0;
300 }
301
302 static void print_value(const struct blkid_control *ctl, int num,
303 const char *devname, const char *value,
304 const char *name, size_t valsz)
305 {
306 if (ctl->output & OUTPUT_VALUE_ONLY) {
307 fputs(value, stdout);
308 fputc('\n', stdout);
309
310 } else if (ctl->output & OUTPUT_UDEV_LIST) {
311 print_udev_format(name, value);
312
313 } else if (ctl->output & OUTPUT_EXPORT_LIST) {
314 if (num == 1 && devname)
315 printf("DEVNAME=%s\n", devname);
316 fputs(name, stdout);
317 fputs("=", stdout);
318 safe_print(ctl, value, valsz, " \\\"'$`<>");
319 fputs("\n", stdout);
320
321 } else {
322 if (num == 1 && devname)
323 printf("%s:", devname);
324 fputs(" ", stdout);
325 fputs(name, stdout);
326 fputs("=\"", stdout);
327 safe_print(ctl, value, valsz, "\"\\");
328 fputs("\"", stdout);
329 }
330 }
331
332 static void print_tags(const struct blkid_control *ctl, blkid_dev dev)
333 {
334 blkid_tag_iterate iter;
335 const char *type, *value, *devname;
336 int num = 1;
337 static int first = 1;
338
339 if (!dev)
340 return;
341
342 if (ctl->output & OUTPUT_PRETTY_LIST) {
343 pretty_print_dev(dev);
344 return;
345 }
346
347 devname = blkid_dev_devname(dev);
348
349 if (ctl->output & OUTPUT_DEVICE_ONLY) {
350 printf("%s\n", devname);
351 return;
352 }
353
354 iter = blkid_tag_iterate_begin(dev);
355 while (blkid_tag_next(iter, &type, &value) == 0) {
356 if (ctl->show[0] && !has_item(ctl, type))
357 continue;
358
359 if (num == 1 && !first &&
360 (ctl->output & (OUTPUT_UDEV_LIST | OUTPUT_EXPORT_LIST)))
361 /* add extra line between output from more devices */
362 fputc('\n', stdout);
363
364 print_value(ctl, num++, devname, value, type, strlen(value));
365 }
366 blkid_tag_iterate_end(iter);
367
368 if (num > 1) {
369 if (!(ctl->output & (OUTPUT_VALUE_ONLY | OUTPUT_UDEV_LIST |
370 OUTPUT_EXPORT_LIST)))
371 printf("\n");
372 first = 0;
373 }
374 }
375
376
377 static int append_str(char **res, size_t *sz, const char *a, const char *b)
378 {
379 char *str = *res;
380 size_t asz = a ? strlen(a) : 0;
381 size_t bsz = b ? strlen(b) : 0;
382 size_t len = *sz + asz + bsz;
383
384 if (!len)
385 return -1;
386
387 *res = str = xrealloc(str, len + 1);
388 str += *sz;
389
390 if (a) {
391 memcpy(str, a, asz);
392 str += asz;
393 }
394 if (b) {
395 memcpy(str, b, bsz);
396 str += bsz;
397 }
398 *str = '\0';
399 *sz = len;
400 return 0;
401 }
402
403 /*
404 * Compose and print ID_FS_AMBIVALENT for udev
405 */
406 static int print_udev_ambivalent(blkid_probe pr)
407 {
408 char *val = NULL;
409 size_t valsz = 0;
410 int count = 0, rc = -1;
411
412 while (!blkid_do_probe(pr)) {
413 const char *usage_txt = NULL, *type = NULL, *version = NULL;
414 char enc[256];
415
416 blkid_probe_lookup_value(pr, "USAGE", &usage_txt, NULL);
417 blkid_probe_lookup_value(pr, "TYPE", &type, NULL);
418 blkid_probe_lookup_value(pr, "VERSION", &version, NULL);
419
420 if (!usage_txt || !type)
421 continue;
422
423 blkid_encode_string(usage_txt, enc, sizeof(enc));
424 if (append_str(&val, &valsz, enc, ":"))
425 goto done;
426
427 blkid_encode_string(type, enc, sizeof(enc));
428 if (append_str(&val, &valsz, enc, version ? ":" : " "))
429 goto done;
430
431 if (version) {
432 blkid_encode_string(version, enc, sizeof(enc));
433 if (append_str(&val, &valsz, enc, " "))
434 goto done;
435 }
436 count++;
437 }
438
439 if (count > 1) {
440 *(val + valsz - 1) = '\0'; /* rem tailing whitespace */
441 printf("ID_FS_AMBIVALENT=%s\n", val);
442 rc = 0;
443 }
444 done:
445 free(val);
446 return rc;
447 }
448
449 static int lowprobe_superblocks(blkid_probe pr, struct blkid_control *ctl)
450 {
451 struct stat st;
452 int rc, fd = blkid_probe_get_fd(pr);
453
454 if (fd < 0 || fstat(fd, &st))
455 return -1;
456
457 blkid_probe_enable_partitions(pr, 1);
458
459 if (!S_ISCHR(st.st_mode) && blkid_probe_get_size(pr) <= 1024 * 1440 &&
460 blkid_probe_is_wholedisk(pr)) {
461 /*
462 * check if the small disk is partitioned, if yes then
463 * don't probe for filesystems.
464 */
465 blkid_probe_enable_superblocks(pr, 0);
466
467 rc = blkid_do_fullprobe(pr);
468 if (rc < 0)
469 return rc; /* -1 = error, 1 = nothing, 0 = success */
470
471 if (blkid_probe_lookup_value(pr, "PTTYPE", NULL, NULL) == 0)
472 return 0; /* partition table detected */
473 }
474
475 if (!ctl->no_part_details)
476 blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS);
477 blkid_probe_enable_superblocks(pr, 1);
478
479 return blkid_do_safeprobe(pr);
480 }
481
482 static int lowprobe_topology(blkid_probe pr)
483 {
484 /* enable topology probing only */
485 blkid_probe_enable_topology(pr, 1);
486
487 blkid_probe_enable_superblocks(pr, 0);
488 blkid_probe_enable_partitions(pr, 0);
489
490 return blkid_do_fullprobe(pr);
491 }
492
493 static int lowprobe_device(blkid_probe pr, const char *devname,
494 struct blkid_control *ctl)
495 {
496 const char *data;
497 const char *name;
498 int nvals = 0, n, num = 1;
499 size_t len;
500 int fd;
501 int rc = 0;
502 static int first = 1;
503
504 fd = open(devname, O_RDONLY|O_CLOEXEC);
505 if (fd < 0) {
506 warn(_("error: %s"), devname);
507 return BLKID_EXIT_NOTFOUND;
508 }
509 if (blkid_probe_set_device(pr, fd, ctl->offset, ctl->size))
510 goto done;
511
512 if (ctl->lowprobe_topology)
513 rc = lowprobe_topology(pr);
514 if (rc >= 0 && ctl->lowprobe_superblocks)
515 rc = lowprobe_superblocks(pr, ctl);
516 if (rc < 0)
517 goto done;
518
519 if (!rc)
520 nvals = blkid_probe_numof_values(pr);
521
522 if (nvals && !first && ctl->output & (OUTPUT_UDEV_LIST | OUTPUT_EXPORT_LIST))
523 /* add extra line between output from devices */
524 fputc('\n', stdout);
525
526 if (nvals && (ctl->output & OUTPUT_DEVICE_ONLY)) {
527 printf("%s\n", devname);
528 goto done;
529 }
530
531 for (n = 0; n < nvals; n++) {
532 if (blkid_probe_get_value(pr, n, &name, &data, &len))
533 continue;
534 if (ctl->show[0] && !has_item(ctl, name))
535 continue;
536 len = strnlen(data, len);
537 print_value(ctl, num++, devname, data, name, len);
538 }
539
540 if (first)
541 first = 0;
542
543 if (nvals >= 1 && !(ctl->output & (OUTPUT_VALUE_ONLY |
544 OUTPUT_UDEV_LIST | OUTPUT_EXPORT_LIST)))
545 printf("\n");
546 done:
547 if (rc == -2) {
548 if (ctl->output & OUTPUT_UDEV_LIST)
549 print_udev_ambivalent(pr);
550 else
551 warnx(_("%s: ambivalent result (probably more "
552 "filesystems on the device, use wipefs(8) "
553 "to see more details)"),
554 devname);
555 }
556 close(fd);
557
558 if (rc == -2)
559 return BLKID_EXIT_AMBIVAL; /* ambivalent probing result */
560 if (!nvals)
561 return BLKID_EXIT_NOTFOUND; /* nothing detected */
562
563 return 0; /* success */
564 }
565
566 /* converts comma separated list to BLKID_USAGE_* mask */
567 static int list_to_usage(const char *list, int *flag)
568 {
569 int mask = 0;
570 const char *word = NULL, *p = list;
571
572 if (p && strncmp(p, "no", 2) == 0) {
573 *flag = BLKID_FLTR_NOTIN;
574 p += 2;
575 }
576 if (!p || !*p)
577 goto err;
578 while(p) {
579 word = p;
580 p = strchr(p, ',');
581 if (p)
582 p++;
583 if (!strncmp(word, "filesystem", 10))
584 mask |= BLKID_USAGE_FILESYSTEM;
585 else if (!strncmp(word, "raid", 4))
586 mask |= BLKID_USAGE_RAID;
587 else if (!strncmp(word, "crypto", 6))
588 mask |= BLKID_USAGE_CRYPTO;
589 else if (!strncmp(word, "other", 5))
590 mask |= BLKID_USAGE_OTHER;
591 else
592 goto err;
593 }
594 return mask;
595 err:
596 *flag = 0;
597 warnx(_("unknown keyword in -u <list> argument: '%s'"),
598 word ? word : list);
599 exit(BLKID_EXIT_OTHER);
600 }
601
602 /* converts comma separated list to types[] */
603 static char **list_to_types(const char *list, int *flag)
604 {
605 int i;
606 const char *p = list;
607 char **res = NULL;
608
609 if (p && strncmp(p, "no", 2) == 0) {
610 *flag = BLKID_FLTR_NOTIN;
611 p += 2;
612 }
613 if (!p || !*p) {
614 warnx(_("error: -u <list> argument is empty"));
615 goto err;
616 }
617 for (i = 1; p && (p = strchr(p, ',')); i++, p++);
618
619 res = xcalloc(i + 1, sizeof(char *));
620 p = *flag & BLKID_FLTR_NOTIN ? list + 2 : list;
621 i = 0;
622
623 while(p) {
624 const char *word = p;
625 p = strchr(p, ',');
626 res[i++] = p ? xstrndup(word, p - word) : xstrdup(word);
627 if (p)
628 p++;
629 }
630 res[i] = NULL;
631 return res;
632 err:
633 *flag = 0;
634 free(res);
635 exit(BLKID_EXIT_OTHER);
636 }
637
638 static void free_types_list(char *list[])
639 {
640 char **n;
641
642 if (!list)
643 return;
644 for (n = list; *n; n++)
645 free(*n);
646 free(list);
647 }
648
649 int main(int argc, char **argv)
650 {
651 struct blkid_control ctl = { .output = OUTPUT_FULL, 0 };
652 blkid_cache cache = NULL;
653 char **devices = NULL;
654 char *search_type = NULL, *search_value = NULL;
655 char *read = NULL;
656 int fltr_usage = 0;
657 char **fltr_type = NULL;
658 int fltr_flag = BLKID_FLTR_ONLYIN;
659 unsigned int numdev = 0, numtag = 0;
660 int err = BLKID_EXIT_OTHER;
661 unsigned int i;
662 int c;
663
664 static const struct option longopts[] = {
665 { "cache-file", required_argument, NULL, 'c' },
666 { "no-encoding", no_argument, NULL, 'd' },
667 { "no-part-details", no_argument, NULL, 'D' },
668 { "garbage-collect", no_argument, NULL, 'g' },
669 { "output", required_argument, NULL, 'o' },
670 { "list-filesystems", no_argument, NULL, 'k' },
671 { "match-tag", required_argument, NULL, 's' },
672 { "match-token", required_argument, NULL, 't' },
673 { "list-one", no_argument, NULL, 'l' },
674 { "label", required_argument, NULL, 'L' },
675 { "uuid", required_argument, NULL, 'U' },
676 { "probe", no_argument, NULL, 'p' },
677 { "info", no_argument, NULL, 'i' },
678 { "size", required_argument, NULL, 'S' },
679 { "offset", required_argument, NULL, 'O' },
680 { "usages", required_argument, NULL, 'u' },
681 { "match-types", required_argument, NULL, 'n' },
682 { "version", no_argument, NULL, 'V' },
683 { "help", no_argument, NULL, 'h' },
684 { NULL, 0, NULL, 0 }
685 };
686
687 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
688 { 'n','u' },
689 { 0 }
690 };
691 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
692
693 setlocale(LC_ALL, "");
694 bindtextdomain(PACKAGE, LOCALEDIR);
695 textdomain(PACKAGE);
696 atexit(close_stdout);
697
698 strutils_set_exitcode(BLKID_EXIT_OTHER);
699
700 while ((c = getopt_long (argc, argv,
701 "c:DdghilL:n:ko:O:ps:S:t:u:U:w:Vv", longopts, NULL)) != -1) {
702
703 err_exclusive_options(c, NULL, excl, excl_st);
704
705 switch (c) {
706 case 'c':
707 read = optarg;
708 break;
709 case 'd':
710 ctl.raw_chars = 1;
711 break;
712 case 'D':
713 ctl.no_part_details = 1;
714 break;
715 case 'L':
716 ctl.eval = 1;
717 search_value = xstrdup(optarg);
718 search_type = xstrdup("LABEL");
719 break;
720 case 'n':
721 fltr_type = list_to_types(optarg, &fltr_flag);
722 break;
723 case 'u':
724 fltr_usage = list_to_usage(optarg, &fltr_flag);
725 break;
726 case 'U':
727 ctl.eval = 1;
728 search_value = xstrdup(optarg);
729 search_type = xstrdup("UUID");
730 break;
731 case 'i':
732 ctl.lowprobe_topology = 1;
733 break;
734 case 'l':
735 ctl.lookup = 1;
736 break;
737 case 'g':
738 ctl.gc = 1;
739 break;
740 case 'k':
741 {
742 size_t idx = 0;
743 const char *name = NULL;
744
745 while (blkid_superblocks_get_name(idx++, &name, NULL) == 0)
746 printf("%s\n", name);
747 exit(EXIT_SUCCESS);
748 }
749 case 'o':
750 if (!strcmp(optarg, "value"))
751 ctl.output = OUTPUT_VALUE_ONLY;
752 else if (!strcmp(optarg, "device"))
753 ctl.output = OUTPUT_DEVICE_ONLY;
754 else if (!strcmp(optarg, "list"))
755 ctl.output = OUTPUT_PRETTY_LIST; /* deprecated */
756 else if (!strcmp(optarg, "udev"))
757 ctl.output = OUTPUT_UDEV_LIST;
758 else if (!strcmp(optarg, "export"))
759 ctl.output = OUTPUT_EXPORT_LIST;
760 else if (!strcmp(optarg, "full"))
761 ctl.output = 0;
762 else
763 errx(BLKID_EXIT_OTHER, _("unsupported output format %s"), optarg);
764 break;
765 case 'O':
766 ctl.offset = strtosize_or_err(optarg, _("invalid offset argument"));
767 break;
768 case 'p':
769 ctl.lowprobe_superblocks = 1;
770 break;
771 case 's':
772 if (numtag + 1 >= sizeof(ctl.show) / sizeof(*ctl.show)) {
773 warnx(_("Too many tags specified"));
774 errtryhelp(err);
775 }
776 ctl.show[numtag++] = optarg;
777 break;
778 case 'S':
779 ctl.size = strtosize_or_err(optarg, _("invalid size argument"));
780 break;
781 case 't':
782 if (search_type) {
783 warnx(_("Can only search for "
784 "one NAME=value pair"));
785 errtryhelp(err);
786 }
787 if (blkid_parse_tag_string(optarg,
788 &search_type,
789 &search_value)) {
790 warnx(_("-t needs NAME=value pair"));
791 errtryhelp(err);
792 }
793 break;
794 case 'V':
795 case 'v':
796 print_version(stdout);
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 }