dovecot-2.0: config: Show time/size setting values as more human...

dovecot at dovecot.org dovecot at dovecot.org
Fri Oct 1 20:58:50 EEST 2010


details:   http://hg.dovecot.org/dovecot-2.0/rev/7c4e4a6b1714
changeset: 12227:7c4e4a6b1714
user:      Timo Sirainen <tss at iki.fi>
date:      Fri Oct 01 18:58:47 2010 +0100
description:
config: Show time/size setting values as more human readable.

diffstat:

 src/config/config-request.c |  50 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 48 insertions(+), 2 deletions(-)

diffs (74 lines):

diff -r 766564bb051f -r 7c4e4a6b1714 src/config/config-request.c
--- a/src/config/config-request.c	Fri Oct 01 18:33:58 2010 +0100
+++ b/src/config/config-request.c	Fri Oct 01 18:58:47 2010 +0100
@@ -30,6 +30,52 @@
 	bool failed;
 };
 
+static void config_export_size(string_t *str, uoff_t size)
+{
+	static const char suffixes[] = { 'B', 'k', 'M', 'G', 'T' };
+	char suffix = suffixes[0];
+	unsigned int i;
+
+	if (size == 0) {
+		str_append_c(str, '0');
+		return;
+	}
+	for (i = 1; i < N_ELEMENTS(suffixes) && (size % 1024) == 0; i++) {
+		suffix = suffixes[i];
+		size /= 1024;
+	}
+	str_printfa(str, "%llu %c", (unsigned long long)size, suffix);
+}
+
+static void config_export_time(string_t *str, unsigned int stamp)
+{
+	const char *suffix = "secs";
+
+	if (stamp == 0) {
+		str_append_c(str, '0');
+		return;
+	}
+
+	if (stamp % 60 == 0) {
+		stamp /= 60;
+		suffix = "mins";
+		if (stamp % 60 == 0) {
+			stamp /= 60;
+			suffix = "hours";
+			if (stamp % 24 == 0) {
+				stamp /= 24;
+				suffix = "days";
+				if (stamp % 7 == 0) {
+					stamp /= 7;
+					suffix = "weeks";
+				}
+			}
+		}
+	}
+
+	str_printfa(str, "%u %s", stamp, suffix);
+}
+
 bool config_export_type(string_t *str, const void *value,
 			const void *default_value,
 			enum setting_type type, bool dump_default,
@@ -47,7 +93,7 @@
 		const uoff_t *val = value, *dval = default_value;
 
 		if (dump_default || dval == NULL || *val != *dval)
-			str_printfa(str, "%llu", (unsigned long long)*val);
+			config_export_size(str, *val);
 		break;
 	}
 	case SET_UINT:
@@ -61,7 +107,7 @@
 				str_printfa(str, "0%o", *val);
 				break;
 			case SET_TIME:
-				str_printfa(str, "%u s", *val);
+				config_export_time(str, *val);
 				break;
 			default:
 				str_printfa(str, "%u", *val);


More information about the dovecot-cvs mailing list