From 5b6215b5ba2d643179f2d91dd74c3346f7701329 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 14 Jan 2009 00:22:22 +0100 Subject: [PATCH] blkid: add -p and low-probe mode to blkid binary Signed-off-by: Karel Zak --- libs/blkid/TODO | 4 +- libs/blkid/bin/Makefile.am | 2 +- libs/blkid/bin/blkid.c | 149 +++++++++++++++++++++++++++++-------- libs/blkid/src/cache.c | 3 +- libs/blkid/src/probe.c | 13 ++-- 5 files changed, 128 insertions(+), 43 deletions(-) diff --git a/libs/blkid/TODO b/libs/blkid/TODO index ad7c8f94..14af7987 100644 --- a/libs/blkid/TODO +++ b/libs/blkid/TODO @@ -1,13 +1,11 @@ - - add blkid --low-probe - - add hex-encoding from libudev - do we really need to depend on libuuid? Maybe the default should be use in-tree code rather and link against libuuid -- especially when we need it for uuid_unparse() only. - - add blkid -o | --format=full|value|udev + - add "blkid -f udev" (vol_id compatible output format) - consolidate "getsize" stuff (see getsize.c and lib/blkdev.c) diff --git a/libs/blkid/bin/Makefile.am b/libs/blkid/bin/Makefile.am index bcb6da4a..358ed17d 100644 --- a/libs/blkid/bin/Makefile.am +++ b/libs/blkid/bin/Makefile.am @@ -1,6 +1,6 @@ include $(top_srcdir)/config/include-Makefile.am -AM_LDFLAGS += -L$(top_builddir)/libs/bkid/src -lblkid +AM_LDFLAGS += -L$(top_builddir)/libs/blkid/src -lblkid AM_CPPFLAGS += -I$(top_builddir)/libs/blkid/src/ sbin_PROGRAMS = blkid diff --git a/libs/blkid/bin/blkid.c b/libs/blkid/bin/blkid.c index 51d3b348..e52c8539 100644 --- a/libs/blkid/bin/blkid.c +++ b/libs/blkid/bin/blkid.c @@ -13,6 +13,9 @@ #include #include #include +#include +#include +#include #ifdef HAVE_TERMIOS_H #include #endif @@ -49,18 +52,23 @@ static void usage(int error) print_version(out); fprintf(out, - "usage:\t%s [-c ] [-ghlLv] [-o format] " - "[-s ] [-t ]\n [-w ] [dev ...]\n" - "\t-c\tcache file (default: /etc/blkid.tab, /dev/null = none)\n" - "\t-h\tprint this usage message and exit\n" - "\t-g\tgarbage collect the blkid cache\n" - "\t-s\tshow specified tag(s) (default show all tags)\n" - "\t-t\tfind device with a specific token (NAME=value pair)\n" - "\t-l\tlookup the the first device with arguments specified by -t\n" - "\t-v\tprint version and exit\n" - "\t-w\twrite cache to different file (/dev/null = no write)\n" - "\tdev\tspecify device(s) to probe (default: all devices)\n", - progname); + "Usage:\n" + " %1$s [-c ] [-ghlLv] [-o format] [-s ] \n" + " [-t ] [-w ] [dev ...]\n\n" + " %1$s -p [-O ] [-S ] [dev ...]\n\n" + "Options:\n" + " -c cache file (default: /etc/blkid.tab, /dev/null = none)\n" + " -h print this usage message and exit\n" + " -g garbage collect the blkid cache\n" + " -p switch to low-probe mode (bypass cache)\n" + " -s show specified tag(s) (default show all tags)\n" + " -t find device with a specific token (NAME=value pair)\n" + " -l lookup the the first device with arguments specified by -t\n" + " -v print version and exit\n" + " -w write cache to different file (/dev/null = no write)\n" + " specify device(s) to probe (default: all devices)\n\n", + progname); + exit(error); } @@ -220,11 +228,33 @@ static void pretty_print_dev(blkid_dev dev) #endif } +static void print_value(int output, int num, blkid_dev dev, + const char *value, const char *name, size_t valsz) +{ + if (output & OUTPUT_VALUE_ONLY) { + fputs(value, stdout); + fputc('\n', stdout); + + /* TODO: print vol_id compatible output + * } else if (output && OUTPUT_UDEV) { + * + */ + + } else { + if (num == 1 && dev) + printf("%s: ", blkid_dev_devname(dev)); + fputs(name, stdout); + fputs("=\"", stdout); + safe_print(value, valsz); + fputs("\" ", stdout); + } +} + static void print_tags(blkid_dev dev, char *show[], int numtag, int output) { blkid_tag_iterate iter; const char *type, *value; - int i, first = 1; + int i, num = 1; if (!dev) return; @@ -248,26 +278,50 @@ static void print_tags(blkid_dev dev, char *show[], int numtag, int output) if (i >= numtag) continue; } - if (output & OUTPUT_VALUE_ONLY) { - fputs(value, stdout); - fputc('\n', stdout); - } else { - if (first) { - printf("%s: ", blkid_dev_devname(dev)); - first = 0; - } - fputs(type, stdout); - fputs("=\"", stdout); - safe_print(value, -1); - fputs("\" ", stdout); - } + print_value(output, num++, dev, value, type, strlen(value)); } blkid_tag_iterate_end(iter); - if (!first && !(output & OUTPUT_VALUE_ONLY)) + if (num > 1 && !(output & OUTPUT_VALUE_ONLY)) printf("\n"); } +static int lowprobe_device(blkid_probe pr, const char *devname, int output, + blkid_loff_t offset, blkid_loff_t size) +{ + unsigned char *data; + const char *name; + int nvals, n; + size_t len; + int fd; + + fd = open(devname, O_RDONLY); + if (fd < 0) { + perror(devname); + return -1; + } + + if (blkid_probe_set_device(pr, fd, offset, size)) + goto error; + if (blkid_do_probe(pr)) + goto error; + + nvals = blkid_probe_numof_values(pr); + + for (n = 0; n < nvals; n++) { + if (blkid_probe_get_value(pr, n, &name, &data, &len)) + continue; + + len = strnlen((char *) data, len); + print_value(output, n + 1, NULL, (char *) data, name, len); + } + + return 0; +error: + close(fd); + return -1; +} + int main(int argc, char **argv) { blkid_cache cache = NULL; @@ -281,10 +335,11 @@ int main(int argc, char **argv) int err = 4; unsigned int i; int output_format = 0; - int lookup = 0, gc = 0; + int lookup = 0, gc = 0, lowprobe = 0; int c; + blkid_loff_t offset = 0, size = 0; - while ((c = getopt (argc, argv, "c:f:ghlLo:s:t:w:v")) != EOF) + while ((c = getopt (argc, argv, "c:f:ghlLo:O:ps:S:t:w:v")) != EOF) switch (c) { case 'c': if (optarg && !*optarg) @@ -319,6 +374,12 @@ int main(int argc, char **argv) exit(1); } break; + case 'O': + offset = strtoll(optarg, NULL, 10); + break; + case 'p': + lowprobe++; + break; case 's': if (numtag >= sizeof(show) / sizeof(*show)) { fprintf(stderr, "Too many tags specified\n"); @@ -326,6 +387,9 @@ int main(int argc, char **argv) } show[numtag++] = optarg; break; + case 'S': + size = strtoll(optarg, NULL, 10); + break; case 't': if (search_type) { fprintf(stderr, "Can only search for " @@ -362,7 +426,7 @@ int main(int argc, char **argv) goto exit; } - if (blkid_get_cache(&cache, read) < 0) + if (!lowprobe && blkid_get_cache(&cache, read) < 0) goto exit; err = 2; @@ -373,7 +437,27 @@ int main(int argc, char **argv) if (output_format & OUTPUT_PRETTY_LIST) pretty_print_dev(NULL); - if (lookup) { + if (lowprobe) { + blkid_probe pr; + + if (!numdev) { + fprintf(stderr, "The low-probe option requires a device\n"); + exit(1); + } + pr = blkid_new_probe(); + if (!pr) + goto exit; + blkid_probe_set_request(pr, + BLKID_PROBREQ_LABEL | BLKID_PROBREQ_LABELRAW | + BLKID_PROBREQ_UUID | BLKID_PROBREQ_UUIDRAW | + BLKID_PROBREQ_TYPE | BLKID_PROBREQ_SECTYPE | + BLKID_PROBREQ_USAGE | BLKID_PROBREQ_VERSION); + + for (i = 0; i < numdev; i++) + err += lowprobe_device(pr, devices[i], + output_format, offset, size); + blkid_free_probe(pr); + } else if (lookup) { blkid_dev dev; if (!search_type) { @@ -427,6 +511,7 @@ exit: free(search_type); if (search_value) free(search_value); - blkid_put_cache(cache); + if (!lowprobe) + blkid_put_cache(cache); return err; } diff --git a/libs/blkid/src/cache.c b/libs/blkid/src/cache.c index 0769027b..228b3155 100644 --- a/libs/blkid/src/cache.c +++ b/libs/blkid/src/cache.c @@ -90,7 +90,8 @@ void blkid_debug_init(int mask) } else blkid_debug_mask = mask; - printf("libblkid: debug mask set to 0x%04x.\n", blkid_debug_mask); + if (blkid_debug_mask) + printf("libblkid: debug mask set to 0x%04x.\n", blkid_debug_mask); blkid_debug_mask |= DEBUG_INIT; } diff --git a/libs/blkid/src/probe.c b/libs/blkid/src/probe.c index 764d82fd..5a4fad74 100644 --- a/libs/blkid/src/probe.c +++ b/libs/blkid/src/probe.c @@ -447,7 +447,7 @@ int blkid_do_probe(blkid_probe pr) id = idinfos[i]; mag = id->magics ? &id->magics[0] : NULL; - DBG(DEBUG_LOWPROBE, printf("%s ", id->name)); + DBG(DEBUG_LOWPROBE, printf("\n %s", id->name)); /* try to detect by magic string */ while(mag && mag->magic) { @@ -460,7 +460,7 @@ int blkid_do_probe(blkid_probe pr) if (buf && !memcmp(mag->magic, buf + (mag->sboff & 0x3ff), mag->len)) { DBG(DEBUG_LOWPROBE, printf( - "{ detected magic string sboff=%d, kboff=%d } ", + "\n --> magic at sboff=%u, kboff=%ld;", mag->sboff, mag->kboff)); break; } @@ -474,7 +474,7 @@ int blkid_do_probe(blkid_probe pr) /* final check by probing function */ if (id->probefunc) { DBG(DEBUG_LOWPROBE, printf( - "{ calling probing function } \n")); + "\n --> calling probing function")); if (id->probefunc(pr, mag) != 0) continue; } @@ -487,10 +487,11 @@ int blkid_do_probe(blkid_probe pr) if (pr->probreq & BLKID_PROBREQ_USAGE) blkid_probe_set_usage(pr, id->usage); - DBG(DEBUG_LOWPROBE, printf("*** leaving probing loop (success)\n")); + DBG(DEBUG_LOWPROBE, + printf("\n*** leaving probing loop (type=%s)\n", id->name)); return 0; } - DBG(DEBUG_LOWPROBE, printf("*** leaving probing loop (failed)\n")); + DBG(DEBUG_LOWPROBE, printf("\n*** leaving probing loop (failed)\n")); return 1; } @@ -516,7 +517,7 @@ static struct blkid_prval *blkid_probe_assign_value( v->name = name; pr->nvals++; - DBG(DEBUG_LOWPROBE, printf("assigning %s value\n", name)); + DBG(DEBUG_LOWPROBE, printf("assigning %s\n", name)); return v; } -- 2.39.5