]> git.ipfire.org Git - thirdparty/util-linux.git/blame - misc-utils/blkid.c
libblkid: check for medium on CDMROMs probing
[thirdparty/util-linux.git] / misc-utils / blkid.c
CommitLineData
51410fc6
KZ
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>
5b6215b5
KZ
16#include <sys/types.h>
17#include <sys/stat.h>
18#include <fcntl.h>
4fc5d65b 19#include <errno.h>
51410fc6 20#include <getopt.h>
51410fc6 21
59b94cc0 22#define OUTPUT_FULL (1 << 0)
ab978962
KZ
23#define OUTPUT_VALUE_ONLY (1 << 1)
24#define OUTPUT_DEVICE_ONLY (1 << 2)
24d741d8
KZ
25#define OUTPUT_PRETTY_LIST (1 << 3) /* deprecated */
26#define OUTPUT_UDEV_LIST (1 << 4) /* deprecated */
ab978962 27#define OUTPUT_EXPORT_LIST (1 << 5)
51410fc6 28
1578ddd6
PU
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
51410fc6
KZ
33#include <blkid.h>
34
2d71a929 35#include "ismounted.h"
3c2662d5 36
8abcf290 37#include "strutils.h"
72b99fc5
SK
38#define OPTUTILS_EXIT_CODE BLKID_EXIT_OTHER /* exclusive_option() */
39#include "optutils.h"
090d8c76 40#define CLOSE_EXIT_CODE BLKID_EXIT_OTHER /* close_stdout() */
c05a80ca 41#include "closestream.h"
090d8c76 42
f5a2b07e 43#include "nls.h"
597cfec5 44#include "ttyutils.h"
778ca2a0
RM
45
46#define XALLOC_EXIT_CODE BLKID_EXIT_OTHER /* x.*alloc(), xstrndup() */
fe20e5d1 47#include "xalloc.h"
2d71a929 48
59b94cc0
SK
49struct 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,
5e91d5dd 61 no_part_details:1,
59b94cc0
SK
62 raw_chars:1;
63};
36a74e1b 64
fa2cd89a 65static void __attribute__((__noreturn__)) usage(void)
51410fc6 66{
fa2cd89a 67 FILE *out = stdout;
51410fc6 68
f5a2b07e 69 fputs(USAGE_HEADER, out);
15a74f75
SK
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);
f5a2b07e 76 fputs(USAGE_OPTIONS, out);
15a74f75
SK
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);
f5a2b07e
SK
90 fputs( "\n", out);
91 fputs(_( "Low-level probing options:\n"), out);
15a74f75
SK
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);
0ab08179 98 fputs(_( " -D, --no-part-details don't print info from partition table\n"), out);
15a74f75
SK
99
100 fputs(USAGE_SEPARATOR, out);
f45f3ec3
RM
101 printf(USAGE_HELP_OPTIONS(28));
102 printf(USAGE_MAN_TAIL("blkid(8)"));
fa2cd89a 103 exit(EXIT_SUCCESS);
51410fc6
KZ
104}
105
106/*
107 * This function does "safe" printing. It will convert non-printable
108 * ASCII characters using '^' and M- notation.
1c9885cd
KZ
109 *
110 * If 'esc' is defined then escape all chars from esc by \.
51410fc6 111 */
59b94cc0
SK
112static void safe_print(const struct blkid_control *ctl, const char *cp, int len,
113 const char *esc)
51410fc6
KZ
114{
115 unsigned char ch;
116
117 if (len < 0)
118 len = strlen(cp);
119
120 while (len--) {
121 ch = *cp++;
59b94cc0 122 if (!ctl->raw_chars) {
305a6122 123 if (ch >= 128) {
36a74e1b
KZ
124 fputs("M-", stdout);
125 ch -= 128;
126 }
127 if ((ch < 32) || (ch == 0x7f)) {
128 fputc('^', stdout);
129 ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
1c9885cd
KZ
130
131 } else if (esc && strchr(esc, ch))
132 fputc('\\', stdout);
51410fc6
KZ
133 }
134 fputc(ch, stdout);
135 }
136}
137
51410fc6
KZ
138static 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;
126d8cf3 150 do {
51410fc6 151 fputc(' ', stdout);
126d8cf3 152 } while (len++ < max_len);
51410fc6
KZ
153 return ret;
154}
155
156static 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
7e9a9af1 165 if (term_width < 0) {
43b4f7ea 166 term_width = get_terminal_width(80);
7e9a9af1 167 }
51410fc6
KZ
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);
fec1339f
KZ
184 pretty_print_word(mtpt, mtpt_len, len, 0);
185
51410fc6
KZ
186 fputs(uuid, stdout);
187 fputc('\n', stdout);
188}
189
190static void pretty_print_dev(blkid_dev dev)
191{
51410fc6
KZ
192 blkid_tag_iterate iter;
193 const char *type, *value, *devname;
194 const char *uuid = "", *fs_type = "", *label = "";
51410fc6
KZ
195 int len, mount_flags;
196 char mtpt[80];
2d71a929 197 int retval;
51410fc6
KZ
198
199 if (dev == NULL) {
200 pretty_print_line("device", "fs_type", "label",
201 "mount point", "UUID");
43b4f7ea 202 for (len=get_terminal_width(0)-1; len > 0; len--)
51410fc6
KZ
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;
2d71a929 226 retval = check_mount_point(devname, &mount_flags, mtpt, sizeof(mtpt));
51410fc6 227 if (retval == 0) {
fa1b64da
KZ
228 const char *msg = NULL;
229
2d71a929 230 if (mount_flags & MF_MOUNTED) {
51410fc6 231 if (!mtpt[0])
fa1b64da 232 msg = _("(mounted, mtpt unknown)");
2d71a929 233 } else if (mount_flags & MF_BUSY)
fa1b64da 234 msg = _("(in use)");
51410fc6 235 else
fa1b64da
KZ
236 msg = _("(not mounted)");
237
238 if (msg)
239 xstrncpy(mtpt, msg, sizeof(mtpt));
51410fc6
KZ
240 }
241
242 pretty_print_line(devname, fs_type, label, mtpt, uuid);
51410fc6
KZ
243}
244
525f3c28 245static void print_udev_format(const char *name, const char *value)
b516dbe0
KZ
246{
247 char enc[265], safe[256];
09792910 248 size_t namelen = strlen(name);
b516dbe0
KZ
249
250 *safe = *enc = '\0';
251
d46c8ab3 252 if (!strcmp(name, "TYPE") || !strcmp(name, "VERSION")) {
b516dbe0 253 blkid_encode_string(value, enc, sizeof(enc));
d46c8ab3 254 printf("ID_FS_%s=%s\n", name, enc);
b516dbe0 255
d46c8ab3 256 } else if (!strcmp(name, "UUID") ||
0d2e8ff9 257 !strncmp(name, "LABEL", 5) ||
b516dbe0
KZ
258 !strcmp(name, "UUID_SUB")) {
259
260 blkid_safe_string(value, safe, sizeof(safe));
d46c8ab3 261 printf("ID_FS_%s=%s\n", name, safe);
b516dbe0 262
d46c8ab3 263 blkid_encode_string(value, enc, sizeof(enc));
b516dbe0 264 printf("ID_FS_%s_ENC=%s\n", name, enc);
e3d8933a 265
834eca4e
KZ
266 } else if (!strcmp(name, "PTUUID")) {
267 printf("ID_PART_TABLE_UUID=%s\n", value);
268
4f74ffdb 269 } else if (!strcmp(name, "PTTYPE")) {
e3d8933a
KZ
270 printf("ID_PART_TABLE_TYPE=%s\n", value);
271
dcc58f62
KZ
272 } else if (!strcmp(name, "PART_ENTRY_NAME") ||
273 !strcmp(name, "PART_ENTRY_TYPE")) {
4f74ffdb 274
4f74ffdb 275 blkid_encode_string(value, enc, sizeof(enc));
0159857e 276 printf("ID_%s=%s\n", name, enc);
4f74ffdb
KZ
277
278 } else if (!strncmp(name, "PART_ENTRY_", 11))
279 printf("ID_%s=%s\n", name, value);
e3d8933a 280
09792910
KZ
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);
d46c8ab3
KZ
286 else
287 printf("ID_FS_%s=%s\n", name, value);
b516dbe0
KZ
288}
289
071147ab 290static int has_item(const struct blkid_control *ctl, const char *item)
7711e215 291{
071147ab 292 char * const *p;
7711e215 293
071147ab 294 for (p = ctl->show; *p != NULL; p++)
7711e215
KZ
295 if (!strcmp(item, *p))
296 return 1;
297 return 0;
298}
299
59b94cc0
SK
300static void print_value(const struct blkid_control *ctl, int num,
301 const char *devname, const char *value,
302 const char *name, size_t valsz)
5b6215b5 303{
59b94cc0 304 if (ctl->output & OUTPUT_VALUE_ONLY) {
5b6215b5
KZ
305 fputs(value, stdout);
306 fputc('\n', stdout);
307
59b94cc0 308 } else if (ctl->output & OUTPUT_UDEV_LIST) {
525f3c28 309 print_udev_format(name, value);
5b6215b5 310
59b94cc0 311 } else if (ctl->output & OUTPUT_EXPORT_LIST) {
dab33573
KZ
312 if (num == 1 && devname)
313 printf("DEVNAME=%s\n", devname);
ab978962
KZ
314 fputs(name, stdout);
315 fputs("=", stdout);
59b94cc0 316 safe_print(ctl, value, valsz, " \\\"'$`<>");
ab978962
KZ
317 fputs("\n", stdout);
318
5b6215b5 319 } else {
cf778d68 320 if (num == 1 && devname)
16714abe
KZ
321 printf("%s:", devname);
322 fputs(" ", stdout);
5b6215b5
KZ
323 fputs(name, stdout);
324 fputs("=\"", stdout);
59b94cc0 325 safe_print(ctl, value, valsz, "\"\\");
16714abe 326 fputs("\"", stdout);
5b6215b5
KZ
327 }
328}
329
59b94cc0 330static void print_tags(const struct blkid_control *ctl, blkid_dev dev)
51410fc6
KZ
331{
332 blkid_tag_iterate iter;
cf778d68 333 const char *type, *value, *devname;
7711e215 334 int num = 1;
ab978962 335 static int first = 1;
51410fc6
KZ
336
337 if (!dev)
338 return;
339
59b94cc0 340 if (ctl->output & OUTPUT_PRETTY_LIST) {
51410fc6
KZ
341 pretty_print_dev(dev);
342 return;
343 }
344
cf778d68
KZ
345 devname = blkid_dev_devname(dev);
346
59b94cc0 347 if (ctl->output & OUTPUT_DEVICE_ONLY) {
cf778d68 348 printf("%s\n", devname);
51410fc6
KZ
349 return;
350 }
351
352 iter = blkid_tag_iterate_begin(dev);
353 while (blkid_tag_next(iter, &type, &value) == 0) {
071147ab 354 if (ctl->show[0] && !has_item(ctl, type))
7711e215 355 continue;
ab978962
KZ
356
357 if (num == 1 && !first &&
59b94cc0 358 (ctl->output & (OUTPUT_UDEV_LIST | OUTPUT_EXPORT_LIST)))
ab978962
KZ
359 /* add extra line between output from more devices */
360 fputc('\n', stdout);
361
59b94cc0 362 print_value(ctl, num++, devname, value, type, strlen(value));
51410fc6
KZ
363 }
364 blkid_tag_iterate_end(iter);
365
ab978962 366 if (num > 1) {
59b94cc0 367 if (!(ctl->output & (OUTPUT_VALUE_ONLY | OUTPUT_UDEV_LIST |
ab978962
KZ
368 OUTPUT_EXPORT_LIST)))
369 printf("\n");
370 first = 0;
371 }
51410fc6
KZ
372}
373
c54670d5
KZ
374
375static int append_str(char **res, size_t *sz, const char *a, const char *b)
376{
293eef71 377 char *str = *res;
c54670d5
KZ
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
293eef71 385 *res = str = xrealloc(str, len + 1);
c54670d5
KZ
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 */
404static 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)) {
f5ee44b1 411 const char *usage_txt = NULL, *type = NULL, *version = NULL;
c54670d5
KZ
412 char enc[256];
413
f5ee44b1 414 blkid_probe_lookup_value(pr, "USAGE", &usage_txt, NULL);
c54670d5
KZ
415 blkid_probe_lookup_value(pr, "TYPE", &type, NULL);
416 blkid_probe_lookup_value(pr, "VERSION", &version, NULL);
417
f5ee44b1 418 if (!usage_txt || !type)
c54670d5
KZ
419 continue;
420
f5ee44b1 421 blkid_encode_string(usage_txt, enc, sizeof(enc));
c54670d5
KZ
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 */
08af448f 439 printf("ID_FS_AMBIVALENT=%s\n", val);
c54670d5
KZ
440 rc = 0;
441 }
442done:
443 free(val);
444 return rc;
445}
446
5e91d5dd 447static int lowprobe_superblocks(blkid_probe pr, struct blkid_control *ctl)
5b6215b5 448{
e3d8933a 449 struct stat st;
d75bcac4 450 int rc, fd = blkid_probe_get_fd(pr);
e3d8933a 451
d75bcac4 452 if (fd < 0 || fstat(fd, &st))
09792910 453 return -1;
e3d8933a
KZ
454
455 blkid_probe_enable_partitions(pr, 1);
4f74ffdb 456
6a2f2f58
KZ
457 if (!S_ISCHR(st.st_mode) && blkid_probe_get_size(pr) <= 1024 * 1440 &&
458 blkid_probe_is_wholedisk(pr)) {
e3d8933a 459 /*
cba1f975
KZ
460 * check if the small disk is partitioned, if yes then
461 * don't probe for filesystems.
e3d8933a 462 */
cba1f975 463 blkid_probe_enable_superblocks(pr, 0);
e3d8933a 464
cba1f975 465 rc = blkid_do_fullprobe(pr);
e3d8933a 466 if (rc < 0)
9e930041 467 return rc; /* -1 = error, 1 = nothing, 0 = success */
cba1f975
KZ
468
469 if (blkid_probe_lookup_value(pr, "PTTYPE", NULL, NULL) == 0)
09792910 470 return 0; /* partition table detected */
e3d8933a 471 }
5b6215b5 472
5e91d5dd
KZ
473 if (!ctl->no_part_details)
474 blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS);
cba1f975
KZ
475 blkid_probe_enable_superblocks(pr, 1);
476
09792910
KZ
477 return blkid_do_safeprobe(pr);
478}
479
480static 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
491static int lowprobe_device(blkid_probe pr, const char *devname,
59b94cc0 492 struct blkid_control *ctl)
09792910
KZ
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
39f5af25 502 fd = open(devname, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
09792910 503 if (fd < 0) {
f5a2b07e 504 warn(_("error: %s"), devname);
1578ddd6 505 return BLKID_EXIT_NOTFOUND;
09792910 506 }
dc30fd43
KZ
507 errno = 0;
508 if (blkid_probe_set_device(pr, fd, ctl->offset, ctl->size)) {
509 if (errno)
510 warn(_("error: %s"), devname);
09792910 511 goto done;
dc30fd43 512 }
09792910 513
59b94cc0 514 if (ctl->lowprobe_topology)
09792910 515 rc = lowprobe_topology(pr);
59b94cc0 516 if (rc >= 0 && ctl->lowprobe_superblocks)
5e91d5dd 517 rc = lowprobe_superblocks(pr, ctl);
cba1f975
KZ
518 if (rc < 0)
519 goto done;
09792910 520
dbcafb87
KZ
521 if (!rc)
522 nvals = blkid_probe_numof_values(pr);
5b6215b5 523
59b94cc0 524 if (nvals && !first && ctl->output & (OUTPUT_UDEV_LIST | OUTPUT_EXPORT_LIST))
9eb44c28
KZ
525 /* add extra line between output from devices */
526 fputc('\n', stdout);
527
59b94cc0 528 if (nvals && (ctl->output & OUTPUT_DEVICE_ONLY)) {
e88517b1
KZ
529 printf("%s\n", devname);
530 goto done;
531 }
532
5b6215b5
KZ
533 for (n = 0; n < nvals; n++) {
534 if (blkid_probe_get_value(pr, n, &name, &data, &len))
535 continue;
071147ab 536 if (ctl->show[0] && !has_item(ctl, name))
7711e215 537 continue;
e736434b
KZ
538 len = strnlen(data, len);
539 print_value(ctl, num++, devname, data, name, len);
5b6215b5
KZ
540 }
541
9eb44c28
KZ
542 if (first)
543 first = 0;
59b94cc0
SK
544
545 if (nvals >= 1 && !(ctl->output & (OUTPUT_VALUE_ONLY |
ab978962 546 OUTPUT_UDEV_LIST | OUTPUT_EXPORT_LIST)))
cf778d68 547 printf("\n");
e88517b1 548done:
c54670d5 549 if (rc == -2) {
59b94cc0 550 if (ctl->output & OUTPUT_UDEV_LIST)
c54670d5
KZ
551 print_udev_ambivalent(pr);
552 else
f5a2b07e 553 warnx(_("%s: ambivalent result (probably more "
c54670d5 554 "filesystems on the device, use wipefs(8) "
f5a2b07e 555 "to see more details)"),
a2f01a1c 556 devname);
c54670d5 557 }
5b6215b5 558 close(fd);
68c88d94
KZ
559
560 if (rc == -2)
1578ddd6 561 return BLKID_EXIT_AMBIVAL; /* ambivalent probing result */
68c88d94 562 if (!nvals)
1578ddd6 563 return BLKID_EXIT_NOTFOUND; /* nothing detected */
68c88d94 564
455fe9a0 565 return 0; /* success */
5b6215b5
KZ
566}
567
0bdd2f97
KZ
568/* converts comma separated list to BLKID_USAGE_* mask */
569static int list_to_usage(const char *list, int *flag)
570{
571 int mask = 0;
962496d4 572 const char *word = NULL, *p = list;
0bdd2f97
KZ
573
574 if (p && strncmp(p, "no", 2) == 0) {
575 *flag = BLKID_FLTR_NOTIN;
576 p += 2;
577 }
962496d4
KZ
578 if (!p || !*p)
579 goto err;
962496d4
KZ
580 while(p) {
581 word = p;
962496d4
KZ
582 p = strchr(p, ',');
583 if (p)
584 p++;
962496d4
KZ
585 if (!strncmp(word, "filesystem", 10))
586 mask |= BLKID_USAGE_FILESYSTEM;
587 else if (!strncmp(word, "raid", 4))
588 mask |= BLKID_USAGE_RAID;
589 else if (!strncmp(word, "crypto", 6))
590 mask |= BLKID_USAGE_CRYPTO;
591 else if (!strncmp(word, "other", 5))
592 mask |= BLKID_USAGE_OTHER;
593 else
594 goto err;
0bdd2f97
KZ
595 }
596 return mask;
962496d4
KZ
597err:
598 *flag = 0;
f5a2b07e 599 warnx(_("unknown keyword in -u <list> argument: '%s'"),
962496d4 600 word ? word : list);
1578ddd6 601 exit(BLKID_EXIT_OTHER);
0bdd2f97
KZ
602}
603
4f946f53
KZ
604/* converts comma separated list to types[] */
605static char **list_to_types(const char *list, int *flag)
606{
607 int i;
608 const char *p = list;
bc867ea3 609 char **res = NULL;
4f946f53
KZ
610
611 if (p && strncmp(p, "no", 2) == 0) {
612 *flag = BLKID_FLTR_NOTIN;
613 p += 2;
614 }
615 if (!p || !*p) {
f5a2b07e 616 warnx(_("error: -u <list> argument is empty"));
4f946f53
KZ
617 goto err;
618 }
619 for (i = 1; p && (p = strchr(p, ',')); i++, p++);
620
5d00280c 621 res = xcalloc(i + 1, sizeof(char *));
4f946f53
KZ
622 p = *flag & BLKID_FLTR_NOTIN ? list + 2 : list;
623 i = 0;
624
625 while(p) {
626 const char *word = p;
627 p = strchr(p, ',');
5d00280c 628 res[i++] = p ? xstrndup(word, p - word) : xstrdup(word);
4f946f53
KZ
629 if (p)
630 p++;
631 }
632 res[i] = NULL;
633 return res;
4f946f53
KZ
634err:
635 *flag = 0;
636 free(res);
1578ddd6 637 exit(BLKID_EXIT_OTHER);
4f946f53
KZ
638}
639
640static void free_types_list(char *list[])
641{
642 char **n;
643
644 if (!list)
645 return;
646 for (n = list; *n; n++)
647 free(*n);
648 free(list);
649}
650
51410fc6
KZ
651int main(int argc, char **argv)
652{
59b94cc0 653 struct blkid_control ctl = { .output = OUTPUT_FULL, 0 };
51410fc6 654 blkid_cache cache = NULL;
ea51c09c 655 char **devices = NULL;
51410fc6
KZ
656 char *search_type = NULL, *search_value = NULL;
657 char *read = NULL;
0bdd2f97 658 int fltr_usage = 0;
4f946f53 659 char **fltr_type = NULL;
0bdd2f97 660 int fltr_flag = BLKID_FLTR_ONLYIN;
51410fc6 661 unsigned int numdev = 0, numtag = 0;
1578ddd6 662 int err = BLKID_EXIT_OTHER;
51410fc6 663 unsigned int i;
51410fc6
KZ
664 int c;
665
15a74f75
SK
666 static const struct option longopts[] = {
667 { "cache-file", required_argument, NULL, 'c' },
668 { "no-encoding", no_argument, NULL, 'd' },
5e91d5dd 669 { "no-part-details", no_argument, NULL, 'D' },
15a74f75
SK
670 { "garbage-collect", no_argument, NULL, 'g' },
671 { "output", required_argument, NULL, 'o' },
672 { "list-filesystems", no_argument, NULL, 'k' },
673 { "match-tag", required_argument, NULL, 's' },
674 { "match-token", required_argument, NULL, 't' },
675 { "list-one", no_argument, NULL, 'l' },
676 { "label", required_argument, NULL, 'L' },
677 { "uuid", required_argument, NULL, 'U' },
678 { "probe", no_argument, NULL, 'p' },
679 { "info", no_argument, NULL, 'i' },
680 { "size", required_argument, NULL, 'S' },
681 { "offset", required_argument, NULL, 'O' },
682 { "usages", required_argument, NULL, 'u' },
683 { "match-types", required_argument, NULL, 'n' },
684 { "version", no_argument, NULL, 'V' },
685 { "help", no_argument, NULL, 'h' },
686 { NULL, 0, NULL, 0 }
687 };
688
a7349ee3 689 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
a8e458b9
KZ
690 { 'n','u' },
691 { 0 }
72b99fc5 692 };
a8e458b9 693 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
72b99fc5 694
f5a2b07e
SK
695 setlocale(LC_ALL, "");
696 bindtextdomain(PACKAGE, LOCALEDIR);
697 textdomain(PACKAGE);
2c308875 698 close_stdout_atexit();
7711e215 699
9c8b9fba
RM
700 strutils_set_exitcode(BLKID_EXIT_OTHER);
701
15a74f75 702 while ((c = getopt_long (argc, argv,
5e91d5dd 703 "c:DdghilL:n:ko:O:ps:S:t:u:U:w:Vv", longopts, NULL)) != -1) {
a8e458b9
KZ
704
705 err_exclusive_options(c, NULL, excl, excl_st);
706
51410fc6
KZ
707 switch (c) {
708 case 'c':
59b94cc0 709 read = optarg;
51410fc6 710 break;
36a74e1b 711 case 'd':
59b94cc0 712 ctl.raw_chars = 1;
36a74e1b 713 break;
5e91d5dd
KZ
714 case 'D':
715 ctl.no_part_details = 1;
716 break;
4786921c 717 case 'L':
59b94cc0 718 ctl.eval = 1;
fe20e5d1
SK
719 search_value = xstrdup(optarg);
720 search_type = xstrdup("LABEL");
4786921c 721 break;
4f946f53 722 case 'n':
4f946f53
KZ
723 fltr_type = list_to_types(optarg, &fltr_flag);
724 break;
0bdd2f97
KZ
725 case 'u':
726 fltr_usage = list_to_usage(optarg, &fltr_flag);
727 break;
4786921c 728 case 'U':
59b94cc0 729 ctl.eval = 1;
fe20e5d1
SK
730 search_value = xstrdup(optarg);
731 search_type = xstrdup("UUID");
4786921c 732 break;
09792910 733 case 'i':
59b94cc0 734 ctl.lowprobe_topology = 1;
09792910 735 break;
51410fc6 736 case 'l':
59b94cc0 737 ctl.lookup = 1;
51410fc6 738 break;
51410fc6 739 case 'g':
59b94cc0 740 ctl.gc = 1;
51410fc6 741 break;
70db6c7e
KZ
742 case 'k':
743 {
744 size_t idx = 0;
745 const char *name = NULL;
746
747 while (blkid_superblocks_get_name(idx++, &name, NULL) == 0)
748 printf("%s\n", name);
a922091d 749 exit(EXIT_SUCCESS);
70db6c7e 750 }
51410fc6
KZ
751 case 'o':
752 if (!strcmp(optarg, "value"))
59b94cc0 753 ctl.output = OUTPUT_VALUE_ONLY;
51410fc6 754 else if (!strcmp(optarg, "device"))
59b94cc0 755 ctl.output = OUTPUT_DEVICE_ONLY;
51410fc6 756 else if (!strcmp(optarg, "list"))
59b94cc0 757 ctl.output = OUTPUT_PRETTY_LIST; /* deprecated */
b516dbe0 758 else if (!strcmp(optarg, "udev"))
59b94cc0 759 ctl.output = OUTPUT_UDEV_LIST;
ab978962 760 else if (!strcmp(optarg, "export"))
59b94cc0 761 ctl.output = OUTPUT_EXPORT_LIST;
51410fc6 762 else if (!strcmp(optarg, "full"))
59b94cc0 763 ctl.output = 0;
f5a2b07e
SK
764 else
765 errx(BLKID_EXIT_OTHER, _("unsupported output format %s"), optarg);
51410fc6 766 break;
5b6215b5 767 case 'O':
59b94cc0 768 ctl.offset = strtosize_or_err(optarg, _("invalid offset argument"));
5b6215b5
KZ
769 break;
770 case 'p':
59b94cc0 771 ctl.lowprobe_superblocks = 1;
5b6215b5 772 break;
51410fc6 773 case 's':
59b94cc0 774 if (numtag + 1 >= sizeof(ctl.show) / sizeof(*ctl.show)) {
f5a2b07e 775 warnx(_("Too many tags specified"));
3b7693ca 776 errtryhelp(err);
51410fc6 777 }
59b94cc0 778 ctl.show[numtag++] = optarg;
51410fc6 779 break;
5b6215b5 780 case 'S':
59b94cc0 781 ctl.size = strtosize_or_err(optarg, _("invalid size argument"));
5b6215b5 782 break;
51410fc6
KZ
783 case 't':
784 if (search_type) {
f5a2b07e
SK
785 warnx(_("Can only search for "
786 "one NAME=value pair"));
3b7693ca 787 errtryhelp(err);
51410fc6
KZ
788 }
789 if (blkid_parse_tag_string(optarg,
790 &search_type,
791 &search_value)) {
f5a2b07e 792 warnx(_("-t needs NAME=value pair"));
3b7693ca 793 errtryhelp(err);
51410fc6
KZ
794 }
795 break;
a9b2c52f 796 case 'V':
51410fc6 797 case 'v':
68224d10
KZ
798 fprintf(stdout, _("%s from %s (libblkid %s, %s)\n"),
799 program_invocation_short_name, PACKAGE_STRING,
800 LIBBLKID_VERSION, LIBBLKID_DATE);
dc46610b 801 err = 0;
93bdbe19 802 goto exit;
51410fc6 803 case 'w':
077b15dc 804 /* ignore - backward compatibility */
51410fc6
KZ
805 break;
806 case 'h':
fa2cd89a 807 usage();
677ec86c 808 break;
51410fc6 809 default:
3b7693ca 810 errtryhelp(EXIT_FAILURE);
51410fc6 811 }
a8e458b9 812 }
51410fc6 813
59b94cc0
SK
814 if (ctl.lowprobe_topology || ctl.lowprobe_superblocks)
815 ctl.lowprobe = 1;
ea51c09c
ES
816
817 /* The rest of the args are device names */
818 if (optind < argc) {
5d00280c 819 devices = xcalloc(argc - optind, sizeof(char *));
54a00b5c
KZ
820 while (optind < argc)
821 devices[numdev++] = argv[optind++];
822 }
51410fc6 823
4786921c 824 /* convert LABEL/UUID lookup to evaluate request */
59b94cc0 825 if (ctl.lookup && ctl.output == OUTPUT_DEVICE_ONLY && search_type &&
4786921c 826 (!strcmp(search_type, "LABEL") || !strcmp(search_type, "UUID"))) {
59b94cc0
SK
827 ctl.eval = 1;
828 ctl.lookup = 0;
4786921c 829 }
830
59b94cc0 831 if (!ctl.lowprobe && !ctl.eval && blkid_get_cache(&cache, read) < 0)
51410fc6
KZ
832 goto exit;
833
59b94cc0 834 if (ctl.gc) {
51410fc6 835 blkid_gc_cache(cache);
324759d6 836 err = 0;
51410fc6
KZ
837 goto exit;
838 }
1578ddd6 839 err = BLKID_EXIT_NOTFOUND;
324759d6 840
59b94cc0
SK
841 if (ctl.eval == 0 && (ctl.output & OUTPUT_PRETTY_LIST)) {
842 if (ctl.lowprobe)
f5a2b07e
SK
843 errx(BLKID_EXIT_OTHER,
844 _("The low-level probing mode does not "
845 "support 'list' output format"));
51410fc6 846 pretty_print_dev(NULL);
2d71a929 847 }
51410fc6 848
59b94cc0 849 if (ctl.lowprobe) {
4786921c 850 /*
851 * Low-level API
852 */
5b6215b5
KZ
853 blkid_probe pr;
854
f5a2b07e
SK
855 if (!numdev)
856 errx(BLKID_EXIT_OTHER,
857 _("The low-level probing mode "
858 "requires a device"));
ab978962
KZ
859
860 /* automatically enable 'export' format for I/O Limits */
59b94cc0
SK
861 if (!ctl.output && ctl.lowprobe_topology)
862 ctl.output = OUTPUT_EXPORT_LIST;
ab978962 863
5b6215b5
KZ
864 pr = blkid_new_probe();
865 if (!pr)
866 goto exit;
1b780848 867
59b94cc0 868 if (ctl.lowprobe_superblocks) {
09792910 869 blkid_probe_set_superblocks_flags(pr,
1b780848
KZ
870 BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID |
871 BLKID_SUBLKS_TYPE | BLKID_SUBLKS_SECTYPE |
872 BLKID_SUBLKS_USAGE | BLKID_SUBLKS_VERSION);
873
59b94cc0
SK
874
875 if (fltr_usage &&
876 blkid_probe_filter_superblocks_usage(pr, fltr_flag, fltr_usage))
09792910
KZ
877 goto exit;
878
59b94cc0
SK
879 else if (fltr_type &&
880 blkid_probe_filter_superblocks_type(pr, fltr_flag, fltr_type))
09792910
KZ
881 goto exit;
882 }
5b6215b5 883
078ad9bf 884 for (i = 0; i < numdev; i++) {
59b94cc0 885 err = lowprobe_device(pr, devices[i], &ctl);
078ad9bf
PU
886 if (err)
887 break;
888 }
5b6215b5 889 blkid_free_probe(pr);
59b94cc0 890 } else if (ctl.eval) {
4786921c 891 /*
892 * Evaluate API
893 */
cee95a95 894 char *res = blkid_evaluate_tag(search_type, search_value, NULL);
324759d6
SJR
895 if (res) {
896 err = 0;
4786921c 897 printf("%s\n", res);
324759d6 898 }
59b94cc0 899 } else if (ctl.lookup) {
4786921c 900 /*
901 * Classic (cache based) API
902 */
51410fc6
KZ
903 blkid_dev dev;
904
f5a2b07e
SK
905 if (!search_type)
906 errx(BLKID_EXIT_OTHER,
907 _("The lookup option requires a "
908 "search type specified using -t"));
51410fc6
KZ
909 /* Load any additional devices not in the cache */
910 for (i = 0; i < numdev; i++)
911 blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL);
912
913 if ((dev = blkid_find_dev_with_tag(cache, search_type,
914 search_value))) {
59b94cc0 915 print_tags(&ctl, dev);
51410fc6
KZ
916 err = 0;
917 }
918 /* If we didn't specify a single device, show all available devices */
919 } else if (!numdev) {
920 blkid_dev_iterate iter;
921 blkid_dev dev;
922
923 blkid_probe_all(cache);
924
925 iter = blkid_dev_iterate_begin(cache);
926 blkid_dev_set_search(iter, search_type, search_value);
927 while (blkid_dev_next(iter, &dev) == 0) {
928 dev = blkid_verify(cache, dev);
929 if (!dev)
930 continue;
59b94cc0 931 print_tags(&ctl, dev);
51410fc6
KZ
932 err = 0;
933 }
934 blkid_dev_iterate_end(iter);
935 /* Add all specified devices to cache (optionally display tags) */
936 } else for (i = 0; i < numdev; i++) {
937 blkid_dev dev = blkid_get_dev(cache, devices[i],
938 BLKID_DEV_NORMAL);
939
940 if (dev) {
941 if (search_type &&
942 !blkid_dev_has_tag(dev, search_type,
943 search_value))
944 continue;
59b94cc0 945 print_tags(&ctl, dev);
51410fc6
KZ
946 err = 0;
947 }
948 }
949
950exit:
4786921c 951 free(search_type);
952 free(search_value);
4f946f53 953 free_types_list(fltr_type);
59b94cc0 954 if (!ctl.lowprobe && !ctl.eval)
5b6215b5 955 blkid_put_cache(cache);
ea51c09c 956 free(devices);
51410fc6
KZ
957 return err;
958}