struct fdisk_context *fdisk_new_context(void) { struct fdisk_context *cxt; cxt = calloc(1, sizeof(*cxt)); if (!cxt) return NULL; DBG(LABEL, dbgprint("new context %p allocated", cxt)); cxt->dev_fd = -1; /* * 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_bsd_label(cxt); cxt->labels[ cxt->nlabels++ ] = fdisk_new_sgi_label(cxt); cxt->labels[ cxt->nlabels++ ] = fdisk_new_sun_label(cxt); return cxt; }
/** * fdisk_new_context: * * Returns: newly allocated libfdisk handler */ struct fdisk_context *fdisk_new_context(void) { struct fdisk_context *cxt; cxt = calloc(1, sizeof(*cxt)); if (!cxt) return NULL; DBG(CXT, ul_debugobj(cxt, "alloc")); cxt->dev_fd = -1; cxt->refcount = 1; /* * 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_bsd_label(cxt); cxt->labels[ cxt->nlabels++ ] = fdisk_new_sgi_label(cxt); cxt->labels[ cxt->nlabels++ ] = fdisk_new_sun_label(cxt); return cxt; }
/** * fdisk_new_nested_context: * @parent: parental context * @name: optional label name (e.g. "bsd") * * This is supported for MBR+BSD and GPT+pMBR only. * * Returns: new context for nested partiton table. */ 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); cxt = calloc(1, sizeof(*cxt)); if (!cxt) return NULL; DBG(CXT, ul_debugobj(parent, "alloc nested [%p]", cxt)); cxt->dev_fd = parent->dev_fd; cxt->refcount = 1; 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->firstsector = parent->firstsector; cxt->ask_cb = parent->ask_cb; cxt->ask_data = parent->ask_data; cxt->geom = parent->geom; if (name) { if (strcmp(name, "bsd") == 0) lb = cxt->labels[ cxt->nlabels++ ] = fdisk_new_bsd_label(cxt); else if (strcmp(name, "dos") == 0) lb = cxt->labels[ cxt->nlabels++ ] = fdisk_new_dos_label(cxt); } if (lb) { DBG(CXT, ul_debugobj(cxt, "probing for nested %s", lb->name)); cxt->label = lb; if (lb->op->probe(cxt) == 1) __fdisk_switch_label(cxt, lb); else { DBG(CXT, ul_debugobj(cxt, "not found %s label", lb->name)); if (lb->op->deinit) lb->op->deinit(lb); cxt->label = NULL; } } return cxt; }
/** * fdisk_new_nested_context: * @parent: parental context * @name: optional label name (e.g. "bsd") * * Create a new nested fdisk context for nested disk labels (e.g. BSD or PMBR). * The function also probes for the nested label on the device if device is * already assigned to parent. * * The new context is initialized according to @parent and both context shares * some settings and file descriptor to the device. The child propagate some * changes (like fdisk_assign_device()) to parent, but it does not work * vice-versa. The behavior is undefined if you assign another device to * parent. * * Returns: new context for nested partition table. */ 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); cxt = calloc(1, sizeof(*cxt)); if (!cxt) return NULL; DBG(CXT, ul_debugobj(parent, "alloc nested [%p] [name=%s]", cxt, name)); cxt->refcount = 1; fdisk_ref_context(parent); cxt->parent = parent; if (init_nested_from_parent(cxt, 1) != 0) { cxt->parent = NULL; fdisk_unref_context(cxt); return NULL; } if (name) { if (strcasecmp(name, "bsd") == 0) lb = cxt->labels[ cxt->nlabels++ ] = fdisk_new_bsd_label(cxt); else if (strcasecmp(name, "dos") == 0 || strcasecmp(name, "mbr") == 0) lb = cxt->labels[ cxt->nlabels++ ] = fdisk_new_dos_label(cxt); } if (lb && parent->dev_fd >= 0) { DBG(CXT, ul_debugobj(cxt, "probing for nested %s", lb->name)); cxt->label = lb; if (lb->op->probe(cxt) == 1) __fdisk_switch_label(cxt, lb); else { DBG(CXT, ul_debugobj(cxt, "not found %s label", lb->name)); if (lb->op->deinit) lb->op->deinit(lb); cxt->label = NULL; } } return cxt; }