From: Davidlohr Bueso <dave@gnu.org>
To: Karel Zak <kzak@redhat.com>, Petr Uzel <petr.uzel@suse.cz>
Cc: util-linux <util-linux@vger.kernel.org>
Subject: [PATCH 03/10] fdisk: API: add fdisk_label_change
Date: Sun, 22 Jul 2012 19:05:04 +0200 [thread overview]
Message-ID: <1342976704.2863.13.camel@offbook> (raw)
From: Davidlohr Bueso <dave@gnu.org>
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.
Signed-off-by: Davidlohr Bueso <dave@gnu.org>
---
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(-)
diff --git a/fdisks/fdisk.h b/fdisks/fdisk.h
index d716824..d7e85f5 100644
--- a/fdisks/fdisk.h
+++ b/fdisks/fdisk.h
@@ -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;
diff --git a/fdisks/fdiskdoslabel.c b/fdisks/fdiskdoslabel.c
index 535afdc..9b9b23a 100644
--- a/fdisks/fdiskdoslabel.c
+++ b/fdisks/fdiskdoslabel.c
@@ -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);
diff --git a/fdisks/fdisksgilabel.c b/fdisks/fdisksgilabel.c
index e38d98f..6001038 100644
--- a/fdisks/fdisksgilabel.c
+++ b/fdisks/fdisksgilabel.c
@@ -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);
diff --git a/fdisks/fdisksunlabel.c b/fdisks/fdisksunlabel.c
index 4123806..b63335c 100644
--- a/fdisks/fdisksunlabel.c
+++ b/fdisks/fdisksunlabel.c
@@ -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);
diff --git a/fdisks/utils.c b/fdisks/utils.c
index cf9484c..48dedfb 100644
--- a/fdisks/utils.c
+++ b/fdisks/utils.c
@@ -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;
--
1.7.4.1
next reply other threads:[~2012-07-22 17:05 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-07-22 17:05 Davidlohr Bueso [this message]
2012-07-24 9:47 ` [PATCH 03/10] fdisk: API: add fdisk_label_change Bernhard Voelker
2012-07-24 9:56 ` Petr Uzel
2012-07-24 10:32 ` Bernhard Voelker
2012-07-24 10:39 ` Davidlohr Bueso
2012-07-24 10:42 ` Petr Uzel
2012-07-24 10:47 ` Bernhard Voelker
2012-07-24 9:52 ` Petr Uzel
2012-07-24 10:41 ` Davidlohr Bueso
2012-07-24 10:52 ` Petr Uzel
2012-07-24 11:35 ` Karel Zak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1342976704.2863.13.camel@offbook \
--to=dave@gnu.org \
--cc=kzak@redhat.com \
--cc=petr.uzel@suse.cz \
--cc=util-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).