]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: improve nested context initialization
authorKarel Zak <kzak@redhat.com>
Thu, 27 Jun 2013 08:52:34 +0000 (10:52 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 16 Sep 2013 14:47:05 +0000 (16:47 +0200)
 - all label prober() function in fdisk_new_nested_context()
 - don't reset device properties for nested contexts

Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/src/context.c
libfdisk/src/label.c

index f44505a5b37b87ba68c1a585c697a3eef11188d6..dc79783c428244e075a4ec5e64c2a2a64292d099 100644 (file)
@@ -32,9 +32,9 @@ struct fdisk_context *fdisk_new_nested_context(struct fdisk_context *parent,
                                const char *name)
 {
        struct fdisk_context *cxt;
+       struct fdisk_label *lb = NULL;
 
        assert(parent);
-       assert(name);
 
        cxt = calloc(1, sizeof(*cxt));
        if (!cxt)
@@ -59,8 +59,23 @@ struct fdisk_context *fdisk_new_nested_context(struct fdisk_context *parent,
 
        cxt->geom = parent->geom;
 
-       if (strcmp(name, "bsd") == 0)
-               cxt->label = cxt->labels[ cxt->nlabels++ ] = fdisk_new_bsd_label(cxt);
+       if (name && strcmp(name, "bsd") == 0)
+               lb = cxt->labels[ cxt->nlabels++ ] = fdisk_new_bsd_label(cxt);
+
+       if (lb) {
+               DBG(LABEL, dbgprint("probing for nested %s", lb->name));
+
+               cxt->label = lb;
+
+               if (lb->op->probe(cxt) == 1)
+                       __fdisk_context_switch_label(cxt, lb);
+               else {
+                       DBG(LABEL, dbgprint("not found %s label", lb->name));
+                       if (lb->op->deinit)
+                               lb->op->deinit(lb);
+                       cxt->label = NULL;
+               }
+       }
 
        return cxt;
 }
index bae9684804b251c5510f51d0891bce5a1ceee8ad..147918e032de710fae8bed039520209cc156b203 100644 (file)
@@ -212,6 +212,7 @@ int fdisk_delete_partition(struct fdisk_context *cxt, size_t partnum)
 int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name)
 {
        int haslabel = 0;
+       struct fdisk_label *lb;
 
        if (!cxt)
                return -EINVAL;
@@ -229,16 +230,18 @@ int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name)
                haslabel = 1;
        }
 
-       cxt->label = fdisk_context_get_label(cxt, name);
-       if (!cxt->label)
+       lb = fdisk_context_get_label(cxt, name);
+       if (!lb)
                return -EINVAL;
-
-       DBG(LABEL, dbgprint("changing to %s label\n", cxt->label->name));
-       if (!cxt->label->op->create)
+       if (!lb->op->create)
                return -ENOSYS;
 
-       if (haslabel)
+       __fdisk_context_switch_label(cxt, lb);
+
+       if (haslabel && !cxt->parent)
                fdisk_reset_device_properties(cxt);
+
+       DBG(LABEL, dbgprint("create a new %s label", lb->name));
        return cxt->label->op->create(cxt);
 }