From: Karel Zak Date: Tue, 5 Mar 2013 14:13:26 +0000 (+0100) Subject: libfdisk: add support for nested contexts X-Git-Tag: v2.23-rc1~92 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=01b207133e46517b6930785834e1ab310ad3f7cf;p=thirdparty%2Futil-linux.git libfdisk: add support for nested contexts Signed-off-by: Karel Zak --- diff --git a/libfdisk/src/context.c b/libfdisk/src/context.c index 352f03ac35..139932620b 100644 --- a/libfdisk/src/context.c +++ b/libfdisk/src/context.c @@ -21,7 +21,6 @@ struct fdisk_context *fdisk_new_context(void) 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); @@ -29,6 +28,42 @@ struct fdisk_context *fdisk_new_context(void) return cxt; } +/* only BSD is supported now */ +struct fdisk_context *fdisk_new_nested_context(struct fdisk_context *parent, + const char *name) +{ + struct fdisk_context *cxt; + + assert(parent); + assert(name); + + cxt = calloc(1, sizeof(*cxt)); + if (!cxt) + return NULL; + + DBG(LABEL, dbgprint("new context %p allocated", cxt)); + cxt->dev_fd = parent->dev_fd; + cxt->parent = parent; + + cxt->io_size = parent->io_size; + cxt->optimal_io_size = parent->optimal_io_size; + cxt->min_io_size = parent->min_io_size; + cxt->phy_sector_size = parent->phy_sector_size; + cxt->sector_size = parent->sector_size; + cxt->alignment_offset = parent->alignment_offset; + cxt->grain = parent->grain; + cxt->first_lba = parent->first_lba; + cxt->total_sectors = parent->total_sectors; + + cxt->geom = parent->geom; + + if (strcmp(name, "bsd") == 0) + cxt->label = cxt->labels[ cxt->nlabels++ ] = fdisk_new_bsd_label(cxt); + + return cxt; +} + + /* * Returns the current label if no name specified. */ @@ -77,7 +112,7 @@ static void reset_context(struct fdisk_context *cxt) fdisk_deinit_label(cxt->labels[i]); /* free device specific stuff */ - if (cxt->dev_fd > -1) + if (!cxt->parent && cxt->dev_fd > -1) close(cxt->dev_fd); free(cxt->dev_path); free(cxt->firstsector); diff --git a/libfdisk/src/fdiskP.h b/libfdisk/src/fdiskP.h index 7a528eb57b..89a96d9647 100644 --- a/libfdisk/src/fdiskP.h +++ b/libfdisk/src/fdiskP.h @@ -274,6 +274,8 @@ struct fdisk_context { int (*ask_cb)(struct fdisk_context *, struct fdisk_ask *, void *); /* fdisk dialogs callback */ void *ask_data; /* ask_cb() data */ + + struct fdisk_context *parent; /* for nested PT */ }; /* context.c */ diff --git a/libfdisk/src/libfdisk.h b/libfdisk/src/libfdisk.h index 92c03b6932..a08a2afa55 100644 --- a/libfdisk/src/libfdisk.h +++ b/libfdisk/src/libfdisk.h @@ -69,6 +69,8 @@ extern void fdisk_init_debug(int mask); /* context.h */ extern struct fdisk_context *fdisk_new_context(void); +extern struct fdisk_context *fdisk_new_nested_context( + struct fdisk_context *parent, const char *name); extern void fdisk_free_context(struct fdisk_context *cxt); extern int fdisk_context_set_ask(struct fdisk_context *cxt,