[sheepdog] [PATCH 3/4] dog: allow format string for confirm()

MORITA Kazutaka morita.kazutaka at gmail.com
Thu Aug 22 06:01:42 CEST 2013


From: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>

This prepares for the next patch.

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 dog/cluster.c |    6 ++++--
 dog/common.c  |   13 ++++++++++---
 dog/dog.h     |    2 +-
 dog/vdi.c     |   14 +++++++++-----
 4 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/dog/cluster.c b/dog/cluster.c
index c73c500..ccceb3a 100644
--- a/dog/cluster.c
+++ b/dog/cluster.c
@@ -92,8 +92,10 @@ static int cluster_format(int argc, char **argv)
 		return EXIT_FAILURE;
 	}
 
-	if (!no_vdi(vdi_inuse))
-		confirm(FORMAT_PRINT);
+	if (!no_vdi(vdi_inuse)) {
+		if (!confirm(FORMAT_PRINT))
+			return EXIT_SUCCESS;
+	}
 
 	gettimeofday(&tv, NULL);
 
diff --git a/dog/common.c b/dog/common.c
index 301b8bf..6e780e7 100644
--- a/dog/common.c
+++ b/dog/common.c
@@ -9,6 +9,8 @@
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <stdarg.h>
+
 #include "dog.h"
 #include "sha1.h"
 #include "sockfd_cache.h"
@@ -245,15 +247,20 @@ int do_generic_subcommand(struct subcommand *sub, int argc, char **argv)
 	return EXIT_FAILURE;
 }
 
-void confirm(const char *message)
+bool confirm(const char *fmt, ...)
 {
+	va_list ap;
 	char input[8] = "";
 	char *ret;
 
-	printf("%s", message);
+	va_start(ap, fmt);
+	vprintf(fmt, ap);
+	va_end(ap);
 	ret = fgets(input, sizeof(input), stdin);
 	if (ret == NULL || strncasecmp(input, "yes", 3) != 0)
-		exit(EXIT_SUCCESS);
+		return false;
+
+	return true;
 }
 
 void work_queue_wait(struct work_queue *q)
diff --git a/dog/dog.h b/dog/dog.h
index 226599a..0b1166c 100644
--- a/dog/dog.h
+++ b/dog/dog.h
@@ -77,7 +77,7 @@ int dog_exec_req(const uint8_t *addr, int port, struct sd_req *hdr,
 int send_light_req(struct sd_req *hdr, const uint8_t *addr, int port);
 int do_generic_subcommand(struct subcommand *sub, int argc, char **argv);
 int update_node_list(int max_nodes);
-void confirm(const char *message);
+bool confirm(const char *fmt, ...) __printf(1, 2);
 void work_queue_wait(struct work_queue *q);
 int do_vdi_create(const char *vdiname, int64_t vdi_size,
 		  uint32_t base_vid, uint32_t *vdi_id, bool snapshot,
diff --git a/dog/vdi.c b/dog/vdi.c
index fb2ff32..a8cfdf4 100644
--- a/dog/vdi.c
+++ b/dog/vdi.c
@@ -804,9 +804,12 @@ static int vdi_rollback(int argc, char **argv)
 	if (ret != EXIT_SUCCESS)
 		return ret;
 
-	if (!vdi_cmd_data.force)
-		confirm("This operation dicards any changes made since the"
-			" previous\nsnapshot was taken.  Continue? [yes/no]: ");
+	if (!vdi_cmd_data.force) {
+		if (!confirm("This operation dicards any changes made since the"
+			     " previous\nsnapshot was taken.  "
+			     "Continue? [yes/no]: "))
+			return EXIT_SUCCESS;
+	}
 
 	ret = do_vdi_delete(vdiname, 0, NULL);
 	if (ret != SD_RES_SUCCESS) {
@@ -2065,8 +2068,9 @@ static int vdi_cache_purge(int argc, char **argv)
 		hdr.flags = SD_FLAG_CMD_WRITE;
 		hdr.data_length = 0;
 	} else {
-		confirm("This operation purges the cache of all the vdi"
-			". Continue? [yes/no]: ");
+		if (!confirm("This operation purges the cache of all the vdi"
+			     ". Continue? [yes/no]: "))
+			return EXIT_SUCCESS;
 	}
 
 	ret = send_light_req(&hdr, sdhost, sdport);
-- 
1.7.9.5




More information about the sheepdog mailing list