sensord: Refactoring of loadConfig()
PATCH 6/8 sensord: Refactoring of loadConfig()
This patch does some refactoring of the loadConfig() function.
---
* Simplifying the conditions makes code flow clearer and eliminates long lines (> 80 chars).
* Removed useless stat() call.
* Return -1 in error case, instead of several positiv values (never defined).
lib.c | 76 +++++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 41 insertions(+), 35 deletions(-)
prog/sensord/lib.c +53 -40
@@ -20,7 +20,7 @@* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301 USA. */ - +#include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h>
@@ -34,46 +34,59 @@static int loadConfig (const char *cfgPath,int reload) { -struct stat stats; -FILE *cfg =NULL ; -int ret =0 ; - -if (cfgPath && !strcmp (cfgPath,"-" )) { -if (!reload) { -if ((ret =sensors_init (stdin))) { -sensorLog (LOG_ERR , -"Error loading sensors configuration file <stdin>: %s" , -sensors_strerror (ret)); - ret =12 ; - } - } - }else if (cfgPath &&stat (cfgPath, &stats) <0 ) { -sensorLog (LOG_ERR , -"Error stating sensors configuration file: %s" , - cfgPath); - ret =10 ; - }else { -if (reload) { -sensorLog (LOG_INFO ,"configuration reloading" ); -sensors_cleanup (); - } -if (cfgPath && !(cfg =fopen (cfgPath,"r" ))) { -sensorLog (LOG_ERR , -"Error opening sensors configuration file: %s" , - cfgPath); - ret =11 ; - }else if ((ret =sensors_init (cfg))) { -sensorLog (LOG_ERR , -"Error loading sensors configuration file %s: %s" , - cfgPath ? cfgPath :"(default)" , -sensors_strerror (ret)); - ret =11 ; - } -if (cfg) -fclose (cfg); - } +int ret; +FILE *fp; -return ret; +/* Load default configuratinon. */ +if (!cfgPath) { +if (reload) +sensors_cleanup (); + + ret =sensors_init (NULL ); +if (ret) { +sensorLog (LOG_ERR ,"Error while loading default" +" configuration file: %s" , +sensors_strerror (ret)); +return -1 ; + } +return 0 ; + } + +/* Read config from stdin. */ +if (!strcmp (cfgPath,"-" )) { +if (reload) +return 0 ; + + ret =sensors_init (stdin); +if (ret) { +sensorLog (LOG_ERR ,"Error loading sensors" +" configuration file <stdin>: %s" , +sensors_strerror (ret)); +return -1 ; + } +return 0 ; + } + + fp =fopen (cfgPath,"r" ); +if (!fp) { +sensorLog (LOG_ERR ,"Error opening config file %s: %s" , +sensors_strerror (ret)); +return -1 ; + } + + ret =sensors_init (fp); +if (ret) { +if (reload) +sensors_cleanup (); + +sensorLog (LOG_ERR ,"Error loading sensors config file %s: %s" , + cfgPath,sensors_strerror (ret)); +fclose (fp); +return -1 ; + } +fclose (fp); + +return 0 ; }int loadLib (const char *cfgPath)
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Quoted 8 lines · Andre Prendel
This patch does some refactoring of the
loadConfig()function.
---* Simplifying the conditions makes code flow clearer and eliminates long lines (> 80 chars).
* Removed uselessstat()call.
* Return -1 in error case, instead of several positiv values (never defined).
Spelling: positive.
If you make loadConfig() always return -1 on error then you also want to clean up the error message that is printed when reloadLib() fails, to no longer include the (now pointless) error value.
This patch adds the following compilation warning:prog/sensord/lib.c: In function ‘loadConfig’:prog/sensord/lib.c:72: warning: ‘ret’ may be used uninitialized in this function Please fix.
prog/sensord/lib.c line 23
Quoted 12 lines
lib.c | 76 +++++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 41 insertions(+), 35 deletions(-)prog/sensord/lib.c +1
-1@@ -20,7 +20,7 @@* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301 USA. */ - +#include <errno.h>
I do like a blank line before the first include ;)
Quoted 40 lines
prog/sensord/lib.c +0
-33#include <stdio.h> #include <stdlib.h> #include <string.h> @@ -34,46 +34,59 @@static int loadConfig (const char *cfgPath,int reload) { -struct stat stats; -FILE *cfg =NULL ; -int ret =0 ; - -if (cfgPath && !strcmp (cfgPath,"-" )) { -if (!reload) { -if ((ret =sensors_init (stdin))) { -sensorLog (LOG_ERR , -"Error loading sensors configuration file <stdin>: %s" , -sensors_strerror (ret)); - ret =12 ; - } - } - }else if (cfgPath &&stat (cfgPath, &stats) <0 ) { -sensorLog (LOG_ERR , -"Error stating sensors configuration file: %s" , - cfgPath); - ret =10 ; - }else { -if (reload) { -sensorLog (LOG_INFO ,"configuration reloading" ); -sensors_cleanup (); - } -if (cfgPath && !(cfg =fopen (cfgPath,"r" ))) { -sensorLog (LOG_ERR , -"Error opening sensors configuration file: %s" , - cfgPath); - ret =11 ; - }else if ((ret =sensors_init (cfg))) { -sensorLog (LOG_ERR , -"Error loading sensors configuration file %s: %s" , - cfgPath ? cfgPath :"(default)" , -sensors_strerror (ret));
prog/sensord/lib.c line 40
Quoted 10 lines
prog/sensord/lib.c +3
-6- ret =11 ; - } -if (cfg) -fclose (cfg); - } +int ret; +FILE *fp; -return ret; +/* Load default configuratinon. */
Typo: configuration.
prog/sensord/lib.c line 43
Quoted 3 lines
prog/sensord/lib.c +3
-0+if (!cfgPath) { +if (reload) +sensors_cleanup ();
The original code was logging that the configuration file was being reloaded. This seemed a useful feature to me, why did you remove it?
prog/sensord/lib.c line 47
Quoted 4 lines
prog/sensord/lib.c +4
-0+ + ret =sensors_init (NULL ); +if (ret) { + sensorLog(LOG_ERR, "Error while loading default"
I suggest dropping the "while" for consistency.
prog/sensord/lib.c line 56
Quoted 9 lines
prog/sensord/lib.c +9
-0+" configuration file: %s" , + sensors_strerror(ret)); +return -1 ; + } +return 0 ; + } + +/* Read config from stdin. */ +if (!strcmp (cfgPath,"-" )) {
As a side note, reading the configuration from stdin doesn't strike me as something terribly useful. I wouldn't mind dropping support for this, to make the code more simple.
Quoted 7 lines
prog/sensord/lib.c +7
-0+if (reload) +return 0 ; + + ret =sensors_init (stdin); +if (ret) { + sensorLog(LOG_ERR, "Error loading sensors" + " configuration file <stdin>: %s",
prog/sensord/lib.c line 73
Quoted 10 lines
prog/sensord/lib.c +10
-0+ sensors_strerror(ret)); +return -1 ; + } +return 0 ; + } + + fp =fopen (cfgPath,"r" ); +if (!fp) { +sensorLog (LOG_ERR ,"Error opening config file %s: %s" , +sensors_strerror (ret));
This call to sensors_strerror() is clearly incorrect, fopen() isn't part of the libsensors API ;)
prog/sensord/lib.c line 80
Quoted 7 lines
prog/sensord/lib.c +7
-0+return -1 ; + } + + ret =sensors_init (fp); +if (ret) { +if (reload) +sensors_cleanup ();
The logic looks all wrong to me. In reload mode, you have to call sensors_cleanup() _before_ sensors_init()!
prog/sensord/lib.c line 83
Quoted 3 lines
prog/sensord/lib.c +3
-0+ +sensorLog (LOG_ERR ,"Error loading sensors config file %s: %s" , + cfgPath,sensors_strerror (ret));
It would also be nice to consistently use "config file" or "configuration file" in all messages. I think the latter has my preference.
Quoted 9 lines
prog/sensord/lib.c +6
-0+fclose (fp); +return -1 ; + } +fclose (fp); + +return 0 ; }int loadLib (const char *cfgPath)
Signature · 6 lines
Jean Delvare _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Quoted 10 lines · Jean Delvare
Quoted 8 lines · Andre Prendel
This patch does some refactoring of the
loadConfig()function.
---* Simplifying the conditions makes code flow clearer and eliminates long lines (> 80 chars).
* Removed uselessstat()call.
* Return -1 in error case, instead of several positiv values (never defined).
Quoted 6 lines
Spelling: positive.
If you make
loadConfig()always return -1 on error then you also want to clean up the error message that is printed whenreloadLib()fails, to no longer include the (now pointless) error value.
You're right, thanks. It seems you're a very attentive reviewer :)
Quoted 4 lines
This patch adds the following compilation warning:
prog/sensord/lib.c: In function ‘loadConfig’:prog/sensord/lib.c:72: warning: ‘ret’ may be used uninitialized in this function Please fix.
Will do that. It's related to the wrong sensors_strerror() call you found.
Quoted 4 lines
Quoted 4 lines
lib.c | 76 +++++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 41 insertions(+), 35 deletions(-)
Quoted 10 lines
prog/sensord/lib.c line 23
Quoted 8 lines
prog/sensord/lib.c +1
-1@@ -20,7 +20,7 @@* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301 USA. */ - +#include <errno.h> I do like a blank line before the first include ;)
That's fine with me.
Quoted 49 lines
Quoted 49 lines
prog/sensord/lib.c +2
-38#include <stdio.h> #include <stdlib.h> #include <string.h> @@ -34,46 +34,59 @@static int loadConfig (const char *cfgPath,int reload) { -struct stat stats; -FILE *cfg =NULL ; -int ret =0 ; - -if (cfgPath && !strcmp (cfgPath,"-" )) { -if (!reload) { -if ((ret =sensors_init (stdin))) { -sensorLog (LOG_ERR , -"Error loading sensors configuration file <stdin>: %s" , -sensors_strerror (ret)); - ret =12 ; - } - } - }else if (cfgPath &&stat (cfgPath, &stats) <0 ) { -sensorLog (LOG_ERR , -"Error stating sensors configuration file: %s" , - cfgPath); - ret =10 ; - }else { -if (reload) { -sensorLog (LOG_INFO ,"configuration reloading" ); -sensors_cleanup (); - } -if (cfgPath && !(cfg =fopen (cfgPath,"r" ))) { -sensorLog (LOG_ERR , -"Error opening sensors configuration file: %s" , - cfgPath); - ret =11 ; - }else if ((ret =sensors_init (cfg))) { -sensorLog (LOG_ERR , -"Error loading sensors configuration file %s: %s" , - cfgPath ? cfgPath :"(default)" , -sensors_strerror (ret)); - ret =11 ; - } -if (cfg) -fclose (cfg); - } +int ret; +FILE *fp;
Quoted 4 lines
prog/sensord/lib.c line 40
Quoted 2 lines
prog/sensord/lib.c +1
-1-return ret; +/* Load default configuratinon. */ Typo: configuration.
Thanks.
Quoted 8 lines
prog/sensord/lib.c line 43
Quoted 4 lines
prog/sensord/lib.c +3
-0+if (!cfgPath) { +if (reload) +sensors_cleanup ();The original code was logging that the configuration file was being reloaded. This seemed a useful feature to me, why did you remove it?
Not a certain reason. So I will bring back the logging.
Quoted 6 lines
prog/sensord/lib.c line 47
Quoted 4 lines
prog/sensord/lib.c +4
-0+ + ret =sensors_init (NULL ); +if (ret) { + sensorLog(LOG_ERR, "Error while loading default"I suggest dropping the "while" for consistency.
Will do it.
Quoted 11 lines
prog/sensord/lib.c line 56
Quoted 10 lines
prog/sensord/lib.c +9
-0+" configuration file: %s" , + sensors_strerror(ret)); +return -1 ; + } +return 0 ; + } + +/* Read config from stdin. */ +if (!strcmp (cfgPath,"-" )) {
Quoted 3 lines
As a side note, reading the configuration from stdin doesn't strike me as something terribly useful. I wouldn't mind dropping support for this, to make the code more simple.
I agree. Will drop that.
Quoted 13 lines
Quoted 13 lines
prog/sensord/lib.c +12
-0+if (reload) +return 0 ; + + ret =sensors_init (stdin); +if (ret) { +sensorLog (LOG_ERR ,"Error loading sensors" +" configuration file <stdin>: %s" , +sensors_strerror (ret)); +return -1 ; + } +return 0 ; + }
Quoted 8 lines
prog/sensord/lib.c line 73
Quoted 5 lines
prog/sensord/lib.c +5
-0+ + fp =fopen (cfgPath,"r" ); +if (!fp) { +sensorLog (LOG_ERR ,"Error opening config file %s: %s" , +sensors_strerror (ret));This call to
sensors_strerror()is clearly incorrect,fopen()isn't part of the libsensors API ;)
You're right again. Will fix it.
Quoted 10 lines
prog/sensord/lib.c line 80
Quoted 7 lines
prog/sensord/lib.c +7
-0+return -1 ; + } + + ret =sensors_init (fp); +if (ret) { +if (reload) +sensors_cleanup ();The logic looks all wrong to me. In reload mode, you have to call
sensors_cleanup()_before_sensors_init()!
And again :)
Quoted 8 lines
prog/sensord/lib.c line 83
Quoted 4 lines
prog/sensord/lib.c +3
-0+ +sensorLog (LOG_ERR ,"Error loading sensors config file %s: %s" , + cfgPath,sensors_strerror (ret));It would also be nice to consistently use "config file" or "configuration file" in all messages. I think the latter has my preference.
And again :))
Quoted 9 lines
Quoted 9 lines
prog/sensord/lib.c +6
-0+fclose (fp); +return -1 ; + } +fclose (fp); + +return 0 ; }
Quoted 5 lines
Quoted 1 line
prog/sensord/lib.c +0
-0int loadLib (const char *cfgPath)Signature · 1 line
Jean Delvare
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors