diff options
author | Karel Zak | 2012-12-11 14:41:57 +0100 |
---|---|---|
committer | Karel Zak | 2013-03-11 12:47:29 +0100 |
commit | 4e0e82531a34691ffa859ffc68df6ada71faaeaa (patch) | |
tree | 906a208c2eee723612c7d1dea1173b7356a980b4 /fdisks | |
parent | libfdisk: split label and label operations (diff) | |
download | kernel-qcow2-util-linux-4e0e82531a34691ffa859ffc68df6ada71faaeaa.tar.gz kernel-qcow2-util-linux-4e0e82531a34691ffa859ffc68df6ada71faaeaa.tar.xz kernel-qcow2-util-linux-4e0e82531a34691ffa859ffc68df6ada71faaeaa.zip |
libfdisk: add fdisk_new_context(), reuse context
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'fdisks')
-rw-r--r-- | fdisks/fdisk.c | 32 | ||||
-rw-r--r-- | fdisks/gpt.c | 51 |
2 files changed, 45 insertions, 38 deletions
diff --git a/fdisks/fdisk.c b/fdisks/fdisk.c index 866d8fe21..c771f6e6d 100644 --- a/fdisks/fdisk.c +++ b/fdisks/fdisk.c @@ -1387,12 +1387,10 @@ static int is_ide_cdrom_or_tape(char *device) } /* Print disk geometry and partition table of a specified device (-l option) */ -static void print_partition_table_from_option(char *device, unsigned long sector_size) +static void print_partition_table_from_option(struct fdisk_context *cxt, + char *device, unsigned long sector_size) { - struct fdisk_context *cxt; - - cxt = fdisk_new_context_from_filename(device, 1); /* read-only */ - if (!cxt) + if (fdisk_context_assign_device(cxt, device, 1) != 0) /* read-only */ err(EXIT_FAILURE, _("cannot open %s"), device); if (sector_size) /* passed -b option, override autodiscovery */ @@ -1413,9 +1411,6 @@ static void print_partition_table_from_option(char *device, unsigned long sector btrydev(cxt); } else list_table(cxt, 0); - - fdisk_free_context(cxt); - cxt = NULL; } /* @@ -1423,7 +1418,8 @@ static void print_partition_table_from_option(char *device, unsigned long sector * try all things in /proc/partitions that look like a full disk */ static void -print_all_partition_table_from_option(unsigned long sector_size) +print_all_partition_table_from_option(struct fdisk_context *cxt, + unsigned long sector_size) { FILE *procpt; char line[128 + 1], ptname[128 + 1], devname[256]; @@ -1445,7 +1441,7 @@ print_all_partition_table_from_option(unsigned long sector_size) char *cn = canonicalize_path(devname); if (cn) { if (!is_ide_cdrom_or_tape(cn)) - print_partition_table_from_option(cn, sector_size); + print_partition_table_from_option(cxt, cn, sector_size); free(cn); } } @@ -1593,13 +1589,18 @@ int main(int argc, char **argv) { int c, optl = 0, opts = 0; unsigned long sector_size = 0; - struct fdisk_context *cxt = NULL; + struct fdisk_context *cxt; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); + fdisk_init_debug(0); + cxt = fdisk_new_context(); + if (!cxt) + err(EXIT_FAILURE, _("failed to allocate libfdisk context")); + while ((c = getopt(argc, argv, "b:c::C:hH:lsS:u::vV")) != -1) { switch (c) { case 'b': @@ -1657,7 +1658,6 @@ int main(int argc, char **argv) } } - fdisk_init_debug(0); if (sector_size && argc-optind != 1) printf(_("Warning: the -b (set sector size) option should" @@ -1668,9 +1668,9 @@ int main(int argc, char **argv) if (argc > optind) { int k; for (k = optind; k < argc; k++) - print_partition_table_from_option(argv[k], sector_size); + print_partition_table_from_option(cxt, argv[k], sector_size); } else - print_all_partition_table_from_option(sector_size); + print_all_partition_table_from_option(cxt, sector_size); exit(EXIT_SUCCESS); } @@ -1692,8 +1692,7 @@ int main(int argc, char **argv) if (argc-optind != 1) usage(stderr); - cxt = fdisk_new_context_from_filename(argv[optind], 0); - if (!cxt) + if (fdisk_context_assign_device(cxt, argv[optind], 0) != 0) err(EXIT_FAILURE, _("cannot open %s"), argv[optind]); if (sector_size) /* passed -b option, override autodiscovery */ @@ -1714,5 +1713,6 @@ int main(int argc, char **argv) command_prompt(cxt); + fdisk_free_context(cxt); return 0; } diff --git a/fdisks/gpt.c b/fdisks/gpt.c index 4f70f962d..f9ba9f072 100644 --- a/fdisks/gpt.c +++ b/fdisks/gpt.c @@ -248,6 +248,8 @@ static struct fdisk_parttype gpt_parttypes[] = DEF_GUID("85D5E45C-237C-11E1-B4B3-E89A8F7FC3A7", N_("MidnightBSD Vinum")), }; +/* FIXME: move these global variables to fdisk_gpt_label */ + /* primary GPT header */ static struct gpt_header *pheader = NULL; /* backup GPT header */ @@ -1009,21 +1011,6 @@ static void gpt_init(struct fdisk_context *cxt) partitions = le32_to_cpu(pheader->npartition_entries); } -/* - * Deinitialize fdisk-specific variables - */ -static void gpt_deinit(struct fdisk_context *cxt) -{ - free(ents); - free(pheader); - free(bheader); - ents = NULL; - pheader = NULL; - bheader = NULL; - - cxt->disklabel = FDISK_DISKLABEL_ANY; - partitions = 0; -} static int gpt_probe_label(struct fdisk_context *cxt) { @@ -1062,6 +1049,7 @@ static int gpt_probe_label(struct fdisk_context *cxt) return 1; failed: + DBG(LABEL, dbgprint("GPT probe failed")); return 0; } @@ -1580,17 +1568,15 @@ static int gpt_create_disklabel(struct fdisk_context *cxt) int rc = 0; ssize_t entry_sz = 0; + /* labe private stuff has to be empty, see gpt_deinit() */ + assert(pheader == NULL); + assert(bheader == NULL); + /* - * Reset space or clear data from headers, pt entries and - * protective MBR. Big fat warning: any previous content is - * overwritten, so ask users to be sure!. - * * When no header, entries or pmbr is set, we're probably * dealing with a new, empty disk - so always allocate memory * to deal with the data structures whatever the case is. */ - gpt_deinit(cxt); - rc = gpt_mknew_pmbr(cxt); if (rc < 0) goto done; @@ -1663,6 +1649,25 @@ static int gpt_set_partition_type(struct fdisk_context *cxt, int i, return 0; } +/* + * Deinitialize fdisk-specific variables + */ +static void gpt_deinit(struct fdisk_label *lb) +{ + free(ents); + free(pheader); + free(bheader); + + ents = NULL; + pheader = NULL; + bheader = NULL; + + if (lb->cxt && lb->cxt->disklabel == FDISK_DISKLABEL_GPT) + lb->cxt->disklabel = FDISK_DISKLABEL_ANY; + + partitions = 0; +} + static const struct fdisk_label_operations gpt_operations = { .probe = gpt_probe_label, @@ -1672,7 +1677,9 @@ static const struct fdisk_label_operations gpt_operations = .part_add = gpt_add_partition, .part_delete = gpt_delete_partition, .part_get_type = gpt_get_partition_type, - .part_set_type = gpt_set_partition_type + .part_set_type = gpt_set_partition_type, + + .deinit = gpt_deinit }; /* |