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