1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-27 18:55:40 +03:00

update blkid builtin

This commit is contained in:
Kay Sievers 2011-12-23 03:16:56 +01:00
parent e216e514cf
commit c38a141b1f
3 changed files with 72 additions and 38 deletions

View File

@ -25,52 +25,57 @@
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <sys/stat.h>
#include <blkid/blkid.h>
#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, "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, "PTTYPE")) {
printf("ID_PART_TABLE_TYPE=%s\n", value);
udev_builtin_add_property(dev, test, "ID_PART_TABLE_TYPE", value);
} else if (!strcmp(name, "PART_ENTRY_NAME") ||
!strcmp(name, "PART_ENTRY_TYPE")) {
} else if (!strcmp(name, "PART_ENTRY_NAME")) {
blkid_encode_string(value, s, sizeof(s));
udev_builtin_add_property(dev, test, "PART_ENTRY_NAME", s);
blkid_encode_string(value, enc, sizeof(enc));
printf("ID_%s=%s\n", name, enc);
} 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 (!strncmp(name, "PART_ENTRY_", 11))
printf("ID_%s=%s\n", name, value);
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);

View File

@ -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;

View File

@ -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