]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: add fdisk_disable_dialogs()
authorKarel Zak <kzak@redhat.com>
Wed, 19 Jul 2017 09:30:57 +0000 (11:30 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 19 Jul 2017 09:30:57 +0000 (11:30 +0200)
The default (for historical reasons) is to use dialog driven partitioning.
It's possible to avoid dialogs by fdisk_partition template for
fdisk_add_partition().

Unfortunately in some case (mostly DOS driver) it's not enough, because
we need to distinguish between logical and primary partitions. If we know
that dialogs are unwanted then we can default to primary partition, etc.

This function simplify semantic of the library for non-interactive
programs.

Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/docs/libfdisk-sections.txt
libfdisk/src/ask.c
libfdisk/src/context.c
libfdisk/src/dos.c
libfdisk/src/fdiskP.h
libfdisk/src/libfdisk.h.in
libfdisk/src/libfdisk.sym
libfdisk/src/partition.c

index 2674ed87b5a7ac55eefdf00e5e937b5197afabe0..68ca0927fdd0f4e9ec6ed05391e36eadb0d00c61 100644 (file)
@@ -292,6 +292,7 @@ fdisk_enable_bootbits_protection
 fdisk_enable_details
 fdisk_enable_listonly
 fdisk_enable_wipe
+fdisk_disable_dialogs
 fdisk_get_alignment_offset
 fdisk_get_collision
 fdisk_get_devfd
@@ -312,6 +313,7 @@ fdisk_get_sector_size
 fdisk_get_size_unit
 fdisk_get_unit
 fdisk_get_units_per_sector
+fdisk_has_dialogs
 fdisk_has_label
 fdisk_has_protected_bootbits
 fdisk_has_wipe
index 5a0952271f68359685730c86185057ebc5544188..babe040ad0c78366a0270745abfcb5283da80a66 100644 (file)
@@ -142,6 +142,14 @@ int fdisk_do_ask(struct fdisk_context *cxt, struct fdisk_ask *ask)
                                ask->type == FDISK_ASKTYPE_WARN  ? "warn" :
                                "?nothing?"));
 
+       if (!fdisk_has_dialogs(cxt) &&
+           !(ask->type == FDISK_ASKTYPE_INFO ||
+             ask->type == FDISK_ASKTYPE_WARNX ||
+             ask->type == FDISK_ASKTYPE_WARN)) {
+               DBG(ASK, ul_debugobj(ask, "dialogs disabled"));
+               return -EINVAL;
+       }
+
        if (!cxt->ask_cb) {
                DBG(ASK, ul_debugobj(ask, "no ask callback specified!"));
                return -EINVAL;
index c7e8a427616cf7c91f58eaea18faca5fea27d442..fb7ad4b443c6be31c119a7428d44f26d50ae820a 100644 (file)
@@ -344,6 +344,40 @@ int fdisk_enable_bootbits_protection(struct fdisk_context *cxt, int enable)
        cxt->protect_bootbits = enable ? 1 : 0;
        return 0;
 }
+/**
+ * fdisk_disable_dialogs
+ * @cxt: fdisk context
+ * @enable: 1 or 0
+ *
+ * The library uses dialog driven partitioning by default.
+ *
+ * Returns: 0 on success, < 0 on error.
+ *
+ * Since: 2.31
+ */
+int fdisk_disable_dialogs(struct fdisk_context *cxt, int disable)
+{
+       if (!cxt)
+               return -EINVAL;
+
+       cxt->no_disalogs = disable;
+       return 0;
+}
+
+/**
+ * fdisk_has_dialogs
+ * @cxt: fdisk context
+ *
+ * See fdisk_disable_dialogs()
+ *
+ * Returns: 1 if dialog driven partitioning enabled (default), or 0.
+ *
+ * Since: 2.31
+ */
+int fdisk_has_dialogs(struct fdisk_context *cxt)
+{
+       return cxt->no_disalogs == 0;
+}
 
 /**
  * fdisk_enable_wipe
index 12d014449682dbc8b8f0fbdfde595812d744a831..6fa9108edddb0240bd972d0900f6bce80233ec6c 100644 (file)
@@ -1597,7 +1597,7 @@ static int dos_add_partition(struct fdisk_context *cxt,
                        DBG(LABEL, ul_debug("DOS: pa template specifies partno>=4 for primary partition"));
                        return -EINVAL;
                }
-               if (pa->type && IS_EXTENDED(pa->type->code)) {
+               if (ext_pe && pa->type && IS_EXTENDED(pa->type->code)) {
                        fdisk_warnx(cxt, _("Extended partition already exists."));
                        return -EINVAL;
                }
@@ -1674,6 +1674,8 @@ static int dos_add_partition(struct fdisk_context *cxt,
                DBG(LABEL, ul_debug("DOS: primary impossible, add logical"));
                if (l->ext_offset) {
                        if (!pa || fdisk_partition_has_start(pa)) {
+                               /* See above case A); here we have start, but
+                                * out of extended partition */
                                const char *msg;
                                if (!free_primary)
                                        msg = _("All primary partitions are in use.");
@@ -1708,7 +1710,7 @@ static int dos_add_partition(struct fdisk_context *cxt,
                int c;
 
                /* the default layout for scripts is to create primary partitions */
-               if (cxt->script) {
+               if (cxt->script || !fdisk_has_dialogs(cxt)) {
                        rc = get_partition_unused_primary(cxt, pa, &res);
                        if (rc == 0)
                                rc = add_partition(cxt, res, pa);
index 434490edd5fffd64125a8eec457a257e3919285f..77ad7610da78e8c3a14541ec49ef3f04f66f7be8 100644 (file)
@@ -378,6 +378,7 @@ struct fdisk_context {
                     display_details : 1,       /* expert display mode */
                     protect_bootbits : 1,      /* don't zeroize first sector */
                     pt_collision : 1,          /* another PT detected by libblkid */
+                    no_disalogs : 1,           /* disable dialog-driven partititoning */
                     listonly : 1;              /* list partition, nothing else */
 
        char *collision;                        /* name of already existing FS/PT */
index eea02dba86138db2def196f3bf67ea5191c0b8a7..1c16fe4b34fc885ec52fd974c73da8612e100c8b 100644 (file)
@@ -187,6 +187,9 @@ int fdisk_is_readonly(struct fdisk_context *cxt);
 int fdisk_is_regfile(struct fdisk_context *cxt);
 int fdisk_device_is_used(struct fdisk_context *cxt);
 
+int fdisk_disable_dialogs(struct fdisk_context *cxt, int disable);
+int fdisk_has_dialogs(struct fdisk_context *cxt);
+
 int fdisk_enable_details(struct fdisk_context *cxt, int enable);
 int fdisk_is_details(struct fdisk_context *cxt);
 
index 7944538cf4eae00be246aec0f6828b49f11d5c3d..0d274847a02b7fef2b2c61d30ce721309e212db6 100644 (file)
@@ -287,4 +287,6 @@ FDISK_2.31 {
        fdisk_reassign_device;
        fdisk_device_is_used;
        fdisk_reread_changes;
+       fdisk_disable_dialogs;
+       fdisk_has_dialogs;
 } FDISK_2.30;
index daa73a4ed26bffa9ea185680f75b24a8cbc6e42e..bc52ea5735cbade7e2f1addbf872a56d34d5be6a 100644 (file)
@@ -705,9 +705,9 @@ int fdisk_partition_is_wholedisk(struct fdisk_partition *pa)
  * If @pa specified and partno-follow-default (see fdisk_partition_partno_follow_default())
  * enabled then returns next expected partno or -ERANGE on error.
  *
- * If @pa is NULL, or @pa does not specify any sepamntic for the next partno
+ * If @pa is NULL, or @pa does not specify any semantic for the next partno
  * then use Ask API to ask user for the next partno. In this case returns 1 if
- * no free partition avaialble.
+ * no free partition avaialble. If fdisk dialogs are disabled then returns -EINVAL.
  *
  * Returns: 0 on success, <0 on error, or 1 for non-free partno by Ask API.
  */
@@ -740,10 +740,12 @@ int fdisk_partition_next_partno(
                    fdisk_is_partition_used(cxt, pa->partno))
                        return -ERANGE;
                *n = pa->partno;
-       } else
+               return 0;
+
+       } else if (fdisk_has_dialogs(cxt))
                return fdisk_ask_partnum(cxt, n, 1);
 
-       return 0;
+       return -EINVAL;
 }
 
 static int probe_partition_content(struct fdisk_context *cxt, struct fdisk_partition *pa)