diff options
author | Karel Zak | 2014-09-09 13:04:57 +0200 |
---|---|---|
committer | Karel Zak | 2014-10-07 14:55:31 +0200 |
commit | c3bc74835259bb1d6443ea52f875ce3c179dff20 (patch) | |
tree | 2f8be35f22336210b7b09adc1272fd360fa03a07 /disk-utils | |
parent | sfdisk: implement command_fdisk() (diff) | |
download | kernel-qcow2-util-linux-c3bc74835259bb1d6443ea52f875ce3c179dff20.tar.gz kernel-qcow2-util-linux-c3bc74835259bb1d6443ea52f875ce3c179dff20.tar.xz kernel-qcow2-util-linux-c3bc74835259bb1d6443ea52f875ce3c179dff20.zip |
libfdisk: return partno when add new partition
* improve the way how sfdisk report results
* the API change simplify applications
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'disk-utils')
-rw-r--r-- | disk-utils/cfdisk.c | 2 | ||||
-rw-r--r-- | disk-utils/fdisk-menu.c | 2 | ||||
-rw-r--r-- | disk-utils/sfdisk.c | 39 |
3 files changed, 30 insertions, 13 deletions
diff --git a/disk-utils/cfdisk.c b/disk-utils/cfdisk.c index 13e98b4b2..4101827f1 100644 --- a/disk-utils/cfdisk.c +++ b/disk-utils/cfdisk.c @@ -1829,7 +1829,7 @@ static int main_menu_action(struct cfdisk *cf, int key) fdisk_partition_set_start(npa, start); fdisk_partition_partno_follow_default(npa, 1); /* add to disk label -- libfdisk will ask for missing details */ - rc = fdisk_add_partition(cf->cxt, npa); + rc = fdisk_add_partition(cf->cxt, npa, NULL); fdisk_unref_partition(npa); if (rc == 0) ref = 1; diff --git a/disk-utils/fdisk-menu.c b/disk-utils/fdisk-menu.c index efd8dc1c6..e9bdc4554 100644 --- a/disk-utils/fdisk-menu.c +++ b/disk-utils/fdisk-menu.c @@ -520,7 +520,7 @@ static int generic_menu_cb(struct fdisk_context **cxt0, list_partition_types(cxt); break; case 'n': - rc = fdisk_add_partition(cxt, NULL); + rc = fdisk_add_partition(cxt, NULL, NULL); break; case 't': change_partition_type(cxt); diff --git a/disk-utils/sfdisk.c b/disk-utils/sfdisk.c index 8dc102506..5381d43dc 100644 --- a/disk-utils/sfdisk.c +++ b/disk-utils/sfdisk.c @@ -317,7 +317,7 @@ static int command_dump(struct sfdisk *sf, int argc, char **argv) static void sfdisk_print_partition(struct sfdisk *sf, int n) { - struct fdisk_partition *pa; + struct fdisk_partition *pa = NULL; char *data; assert(sf); @@ -355,6 +355,7 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv) struct fdisk_table *tb = NULL; const char *devname = NULL, *label; char buf[BUFSIZ]; + size_t next_partno = (size_t) -1; if (argc) devname = argv[0]; @@ -387,27 +388,38 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv) printf(_("\nInput in the following format; absent fields get a default value.\n" "<start> <size> <type [uuid, hex or E,S,L,X]> <bootable [-,*]>\n" - "Usually you only need to specify <start> and <size>\n")); + "Usually you only need to specify <start> and <size>\n\n")); if (!fdisk_has_label(sf->cxt)) - printf(_("\nsfdisk is going to create a new '%s' disk label.\n" + printf(_("sfdisk is going to create a new '%s' disk label.\n" "Use 'label: <name>' before you define a first partition\n" - "to override the default.\n"), label); + "to override the default.\n\n"), label); tb = fdisk_script_get_table(dp); + assert(tb); do { - size_t nparts = fdisk_table_get_nents(tb); + size_t nparts; char *partname; - partname = fdisk_partname(devname, nparts + 1); + DBG(PARSE, ul_debug("<---next-line--->")); + if (next_partno == (size_t) -1) + next_partno = fdisk_table_get_nents(tb); + + partname = fdisk_partname(devname, next_partno + 1); if (!partname) err(EXIT_FAILURE, _("failed to allocate partition name")); + fflush(stdout); printf("%s: ", partname); free(partname); rc = fdisk_script_read_line(dp, stdin, buf, sizeof(buf)); - if (rc) { + + if (rc == 1) { /* end of file */ + rc = 0; + break; + } else if (rc < 0) { + DBG(PARSE, ul_debug("script parsing failed, trying sfdisk specific commands")); buf[sizeof(buf) - 1] = '\0'; if (strcmp(buf, "print") == 0) @@ -424,18 +436,23 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv) nparts = fdisk_table_get_nents(tb); if (nparts) { + size_t cur_partno; struct fdisk_partition *pa = fdisk_table_get_partition(tb, nparts - 1); + fputc('\n', stdout); + + assert(pa); if (!created) { /* create a disklabel */ rc = fdisk_apply_script_headers(sf->cxt, dp); created = !rc; } if (!rc) /* cretate partition */ - rc = fdisk_add_partition(sf->cxt, pa); + rc = fdisk_add_partition(sf->cxt, pa, &cur_partno); - if (!rc) /* sucess, print reult */ - sfdisk_print_partition(sf, nparts - 1); - else if (pa) /* error, drop partition from script */ + if (!rc) { /* success, print reult */ + sfdisk_print_partition(sf, cur_partno); + next_partno = cur_partno + 1; + } else if (pa) /* error, drop partition from script */ fdisk_table_remove_partition(tb, pa); } else printf(_("OK\n")); /* probably added script header */ |