diff options
author | David Sommerseth <davids@redhat.com> | 2009-12-17 19:13:58 +0100 |
---|---|---|
committer | David Sommerseth <davids@redhat.com> | 2009-12-17 19:13:58 +0100 |
commit | 2963939d58332c509d1849e438352ea0e3490e5b (patch) | |
tree | 84d5d5ed52eeb83f0d68b91c7dc164af53f6b5af | |
parent | 7a93063f3c85de467890902c3e1cdf3913f1d5c8 (diff) | |
download | python-dmidecode-2963939d58332c509d1849e438352ea0e3490e5b.tar.gz python-dmidecode-2963939d58332c509d1849e438352ea0e3490e5b.tar.xz python-dmidecode-2963939d58332c509d1849e438352ea0e3490e5b.zip |
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.
-rw-r--r-- | src/dmidecodemodule.c | 22 |
1 files 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) |