[Ocfs2-tools-devel] [PATCH] libtools-internal: Move verbosef to an internal library.

Joel Becker Joel.Becker at oracle.com
Mon Aug 25 16:36:05 PDT 2008


The verbosity routines would love to be used by other programs.  Take
them out of libocfs2ne and move them to their own place,
libtools-internal.  Rename the functions as appropriate.

Signed-off-by: Joel Becker <joel.becker at oracle.com>
---
 Makefile                                 |    2 +-
 include/Makefile                         |    2 +-
 include/tools-internal/Makefile          |    9 ++
 include/tools-internal/verbose.h         |   72 ++++++++++++
 libtools-internal/.gitignore             |    3 +
 libtools-internal/Makefile               |   44 +++++++
 libtools-internal/verbose.c              |  179 ++++++++++++++++++++++++++++
 tunefs.ocfs2/Makefile                    |   15 ++-
 tunefs.ocfs2/feature_backup_super.c      |   12 +-
 tunefs.ocfs2/feature_extended_slotmap.c  |   12 +-
 tunefs.ocfs2/feature_inline_data.c       |   12 +-
 tunefs.ocfs2/feature_local.c             |   12 +-
 tunefs.ocfs2/feature_sparse_files.c      |   12 +-
 tunefs.ocfs2/feature_unwritten_extents.c |   12 +-
 tunefs.ocfs2/libocfs2ne.c                |  188 +++---------------------------
 tunefs.ocfs2/libocfs2ne.h                |   50 +--------
 tunefs.ocfs2/ocfs2ne.c                   |   17 ++--
 tunefs.ocfs2/op_reset_uuid.c             |    4 +-
 tunefs.ocfs2/op_resize_volume.c          |    6 +-
 tunefs.ocfs2/op_set_journal_size.c       |    6 +-
 tunefs.ocfs2/op_set_label.c              |   10 +-
 tunefs.ocfs2/op_set_slot_count.c         |    6 +-
 tunefs.ocfs2/op_update_cluster_stack.c   |    2 +-
 23 files changed, 398 insertions(+), 289 deletions(-)
 create mode 100644 include/tools-internal/Makefile
 create mode 100644 include/tools-internal/verbose.h
 create mode 100644 libtools-internal/.gitignore
 create mode 100644 libtools-internal/Makefile
 create mode 100644 libtools-internal/verbose.c

diff --git a/Makefile b/Makefile
index eed024d..0871849 100644
--- a/Makefile
+++ b/Makefile
@@ -20,7 +20,7 @@ CHKCONFIG_DEP = chkconfig
 COMPILE_PY = 1
 endif
 
-SUBDIRS = include libo2dlm libo2cb libocfs2 fsck.ocfs2 mkfs.ocfs2 mounted.ocfs2 tunefs.ocfs2 debugfs.ocfs2 o2cb_ctl ocfs2_hb_ctl mount.ocfs2 ocfs2_controld o2image listuuid sizetest extras patches
+SUBDIRS = include libtools-internal libo2dlm libo2cb libocfs2 fsck.ocfs2 mkfs.ocfs2 mounted.ocfs2 tunefs.ocfs2 debugfs.ocfs2 o2cb_ctl ocfs2_hb_ctl mount.ocfs2 ocfs2_controld o2image listuuid sizetest extras patches
 
 ifdef BUILD_OCFS2CONSOLE
 SUBDIRS += ocfs2console
diff --git a/include/Makefile b/include/Makefile
index 512d8cf..75c34b2 100644
--- a/include/Makefile
+++ b/include/Makefile
@@ -2,6 +2,6 @@ TOPDIR = ..
 
 include $(TOPDIR)/Preamble.make
 
-SUBDIRS = ocfs2-kernel o2dlm o2cb ocfs2
+SUBDIRS = tools-internal ocfs2-kernel o2dlm o2cb ocfs2
 
 include $(TOPDIR)/Postamble.make
diff --git a/include/tools-internal/Makefile b/include/tools-internal/Makefile
new file mode 100644
index 0000000..374f18b
--- /dev/null
+++ b/include/tools-internal/Makefile
@@ -0,0 +1,9 @@
+TOPDIR = ../..
+
+include $(TOPDIR)/Preamble.make
+
+HFILES = verbose.h
+
+DIST_FILES = $(HFILES)
+
+include $(TOPDIR)/Postamble.make
diff --git a/include/tools-internal/verbose.h b/include/tools-internal/verbose.h
new file mode 100644
index 0000000..bd60c9f
--- /dev/null
+++ b/include/tools-internal/verbose.h
@@ -0,0 +1,72 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * verbose.h
+ *
+ * Internal verbose output functions.
+ *
+ * Copyright (C) 2008 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+
+#ifndef _INTERNAL_VERBOSE_H
+#define _INTERNAL_VERBOSE_H
+
+/* Verbosity levels for verbosef/errorf/tcom_err */
+#define VL_FLAG_STDOUT	0x100	/* or'd with a level, output to stdout */
+enum tools_verbosity_level {
+	VL_CRIT	 	= 0,	/* Don't use this!  I still haven't
+				   thought of anything so critical that
+				   -q should be ignored */
+	VL_ERR		= 1,	/* Error messages */
+
+/* Regular output is the same level as errors */
+#define VL_OUT		(VL_ERR | VL_FLAG_STDOUT)
+
+	VL_APP		= 2,	/* Verbose application status */
+	VL_LIB		= 3, 	/* Status from shared code */
+	VL_DEBUG	= 4, 	/* Debugging output */
+};
+
+/* Call this to set the program name */
+void tools_setup_argv0(const char *argv0);
+
+/* Returns the program name from argv0 */
+const char *tools_progname(void);
+
+/* Prints the version of ocfs2ne */
+void tools_version(void);
+
+/* Increase and decrease the verbosity level */
+void tools_verbose(void);
+void tools_quiet(void);
+
+/* Sets the process interactive */
+void tools_interactive(void);
+
+/*
+ * Output that honors the verbosity level.  tcom_err() is for errcode_t
+ * errors.  errorf() is for all other errors.  verbosef() is for verbose
+ * output.
+ */
+void verbosef(enum tools_verbosity_level level, const char *fmt, ...)
+	__attribute__ ((format (printf, 2, 3)));
+void errorf(const char *fmt, ...)
+	__attribute__ ((format (printf, 1, 2)));
+void tcom_err(errcode_t code, const char *fmt, ...)
+	__attribute__ ((format (printf, 2, 3)));
+int tools_interact(const char *fmt, ...)
+	__attribute__ ((format (printf, 1, 2)));
+int tools_interact_critical(const char *fmt, ...)
+	__attribute__ ((format (printf, 1, 2)));
+
+#endif  /* _INTERNAL_VERBOSE_H */
diff --git a/libtools-internal/.gitignore b/libtools-internal/.gitignore
new file mode 100644
index 0000000..f1b4775
--- /dev/null
+++ b/libtools-internal/.gitignore
@@ -0,0 +1,3 @@
+cscope.*
+libtools-internal.a
+*.d
diff --git a/libtools-internal/Makefile b/libtools-internal/Makefile
new file mode 100644
index 0000000..e1ebf36
--- /dev/null
+++ b/libtools-internal/Makefile
@@ -0,0 +1,44 @@
+TOPDIR = ..
+
+include $(TOPDIR)/Preamble.make
+
+INCLUDES = -I. -I$(TOPDIR)/include
+
+UNINST_LIBRARIES = libtools-internal.a
+
+CFLAGS += -fPIC
+
+DEFINES += -DVERSION=\"$(VERSION)\"
+
+ifneq ($(OCFS2_DEBUG_EXE),)
+DEBUG_EXE_FILES = $(shell awk '/DEBUG_EXE/{if (k[FILENAME] == 0) {print FILENAME; k[FILENAME] = 1;}}' $(CFILES))
+DEBUG_EXE_PROGRAMS = $(addprefix debug_,$(subst .c,,$(DEBUG_EXE_FILES)))
+
+.SECONDARY:
+
+UNINST_PROGRAMS += $(DEBUG_EXE_PROGRAMS)
+
+debug_%.o : %.c 
+	$(CC) $(CFLAGS) $(LOCAL_CFLAGS) $(CPPFLAGS) $(LOCAL_CPPFLAGS) \
+		$(INCLUDES) $(DEFINES) \
+		-DDEBUG_EXE -o $@ -c $<
+
+debug_%: debug_%.o
+	$(LINK) $(COM_ERR_LIBS)
+
+endif
+
+CFILES = verbose.c
+
+OBJS = $(subst .c,.o,$(CFILES))
+
+$(OBJS): $(HFILES) $(HFILES_GEN)
+
+libtools-internal.a: $(OBJS)
+	rm -f $@
+	$(AR) r $@ $^
+	$(RANLIB) $@
+
+DIST_FILES = $(CFILES) $(HFILES)
+
+include $(TOPDIR)/Postamble.make
diff --git a/libtools-internal/verbose.c b/libtools-internal/verbose.c
new file mode 100644
index 0000000..1000799
--- /dev/null
+++ b/libtools-internal/verbose.c
@@ -0,0 +1,179 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * verbose.c
+ *
+ * Internal routines for verbose output.
+ *
+ * Copyright (C) 2008 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+
+#define _LARGEFILE64_SOURCE
+#define _GNU_SOURCE /* for getopt_long and O_DIRECT */
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <string.h>
+#include <ctype.h>
+#include <limits.h>
+#include <com_err.h>
+
+#include "tools-internal/verbose.h"
+
+static char progname[PATH_MAX] = "(Unknown)";
+static int verbosity = 1;
+static int interactive = 0;
+
+void tools_setup_argv0(const char *argv0)
+{
+	char *pname;
+	char pathtmp[PATH_MAX];
+
+	/* This shouldn't care which basename(3) we get */
+	snprintf(pathtmp, PATH_MAX, "%s", argv0);
+	pname = basename(pathtmp);
+	snprintf(progname, PATH_MAX, "%s", pname);
+}
+
+/* If all verbosity is turned off, make sure com_err() prints nothing. */
+static void quiet_com_err(const char *prog, long errcode, const char *fmt,
+			  va_list args)
+{
+	return;
+}
+
+void tools_verbose(void)
+{
+	verbosity++;
+	if (verbosity == 1)
+		reset_com_err_hook();
+}
+
+void tools_quiet(void)
+{
+	if (verbosity == 1)
+		set_com_err_hook(quiet_com_err);
+	verbosity--;
+}
+
+static void vfverbosef(FILE *f, int level, const char *fmt, va_list args)
+{
+	if (level <= verbosity)
+		vfprintf(f, fmt, args);
+}
+
+static void fverbosef(FILE *f, int level, const char *fmt, ...)
+	__attribute__ ((format (printf, 3, 4)));
+static void fverbosef(FILE *f, int level, const char *fmt, ...)
+{
+	va_list args;
+
+	va_start(args, fmt);
+	vfverbosef(f, level, fmt, args);
+	va_end(args);
+}
+
+void verbosef(enum tools_verbosity_level level, const char *fmt, ...)
+{
+	va_list args;
+	FILE *f = stderr;
+
+	if (level & VL_FLAG_STDOUT) {
+		f = stdout;
+		level &= ~VL_FLAG_STDOUT;
+	}
+
+	va_start(args, fmt);
+	vfverbosef(f, level, fmt, args);
+	va_end(args);
+}
+
+void errorf(const char *fmt, ...)
+{
+	va_list args;
+
+	va_start(args, fmt);
+	fverbosef(stderr, VL_ERR, "%s: ", progname);
+	vfverbosef(stderr, VL_ERR, fmt, args);
+	va_end(args);
+}
+
+void tcom_err(errcode_t code, const char *fmt, ...)
+{
+	va_list args;
+
+	va_start(args, fmt);
+	com_err_va(progname, code, fmt, args);
+	va_end(args);
+}
+
+static int vtools_interact(enum tools_verbosity_level level,
+			   const char *fmt, va_list args)
+{
+	char *s, buffer[NAME_MAX];
+
+	vfverbosef(stderr, level, fmt, args);
+
+	s = fgets(buffer, sizeof(buffer), stdin);
+	if (s && *s) {
+		*s = tolower(*s);
+		if (*s == 'y')
+			return 1;
+	}
+
+	return 0;
+}
+
+void tools_interactive(void)
+{
+	interactive = 1;
+}
+
+/* Pass this a question without a newline. */
+int tools_interact(const char *fmt, ...)
+{
+	int rc;
+	va_list args;
+
+	if (!interactive)
+		return 1;
+
+	va_start(args, fmt);
+	rc = vtools_interact(VL_ERR, fmt, args);
+	va_end(args);
+
+	return rc;
+}
+
+/* Only for "DON'T DO THIS WITHOUT REALLY CHECKING!" stuff */
+int tools_interact_critical(const char *fmt, ...)
+{
+	int rc;
+	va_list args;
+
+	va_start(args, fmt);
+	rc = vtools_interact(VL_CRIT, fmt, args);
+	va_end(args);
+
+	return rc;
+}
+
+void tools_version(void)
+{
+	verbosef(VL_ERR, "%s %s\n", progname, VERSION);
+}
+
+const char *tools_progname(void)
+{
+	return progname;
+}
+
diff --git a/tunefs.ocfs2/Makefile b/tunefs.ocfs2/Makefile
index c0e70af..a1c00c7 100644
--- a/tunefs.ocfs2/Makefile
+++ b/tunefs.ocfs2/Makefile
@@ -2,6 +2,9 @@ TOPDIR = ..
 
 include $(TOPDIR)/Preamble.make
 
+LIBTOOLS_INTERNAL_LIBS = -L$(TOPDIR)/libtools-internal -ltools-internal
+LIBTOOLS_INTERNAL_DEPS = $(TOPDIR)/libtools-internal/libtools-internal.a
+
 LIBOCFS2_LIBS = -L$(TOPDIR)/libocfs2 -locfs2
 LIBOCFS2_DEPS = $(TOPDIR)/libocfs2/libocfs2.a
 
@@ -53,13 +56,13 @@ debug_%.o : %.c
 		$(INCLUDES) $(DEFINES) \
 		-DDEBUG_EXE -o $@ -c $<
 
-debug_op_features: debug_op_features.o $(OCFS2NE_FEATURE_OBJS) libocfs2ne.a $(LIBOCFS2_DEPS) $(LIBO2DLM_DEPS) $(LIBO2CB_DEPS)
+debug_op_features: debug_op_features.o $(OCFS2NE_FEATURE_OBJS) libocfs2ne.a $(LIBOCFS2_DEPS) $(LIBO2DLM_DEPS) $(LIBO2CB_DEPS) $(LIBTOOLS_INTERNAL_DEPS)
 	$(LINK) $(LIBOCFS2_LIBS) $(UUID_LIBS) $(LIBO2DLM_LIBS) \
-		$(LIBO2CB_LIBS) $(COM_ERR_LIBS)
+		$(LIBO2CB_LIBS) $(LIBTOOLS_INTERNAL_LIBS) $(COM_ERR_LIBS)
 
-debug_%: debug_%.o libocfs2ne.a $(LIBOCFS2_DEPS) $(LIBO2DLM_DEPS) $(LIBO2CB_DEPS)
+debug_%: debug_%.o libocfs2ne.a $(LIBOCFS2_DEPS) $(LIBO2DLM_DEPS) $(LIBO2CB_DEPS) $(LIBTOOLS_INTERNAL_DEPS)
 	$(LINK) $(LIBOCFS2_LIBS) $(UUID_LIBS) $(LIBO2DLM_LIBS) \
-		$(LIBO2CB_LIBS) $(COM_ERR_LIBS)
+		$(LIBO2CB_LIBS) $(LIBTOOLS_INTERNAL_LIBS) $(COM_ERR_LIBS)
 endif
 
 LIBOCFS2NE_CFILES = libocfs2ne.c
@@ -91,9 +94,9 @@ libocfs2ne.a: $(LIBOCFS2NE_OBJS)
 	$(AR) r $@ $^
 	$(RANLIB) $@
 
-ocfs2ne: $(OCFS2NE_OBJS) libocfs2ne.a $(LIBOCFS2_DEPS) $(LIBO2DLM_DEPS) $(LIBO2CB_DEPS)
+ocfs2ne: $(OCFS2NE_OBJS) libocfs2ne.a $(LIBOCFS2_DEPS) $(LIBO2DLM_DEPS) $(LIBO2CB_DEPS) $(LIBTOOLS_INTERNAL_DEPS)
 	$(LINK) $(LIBOCFS2_LIBS) $(UUID_LIBS) $(LIBO2DLM_LIBS) \
-		$(LIBO2CB_LIBS) $(COM_ERR_LIBS)
+		$(LIBO2CB_LIBS) $(LIBTOOLS_INTERNAL_LIBS) $(COM_ERR_LIBS)
 
 tunefs.ocfs2: ocfs2ne
 	ln -f ocfs2ne tunefs.ocfs2
diff --git a/tunefs.ocfs2/feature_backup_super.c b/tunefs.ocfs2/feature_backup_super.c
index a8af858..783c176 100644
--- a/tunefs.ocfs2/feature_backup_super.c
+++ b/tunefs.ocfs2/feature_backup_super.c
@@ -73,9 +73,9 @@ static int disable_backup_super(ocfs2_filesys *fs, int flags)
 		goto out;
 	}
 
-	if (!tunefs_interact("Disable the backup superblock feature on "
-			     "device \"%s\"? ",
-			     fs->fs_devname))
+	if (!tools_interact("Disable the backup superblock feature on "
+			    "device \"%s\"? ",
+			    fs->fs_devname))
 		goto out;
 
 	tunefs_block_signals();
@@ -190,9 +190,9 @@ static int enable_backup_super(ocfs2_filesys *fs, int flags)
 		goto out;
 	}
 
-	if (!tunefs_interact("Enable the backup superblock feature on "
-			     "device \"%s\"? ",
-			     fs->fs_devname))
+	if (!tools_interact("Enable the backup superblock feature on "
+			    "device \"%s\"? ",
+			    fs->fs_devname))
 		goto out;
 
 	tunefs_block_signals();
diff --git a/tunefs.ocfs2/feature_extended_slotmap.c b/tunefs.ocfs2/feature_extended_slotmap.c
index 80eda3b..1aff528 100644
--- a/tunefs.ocfs2/feature_extended_slotmap.c
+++ b/tunefs.ocfs2/feature_extended_slotmap.c
@@ -39,9 +39,9 @@ static int enable_extended_slotmap(ocfs2_filesys *fs, int flags)
 		goto out;
 	}
 
-	if (!tunefs_interact("Enable the extended slot map feature on "
-			     "device \"%s\"? ",
-			     fs->fs_devname))
+	if (!tools_interact("Enable the extended slot map feature on "
+			    "device \"%s\"? ",
+			    fs->fs_devname))
 		goto out;
 
 	OCFS2_SET_INCOMPAT_FEATURE(super,
@@ -72,9 +72,9 @@ static int disable_extended_slotmap(ocfs2_filesys *fs, int flags)
 		goto out;
 	}
 
-	if (!tunefs_interact("Disable the extended slot map feature on "
-			     "device \"%s\"? ",
-			     fs->fs_devname))
+	if (!tools_interact("Disable the extended slot map feature on "
+			    "device \"%s\"? ",
+			    fs->fs_devname))
 		goto out;
 
 	OCFS2_CLEAR_INCOMPAT_FEATURE(super,
diff --git a/tunefs.ocfs2/feature_inline_data.c b/tunefs.ocfs2/feature_inline_data.c
index a95da64..f9f878d 100644
--- a/tunefs.ocfs2/feature_inline_data.c
+++ b/tunefs.ocfs2/feature_inline_data.c
@@ -59,9 +59,9 @@ static int enable_inline_data(ocfs2_filesys *fs, int flags)
 		goto out;
 	}
 
-	if (!tunefs_interact("Enable the inline data feature on device "
-			     "\"%s\"? ",
-			     fs->fs_devname))
+	if (!tools_interact("Enable the inline data feature on device "
+			    "\"%s\"? ",
+			    fs->fs_devname))
 		goto out;
 
 	OCFS2_SET_INCOMPAT_FEATURE(super,
@@ -178,9 +178,9 @@ static int disable_inline_data(ocfs2_filesys *fs, int flags)
 		goto out;
 	}
 
-	if (!tunefs_interact("Disable the inline data feature on device "
-			     "\"%s\"? ",
-			     fs->fs_devname))
+	if (!tools_interact("Disable the inline data feature on device "
+			    "\"%s\"? ",
+			    fs->fs_devname))
 		goto out;
 
 	memset(&ctxt, 0, sizeof(ctxt));
diff --git a/tunefs.ocfs2/feature_local.c b/tunefs.ocfs2/feature_local.c
index 40a483d..711bc3a 100644
--- a/tunefs.ocfs2/feature_local.c
+++ b/tunefs.ocfs2/feature_local.c
@@ -40,9 +40,9 @@ static int disable_local(ocfs2_filesys *fs, int flags)
 		goto out;
 	}
 
-	if (!tunefs_interact("Make device \"%s\" a cluster-aware "
-			     "filesystem? ",
-			     fs->fs_devname))
+	if (!tools_interact("Make device \"%s\" a cluster-aware "
+			    "filesystem? ",
+			    fs->fs_devname))
 		goto out;
 
 	/*
@@ -97,9 +97,9 @@ static int enable_local(ocfs2_filesys *fs, int flags)
 		goto out;
 	}
 
-	if (!tunefs_interact("Make device \"%s\" a single-node "
-			     "(non-clustered) filesystem? ",
-			     fs->fs_devname))
+	if (!tools_interact("Make device \"%s\" a single-node "
+			    "(non-clustered) filesystem? ",
+			    fs->fs_devname))
 		goto out;
 
 	sb->s_feature_incompat |=
diff --git a/tunefs.ocfs2/feature_sparse_files.c b/tunefs.ocfs2/feature_sparse_files.c
index d2984d6..690cc6f 100644
--- a/tunefs.ocfs2/feature_sparse_files.c
+++ b/tunefs.ocfs2/feature_sparse_files.c
@@ -116,9 +116,9 @@ static int enable_sparse_files(ocfs2_filesys *fs, int flags)
 		goto out;
 	}
 
-	if (!tunefs_interact("Enable the sparse file feature on device "
-			     "\"%s\"? ",
-			     fs->fs_devname))
+	if (!tools_interact("Enable the sparse file feature on device "
+			    "\"%s\"? ",
+			    fs->fs_devname))
 		goto out;
 
 	ret = tunefs_foreach_inode(fs, truncate_to_i_size, NULL);
@@ -452,9 +452,9 @@ static int disable_sparse_files(ocfs2_filesys *fs, int flags)
 		goto out;
 	}
 
-	if (!tunefs_interact("Disable the sparse file feature on device "
-			     "\"%s\"? ",
-			     fs->fs_devname))
+	if (!tools_interact("Disable the sparse file feature on device "
+			    "\"%s\"? ",
+			    fs->fs_devname))
 		goto out;
 
 	memset(&ctxt, 0, sizeof(ctxt));
diff --git a/tunefs.ocfs2/feature_unwritten_extents.c b/tunefs.ocfs2/feature_unwritten_extents.c
index 3dc3b7c..2a1f8a6 100644
--- a/tunefs.ocfs2/feature_unwritten_extents.c
+++ b/tunefs.ocfs2/feature_unwritten_extents.c
@@ -51,9 +51,9 @@ static int enable_unwritten_extents(ocfs2_filesys *fs, int flags)
 		goto out;
 	}
 
-	if (!tunefs_interact("Enable the unwritten extents feature on "
-			     "device \"%s\"? ",
-			     fs->fs_devname))
+	if (!tools_interact("Enable the unwritten extents feature on "
+			    "device \"%s\"? ",
+			    fs->fs_devname))
 		goto out;
 
 	OCFS2_SET_RO_COMPAT_FEATURE(super,
@@ -140,9 +140,9 @@ static int disable_unwritten_extents(ocfs2_filesys *fs, int flags)
 		goto out;
 	}
 
-	if (!tunefs_interact("Disable the unwritten extents feature on "
-			     "device \"%s\"? ",
-			     fs->fs_devname))
+	if (!tools_interact("Disable the unwritten extents feature on "
+			    "device \"%s\"? ",
+			    fs->fs_devname))
 		goto out;
 
 	ret = clear_unwritten_extents(fs);
diff --git a/tunefs.ocfs2/libocfs2ne.c b/tunefs.ocfs2/libocfs2ne.c
index 080cb70..ec2b4be 100644
--- a/tunefs.ocfs2/libocfs2ne.c
+++ b/tunefs.ocfs2/libocfs2ne.c
@@ -21,7 +21,6 @@
 #define _GNU_SOURCE /* for getopt_long and O_DIRECT */
 
 #include <stdio.h>
-#include <stdarg.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/types.h>
@@ -33,7 +32,6 @@
 #include <inttypes.h>
 #include <limits.h>
 #include <getopt.h>
-#include <ctype.h>
 #include <assert.h>
 
 #include "ocfs2/ocfs2.h"
@@ -95,10 +93,6 @@ struct tunefs_private {
 /* List of all ocfs2_filesys objects opened by tunefs_open() */
 static LIST_HEAD(fs_list);
 
-static char progname[PATH_MAX] = "(Unknown)";
-static int verbosity = 1;
-static int interactive = 0;
-
 /* Refcount for calls to tunefs_[un]block_signals() */
 static unsigned int blocked_signals_count;
 
@@ -160,144 +154,6 @@ static errcode_t tunefs_set_state(ocfs2_filesys *fs)
 
 
 /*
- * Code for interacting with the user.
- */
-
-/* If all verbosity is turned off, make sure com_err() prints nothing. */
-static void quiet_com_err(const char *prog, long errcode, const char *fmt,
-			  va_list args)
-{
-	return;
-}
-
-void tunefs_verbose(void)
-{
-	verbosity++;
-	if (verbosity == 1)
-		reset_com_err_hook();
-}
-
-void tunefs_quiet(void)
-{
-	if (verbosity == 1)
-		set_com_err_hook(quiet_com_err);
-	verbosity--;
-}
-
-static void vfverbosef(FILE *f, int level, const char *fmt, va_list args)
-{
-	if (level <= verbosity)
-		vfprintf(f, fmt, args);
-}
-
-static void fverbosef(FILE *f, int level, const char *fmt, ...)
-	__attribute__ ((format (printf, 3, 4)));
-static void fverbosef(FILE *f, int level, const char *fmt, ...)
-{
-	va_list args;
-
-	va_start(args, fmt);
-	vfverbosef(f, level, fmt, args);
-	va_end(args);
-}
-
-void verbosef(enum tunefs_verbosity_level level, const char *fmt, ...)
-{
-	va_list args;
-	FILE *f = stderr;
-
-	if (level & VL_FLAG_STDOUT) {
-		f = stdout;
-		level &= ~VL_FLAG_STDOUT;
-	}
-
-	va_start(args, fmt);
-	vfverbosef(f, level, fmt, args);
-	va_end(args);
-}
-
-void errorf(const char *fmt, ...)
-{
-	va_list args;
-
-	va_start(args, fmt);
-	fverbosef(stderr, VL_ERR, "%s: ", progname);
-	vfverbosef(stderr, VL_ERR, fmt, args);
-	va_end(args);
-}
-
-void tcom_err(errcode_t code, const char *fmt, ...)
-{
-	va_list args;
-
-	va_start(args, fmt);
-	com_err_va(progname, code, fmt, args);
-	va_end(args);
-}
-
-static int vtunefs_interact(enum tunefs_verbosity_level level,
-			    const char *fmt, va_list args)
-{
-	char *s, buffer[NAME_MAX];
-
-	vfverbosef(stderr, level, fmt, args);
-
-	s = fgets(buffer, sizeof(buffer), stdin);
-	if (s && *s) {
-		*s = tolower(*s);
-		if (*s == 'y')
-			return 1;
-	}
-
-	return 0;
-}
-
-void tunefs_interactive(void)
-{
-	interactive = 1;
-}
-
-/* Pass this a question without a newline. */
-int tunefs_interact(const char *fmt, ...)
-{
-	int rc;
-	va_list args;
-
-	if (!interactive)
-		return 1;
-
-	va_start(args, fmt);
-	rc = vtunefs_interact(VL_ERR, fmt, args);
-	va_end(args);
-
-	return rc;
-}
-
-/* Only for "DON'T DO THIS WITHOUT REALLY CHECKING!" stuff */
-int tunefs_interact_critical(const char *fmt, ...)
-{
-	int rc;
-	va_list args;
-
-	va_start(args, fmt);
-	rc = vtunefs_interact(VL_CRIT, fmt, args);
-	va_end(args);
-
-	return rc;
-}
-
-void tunefs_version(void)
-{
-	verbosef(VL_ERR, "%s %s\n", progname, VERSION);
-}
-
-const char *tunefs_progname(void)
-{
-	return progname;
-}
-
-
-/*
  * Functions for use by operations.
  */
 
@@ -774,18 +630,6 @@ static int setup_signals(void)
 	return rc;
 }
 
-
-static void setup_argv0(const char *argv0)
-{
-	char *pname;
-	char pathtmp[PATH_MAX];
-
-	/* This shouldn't care which basename(3) we get */
-	snprintf(pathtmp, PATH_MAX, "%s", argv0);
-	pname = basename(pathtmp);
-	snprintf(progname, PATH_MAX, "%s", pname);
-}
-
 void tunefs_init(const char *argv0)
 {
 	initialize_o2ne_error_table();
@@ -793,7 +637,7 @@ void tunefs_init(const char *argv0)
 	initialize_o2dl_error_table();
 	initialize_o2cb_error_table();
 
-	setup_argv0(argv0);
+	tools_setup_argv0(argv0);
 
 	setbuf(stdout, NULL);
 	setbuf(stderr, NULL);
@@ -1640,19 +1484,19 @@ static void shuffle_argv(int *argc, int optind, char **argv)
 
 static void tunefs_debug_usage(int error)
 {
-	FILE *f = stderr;
+	enum tools_verbosity_level level = VL_ERR;
 
 	if (!error)
-		f = stdout;
-
-	fverbosef(f, VL_ERR, "%s", usage_string ? usage_string : "(null)");
-	fverbosef(f, VL_ERR,
-		  "[opts] can be any mix of:\n"
-		  "\t-i|--interactive\n"
-		  "\t-v|--verbose (more than one increases verbosity)\n"
-		  "\t-q|--quiet (more than one decreases verbosity)\n"
-		  "\t-h|--help\n"
-		  "\t-V|--version\n");
+		level = VL_OUT;
+
+	verbosef(level, "%s", usage_string ? usage_string : "(null)");
+	verbosef(level,
+		 "[opts] can be any mix of:\n"
+		 "\t-i|--interactive\n"
+		 "\t-v|--verbose (more than one increases verbosity)\n"
+		 "\t-q|--quiet (more than one decreases verbosity)\n"
+		 "\t-h|--help\n"
+		 "\t-V|--version\n");
 }
 
 extern int optind, opterr, optopt;
@@ -1694,15 +1538,15 @@ static void tunefs_parse_core_options(int *argc, char ***argv, char *usage)
 				break;
 
 			case 'v':
-				tunefs_verbose();
+				tools_verbose();
 				break;
 
 			case 'q':
-				tunefs_quiet();
+				tools_quiet();
 				break;
 
 			case 'i':
-				tunefs_interactive();
+				tools_interactive();
 				break;
 
 			case '?':
@@ -1734,7 +1578,7 @@ static void tunefs_parse_core_options(int *argc, char ***argv, char *usage)
 		errorf("%s\n", error);
 
 	if (print_version)
-		tunefs_version();
+		tools_version();
 
 	if (print_usage)
 		tunefs_debug_usage(*error != '\0');
diff --git a/tunefs.ocfs2/libocfs2ne.h b/tunefs.ocfs2/libocfs2ne.h
index cbe89a7..fd18d48 100644
--- a/tunefs.ocfs2/libocfs2ne.h
+++ b/tunefs.ocfs2/libocfs2ne.h
@@ -21,8 +21,8 @@
 #ifndef _LIBTUNEFS_H
 #define _LIBTUNEFS_H
 
-#define PROGNAME "tunefs.ocfs2"
 #include "o2ne_err.h"
+#include "tools-internal/verbose.h"
 
 /*
  * Adding a capability to ocfs2ne is pretty simple.  You create a source
@@ -43,8 +43,8 @@
  * - It must be idempotent.  If filesystem is already in the correct
  *   state, the method should do nothing and return success.
  *
- * - It must use tunefs_interact() before writing any changes.  If the
- *   user specified -i, tunefs_interact() will ask the user before
+ * - It must use tools_interact() before writing any changes.  If the
+ *   user specified -i, tools_interact() will ask the user before
  *   proceeding.  Otherwise, it always returns "go ahead", so you can
  *   always call it safely.
  *
@@ -71,21 +71,6 @@
 #define TUNEFS_FLAG_ALLOCATION	0x08	/* Operation will use the
 					   allocator */
 
-/* Verbosity levels for verbosef/errorf/tcom_err */
-#define VL_FLAG_STDOUT	0x100	/* or'd with a level, output to stdout */
-enum tunefs_verbosity_level {
-	VL_CRIT	 	= 0,	/* Don't use this!  I still haven't
-				   thought of anything so critical that
-				   -q should be ignored */
-	VL_ERR		= 1,	/* Error messages */
-
-/* Regular output is the same level as errors */
-#define VL_OUT		(VL_ERR | VL_FLAG_STDOUT)
-
-	VL_APP		= 2,	/* Verbose application status */
-	VL_LIB		= 3, 	/* libocfs2ne status */
-	VL_DEBUG	= 4, 	/* Debugging output */
-};
 
 /* What to do with a feature */
 enum tunefs_feature_action {
@@ -253,35 +238,6 @@ errcode_t tunefs_dlm_lock(ocfs2_filesys *fs, const char *lockid,
 			  int flags, enum o2dlm_lock_level level);
 errcode_t tunefs_dlm_unlock(ocfs2_filesys *fs, char *lockid);
 
-/* Returns the program name from argv0 */
-const char *tunefs_progname(void);
-
-/* Prints the version of ocfs2ne */
-void tunefs_version(void);
-
-/* Increase and decrease the verbosity level */
-void tunefs_verbose(void);
-void tunefs_quiet(void);
-
-/* Sets the process interactive */
-void tunefs_interactive(void);
-
-/*
- * Output that honors the verbosity level.  tcom_err() is for errcode_t
- * errors.  errorf() is for all other errors.  verbosef() is for verbose
- * output.
- */
-void verbosef(enum tunefs_verbosity_level level, const char *fmt, ...)
-	__attribute__ ((format (printf, 2, 3)));
-void errorf(const char *fmt, ...)
-	__attribute__ ((format (printf, 1, 2)));
-void tcom_err(errcode_t code, const char *fmt, ...)
-	__attribute__ ((format (printf, 2, 3)));
-int tunefs_interact(const char *fmt, ...)
-	__attribute__ ((format (printf, 1, 2)));
-int tunefs_interact_critical(const char *fmt, ...)
-	__attribute__ ((format (printf, 1, 2)));
-
 /*
  * A wrapper for inode scanning.  Calls func() for each valid inode.
  */
diff --git a/tunefs.ocfs2/ocfs2ne.c b/tunefs.ocfs2/ocfs2ne.c
index e5200fc..04022df 100644
--- a/tunefs.ocfs2/ocfs2ne.c
+++ b/tunefs.ocfs2/ocfs2ne.c
@@ -20,7 +20,6 @@
 #define _GNU_SOURCE /* for getopt_long and O_DIRECT */
 #include <stdio.h>
 #include <string.h>
-#include <ctype.h>
 #include <inttypes.h>
 #include <getopt.h>
 #include <assert.h>
@@ -164,7 +163,7 @@ static int handle_help(struct tunefs_option *opt, char *arg)
 
 static int handle_version(struct tunefs_option *opt, char *arg)
 {
-	tunefs_version();
+	tools_version();
 	exit(0);
 	return 1;
 }
@@ -176,11 +175,11 @@ static int handle_verbosity(struct tunefs_option *opt, char *arg)
 	switch (opt->opt_option.val)
 	{
 		case 'v':
-			tunefs_verbose();
+			tools_verbose();
 			break;
 
 		case 'q':
-			tunefs_quiet();
+			tools_quiet();
 			break;
 
 		default:
@@ -197,7 +196,7 @@ static int handle_verbosity(struct tunefs_option *opt, char *arg)
 
 static int handle_interactive(struct tunefs_option *opt, char *arg)
 {
-	tunefs_interactive();
+	tools_interactive();
 	return 0;
 }
 
@@ -561,15 +560,15 @@ static struct tunefs_option *find_option_by_val(int val)
 static void print_usage(int rc)
 {
 	int i;
-	enum tunefs_verbosity_level level = VL_ERR;
+	enum tools_verbosity_level level = VL_ERR;
 
 	if (!rc)
 		level = VL_OUT;
 
 	verbosef(level, "Usage: %s [options] <device> [new-size]\n",
-		 tunefs_progname());
-	verbosef(level, "       %s -h|--help\n", tunefs_progname());
-	verbosef(level, "       %s -V|--version\n", tunefs_progname());
+		 tools_progname());
+	verbosef(level, "       %s -h|--help\n", tools_progname());
+	verbosef(level, "       %s -V|--version\n", tools_progname());
 	verbosef(level, "[options] can be any mix of:\n");
 	for (i = 0; options[i]; i++) {
 		if (options[i]->opt_help)
diff --git a/tunefs.ocfs2/op_reset_uuid.c b/tunefs.ocfs2/op_reset_uuid.c
index f7b705e..a1acc3c 100644
--- a/tunefs.ocfs2/op_reset_uuid.c
+++ b/tunefs.ocfs2/op_reset_uuid.c
@@ -33,8 +33,8 @@ static errcode_t update_volume_uuid(ocfs2_filesys *fs)
 	errcode_t err;
 	unsigned char new_uuid[OCFS2_VOL_UUID_LEN];
 
-	if (!tunefs_interact("Reset the volume UUID on device \"%s\"? ",
-			     fs->fs_devname))
+	if (!tools_interact("Reset the volume UUID on device \"%s\"? ",
+			    fs->fs_devname))
 		return 0;
 
 	uuid_generate(new_uuid);
diff --git a/tunefs.ocfs2/op_resize_volume.c b/tunefs.ocfs2/op_resize_volume.c
index ff4b07f..11552ff 100644
--- a/tunefs.ocfs2/op_resize_volume.c
+++ b/tunefs.ocfs2/op_resize_volume.c
@@ -661,9 +661,9 @@ static errcode_t update_volume_size(ocfs2_filesys *fs, uint64_t new_size,
 		goto out;
 	}
 
-	if (!tunefs_interact("Grow the filesystem on device \"%s\" from "
-			     "%"PRIu32" to %"PRIu32" clusters? ",
-			     fs->fs_devname, fs->fs_clusters, new_clusters))
+	if (!tools_interact("Grow the filesystem on device \"%s\" from "
+			    "%"PRIu32" to %"PRIu32" clusters? ",
+			    fs->fs_devname, fs->fs_clusters, new_clusters))
 		goto out;
 
 	if (online)
diff --git a/tunefs.ocfs2/op_set_journal_size.c b/tunefs.ocfs2/op_set_journal_size.c
index f6b998f..b671950 100644
--- a/tunefs.ocfs2/op_set_journal_size.c
+++ b/tunefs.ocfs2/op_set_journal_size.c
@@ -70,9 +70,9 @@ static int set_journal_size_run(struct tunefs_operation *op,
 	ocfs2_free(&argp);
 	op->to_private = NULL;
 
-	if (!tunefs_interact("Resize journals on device \"%s\" to "
-			     "%"PRIu64"? ",
-			     fs->fs_devname, new_size))
+	if (!tools_interact("Resize journals on device \"%s\" to "
+			    "%"PRIu64"? ",
+			    fs->fs_devname, new_size))
 		goto out;
 
 	tunefs_block_signals();
diff --git a/tunefs.ocfs2/op_set_label.c b/tunefs.ocfs2/op_set_label.c
index ee5e680..7aeda91 100644
--- a/tunefs.ocfs2/op_set_label.c
+++ b/tunefs.ocfs2/op_set_label.c
@@ -42,11 +42,11 @@ static errcode_t update_volume_label(ocfs2_filesys *fs, const char *label)
 		return 0;
 	}
 
-	if (!tunefs_interact("Change the label on device \"%s\" from "
-			     "\"%.*s\" to \"%.*s\"? ",
-			     fs->fs_devname, OCFS2_MAX_VOL_LABEL_LEN,
-			     OCFS2_RAW_SB(fs->fs_super)->s_label,
-			     OCFS2_MAX_VOL_LABEL_LEN, label))
+	if (!tools_interact("Change the label on device \"%s\" from "
+			    "\"%.*s\" to \"%.*s\"? ",
+			    fs->fs_devname, OCFS2_MAX_VOL_LABEL_LEN,
+			    OCFS2_RAW_SB(fs->fs_super)->s_label,
+			    OCFS2_MAX_VOL_LABEL_LEN, label))
 		return 0;
 
 	memset(OCFS2_RAW_SB(fs->fs_super)->s_label, 0,
diff --git a/tunefs.ocfs2/op_set_slot_count.c b/tunefs.ocfs2/op_set_slot_count.c
index 72dae75..07bc0e4 100644
--- a/tunefs.ocfs2/op_set_slot_count.c
+++ b/tunefs.ocfs2/op_set_slot_count.c
@@ -955,9 +955,9 @@ static errcode_t update_slot_count(ocfs2_filesys *fs, int num_slots)
 		goto out;
 	}
 
-	if (!tunefs_interact("Change the number of node slots on device "
-			     "\"%s\" from %d to %d? ",
-			     fs->fs_devname, orig_slots, num_slots))
+	if (!tools_interact("Change the number of node slots on device "
+			    "\"%s\" from %d to %d? ",
+			    fs->fs_devname, orig_slots, num_slots))
 		goto out;
 
 	tunefs_block_signals();
diff --git a/tunefs.ocfs2/op_update_cluster_stack.c b/tunefs.ocfs2/op_update_cluster_stack.c
index e7d6777..3ae1f93 100644
--- a/tunefs.ocfs2/op_update_cluster_stack.c
+++ b/tunefs.ocfs2/op_update_cluster_stack.c
@@ -32,7 +32,7 @@ static errcode_t update_cluster(ocfs2_filesys *fs)
 	errcode_t ret;
 	struct o2cb_cluster_desc desc;
 
-	if (!tunefs_interact_critical(
+	if (!tools_interact_critical(
 		"Updating on-disk cluster information "
 		"to match the running cluster.\n"
 		"DANGER: YOU MUST BE ABSOLUTELY SURE THAT NO OTHER NODE "
-- 
1.5.6.3


-- 

"If you took all of the grains of sand in the world, and lined
 them up end to end in a row, you'd be working for the government!"
	- Mr. Interesting

Joel Becker
Principal Software Developer
Oracle
E-mail: joel.becker at oracle.com
Phone: (650) 506-8127



More information about the Ocfs2-tools-devel mailing list