summaryrefslogtreecommitdiffstats
path: root/libfdisk/src/context.c
diff options
context:
space:
mode:
authorKarel Zak2012-12-11 12:56:27 +0100
committerKarel Zak2013-03-11 12:47:29 +0100
commit0c5d095e46306ed4d6009f56e0339857d733d72e (patch)
treeea751328b6a158c199c7595faf3200a8039dd176 /libfdisk/src/context.c
parentlibfdisk: cleanup the rest of fdisks/utils.c stuff (diff)
downloadkernel-qcow2-util-linux-0c5d095e46306ed4d6009f56e0339857d733d72e.tar.gz
kernel-qcow2-util-linux-0c5d095e46306ed4d6009f56e0339857d733d72e.tar.xz
kernel-qcow2-util-linux-0c5d095e46306ed4d6009f56e0339857d733d72e.zip
libfdisk: split label and label operations
Changes: - fdisk_label is a private label driver struct - generic header of the fdisk_label points to fdisk_label_operations - the private fdisk_label stuff is always allocated for all drivers during fdisk_context initialization - context->labels[] contains pointers to all supported labels (drivers) - context->label is a pointer to the currently used label This change allows to: - store label specific global settings (e.g. dos compatible mode, display units, ...) independently on the current label - add label specific function to the API Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libfdisk/src/context.c')
-rw-r--r--libfdisk/src/context.c66
1 files changed, 63 insertions, 3 deletions
diff --git a/libfdisk/src/context.c b/libfdisk/src/context.c
index 302020590..83420a977 100644
--- a/libfdisk/src/context.c
+++ b/libfdisk/src/context.c
@@ -1,8 +1,54 @@
#include "fdiskP.h"
+static struct fdisk_context *fdisk_new_context(void)
+{
+ struct fdisk_context *cxt;
+ size_t i;
+
+ cxt = calloc(1, sizeof(*cxt));
+ if (!cxt)
+ return NULL;
+
+ /*
+ * Allocate label specific structs.
+ *
+ * This is necessary (for example) to store label specific
+ * context setting.
+ */
+ cxt->labels[ cxt->nlabels++ ] = fdisk_new_gpt_label(cxt);
+ cxt->labels[ cxt->nlabels++ ] = fdisk_new_dos_label(cxt);
+ cxt->labels[ cxt->nlabels++ ] = fdisk_new_aix_label(cxt);
+ cxt->labels[ cxt->nlabels++ ] = fdisk_new_bsd_label(cxt);
+ cxt->labels[ cxt->nlabels++ ] = fdisk_new_mac_label(cxt);
+ cxt->labels[ cxt->nlabels++ ] = fdisk_new_sgi_label(cxt);
+ cxt->labels[ cxt->nlabels++ ] = fdisk_new_sun_label(cxt);
+
+ DBG(CONTEXT, dbgprint("supported labels:"));
+ for (i = 0; i < cxt->nlabels; i++) {
+ DBG(CONTEXT, dbgprint(" %s", cxt->labels[i]->name));
+ cxt->labels[i]->cxt = cxt;
+ }
+
+ return cxt;
+}
+
+struct fdisk_label *fdisk_context_get_label(struct fdisk_context *cxt, const char *name)
+{
+ size_t i;
+
+ assert(cxt);
+
+ for (i = 0; i < cxt->nlabels; i++)
+ if (strcmp(cxt->labels[i]->name, name) == 0)
+ return cxt->labels[i];
+
+ DBG(LABEL, dbgprint("failed to found %s label driver\n", name));
+ return NULL;
+}
+
/**
- * fdisk_new_context:
+ * fdisk_new_context_from_filename:
* @fname: path to the device to be handled
* @readonly: how to open the device
*
@@ -25,7 +71,7 @@ struct fdisk_context *fdisk_new_context_from_filename(const char *fname, int rea
readonly = 1;
}
- cxt = calloc(1, sizeof(*cxt));
+ cxt = fdisk_new_context();
if (!cxt)
goto fail;
@@ -67,12 +113,26 @@ fail:
*/
void fdisk_free_context(struct fdisk_context *cxt)
{
+ int i;
+
if (!cxt)
return;
DBG(CONTEXT, dbgprint("freeing context %p for %s", cxt, cxt->dev_path));
- close(cxt->dev_fd);
+ if (cxt->dev_fd > -1)
+ close(cxt->dev_fd);
free(cxt->dev_path);
free(cxt->firstsector);
+
+ /* deallocate label's private stuff */
+ for (i = 0; i < cxt->nlabels; i++) {
+ if (!cxt->labels[i])
+ continue;
+ if (cxt->labels[i]->op->free)
+ cxt->labels[i]->op->free(cxt->labels[i]);
+ else
+ free(cxt->labels[i]);
+ }
+
free(cxt);
}