]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: add parttype code
authorKarel Zak <kzak@redhat.com>
Fri, 30 Nov 2012 15:39:57 +0000 (16:39 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 11 Mar 2013 10:20:40 +0000 (11:20 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
fdisks/fdisk.h
fdisks/utils.c
libfdisk/src/Makemodule.am
libfdisk/src/libfdisk.h
libfdisk/src/parttype.c [new file with mode: 0644]

index efc9969b688dde155cdfab2afae38949059cabb8..82313c91e83feea231da0e8b27f247d6ddd47219 100644 (file)
@@ -95,16 +95,6 @@ extern struct fdisk_parttype *fdisk_get_partition_type(struct fdisk_context *cxt
 extern int fdisk_set_partition_type(struct fdisk_context *cxt, int partnum,
                             struct fdisk_parttype *t);
 
-extern size_t fdisk_get_nparttypes(struct fdisk_context *cxt);
-extern struct fdisk_parttype *fdisk_get_parttype_from_code(struct fdisk_context *cxt,
-                                unsigned int code);
-extern struct fdisk_parttype *fdisk_get_parttype_from_string(struct fdisk_context *cxt,
-                                const char *str);
-extern struct fdisk_parttype *fdisk_parse_parttype(struct fdisk_context *cxt, const char *str);
-
-extern struct fdisk_parttype *fdisk_new_unknown_parttype(unsigned int type, const char *typestr);
-extern void fdisk_free_parttype(struct fdisk_parttype *type);
-
 extern sector_t fdisk_topology_get_first_lba(struct fdisk_context *cxt);
 extern unsigned long fdisk_topology_get_grain(struct fdisk_context *cxt);
 
index d1ea819594c8ee70aa7d6ec28b43a0600653de72..9588311fc992dc30cda1522a532ca755a5bf903f 100644 (file)
@@ -646,211 +646,5 @@ void fdisk_free_context(struct fdisk_context *cxt)
        free(cxt);
 }
 
-/**
- * fdisk_get_nparttypes:
- * @cxt: fdisk context
- *
- * Returns: number of partition types supported by the current label
- */
-size_t fdisk_get_nparttypes(struct fdisk_context *cxt)
-{
-       if (!cxt || !cxt->label)
-               return 0;
-
-       return cxt->label->nparttypes;
-}
-
-/**
- * fdisk_get_parttype_from_code:
- * @cxt: fdisk context
- * @code: code to search for
- *
- * Search in lable-specific table of supported partition types by code.
- *
- * Returns partition type or NULL upon failure or invalid @code.
- */
-struct fdisk_parttype *fdisk_get_parttype_from_code(
-                               struct fdisk_context *cxt,
-                               unsigned int code)
-{
-       size_t i;
-
-       if (!fdisk_get_nparttypes(cxt))
-               return NULL;
-
-       for (i = 0; i < cxt->label->nparttypes; i++)
-               if (cxt->label->parttypes[i].type == code)
-                       return &cxt->label->parttypes[i];
-
-       return NULL;
-}
-
-/**
- * fdisk_get_parttype_from_string:
- * @cxt: fdisk context
- * @str: string to search for
- *
- * Search in lable-specific table of supported partition types by typestr.
- *
- * Returns partition type or NULL upon failure or invalid @str.
- */
-struct fdisk_parttype *fdisk_get_parttype_from_string(
-                               struct fdisk_context *cxt,
-                               const char *str)
-{
-       size_t i;
-
-       if (!fdisk_get_nparttypes(cxt))
-               return NULL;
-
-       for (i = 0; i < cxt->label->nparttypes; i++)
-               if (cxt->label->parttypes[i].typestr
-                   &&strcasecmp(cxt->label->parttypes[i].typestr, str) == 0)
-                       return &cxt->label->parttypes[i];
-
-       return NULL;
-}
-
-/**
- * fdisk_new_unknown_parttype:
- * @type: type as number
- * @typestr: type as string
-
- * Allocates new 'unknown' partition type. Use fdisk_free_parttype() to
- * deallocate.
- *
- * Returns newly allocated partition type, or NULL upon failure.
- */
-struct fdisk_parttype *fdisk_new_unknown_parttype(unsigned int type,
-                                                 const char *typestr)
-{
-       struct fdisk_parttype *t;
-
-       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->type = type;
-       t->flags |= FDISK_PARTTYPE_UNKNOWN | FDISK_PARTTYPE_ALLOCATED;
-
-       DBG(LABEL, dbgprint("allocated new unknown type [%p]", t));
-       return t;
-}
-
-/**
- * fdisk_parse_parttype:
- * @cxt: fdisk context
- * @str: string to parse from
- *
- * Returns pointer to static table of the partition types, or newly allocated
- * partition type for unknown types. It's safe to call fdisk_free_parttype()
- * for all results.
- */
-struct fdisk_parttype *fdisk_parse_parttype(
-                               struct fdisk_context *cxt,
-                               const char *str)
-{
-       struct fdisk_parttype *types, *ret;
-       unsigned int code = 0;
-       char *typestr = NULL, *end = NULL;
-
-       if (!fdisk_get_nparttypes(cxt))
-               return NULL;
-
-       DBG(LABEL, dbgprint("parsing '%s' partition type", str));
-
-       types = cxt->label->parttypes;
 
-       if (types[0].typestr == NULL && isxdigit(*str)) {
-
-               errno = 0;
-               code = strtol(str, &end, 16);
-
-               if (errno || *end != '\0') {
-                       DBG(LABEL, dbgprint("parsing failed: %m"));
-                       return NULL;
-               }
-               ret = fdisk_get_parttype_from_code(cxt, code);
-               if (ret)
-                       goto done;
-       } else {
-               int i;
-
-               /* maybe specified by type string (e.g. UUID) */
-               ret = fdisk_get_parttype_from_string(cxt, str);
-               if (ret)
-                       goto done;
-
-               /* maybe specified by order number */
-               errno = 0;
-               i = strtol(str, &end, 0);
-               if (errno == 0 && *end == '\0' && i > 0
-                   && i - 1 < (int) fdisk_get_nparttypes(cxt)) {
-                       ret = &types[i - 1];
-                       goto done;
-               }
-       }
-
-       ret = fdisk_new_unknown_parttype(code, typestr);
-done:
-       DBG(LABEL, dbgprint("returns '%s' partition type", ret->name));
-       return ret;
-}
-
-/**
- * fdisk_free_parttype:
- * @t: new type
- *
- * Free the @type.
- */
-void fdisk_free_parttype(struct fdisk_parttype *t)
-{
-       if (t && (t->flags & FDISK_PARTTYPE_ALLOCATED)) {
-               DBG(LABEL, dbgprint("freeing %p partition type", t));
-               free(t->typestr);
-               free(t);
-       }
-}
-
-/**
- * fdisk_get_partition_type:
- * @cxt: fdisk context
- * @partnum: partition number
- *
- * Returns partition type or NULL upon failure.
- */
-struct fdisk_parttype *fdisk_get_partition_type(struct fdisk_context *cxt, int partnum)
-{
-       if (!cxt || !cxt->label || !cxt->label->part_get_type)
-               return NULL;
-
-       DBG(LABEL, dbgprint("partition: %d: get type", partnum));
-       return cxt->label->part_get_type(cxt, partnum);
-}
-
-/**
- * fdisk_set_partition_type:
- * @cxt: fdisk context
- * @partnum: partition number
- * @t: new type
- *
- * Returns 0 on success, < 0 on error.
- */
-int fdisk_set_partition_type(struct fdisk_context *cxt, int partnum,
-                            struct fdisk_parttype *t)
-{
-       if (!cxt || !cxt->label || !cxt->label->part_set_type)
-               return -EINVAL;
-
-       DBG(LABEL, dbgprint("partition: %d: set type", partnum));
-       return cxt->label->part_set_type(cxt, partnum, t);
-}
 
index 4b2d0e7f14c0fca4c34e4aad1967a5fbfeb8344f..66cbe872c9eb7d82179cca07f1125be6a0060161 100644 (file)
@@ -8,7 +8,8 @@ noinst_LTLIBRARIES += libfdisk.la
 libfdisk_la_SOURCES = \
        libfdisk/src/libfdisk.h \
        \
-       libfdisk/src/init.c
+       libfdisk/src/init.c \
+       libfdisk/src/parttype.c
 
 
 nodist_libfdisk_la_SOURCES = libfdisk/src/fdiskP.h
index fcb69b76d1c63bd382d2c2def9f72bd02a37bc50..33187fe5a2bc2f512a9f58b2ec03b006edf8ebed 100644 (file)
 extern "C" {
 #endif
 
+struct fdisk_context;
+struct fdisk_parttype;
+
 /* init.c */
 extern void fdisk_init_debug(int mask);
 
+/* parttype.c */
+extern struct fdisk_parttype *fdisk_get_parttype_from_code(struct fdisk_context *cxt,
+                                unsigned int code);
+extern struct fdisk_parttype *fdisk_get_parttype_from_string(struct fdisk_context *cxt,
+                                const char *str);
+extern struct fdisk_parttype *fdisk_parse_parttype(struct fdisk_context *cxt, const char *str);
+
+extern struct fdisk_parttype *fdisk_new_unknown_parttype(unsigned int type, const char *typestr);
+extern void fdisk_free_parttype(struct fdisk_parttype *type);
+extern size_t fdisk_get_nparttypes(struct fdisk_context *cxt);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/libfdisk/src/parttype.c b/libfdisk/src/parttype.c
new file mode 100644 (file)
index 0000000..1d9f4e8
--- /dev/null
@@ -0,0 +1,214 @@
+
+#include <ctype.h>
+
+#include "nls.h"
+
+#include "fdiskP.h"
+
+/**
+ * fdisk_get_parttype_from_code:
+ * @cxt: fdisk context
+ * @code: code to search for
+ *
+ * Search in lable-specific table of supported partition types by code.
+ *
+ * Returns partition type or NULL upon failure or invalid @code.
+ */
+struct fdisk_parttype *fdisk_get_parttype_from_code(
+                               struct fdisk_context *cxt,
+                               unsigned int code)
+{
+       size_t i;
+
+       if (!fdisk_get_nparttypes(cxt))
+               return NULL;
+
+       for (i = 0; i < cxt->label->nparttypes; i++)
+               if (cxt->label->parttypes[i].type == code)
+                       return &cxt->label->parttypes[i];
+
+       return NULL;
+}
+
+/**
+ * fdisk_get_parttype_from_string:
+ * @cxt: fdisk context
+ * @str: string to search for
+ *
+ * Search in lable-specific table of supported partition types by typestr.
+ *
+ * Returns partition type or NULL upon failure or invalid @str.
+ */
+struct fdisk_parttype *fdisk_get_parttype_from_string(
+                               struct fdisk_context *cxt,
+                               const char *str)
+{
+       size_t i;
+
+       if (!fdisk_get_nparttypes(cxt))
+               return NULL;
+
+       for (i = 0; i < cxt->label->nparttypes; i++)
+               if (cxt->label->parttypes[i].typestr
+                   &&strcasecmp(cxt->label->parttypes[i].typestr, str) == 0)
+                       return &cxt->label->parttypes[i];
+
+       return NULL;
+}
+
+/**
+ * fdisk_new_unknown_parttype:
+ * @type: type as number
+ * @typestr: type as string
+
+ * Allocates new 'unknown' partition type. Use fdisk_free_parttype() to
+ * deallocate.
+ *
+ * Returns newly allocated partition type, or NULL upon failure.
+ */
+struct fdisk_parttype *fdisk_new_unknown_parttype(unsigned int type,
+                                                 const char *typestr)
+{
+       struct fdisk_parttype *t;
+
+       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->type = type;
+       t->flags |= FDISK_PARTTYPE_UNKNOWN | FDISK_PARTTYPE_ALLOCATED;
+
+       DBG(LABEL, dbgprint("allocated new unknown type [%p]", t));
+       return t;
+}
+
+/**
+ * fdisk_parse_parttype:
+ * @cxt: fdisk context
+ * @str: string to parse from
+ *
+ * Returns pointer to static table of the partition types, or newly allocated
+ * partition type for unknown types. It's safe to call fdisk_free_parttype()
+ * for all results.
+ */
+struct fdisk_parttype *fdisk_parse_parttype(
+                               struct fdisk_context *cxt,
+                               const char *str)
+{
+       struct fdisk_parttype *types, *ret;
+       unsigned int code = 0;
+       char *typestr = NULL, *end = NULL;
+
+       if (!fdisk_get_nparttypes(cxt))
+               return NULL;
+
+       DBG(LABEL, dbgprint("parsing '%s' partition type", str));
+
+       types = cxt->label->parttypes;
+
+       if (types[0].typestr == NULL && isxdigit(*str)) {
+
+               errno = 0;
+               code = strtol(str, &end, 16);
+
+               if (errno || *end != '\0') {
+                       DBG(LABEL, dbgprint("parsing failed: %m"));
+                       return NULL;
+               }
+               ret = fdisk_get_parttype_from_code(cxt, code);
+               if (ret)
+                       goto done;
+       } else {
+               int i;
+
+               /* maybe specified by type string (e.g. UUID) */
+               ret = fdisk_get_parttype_from_string(cxt, str);
+               if (ret)
+                       goto done;
+
+               /* maybe specified by order number */
+               errno = 0;
+               i = strtol(str, &end, 0);
+               if (errno == 0 && *end == '\0' && i > 0
+                   && i - 1 < (int) fdisk_get_nparttypes(cxt)) {
+                       ret = &types[i - 1];
+                       goto done;
+               }
+       }
+
+       ret = fdisk_new_unknown_parttype(code, typestr);
+done:
+       DBG(LABEL, dbgprint("returns '%s' partition type", ret->name));
+       return ret;
+}
+
+/**
+ * fdisk_free_parttype:
+ * @t: new type
+ *
+ * Free the @type.
+ */
+void fdisk_free_parttype(struct fdisk_parttype *t)
+{
+       if (t && (t->flags & FDISK_PARTTYPE_ALLOCATED)) {
+               DBG(LABEL, dbgprint("freeing %p partition type", t));
+               free(t->typestr);
+               free(t);
+       }
+}
+
+/**
+ * fdisk_get_partition_type:
+ * @cxt: fdisk context
+ * @partnum: partition number
+ *
+ * Returns partition type or NULL upon failure.
+ */
+struct fdisk_parttype *fdisk_get_partition_type(struct fdisk_context *cxt, int partnum)
+{
+       if (!cxt || !cxt->label || !cxt->label->part_get_type)
+               return NULL;
+
+       DBG(LABEL, dbgprint("partition: %d: get type", partnum));
+       return cxt->label->part_get_type(cxt, partnum);
+}
+
+/**
+ * fdisk_set_partition_type:
+ * @cxt: fdisk context
+ * @partnum: partition number
+ * @t: new type
+ *
+ * Returns 0 on success, < 0 on error.
+ */
+int fdisk_set_partition_type(struct fdisk_context *cxt, int partnum,
+                            struct fdisk_parttype *t)
+{
+       if (!cxt || !cxt->label || !cxt->label->part_set_type)
+               return -EINVAL;
+
+       DBG(LABEL, dbgprint("partition: %d: set type", partnum));
+       return cxt->label->part_set_type(cxt, partnum, t);
+}
+
+/**
+ * fdisk_get_nparttypes:
+ * @cxt: fdisk context
+ *
+ * Returns: number of partition types supported by the current label
+ */
+size_t fdisk_get_nparttypes(struct fdisk_context *cxt)
+{
+       if (!cxt || !cxt->label)
+               return 0;
+
+       return cxt->label->nparttypes;
+}