From 2963939d58332c509d1849e438352ea0e3490e5b Mon Sep 17 00:00:00 2001 From: David Sommerseth Date: Thu, 17 Dec 2009 19:13:58 +0100 Subject: Improved set_dev() function This function would often fail during the unit test. Most probably because the complete error checking was a bit odd. Rewritten the error checks to be a bit more sane. Also improved error situations, where it will now throw an exception with an error message instead of just returning False. --- src/dmidecodemodule.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/dmidecodemodule.c b/src/dmidecodemodule.c index 5c74124..003051b 100644 --- a/src/dmidecodemodule.c +++ b/src/dmidecodemodule.c @@ -594,24 +594,36 @@ static PyObject *dmidecode_set_dev(PyObject * self, PyObject * arg) && (strcmp(global_options->dumpfile, f) == 0) ) { Py_RETURN_TRUE; } + if( (f == NULL) || (strlen(f) < 0) ) { + PyReturnError(PyExc_RuntimeError, "set_dev() file name string cannot be empty"); + } - stat(f, &buf); + errno = 0; + if( stat(f, &buf) < 0 ) { + if( errno == ENOENT ) { + // If this file does not exist, that's okay. + // python-dmidecode will create it. + global_options->dumpfile = strdup(f); + Py_RETURN_TRUE; + } + PyReturnError(PyExc_RuntimeError, strerror(errno)); + } if(S_ISCHR(buf.st_mode)) { - if(memcmp(PyString_AsString(arg), "/dev/mem", 8) == 0) { + if(memcmp(f, "/dev/mem", 8) == 0) { if( global_options->dumpfile != NULL ) { free(global_options->dumpfile); global_options->dumpfile = NULL; } Py_RETURN_TRUE; } else { - Py_RETURN_FALSE; + PyReturnError(PyExc_RuntimeError, "Invalid memory device: %s", f); } - } else if(!S_ISDIR(buf.st_mode)) { + } else if(S_ISREG(buf.st_mode) || S_ISLNK(buf.st_mode) ) { global_options->dumpfile = strdup(f); Py_RETURN_TRUE; } } - Py_RETURN_FALSE; + PyReturnError(PyExc_RuntimeError, "set_dev(): Invalid input"); } static PyObject *dmidecode_set_pythonxmlmap(PyObject * self, PyObject * arg) -- cgit