]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: add fdisk_label_get_field_by_name() and const for labels
authorKarel Zak <kzak@redhat.com>
Fri, 3 Oct 2014 10:12:23 +0000 (12:12 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 7 Oct 2014 12:55:32 +0000 (14:55 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/src/label.c
libfdisk/src/libfdisk.h
libfdisk/src/parttype.c

index 071657c57742af139a7abb31e228f340e69a5bb9..a3a4e88123cb4c7e996c0174a4ae376fd1e48120 100644 (file)
@@ -65,7 +65,7 @@ int fdisk_probe_labels(struct fdisk_context *cxt)
  *
  * Returns: label name
  */
-const char *fdisk_label_get_name(struct fdisk_label *lb)
+const char *fdisk_label_get_name(const struct fdisk_label *lb)
 {
        return lb ? lb->name : NULL;
 }
@@ -76,7 +76,7 @@ const char *fdisk_label_get_name(struct fdisk_label *lb)
  *
  * Returns: FDISK_DISKLABEL_*.
  */
-int fdisk_label_get_type(struct fdisk_label *lb)
+int fdisk_label_get_type(const struct fdisk_label *lb)
 {
        return lb->id;
 }
@@ -87,7 +87,7 @@ int fdisk_label_get_type(struct fdisk_label *lb)
  *
  * Returns: 1 if label requires CHS geometry
  */
-int fdisk_label_require_geometry(struct fdisk_label *lb)
+int fdisk_label_require_geometry(const struct fdisk_label *lb)
 {
        assert(lb);
 
@@ -109,7 +109,7 @@ int fdisk_label_require_geometry(struct fdisk_label *lb)
  * Returns 0 on success, otherwise, a corresponding error.
  */
 int fdisk_label_get_fields_ids(
-               struct fdisk_label *lb,
+               const struct fdisk_label *lb,
                struct fdisk_context *cxt,
                int **ids, size_t *nids)
 {
@@ -162,7 +162,7 @@ int fdisk_label_get_fields_ids(
  *
  * Returns: pointer to static instance of the field.
  */
-const struct fdisk_field *fdisk_label_get_field(struct fdisk_label *lb, int id)
+const struct fdisk_field *fdisk_label_get_field(const struct fdisk_label *lb, int id)
 {
        size_t i;
 
@@ -177,6 +177,31 @@ const struct fdisk_field *fdisk_label_get_field(struct fdisk_label *lb, int id)
        return NULL;
 }
 
+/**
+ * fdisk_label_get_field_by_name
+ * @lb: label
+ * @name: field name
+ *
+ * Returns: pointer to static instance of the field.
+ */
+const struct fdisk_field *fdisk_label_get_field_by_name(
+                               const struct fdisk_label *lb,
+                               const char *name)
+{
+       size_t i;
+
+       assert(lb);
+       assert(name);
+
+       for (i = 0; i < lb->nfields; i++) {
+               if (lb->fields[i].name && strcasecmp(lb->fields[i].name, name) == 0)
+                       return &lb->fields[i];
+       }
+
+       return NULL;
+}
+
+
 /**
  * fdisk_field_get_id:
  * @field: field instance
@@ -495,7 +520,7 @@ void fdisk_label_set_changed(struct fdisk_label *lb, int changed)
  *
  * Returns: 1 if in-memory data has been changed.
  */
-int fdisk_label_is_changed(struct fdisk_label *lb)
+int fdisk_label_is_changed(const struct fdisk_label *lb)
 {
        assert(lb);
        return lb ? lb->changed : 0;
@@ -524,7 +549,7 @@ void fdisk_label_set_disabled(struct fdisk_label *lb, int disabled)
  *
  * Returns: 1 if label driver disabled.
  */
-int fdisk_label_is_disabled(struct fdisk_label *lb)
+int fdisk_label_is_disabled(const struct fdisk_label *lb)
 {
        assert(lb);
        return lb ? lb->disabled : 0;
index ac0aad263dd30618820e733f4e8cd7b2a08d99c3..96c6bfaef0e5d30e9e8a77ea093cc3c33ed94d68 100644 (file)
@@ -132,21 +132,21 @@ sector_t fdisk_get_geom_sectors(struct fdisk_context *cxt);
 sector_t fdisk_get_geom_cylinders(struct fdisk_context *cxt);
 
 /* parttype.c */
-const struct fdisk_parttype *fdisk_label_get_parttype(struct fdisk_label *lb, size_t n);
-size_t fdisk_label_get_nparttypes(struct fdisk_label *lb);
+const struct fdisk_parttype *fdisk_label_get_parttype(const struct fdisk_label *lb, size_t n);
+size_t fdisk_label_get_nparttypes(const struct fdisk_label *lb);
 
-int fdisk_label_has_code_parttypes(struct fdisk_label *lb);
+int fdisk_label_has_code_parttypes(const struct fdisk_label *lb);
 struct fdisk_parttype *fdisk_label_get_parttype_from_code(
-                               struct fdisk_label *lb,
+                               const struct fdisk_label *lb,
                                unsigned int code);
 
 struct fdisk_parttype *fdisk_label_get_parttype_from_string(
-                               struct fdisk_label *lb,
+                               const struct fdisk_label *lb,
                                const char *str);
 struct fdisk_parttype *fdisk_new_unknown_parttype(unsigned int code,
                                                  const char *typestr);
 struct fdisk_parttype *fdisk_label_parse_parttype(
-                               struct fdisk_label *lb,
+                               const struct fdisk_label *lb,
                                const char *str);
 
 struct fdisk_parttype *fdisk_copy_parttype(const struct fdisk_parttype *type);
@@ -181,11 +181,13 @@ enum {
        FDISK_FIELD_NAME,
        FDISK_FIELD_SADDR,
        FDISK_FIELD_UUID,
+
+       FDISK_NFIELDS           /* must be last */
 };
 
-int fdisk_label_get_type(struct fdisk_label *lb);
-const char *fdisk_label_get_name(struct fdisk_label *lb);
-int fdisk_label_require_geometry(struct fdisk_label *lb);
+int fdisk_label_get_type(const struct fdisk_label *lb);
+const char *fdisk_label_get_name(const struct fdisk_label *lb);
+int fdisk_label_require_geometry(const struct fdisk_label *lb);
 
 
 extern int fdisk_write_disklabel(struct fdisk_context *cxt);
@@ -209,10 +211,14 @@ extern int fdisk_set_partition_type(struct fdisk_context *cxt, size_t partnum,
 
 
 extern int fdisk_label_get_fields_ids(
-                       struct fdisk_label *lb,
+                       const struct fdisk_label *lb,
                        struct fdisk_context *cxt,
                        int **ids, size_t *nids);
-extern const struct fdisk_field *fdisk_label_get_field(struct fdisk_label *lb, int id);
+
+extern const struct fdisk_field *fdisk_label_get_field(const struct fdisk_label *lb, int id);
+extern const struct fdisk_field *fdisk_label_get_field_by_name(
+                       const struct fdisk_label *lb,
+                       const char *name);
 
 extern int fdisk_field_get_id(const struct fdisk_field *fl);
 extern const char *fdisk_field_get_name(const struct fdisk_field *fl);
@@ -221,10 +227,10 @@ extern int fdisk_field_is_number(const struct fdisk_field *fl);
 
 
 extern void fdisk_label_set_changed(struct fdisk_label *lb, int changed);
-extern int fdisk_label_is_changed(struct fdisk_label *lb);
+extern int fdisk_label_is_changed(const struct fdisk_label *lb);
 
 extern void fdisk_label_set_disabled(struct fdisk_label *lb, int disabled);
-extern int fdisk_label_is_disabled(struct fdisk_label *lb);
+extern int fdisk_label_is_disabled(const struct fdisk_label *lb);
 
 extern int fdisk_is_partition_used(struct fdisk_context *cxt, size_t n);
 
index 21baedcd198a3da638ea20e9eb4bcb078221d4bb..8c4ac61ef1b2afabf2ef15d7ed8bdc533c7b5db1 100644 (file)
@@ -12,7 +12,7 @@
  *
  * Returns: number of types supported by label.
  */
-size_t fdisk_label_get_nparttypes(struct fdisk_label *lb)
+size_t fdisk_label_get_nparttypes(const struct fdisk_label *lb)
 {
        if (!lb)
                return 0;
@@ -26,7 +26,7 @@ size_t fdisk_label_get_nparttypes(struct fdisk_label *lb)
  *
  * Returns: return parttype
  */
-const struct fdisk_parttype *fdisk_label_get_parttype(struct fdisk_label *lb, size_t n)
+const struct fdisk_parttype *fdisk_label_get_parttype(const struct fdisk_label *lb, size_t n)
 {
        if (!lb || n >= lb->nparttypes)
                return NULL;
@@ -40,7 +40,7 @@ const struct fdisk_parttype *fdisk_label_get_parttype(struct fdisk_label *lb, si
  * Returns: 1 if the label uses code as partition type
  *          identifiers (e.g. MBR) or 0.
  */
-int fdisk_label_has_code_parttypes(struct fdisk_label *lb)
+int fdisk_label_has_code_parttypes(const struct fdisk_label *lb)
 {
        assert(lb);
 
@@ -60,7 +60,7 @@ int fdisk_label_has_code_parttypes(struct fdisk_label *lb)
  * Returns: partition type or NULL upon failure or invalid @code.
  */
 struct fdisk_parttype *fdisk_label_get_parttype_from_code(
-                               struct fdisk_label *lb,
+                               const struct fdisk_label *lb,
                                unsigned int code)
 {
        size_t i;
@@ -87,7 +87,7 @@ struct fdisk_parttype *fdisk_label_get_parttype_from_code(
  * Returns: partition type or NULL upon failure or invalid @str.
  */
 struct fdisk_parttype *fdisk_label_get_parttype_from_string(
-                               struct fdisk_label *lb,
+                               const struct fdisk_label *lb,
                                const char *str)
 {
        size_t i;
@@ -172,7 +172,7 @@ struct fdisk_parttype *fdisk_copy_parttype(const struct fdisk_parttype *type)
  * for all results.
  */
 struct fdisk_parttype *fdisk_label_parse_parttype(
-                               struct fdisk_label *lb,
+                               const struct fdisk_label *lb,
                                const char *str)
 {
        struct fdisk_parttype *types, *ret;
@@ -184,7 +184,7 @@ struct fdisk_parttype *fdisk_label_parse_parttype(
        if (!lb->nparttypes)
                return NULL;
 
-       DBG(LABEL, ul_debugobj(lb, "parsing '%s' (%s) partition type",
+       DBG(LABEL, ul_debugobj((void *) lb, "parsing '%s' (%s) partition type",
                                str, lb->name));
 
        types = lb->parttypes;
@@ -195,7 +195,7 @@ struct fdisk_parttype *fdisk_label_parse_parttype(
                code = strtol(str, &end, 16);
 
                if (errno || *end != '\0') {
-                       DBG(LABEL, ul_debugobj(lb, "parsing failed: %m"));
+                       DBG(LABEL, ul_debugobj((void *) lb, "parsing failed: %m"));
                        return NULL;
                }
                ret = fdisk_label_get_parttype_from_code(lb, code);