Skip to contentSkip to conversation

fdisk: API: add fdisk_label_change

PATCH In review Davidlohr Bueso 4 participants 11 messages Jul 22, 2012 – Jul 24, 2012 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 3/10 fdisk: API: add fdisk_label_change

From: Davidlohr Bueso <[email protected]>

A new fdisk_label_change() function is added for situations when the disk label is changed (ie: creating a new sun label). This function only updates the label pointer in the context to use the newly specified label type.

---
 fdisks/fdisk.h         |    2 ++
 fdisks/fdiskdoslabel.c |    1 +
 fdisks/fdisksgilabel.c |    2 ++
 fdisks/fdisksunlabel.c |    1 +
 fdisks/utils.c         |   37 +++++++++++++++++++++++++++++++++++++
 5 files changed, 43 insertions(+), 0 deletions(-)
fdisks/fdisk.h +2 -0
index d716824..d7e85f5 100644
@@ -104,6 +104,7 @@ enum fdisk_error {
 	FDISK_ERROR_WRITE,
 	FDISK_ERROR_IOCTL,
 	FDISK_ERROR_PROBE,
+	FDISK_ERROR_NOLABEL,
 	FDISK_ERROR_UNKNOWN
 };
 
@@ -164,6 +165,7 @@ extern void fdisk_mbr_zeroize(struct fdisk_context *cxt);
 extern void fdisk_geom_set_cyls(struct fdisk_context *cxt);
 extern const char *fdisk_error_name(enum fdisk_error errcode);
 extern void fdisk_error_fatal(struct fdisk_context *cxt, enum fdisk_error errcode);
+extern int fdisk_label_change(struct fdisk_context *cxt, const char *name);
 
 /* prototypes for fdisk.c */
 extern char *disk_device, *line_ptr;
fdisks/fdiskdoslabel.c +1 -0
index 535afdc..9b9b23a 100644
@@ -230,6 +230,7 @@ void create_doslabel(struct fdisk_context *cxt)
 
 	dos_init(cxt);
 	fdisk_mbr_zeroize(cxt);
+	fdisk_label_change(cxt, "dos");
 	set_all_unchanged();
 	set_changed(0);
 
fdisks/fdisksgilabel.c +2 -0
index e38d98f..6001038 100644
@@ -780,6 +780,8 @@ create_sgilabel(struct fdisk_context *cxt)
 		}
 
 	fdisk_mbr_zeroize(cxt);
+	fdisk_label_change(cxt, "sgi");
+
 	sgilabel->magic = SSWAP32(SGI_LABEL_MAGIC);
 	sgilabel->boot_part = SSWAP16(0);
 	sgilabel->swap_part = SSWAP16(1);
fdisks/fdisksunlabel.c +1 -0
index 4123806..b63335c 100644
@@ -161,6 +161,7 @@ void create_sunlabel(struct fdisk_context *cxt)
 
 	init();
 	fdisk_mbr_zeroize(cxt);
+	fdisk_label_change(cxt, "sun");
 
 	sunlabel->magic = SSWAP16(SUN_LABEL_MAGIC);
 	sunlabel->sanity = SSWAP32(SUN_LABEL_SANE);
fdisks/utils.c +37 -0
index cf9484c..48dedfb 100644
@@ -44,6 +44,43 @@ static const struct fdisk_label *labels[] =
 	&mac_label,
 };
 
+/**
+ * fdisk_label_change:
+ * @cxt: fdisk context
+ * @name: new label name
+ *
+ * Updates the disk label type to the one specified by @name.
+ *
+ * Returns 0 on success, otherwise, a corresponding error.
+ */
+int fdisk_label_change(struct fdisk_context *cxt, const char *name)
+{
+	int i;
+
+	if (!cxt || !cxt->label || !name)
+		return FDISK_ERROR_UNKNOWN;
+
+	/* not really changing the label */
+	if (!strncmp(name, cxt->label->name, strlen(name)))
+		goto done;
+
+	for (i = 0; i < ARRAY_SIZE(labels); i++) {
+		if (strncmp(name, labels[i]->name, strlen(name)))
+			continue;
+	
+		/* found the new label */
+		memset(cxt->label, 0, sizeof(struct fdisk_label));
+		memcpy(cxt->label, labels[i], sizeof(struct fdisk_label));
+		DBG(LABEL, dbgprint("changing to a %s label\n", labels[i]->name));
+		goto done;
+	}
+
+	/* couldn't find the requested label type */
+	return FDISK_ERROR_NOLABEL;
+done:
+	return 0;
+}
+
 static int __probe_labels(struct fdisk_context *cxt)
 {
 	int i, rc = 0;
Signature · 1 line
1.7.4.1
#1 lore raw
Quoted 8 lines · Davidlohr Bueso
fdisks/fdisksunlabel.c +1 -0
@@ -161,6 +161,7 @@ void create_sunlabel(struct fdisk_context *cxt)
 
 	init();
 	fdisk_mbr_zeroize(cxt);
+	fdisk_label_change(cxt, "sun");
 

...

Quoted 4 lines
fdisks/fdisksunlabel.c +4 -0
+int fdisk_label_change(struct fdisk_context *cxt, const char *name)
+{
+	int i;
+

fdisks/fdisksunlabel.c

Quoted 10 lines
fdisks/fdisksunlabel.c +10 -0
+	if (!cxt || !cxt->label || !name)
+		return FDISK_ERROR_UNKNOWN;
+
+	/* not really changing the label */
+	if (!strncmp(name, cxt->label->name, strlen(name)))
+		goto done;
+
+	for (i = 0; i < ARRAY_SIZE(labels); i++) {
+		if (strncmp(name, labels[i]->name, strlen(name)))
+			continue;

Shouldn't we use something else than hardcoded "sun"|"dos"|"sgi"|... strings here? (See also "[PATCH 08/10] fdisk: API: add create disklabel to label operations".)

Have a nice day,
Berny

#2 lore raw
Quoted 23 lines · Bernhard Voelker
Quoted 8 lines · Davidlohr Bueso
fdisks/fdisksunlabel.c +1 -0
@@ -161,6 +161,7 @@ void create_sunlabel(struct fdisk_context *cxt)
 
 	init();
 	fdisk_mbr_zeroize(cxt);
+	fdisk_label_change(cxt, "sun");
 

...

Quoted 9 lines
fdisks/fdisksunlabel.c +9 -0
+int fdisk_label_change(struct fdisk_context *cxt, const char *name)
+{
+	int i;
+
+	if (!cxt || !cxt->label || !name)
+		return FDISK_ERROR_UNKNOWN;
+
+	/* not really changing the label */
+	if (!strncmp(name, cxt->label->name, strlen(name)))
Quoted 8 lines

fdisks/fdisksunlabel.c

Quoted 5 lines
fdisks/fdisksunlabel.c +5 -0
+		goto done;
+
+	for (i = 0; i < ARRAY_SIZE(labels); i++) {
+		if (strncmp(name, labels[i]->name, strlen(name)))
+			continue;

Shouldn't we use something else than hardcoded "sun"|"dos"|"sgi"|... strings here?

What's the problem with these strings? I think these are well recognized with clear meaning. What alternative do you propose?

Thanks,

Petr

Quoted 9 lines

(See also "[PATCH 08/10] fdisk: API: add create disklabel to label operations".)

Have a nice day,
Berny
--
To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Petr

Signature · 2 lines
Petr Uzel
IRC: ptr_uzl @ freenode
#3 lore raw
Quoted 15 lines · Petr Uzel
Quoted 14 lines · Bernhard Voelker
Quoted 8 lines · Davidlohr Bueso
fdisks/fdisksunlabel.c +1 -0
@@ -161,6 +161,7 @@ void create_sunlabel(struct fdisk_context *cxt)
 
 	init();
 	fdisk_mbr_zeroize(cxt);
+	fdisk_label_change(cxt, "sun");
 
Quoted 5 lines
Quoted 2 lines

Shouldn't we use something else than hardcoded "sun"|"dos"|"sgi"|... strings here?

What's the problem with these strings? I think these are well recognized with clear meaning. What alternative do you propose?

Sorry, I should've proposed this already in my first mail:

- fdisk_label_change(cxt, "sun");
+ fdisk_label_change(cxt, sun_label.name);

It's already defined ;-)

Have a nice day,
Berny

#4 lore raw
Quoted 26 lines · Bernhard Voelker
Quoted 15 lines · Petr Uzel
Quoted 14 lines · Bernhard Voelker
Quoted 8 lines · Davidlohr Bueso
fdisks/fdisksunlabel.c +1 -0
@@ -161,6 +161,7 @@ void create_sunlabel(struct fdisk_context *cxt)
 
 	init();
 	fdisk_mbr_zeroize(cxt);
+	fdisk_label_change(cxt, "sun");
 
Quoted 5 lines
Quoted 2 lines

Shouldn't we use something else than hardcoded "sun"|"dos"|"sgi"|... strings here?

What's the problem with these strings? I think these are well recognized with clear meaning. What alternative do you propose?

Sorry, I should've proposed this already in my first mail:

Review

Quoted 2 lines
(quoted diff) +1 -1
-	fdisk_label_change(cxt, "sun");
+	fdisk_label_change(cxt, sun_label.name);

I'm planning on adding the disklabel type (numeric) to the context structure in a near future, so we'd then have two ways of identifying labels. I really do not see much difference between numbers and strings
- this is not a performance critical program.

Thanks,
Davidlohr

Quoted 10 lines

It's already defined ;-)

Have a nice day,
Berny

--
To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw
Quoted 10 lines · Davidlohr Bueso

Review

Quoted 10 lines
Quoted 5 lines
Quoted 2 lines

Shouldn't we use something else than hardcoded "sun"|"dos"|"sgi"|... strings here?

What's the problem with these strings? I think these are well recognized with clear meaning. What alternative do you propose?

Sorry, I should've proposed this already in my first mail:

(quoted diff) +1 -1
-	fdisk_label_change(cxt, "sun");
+	fdisk_label_change(cxt, sun_label.name);

Ah, right. Deduplicating the strings makes sense, IMO.

Quoted 4 lines

I'm planning on adding the disklabel type (numeric) to the context structure in a near future, so we'd then have two ways of identifying labels. I really do not see much difference between numbers and strings
- this is not a performance critical program.

The concern here is not speed, but maintainability ;)

Best,

Petr

Signature · 2 lines
Petr Uzel
IRC: ptr_uzl @ freenode
#6 lore raw
Quoted 14 lines · Petr Uzel
Quoted 10 lines · Davidlohr Bueso

Review

Quoted 10 lines
Quoted 5 lines
Quoted 2 lines

Shouldn't we use something else than hardcoded "sun"|"dos"|"sgi"|... strings here?

What's the problem with these strings? I think these are well recognized with clear meaning. What alternative do you propose?

Sorry, I should've proposed this already in my first mail:

(quoted diff) +1 -1
-	fdisk_label_change(cxt, "sun");
+	fdisk_label_change(cxt, sun_label.name);

Ah, right. Deduplicating the strings makes sense, IMO.

Quoted 6 lines
Quoted 4 lines

I'm planning on adding the disklabel type (numeric) to the context structure in a near future, so we'd then have two ways of identifying labels. I really do not see much difference between numbers and strings
- this is not a performance critical program.

The concern here is not speed, but maintainability ;)

Right ... and to save a bit size of the binary (well, not too much in this case) ;-)

Have a nice day,
Berny

#7 lore raw
In reply to Davidlohr Bueso
Quoted 5 lines · Davidlohr Bueso

From: Davidlohr Bueso <[email protected]>

A new fdisk_label_change() function is added for situations when the disk label is changed (ie: creating a new sun label). This function only updates the label pointer in the context to use the newly specified label type.

Why can't we just call __probe_labels() after creating new label, e.g. in soon-to-be introduced fdisk_label_create()?

Quoted 125 lines
---
 fdisks/fdisk.h         |    2 ++
 fdisks/fdiskdoslabel.c |    1 +
 fdisks/fdisksgilabel.c |    2 ++
 fdisks/fdisksunlabel.c |    1 +
 fdisks/utils.c         |   37 +++++++++++++++++++++++++++++++++++++
 5 files changed, 43 insertions(+), 0 deletions(-)
fdisks/fdisk.h +2 -0
index d716824..d7e85f5 100644
@@ -104,6 +104,7 @@ enum fdisk_error {
 	FDISK_ERROR_WRITE,
 	FDISK_ERROR_IOCTL,
 	FDISK_ERROR_PROBE,
+	FDISK_ERROR_NOLABEL,
 	FDISK_ERROR_UNKNOWN
 };
 
@@ -164,6 +165,7 @@ extern void fdisk_mbr_zeroize(struct fdisk_context *cxt);
 extern void fdisk_geom_set_cyls(struct fdisk_context *cxt);
 extern const char *fdisk_error_name(enum fdisk_error errcode);
 extern void fdisk_error_fatal(struct fdisk_context *cxt, enum fdisk_error errcode);
+extern int fdisk_label_change(struct fdisk_context *cxt, const char *name);
 
 /* prototypes for fdisk.c */
 extern char *disk_device, *line_ptr;
fdisks/fdiskdoslabel.c +1 -0
index 535afdc..9b9b23a 100644
@@ -230,6 +230,7 @@ void create_doslabel(struct fdisk_context *cxt)
 
 	dos_init(cxt);
 	fdisk_mbr_zeroize(cxt);
+	fdisk_label_change(cxt, "dos");
 	set_all_unchanged();
 	set_changed(0);
 
fdisks/fdisksgilabel.c +2 -0
index e38d98f..6001038 100644
@@ -780,6 +780,8 @@ create_sgilabel(struct fdisk_context *cxt)
 		}
 
 	fdisk_mbr_zeroize(cxt);
+	fdisk_label_change(cxt, "sgi");
+
 	sgilabel->magic = SSWAP32(SGI_LABEL_MAGIC);
 	sgilabel->boot_part = SSWAP16(0);
 	sgilabel->swap_part = SSWAP16(1);
fdisks/fdisksunlabel.c +1 -0
index 4123806..b63335c 100644
@@ -161,6 +161,7 @@ void create_sunlabel(struct fdisk_context *cxt)
 
 	init();
 	fdisk_mbr_zeroize(cxt);
+	fdisk_label_change(cxt, "sun");
 
 	sunlabel->magic = SSWAP16(SUN_LABEL_MAGIC);
 	sunlabel->sanity = SSWAP32(SUN_LABEL_SANE);
fdisks/utils.c +37 -0
index cf9484c..48dedfb 100644
@@ -44,6 +44,43 @@ static const struct fdisk_label *labels[] =
 	&mac_label,
 };
 
+/**
+ * fdisk_label_change:
+ * @cxt: fdisk context
+ * @name: new label name
+ *
+ * Updates the disk label type to the one specified by @name.
+ *
+ * Returns 0 on success, otherwise, a corresponding error.
+ */
+int fdisk_label_change(struct fdisk_context *cxt, const char *name)
+{
+	int i;
+
+	if (!cxt || !cxt->label || !name)
+		return FDISK_ERROR_UNKNOWN;
+
+	/* not really changing the label */
+	if (!strncmp(name, cxt->label->name, strlen(name)))
+		goto done;
+
+	for (i = 0; i < ARRAY_SIZE(labels); i++) {
+		if (strncmp(name, labels[i]->name, strlen(name)))
+			continue;
+	
+		/* found the new label */
+		memset(cxt->label, 0, sizeof(struct fdisk_label));
+		memcpy(cxt->label, labels[i], sizeof(struct fdisk_label));
+		DBG(LABEL, dbgprint("changing to a %s label\n", labels[i]->name));
+		goto done;
+	}
+
+	/* couldn't find the requested label type */
+	return FDISK_ERROR_NOLABEL;
+done:
+	return 0;
+}
+
 static int __probe_labels(struct fdisk_context *cxt)
 {
 	int i, rc = 0;
Signature · 9 lines
1.7.4.1




--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Petr

Signature · 2 lines
Petr Uzel
IRC: ptr_uzl @ freenode
#8 lore raw
Quoted 9 lines · Petr Uzel
Quoted 5 lines · Davidlohr Bueso

From: Davidlohr Bueso <[email protected]>

A new fdisk_label_change() function is added for situations when the disk label is changed (ie: creating a new sun label). This function only updates the label pointer in the context to use the newly specified label type.

Why can't we just call __probe_labels() after creating new label, e.g. in soon-to-be introduced fdisk_label_create()?

Because this way we give users extra functionality in the API. There should be a way of changing label types on the fly, and since we don't export __probe_labels(), this is the other option.

Thanks,
Davidlohr

Quoted 129 lines
Quoted 126 lines
---
 fdisks/fdisk.h         |    2 ++
 fdisks/fdiskdoslabel.c |    1 +
 fdisks/fdisksgilabel.c |    2 ++
 fdisks/fdisksunlabel.c |    1 +
 fdisks/utils.c         |   37 +++++++++++++++++++++++++++++++++++++
 5 files changed, 43 insertions(+), 0 deletions(-)
fdisks/fdisk.h +2 -0
index d716824..d7e85f5 100644
@@ -104,6 +104,7 @@ enum fdisk_error {
 	FDISK_ERROR_WRITE,
 	FDISK_ERROR_IOCTL,
 	FDISK_ERROR_PROBE,
+	FDISK_ERROR_NOLABEL,
 	FDISK_ERROR_UNKNOWN
 };
 
@@ -164,6 +165,7 @@ extern void fdisk_mbr_zeroize(struct fdisk_context *cxt);
 extern void fdisk_geom_set_cyls(struct fdisk_context *cxt);
 extern const char *fdisk_error_name(enum fdisk_error errcode);
 extern void fdisk_error_fatal(struct fdisk_context *cxt, enum fdisk_error errcode);
+extern int fdisk_label_change(struct fdisk_context *cxt, const char *name);
 
 /* prototypes for fdisk.c */
 extern char *disk_device, *line_ptr;
fdisks/fdiskdoslabel.c +1 -0
index 535afdc..9b9b23a 100644
@@ -230,6 +230,7 @@ void create_doslabel(struct fdisk_context *cxt)
 
 	dos_init(cxt);
 	fdisk_mbr_zeroize(cxt);
+	fdisk_label_change(cxt, "dos");
 	set_all_unchanged();
 	set_changed(0);
 
fdisks/fdisksgilabel.c +2 -0
index e38d98f..6001038 100644
@@ -780,6 +780,8 @@ create_sgilabel(struct fdisk_context *cxt)
 		}
 
 	fdisk_mbr_zeroize(cxt);
+	fdisk_label_change(cxt, "sgi");
+
 	sgilabel->magic = SSWAP32(SGI_LABEL_MAGIC);
 	sgilabel->boot_part = SSWAP16(0);
 	sgilabel->swap_part = SSWAP16(1);
fdisks/fdisksunlabel.c +1 -0
index 4123806..b63335c 100644
@@ -161,6 +161,7 @@ void create_sunlabel(struct fdisk_context *cxt)
 
 	init();
 	fdisk_mbr_zeroize(cxt);
+	fdisk_label_change(cxt, "sun");
 
 	sunlabel->magic = SSWAP16(SUN_LABEL_MAGIC);
 	sunlabel->sanity = SSWAP32(SUN_LABEL_SANE);
fdisks/utils.c +37 -0
index cf9484c..48dedfb 100644
@@ -44,6 +44,43 @@ static const struct fdisk_label *labels[] =
 	&mac_label,
 };
 
+/**
+ * fdisk_label_change:
+ * @cxt: fdisk context
+ * @name: new label name
+ *
+ * Updates the disk label type to the one specified by @name.
+ *
+ * Returns 0 on success, otherwise, a corresponding error.
+ */
+int fdisk_label_change(struct fdisk_context *cxt, const char *name)
+{
+	int i;
+
+	if (!cxt || !cxt->label || !name)
+		return FDISK_ERROR_UNKNOWN;
+
+	/* not really changing the label */
+	if (!strncmp(name, cxt->label->name, strlen(name)))
+		goto done;
+
+	for (i = 0; i < ARRAY_SIZE(labels); i++) {
+		if (strncmp(name, labels[i]->name, strlen(name)))
+			continue;
+	
+		/* found the new label */
+		memset(cxt->label, 0, sizeof(struct fdisk_label));
+		memcpy(cxt->label, labels[i], sizeof(struct fdisk_label));
+		DBG(LABEL, dbgprint("changing to a %s label\n", labels[i]->name));
+		goto done;
+	}
+
+	/* couldn't find the requested label type */
+	return FDISK_ERROR_NOLABEL;
+done:
+	return 0;
+}
+
 static int __probe_labels(struct fdisk_context *cxt)
 {
 	int i, rc = 0;
Signature · 9 lines
1.7.4.1




--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Petr

#9 lore raw
Quoted 8 lines · Davidlohr Bueso
Quoted 7 lines · Petr Uzel
Quoted 5 lines · Davidlohr Bueso

From: Davidlohr Bueso <[email protected]>

A new fdisk_label_change() function is added for situations when the disk label is changed (ie: creating a new sun label). This function only updates the label pointer in the context to use the newly specified label type.

Quoted 6 lines
Quoted 2 lines

Why can't we just call __probe_labels() after creating new label, e.g. in soon-to-be introduced fdisk_label_create()?

Because this way we give users extra functionality in the API. There should be a way of changing label types on the fly, and since we don't export __probe_labels(), this is the other option.

What operation could change label type besides creating a new label?

Quoted 135 lines

Thanks,
Davidlohr

Quoted 122 lines
Quoted 122 lines
---
 fdisks/fdisk.h         |    2 ++
 fdisks/fdiskdoslabel.c |    1 +
 fdisks/fdisksgilabel.c |    2 ++
 fdisks/fdisksunlabel.c |    1 +
 fdisks/utils.c         |   37 +++++++++++++++++++++++++++++++++++++
 5 files changed, 43 insertions(+), 0 deletions(-)
fdisks/fdisk.h +2 -0
index d716824..d7e85f5 100644
@@ -104,6 +104,7 @@ enum fdisk_error {
 	FDISK_ERROR_WRITE,
 	FDISK_ERROR_IOCTL,
 	FDISK_ERROR_PROBE,
+	FDISK_ERROR_NOLABEL,
 	FDISK_ERROR_UNKNOWN
 };
 
@@ -164,6 +165,7 @@ extern void fdisk_mbr_zeroize(struct fdisk_context *cxt);
 extern void fdisk_geom_set_cyls(struct fdisk_context *cxt);
 extern const char *fdisk_error_name(enum fdisk_error errcode);
 extern void fdisk_error_fatal(struct fdisk_context *cxt, enum fdisk_error errcode);
+extern int fdisk_label_change(struct fdisk_context *cxt, const char *name);
 
 /* prototypes for fdisk.c */
 extern char *disk_device, *line_ptr;
fdisks/fdiskdoslabel.c +1 -0
index 535afdc..9b9b23a 100644
@@ -230,6 +230,7 @@ void create_doslabel(struct fdisk_context *cxt)
 
 	dos_init(cxt);
 	fdisk_mbr_zeroize(cxt);
+	fdisk_label_change(cxt, "dos");
 	set_all_unchanged();
 	set_changed(0);
 
fdisks/fdisksgilabel.c +2 -0
index e38d98f..6001038 100644
@@ -780,6 +780,8 @@ create_sgilabel(struct fdisk_context *cxt)
 		}
 
 	fdisk_mbr_zeroize(cxt);
+	fdisk_label_change(cxt, "sgi");
+
 	sgilabel->magic = SSWAP32(SGI_LABEL_MAGIC);
 	sgilabel->boot_part = SSWAP16(0);
 	sgilabel->swap_part = SSWAP16(1);
fdisks/fdisksunlabel.c +1 -0
index 4123806..b63335c 100644
@@ -161,6 +161,7 @@ void create_sunlabel(struct fdisk_context *cxt)
 
 	init();
 	fdisk_mbr_zeroize(cxt);
+	fdisk_label_change(cxt, "sun");
 
 	sunlabel->magic = SSWAP16(SUN_LABEL_MAGIC);
 	sunlabel->sanity = SSWAP32(SUN_LABEL_SANE);
fdisks/utils.c +37 -0
index cf9484c..48dedfb 100644
@@ -44,6 +44,43 @@ static const struct fdisk_label *labels[] =
 	&mac_label,
 };
 
+/**
+ * fdisk_label_change:
+ * @cxt: fdisk context
+ * @name: new label name
+ *
+ * Updates the disk label type to the one specified by @name.
+ *
+ * Returns 0 on success, otherwise, a corresponding error.
+ */
+int fdisk_label_change(struct fdisk_context *cxt, const char *name)
+{
+	int i;
+
+	if (!cxt || !cxt->label || !name)
+		return FDISK_ERROR_UNKNOWN;
+
+	/* not really changing the label */
+	if (!strncmp(name, cxt->label->name, strlen(name)))
+		goto done;
+
+	for (i = 0; i < ARRAY_SIZE(labels); i++) {
+		if (strncmp(name, labels[i]->name, strlen(name)))
+			continue;
+	
+		/* found the new label */
+		memset(cxt->label, 0, sizeof(struct fdisk_label));
+		memcpy(cxt->label, labels[i], sizeof(struct fdisk_label));
+		DBG(LABEL, dbgprint("changing to a %s label\n", labels[i]->name));
+		goto done;
+	}
+
+	/* couldn't find the requested label type */
+	return FDISK_ERROR_NOLABEL;
+done:
+	return 0;
+}
+
 static int __probe_labels(struct fdisk_context *cxt)
 {
 	int i, rc = 0;
Signature · 1 line
1.7.4.1
Quoted 7 lines
Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Petr

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Petr

Signature · 2 lines
Petr Uzel
IRC: ptr_uzl @ freenode
maintainer
#10 lore raw
In reply to Davidlohr Bueso
Quoted 1 line · Davidlohr Bueso
 5 files changed, 43 insertions(+), 0 deletions(-)

Not applied, it seems unnecessary.

I have temporary added fdisk_create_default_disklabel, but the final code (the current master) contains your fdisk_create_disklabel() where NULL name means a default disk label.

I have renamed many functions :-) It seems better to use

   fdisk_create_disklabel()   than  fdisk_label_create()
   fdisk_add_partition()      than  fdisk_label_partition_new()

.. etc.

fdisks/utils.c static const struct fdisk_label *labels[] = · line 65

Quoted 3 lines
fdisks/utils.c +3 -0
+	/* not really changing the label */
+	if (!strncmp(name, cxt->label->name, strlen(name)))
+		goto done;

BTW, I don't see a problem with strings -- very probably we will use strings in user interface, and add extra layer to translate strings to IDs seems like unnecessary optimization and complexity.

fdisks/utils.c static const struct fdisk_label *labels[] = · line 76

Quoted 10 lines
fdisks/utils.c +10 -0
+	for (i = 0; i < ARRAY_SIZE(labels); i++) {
+		if (strncmp(name, labels[i]->name, strlen(name)))
+			continue;
+	
+		/* found the new label */
+		memset(cxt->label, 0, sizeof(struct fdisk_label));
+		memcpy(cxt->label, labels[i], sizeof(struct fdisk_label));
+		DBG(LABEL, dbgprint("changing to a %s label\n", labels[i]->name));
+		goto done;
+	}

Oh no, let's use labels[] as constant array and cxt->label as const struct pointer. The list of functions does not have to be modifiable, and

    cxt->label = labels[i];

is definitely better than memcpy().

Note that it's will probably necessary to add

    void *labeldata;

to fdisk_context for label private data. (For example to kill MBRbuffer_changed or another DOS specific global variables.)

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com