From 0e8a806546c4daa68a059c65b5a11a2d9dbaec0d Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 17 Sep 2009 17:01:19 +0200 Subject: [PATCH] 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/docs/libblkid-sections.txt | 1 + shlibs/blkid/samples/partitions.c | 3 + shlibs/blkid/src/blkid.h | 1 + shlibs/blkid/src/blkid.sym | 1 + 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 ++ tests/expected/blkid/lowprobe-pt-gpt | 10 ++-- 9 files changed, 86 insertions(+), 17 deletions(-) diff --git a/shlibs/blkid/docs/libblkid-sections.txt b/shlibs/blkid/docs/libblkid-sections.txt index 4d98b160..af94607e 100644 --- a/shlibs/blkid/docs/libblkid-sections.txt +++ b/shlibs/blkid/docs/libblkid-sections.txt @@ -74,6 +74,7 @@ blkid_partition_get_size blkid_partition_get_start blkid_partition_get_table blkid_partition_get_type +blkid_partition_get_type_string blkid_partition_get_uuid blkid_partition_is_extended blkid_partition_is_logical diff --git a/shlibs/blkid/samples/partitions.c b/shlibs/blkid/samples/partitions.c index 78bcc0be..7e2648ab 100644 --- a/shlibs/blkid/samples/partitions.c +++ b/shlibs/blkid/samples/partitions.c @@ -83,6 +83,9 @@ int main(int argc, char *argv[]) p = blkid_partition_get_uuid(par); if (p) printf(" uuid='%s'", p); + p = blkid_partition_get_type_string(par); + if (p) + printf(" type='%s'", p); putc('\n', stdout); } diff --git a/shlibs/blkid/src/blkid.h b/shlibs/blkid/src/blkid.h index d0eb4b6a..057e3130 100644 --- a/shlibs/blkid/src/blkid.h +++ b/shlibs/blkid/src/blkid.h @@ -268,6 +268,7 @@ extern int blkid_partition_get_partno(blkid_partition par); extern blkid_loff_t blkid_partition_get_start(blkid_partition par); extern blkid_loff_t blkid_partition_get_size(blkid_partition par); extern int blkid_partition_get_type(blkid_partition par); +extern const char *blkid_partition_get_type_string(blkid_partition par); extern int blkid_partition_is_logical(blkid_partition par); extern int blkid_partition_is_extended(blkid_partition par); extern int blkid_partition_is_primary(blkid_partition par); diff --git a/shlibs/blkid/src/blkid.sym b/shlibs/blkid/src/blkid.sym index 89baf483..8ac2081a 100644 --- a/shlibs/blkid/src/blkid.sym +++ b/shlibs/blkid/src/blkid.sym @@ -80,6 +80,7 @@ global: blkid_partition_get_start; blkid_partition_get_table; blkid_partition_get_type; + blkid_partition_get_type_string; blkid_partition_get_uuid; blkid_partition_is_extended; blkid_partition_is_logical; diff --git a/shlibs/blkid/src/partitions/gpt.c b/shlibs/blkid/src/partitions/gpt.c index b26dd6cd..f2d54d74 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 2d43d38b..c51b11ab 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 b91e893c..94d1556e 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 e1678b35..e017768a 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 */ diff --git a/tests/expected/blkid/lowprobe-pt-gpt b/tests/expected/blkid/lowprobe-pt-gpt index e9be134e..8588f83e 100644 --- a/tests/expected/blkid/lowprobe-pt-gpt +++ b/tests/expected/blkid/lowprobe-pt-gpt @@ -1,7 +1,7 @@ size: 10485760, sector size: 512, PT: gpt, offset: 512 --- -#1: 34 2013 0x0 name='ThisIsName' uuid='bc10cf1d-7e63-524c-8203-087ae10a820b' -#2: 2048 2047 0x0 name='ThisIsOtherName' uuid='963ad0a1-3872-c646-bbb3-789cbe173ec7' -#3: 4096 2047 0x0 name='primary' uuid='6c1b10a7-8c46-df47-aff6-cd444d12af61' -#4: 6144 2047 0x0 name='primary' uuid='0a95c4af-f1f0-dd4a-802c-5957133486d1' -#5: 8192 2047 0x0 name='primary' uuid='87a7b00d-6bc1-8648-af3a-fbb97299677c' +#1: 34 2013 0x0 name='ThisIsName' uuid='bc10cf1d-7e63-524c-8203-087ae10a820b' type='a2a0d0eb-e5b9-3344-87c0-68b6b72699c7' +#2: 2048 2047 0x0 name='ThisIsOtherName' uuid='963ad0a1-3872-c646-bbb3-789cbe173ec7' type='a2a0d0eb-e5b9-3344-87c0-68b6b72699c7' +#3: 4096 2047 0x0 name='primary' uuid='6c1b10a7-8c46-df47-aff6-cd444d12af61' type='a2a0d0eb-e5b9-3344-87c0-68b6b72699c7' +#4: 6144 2047 0x0 name='primary' uuid='0a95c4af-f1f0-dd4a-802c-5957133486d1' type='a2a0d0eb-e5b9-3344-87c0-68b6b72699c7' +#5: 8192 2047 0x0 name='primary' uuid='87a7b00d-6bc1-8648-af3a-fbb97299677c' type='a2a0d0eb-e5b9-3344-87c0-68b6b72699c7' -- 2.39.5