]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: add fdisk_copy_parttype()
authorKarel Zak <kzak@redhat.com>
Wed, 10 Sep 2014 12:31:08 +0000 (14:31 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 7 Oct 2014 12:55:31 +0000 (14:55 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/src/libfdisk.h
libfdisk/src/partition.c
libfdisk/src/parttype.c

index 9f9f28faf9d53e3995bd880396562e9c2c77ae7e..31f0eee32de97eac35ee4758f8822e160ac660ec 100644 (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);
index 717eea7115db16638876c890a258c55ba7358c18..4e57c74b8ae5c3bc5d1202fa2f70e4c8dc6cbb95 100644 (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;
 }
 
index bb25cc6dd41512a6b9b310a3b944ee09c12f426e..21baedcd198a3da638ea20e9eb4bcb078221d4bb 100644 (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