All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andre Prendel <andre.prendel@gmx.de>
To: lm-sensors@vger.kernel.org
Subject: [lm-sensors] [PATCH v2 7/7] sensord: Refactoring of rrdInit()
Date: Mon, 11 May 2009 16:04:35 +0000	[thread overview]
Message-ID: <20090511160435.GH17530@ubuntu> (raw)

Refactoring of the rrdInit() function.

* Fix deep indentation levels
* Return with unique error code (-1)

Changes in v2:

Fix wrong condition.
Rename buffer for struct stat to sb.

 rrd.c |   87 ++++++++++++++++++++++++++++++------------------------------------
 1 file changed, 40 insertions(+), 47 deletions(-)

Index: quilt-sensors/prog/sensord/rrd.c
=================================--- quilt-sensors.orig/prog/sensord/rrd.c	2009-05-08 19:55:45.000000000 +0200
+++ quilt-sensors/prog/sensord/rrd.c	2009-05-08 20:17:04.000000000 +0200
@@ -242,58 +242,51 @@
 
 int rrdInit(void)
 {
-	int ret = 0;
-	struct stat tmp;
+	int ret;
+	struct stat sb;
+	char stepBuff[STEP_BUFF], rraBuff[RRA_BUFF];
+	int argc = 4, num;
+	const char *argv[6 + MAX_RRD_SENSORS] = {
+		"sensord", sensord_args.rrdFile, "-s", stepBuff
+	};
 
 	sensorLog(LOG_DEBUG, "sensor RRD init");
-	if (stat(sensord_args.rrdFile, &tmp)) {
-		if (errno = ENOENT) {
-			char stepBuff[STEP_BUFF], rraBuff[RRA_BUFF];
-			int argc = 4, num;
-			const char *argv[6 + MAX_RRD_SENSORS] = {
-				"sensord", sensord_args.rrdFile, "-s", stepBuff
-			};
-
-			sensorLog(LOG_INFO, "creating round robin database");
-			num = rrdGetSensors(argv + argc);
-			if (num = 0) {
-				sensorLog(LOG_ERR,
-					  "Error creating RRD: %s: %s",
-					  sensord_args.rrdFile,
-					  "No sensors detected");
-				ret = 2;
-			} else if (num < 0) {
-				ret = -num;
-			} else {
-				sprintf(stepBuff, "%d", sensord_args.rrdTime);
-				sprintf(rraBuff, "RRA:%s:%f:%d:%d",
-					sensord_args.rrdNoAverage ? "LAST" :
-					"AVERAGE",
-					0.5 /* fraction of non-unknown samples needed per entry */,
-					1 /* samples per entry */,
-					7 * 24 * 60 * 60 /
-					sensord_args.rrdTime /* 1 week */);
-
-				argc += num;
-				argv[argc ++] = rraBuff;
-				argv[argc] = NULL;
-				if ((ret = rrd_create(argc,
-						      (char **) /* WEAK */ argv))) {
-					sensorLog(LOG_ERR,
-						  "Error creating RRD file: %s: %s",
-						  sensord_args.rrdFile,
-						  rrd_get_error());
-				}
-			}
-		} else {
-			sensorLog(LOG_ERR, "Error stat()ing RRD: %s: %s",
-				  sensord_args.rrdFile, strerror(errno));
-			ret = 1;
+
+	/* Create RRD if it does not exist. */
+	if (stat(sensord_args.rrdFile, &sb)) {
+		if (errno != ENOENT) {
+			sensorLog(LOG_ERR, "Could not stat rrd file: %s\n",
+				  sensord_args.rrdFile);
+			return -1;
+		}
+		sensorLog(LOG_INFO, "Creating round robin database");
+
+		num = rrdGetSensors(argv + argc);
+		if (num < 1) {
+			sensorLog(LOG_ERR, "Error creating RRD: %s: %s",
+				  sensord_args.rrdFile, "No sensors detected");
+			return -1;
+		}
+
+		sprintf(stepBuff, "%d", sensord_args.rrdTime);
+		sprintf(rraBuff, "RRA:%s:%f:%d:%d",
+			sensord_args.rrdNoAverage ? "LAST" :"AVERAGE",
+			0.5, 1, 7 * 24 * 60 * 60 / sensord_args.rrdTime);
+
+		argc += num;
+		argv[argc++] = rraBuff;
+		argv[argc] = NULL;
+
+		ret = rrd_create(argc, (char**) argv);
+		if (ret = -1) {
+			sensorLog(LOG_ERR, "Error creating RRD file: %s: %s",
+				  sensord_args.rrdFile, rrd_get_error());
+			return -1;
 		}
 	}
-	sensorLog(LOG_DEBUG, "sensor RRD inited");
 
-	return ret;
+	sensorLog(LOG_DEBUG, "sensor RRD initialized");
+	return 0;
 }
 
 #define RRDCGI "/usr/bin/rrdcgi"

_______________________________________________
lm-sensors mailing list
lm-sensors@lm-sensors.org
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

             reply	other threads:[~2009-05-11 16:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-11 16:04 Andre Prendel [this message]
2009-05-12 19:48 ` [lm-sensors] [PATCH v2 7/7] sensord: Refactoring of rrdInit() Jean Delvare

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090511160435.GH17530@ubuntu \
    --to=andre.prendel@gmx.de \
    --cc=lm-sensors@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.