libfdisk: add fdisk_copy_parttype()

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2014-09-10 14:31:08 +02:00
parent e881349460
commit 78049787a5
3 changed files with 45 additions and 19 deletions

View File

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

View File

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

View File

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