From 78049787a5872e530f7975db656343ccafeb9eaf Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 10 Sep 2014 14:31:08 +0200 Subject: [PATCH] libfdisk: add fdisk_copy_parttype() Signed-off-by: Karel Zak --- libfdisk/src/libfdisk.h | 6 +++-- libfdisk/src/partition.c | 5 ++-- libfdisk/src/parttype.c | 53 ++++++++++++++++++++++++++++------------ 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/libfdisk/src/libfdisk.h b/libfdisk/src/libfdisk.h index 9f9f28faf..31f0eee32 100644 --- a/libfdisk/src/libfdisk.h +++ b/libfdisk/src/libfdisk.h @@ -148,7 +148,9 @@ struct fdisk_parttype *fdisk_new_unknown_parttype(unsigned int code, struct fdisk_parttype *fdisk_label_parse_parttype( struct fdisk_label *lb, const char *str); -void fdisk_free_parttype(struct fdisk_parttype *t); + +struct fdisk_parttype *fdisk_copy_parttype(const struct fdisk_parttype *type); +void fdisk_free_parttype(struct fdisk_parttype *t); /* TODO: use refcount */ const char *fdisk_parttype_get_string(const struct fdisk_parttype *t); unsigned int fdisk_parttype_get_code(const struct fdisk_parttype *t); @@ -247,7 +249,7 @@ extern size_t fdisk_partition_get_partno(struct fdisk_partition *pa); extern int fdisk_partition_cmp_partno(struct fdisk_partition *a, struct fdisk_partition *b); -extern int fdisk_partition_set_type(struct fdisk_partition *pa, struct fdisk_parttype *type); +extern int fdisk_partition_set_type(struct fdisk_partition *pa, const struct fdisk_parttype *type); extern const struct fdisk_parttype *fdisk_partition_get_type(struct fdisk_partition *pa); extern int fdisk_partition_set_name(struct fdisk_partition *pa, const char *name); extern const char *fdisk_partition_get_name(struct fdisk_partition *pa); diff --git a/libfdisk/src/partition.c b/libfdisk/src/partition.c index 717eea711..4e57c74b8 100644 --- a/libfdisk/src/partition.c +++ b/libfdisk/src/partition.c @@ -207,12 +207,13 @@ int fdisk_partition_cmp_partno(struct fdisk_partition *a, return a->partno - b->partno; } -int fdisk_partition_set_type(struct fdisk_partition *pa, struct fdisk_parttype *type) +int fdisk_partition_set_type(struct fdisk_partition *pa, + const struct fdisk_parttype *type) { if (!pa) return -EINVAL; fdisk_free_parttype(pa->type); - pa->type = type; + pa->type = fdisk_copy_parttype(type); return 0; } diff --git a/libfdisk/src/parttype.c b/libfdisk/src/parttype.c index bb25cc6dd..21baedcd1 100644 --- a/libfdisk/src/parttype.c +++ b/libfdisk/src/parttype.c @@ -105,6 +105,29 @@ struct fdisk_parttype *fdisk_label_get_parttype_from_string( return NULL; } +static struct fdisk_parttype *new_parttype(unsigned int code, + const char *typestr, + const char *name) +{ + struct fdisk_parttype *t= calloc(1, sizeof(*t)); + + if (!t) + return NULL; + if (typestr) { + t->typestr = strdup(typestr); + if (!t->typestr) { + free(t); + return NULL; + } + } + t->name = name; + t->code = code; + t->flags |= FDISK_PARTTYPE_ALLOCATED; + + DBG(PARTTYPE, ul_debugobj(t, "allocated new %s type", name)); + return t; +} + /** * fdisk_new_unknown_parttype: * @code: type as number @@ -118,27 +141,27 @@ struct fdisk_parttype *fdisk_label_get_parttype_from_string( struct fdisk_parttype *fdisk_new_unknown_parttype(unsigned int code, const char *typestr) { - struct fdisk_parttype *t; + struct fdisk_parttype *t = new_parttype(code, typestr, _("unknown")); - t = calloc(1, sizeof(*t)); if (!t) return NULL; - - if (typestr) { - t->typestr = strdup(typestr); - if (!t->typestr) { - free(t); - return NULL; - } - } - t->name = _("unknown"); - t->code = code; - t->flags |= FDISK_PARTTYPE_UNKNOWN | FDISK_PARTTYPE_ALLOCATED; - - DBG(PARTTYPE, ul_debugobj(t, "allocated new unknown type")); + t->flags |= FDISK_PARTTYPE_UNKNOWN; return t; } +/** + * fdisk_copy_parttype: + * @type: type to copy + * + * Use fdisk_free_parttype() to deallocate. + * + * Returns: newly allocated partition type, or NULL upon failure. + */ +struct fdisk_parttype *fdisk_copy_parttype(const struct fdisk_parttype *type) +{ + return new_parttype(type->code, type->typestr, type->name); +} + /** * fdisk_label_parse_parttype: * @lb: label