From c38a141b1f66ef48f8e4f71e26d1861de63bd151 Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Fri, 23 Dec 2011 03:16:56 +0100 Subject: [PATCH] update blkid builtin --- udev/udev-builtin-blkid.c | 105 +++++++++++++++++++++++++------------- udev/udev-builtin.c | 3 +- udev/udev.h | 2 +- 3 files changed, 72 insertions(+), 38 deletions(-) diff --git a/udev/udev-builtin-blkid.c b/udev/udev-builtin-blkid.c index 04ad6a444..3187ab0b0 100644 --- a/udev/udev-builtin-blkid.c +++ b/udev/udev-builtin-blkid.c @@ -25,52 +25,57 @@ #include #include #include +#include #include #include #include "udev.h" -static void print_property(const char *name, const char *value) +static void print_property(struct udev_device *dev, bool test, const char *name, const char *value) { - char enc[265], safe[256]; - size_t namelen = strlen(name); + char s[265]; - enc[0] = '\0'; - safe[0] = '\0'; + s[0] = '\0'; - if (!strcmp(name, "TYPE") || !strcmp(name, "VERSION")) { - blkid_encode_string(value, enc, sizeof(enc)); - printf("ID_FS_%s=%s\n", name, enc); + if (!strcmp(name, "TYPE")) { + udev_builtin_add_property(dev, test, "ID_FS_TYPE", value); - } else if (!strcmp(name, "UUID") || - !strcmp(name, "LABEL") || - !strcmp(name, "UUID_SUB")) { + } else if (!strcmp(name, "VERSION")) { + udev_builtin_add_property(dev, test, "ID_FS_VERSION", value); - blkid_safe_string(value, safe, sizeof(safe)); - printf("ID_FS_%s=%s\n", name, safe); + } else if (!strcmp(name, "UUID")) { + blkid_safe_string(value, s, sizeof(s)); + udev_builtin_add_property(dev, test, "ID_FS_UUID", s); + blkid_encode_string(value, s, sizeof(s)); + udev_builtin_add_property(dev, test, "ID_FS_UUID_ENC", s); - blkid_encode_string(value, enc, sizeof(enc)); - printf("ID_FS_%s_ENC=%s\n", name, enc); + } else if (!strcmp(name, "UUID_SUB")) { + blkid_safe_string(value, s, sizeof(s)); + udev_builtin_add_property(dev, test, "ID_FS_UUID_SUB", s); + blkid_encode_string(value, s, sizeof(s)); + udev_builtin_add_property(dev, test, "ID_FS_UUID_SUB_ENC", s); - } else if (!strcmp(name, "PTTYPE")) { - printf("ID_PART_TABLE_TYPE=%s\n", value); + } else if (!strcmp(name, "LABEL")) { + blkid_safe_string(value, s, sizeof(s)); + udev_builtin_add_property(dev, test, "ID_FS_LABEL", s); + blkid_encode_string(value, s, sizeof(s)); + udev_builtin_add_property(dev, test, "ID_FS_LABEL_ENC", s); - } else if (!strcmp(name, "PART_ENTRY_NAME") || - !strcmp(name, "PART_ENTRY_TYPE")) { + } else if (!strcmp(name, "PTTYPE")) { + udev_builtin_add_property(dev, test, "ID_PART_TABLE_TYPE", value); - blkid_encode_string(value, enc, sizeof(enc)); - printf("ID_%s=%s\n", name, enc); + } else if (!strcmp(name, "PART_ENTRY_NAME")) { + blkid_encode_string(value, s, sizeof(s)); + udev_builtin_add_property(dev, test, "PART_ENTRY_NAME", s); - } else if (!strncmp(name, "PART_ENTRY_", 11)) - printf("ID_%s=%s\n", name, value); + } else if (!strcmp(name, "PART_ENTRY_TYPE")) { + blkid_encode_string(value, s, sizeof(s)); + udev_builtin_add_property(dev, test, "PART_ENTRY_TYPE", s); - else if (namelen >= 15 && ( - !strcmp(name + (namelen - 12), "_SECTOR_SIZE") || - !strcmp(name + (namelen - 8), "_IO_SIZE") || - !strcmp(name, "ALIGNMENT_OFFSET"))) - printf("ID_IOLIMIT_%s=%s\n", name, value); - else - printf("ID_FS_%s=%s\n", name, value); + } else if (!strncmp(name, "PART_ENTRY_", 11)) { + util_strscpyl(s, sizeof(s), "ID_", name, NULL); + udev_builtin_add_property(dev, test, name, value); + } } static int probe_superblocks(blkid_probe pr) @@ -107,9 +112,9 @@ static int probe_superblocks(blkid_probe pr) static int builtin_blkid(struct udev_device *dev, int argc, char *argv[], bool test) { - char *device = "/dev/sda3"; + struct udev *udev = udev_device_get_udev(dev); int64_t offset = 0; - //int noraid = 0; + bool noraid = false; int fd = -1; blkid_probe pr; const char *data; @@ -119,7 +124,28 @@ static int builtin_blkid(struct udev_device *dev, int argc, char *argv[], bool t size_t len; int err = 0; - //FIXME: read offset, read noraid + static const struct option options[] = { + { "offset", optional_argument, NULL, 'o' }, + { "noraid", no_argument, NULL, 'R' }, + {} + }; + + for (;;) { + int option; + + option = getopt_long(argc, argv, "oR", options, NULL); + if (option == -1) + break; + + switch (option) { + case 'o': + offset = strtoull(optarg, NULL, 0); + break; + case 'R': + noraid = true; + break; + } + } pr = blkid_new_probe(); if (!pr) { @@ -132,9 +158,12 @@ static int builtin_blkid(struct udev_device *dev, int argc, char *argv[], bool t BLKID_SUBLKS_TYPE | BLKID_SUBLKS_SECTYPE | BLKID_SUBLKS_USAGE | BLKID_SUBLKS_VERSION); - fd = open(device, O_RDONLY|O_CLOEXEC); + if (noraid) + blkid_probe_filter_superblocks_usage(pr, BLKID_FLTR_NOTIN, BLKID_USAGE_RAID); + + fd = open(udev_device_get_devnode(dev), O_RDONLY|O_CLOEXEC); if (fd < 0) { - fprintf(stderr, "error: %s: %m\n", device); + fprintf(stderr, "error: %s: %m\n", udev_device_get_devnode(dev)); goto out; } @@ -142,6 +171,10 @@ static int builtin_blkid(struct udev_device *dev, int argc, char *argv[], bool t if (err < 0) goto out; + info(udev, "probe %s %sraid offset=%llu", + udev_device_get_devnode(dev), + noraid ? "no" : "", (unsigned long long) offset); + err = probe_superblocks(pr); if (err < 0) goto out; @@ -151,7 +184,7 @@ static int builtin_blkid(struct udev_device *dev, int argc, char *argv[], bool t if (blkid_probe_get_value(pr, i, &name, &data, &len)) continue; len = strnlen((char *) data, len); - print_property(name, (char *) data); + print_property(dev, test, name, (char *) data); } blkid_free_probe(pr); diff --git a/udev/udev-builtin.c b/udev/udev-builtin.c index f17b04f67..6dfd00671 100644 --- a/udev/udev-builtin.c +++ b/udev/udev-builtin.c @@ -94,12 +94,13 @@ int udev_builtin_run(struct udev_device *dev, enum udev_builtin_cmd cmd, const c int argc; char *argv[128]; + optind = 0; util_strscpy(arg, sizeof(arg), command); udev_build_argv(udev_device_get_udev(dev), arg, &argc, argv); return builtins[cmd]->cmd(dev, argc, argv, test); } -int udev_builtin_add_property(struct udev_device *dev, bool test, const char *key, const char *val, ...) +int udev_builtin_add_property(struct udev_device *dev, bool test, const char *key, const char *val) { struct udev_list_entry *entry; diff --git a/udev/udev.h b/udev/udev.h index d5445e84e..aafa15cd3 100644 --- a/udev/udev.h +++ b/udev/udev.h @@ -175,5 +175,5 @@ const char *udev_builtin_name(enum udev_builtin_cmd cmd); bool udev_builtin_run_once(enum udev_builtin_cmd cmd); int udev_builtin_run(struct udev_device *dev, enum udev_builtin_cmd cmd, const char *command, bool test); int udev_builtin_list(struct udev *udev); -int udev_builtin_add_property(struct udev_device *dev, bool test, const char *key, const char *val, ...); +int udev_builtin_add_property(struct udev_device *dev, bool test, const char *key, const char *val); #endif -- 2.30.2