From 0e8a806546c4daa68a059c65b5a11a2d9dbaec0d Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 17 Sep 2009 17:01:19 +0200 Subject: libblkid: add blkid_partition_get_type_string() .. because for example Mac and GPT don't use numbers, but strings or UUIDs as partition type identifier. Signed-off-by: Karel Zak --- shlibs/blkid/src/partitions/gpt.c | 3 ++ shlibs/blkid/src/partitions/mac.c | 3 ++ shlibs/blkid/src/partitions/partitions.c | 75 +++++++++++++++++++++++++++----- shlibs/blkid/src/partitions/partitions.h | 6 +++ 4 files changed, 75 insertions(+), 12 deletions(-) (limited to 'shlibs/blkid/src/partitions') diff --git a/shlibs/blkid/src/partitions/gpt.c b/shlibs/blkid/src/partitions/gpt.c index b26dd6cd4..f2d54d741 100644 --- a/shlibs/blkid/src/partitions/gpt.c +++ b/shlibs/blkid/src/partitions/gpt.c @@ -324,6 +324,9 @@ static int probe_gpt_pt(blkid_probe pr, const struct blkid_idmag *mag) blkid_partition_set_uuid(par, (const unsigned char *) &e->unique_partition_guid); + + blkid_partition_set_type_uuid(par, + (const unsigned char *) &e->partition_type_guid); } return 0; diff --git a/shlibs/blkid/src/partitions/mac.c b/shlibs/blkid/src/partitions/mac.c index 2d43d38b3..c51b11aba 100644 --- a/shlibs/blkid/src/partitions/mac.c +++ b/shlibs/blkid/src/partitions/mac.c @@ -150,6 +150,9 @@ static int probe_mac_pt(blkid_probe pr, const struct blkid_idmag *mag) blkid_partition_set_name(par, (unsigned char *) p->name, sizeof(p->name)); + + blkid_partition_set_type_string(par, (unsigned char *) p->type, + sizeof(p->type)); } return 0; diff --git a/shlibs/blkid/src/partitions/partitions.c b/shlibs/blkid/src/partitions/partitions.c index b91e893cd..94d1556e9 100644 --- a/shlibs/blkid/src/partitions/partitions.c +++ b/shlibs/blkid/src/partitions/partitions.c @@ -152,7 +152,9 @@ struct blkid_struct_parttable { struct blkid_struct_partition { blkid_loff_t start; /* begin of the partition */ blkid_loff_t size; /* size of the partitions */ + int type; /* partition type */ + char typestr[37]; /* partition type string (GPT and Mac) */ int partno; /* partition number */ char uuid[37]; /* UUID (when supported by PT), e.g GPT */ @@ -850,26 +852,33 @@ int blkid_partition_is_logical(blkid_partition par) return partition_get_logical_type(par) == 'L' ? TRUE : FALSE; } -int blkid_partition_set_name(blkid_partition par, - const unsigned char *name, size_t len) +static void set_string(unsigned char *item, size_t max, + const unsigned char *data, size_t len) { int i; - if (!par) - return -1; - if (len >= sizeof(par->name)) - len = sizeof(par->name) - 1; + if (len >= max) + len = max - 1; - memcpy(par->name, name, len); - par->name[len] = '\0'; + memcpy(item, data, len); + item[len] = '\0'; /* remove trailing whitespace */ - i = strlen((char *) par->name); + i = strlen((char *) item); while (i--) { - if (!isspace(par->name[i])) + if (!isspace(item[i])) break; } - par->name[++i] = '\0'; + item[++i] = '\0'; +} + +int blkid_partition_set_name(blkid_partition par, + const unsigned char *name, size_t len) +{ + if (!par) + return -1; + + set_string(par->name, sizeof(par->name), name, len); return 0; } @@ -973,9 +982,51 @@ blkid_loff_t blkid_partition_get_size(blkid_partition par) * blkid_partition_get_type: * @par: partition * - * Returns: partition type (see BLKID_*_PARTITION in blkid_parttypes.h). + * Returns: partition type. */ int blkid_partition_get_type(blkid_partition par) { return par ? par->type : 0; } + +/* Sets partition 'type' for PT where the type is defined by string rather + * than by number + */ +int blkid_partition_set_type_string(blkid_partition par, + const unsigned char *type, size_t len) +{ + if (!par) + return -1; + + set_string((unsigned char *) par->typestr, + sizeof(par->typestr), type, len); + return 0; +} + +/* Sets partition 'type' for PT where the type is defined by UUIDrather + * than by number + */ +int blkid_partition_set_type_uuid(blkid_partition par, const unsigned char *uuid) +{ + if (!par) + return -1; + + blkid_unparse_uuid(uuid, par->typestr, sizeof(par->typestr)); + return 0; +} + +/** + * blkid_partition_get_type_string: + * @par: partition + * + * The type string is supported by a small subset of partition tables (e.g Mac + * and EFI GPT). Note that GPT uses type UUID and this function returns this + * UUID as string. + * + * Returns: partition type string or NULL. + */ +const char *blkid_partition_get_type_string(blkid_partition par) +{ + return par && *par->typestr ? par->typestr : NULL; +} + diff --git a/shlibs/blkid/src/partitions/partitions.h b/shlibs/blkid/src/partitions/partitions.h index e1678b350..e017768a1 100644 --- a/shlibs/blkid/src/partitions/partitions.h +++ b/shlibs/blkid/src/partitions/partitions.h @@ -32,6 +32,12 @@ extern int blkid_partition_set_utf8name(blkid_partition par, extern int blkid_partition_set_uuid(blkid_partition par, const unsigned char *uuid); +extern int blkid_partition_set_type_string(blkid_partition par, + const unsigned char *type, size_t len); + +extern int blkid_partition_set_type_uuid(blkid_partition par, + const unsigned char *uuid); + /* * partition probers */ -- cgit v1.2.3-55-g7522