From 47013783578b41a000b755e2726f057ecd4cd1cb Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Tue, 30 Jan 2018 17:36:04 +0100 Subject: add option to retrieve error message instead of raising a warning --- README.md | 11 +++++++++++ REFLECTION | 9 +++++++-- package.xml | 3 ++- rpminfo.c | 37 +++++++++++++++++++++++++------------ tests/003-rpminfo.phpt | 9 --------- tests/006-rpminfo-errors.phpt | 36 ++++++++++++++++++++++++++++++++++++ 6 files changed, 81 insertions(+), 24 deletions(-) create mode 100644 tests/006-rpminfo-errors.phpt diff --git a/README.md b/README.md index d572920..cbdc106 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ The return value is a hash table, or false if it fails. [Release] => 1.fc25.remi [Arch] => x86_64 ) + php > print_r(rpminfo("tests/bidon.rpm", true)); Array ( @@ -66,6 +67,16 @@ The return value is a hash table, or false if it fails. ... [IsSource] => ) + + php > var_dump(rpminfo("missing.rpm")); + Warning: rpminfo(): Can't open 'missing.rpm': No such file or directory in php shell code on line 1 + bool(false) + + php > var_dump(rpminfo("missing.rpm", false, $error)); + bool(false) + php > echo $error; + Can't open 'missing.rpm': No such file or directory + ---- diff --git a/REFLECTION b/REFLECTION index 630606c..0c99556 100644 --- a/REFLECTION +++ b/REFLECTION @@ -1,11 +1,16 @@ -Extension [ extension #15 rpminfo version 0.1.0 ] { +Extension [ extension #15 rpminfo version 0.1.2-dev ] { + + - Constants [1] { + Constant [ string RPMVERSION ] { 4.14.0 } + } - Functions { Function [ function rpminfo ] { - - Parameters [2] { + - Parameters [3] { Parameter #0 [ $path ] Parameter #1 [ $full ] + Parameter #2 [ &$error ] } } Function [ function rpmvercmp ] { diff --git a/package.xml b/package.xml index d806c47..57ef292 100644 --- a/package.xml +++ b/package.xml @@ -21,7 +21,7 @@ PHP 3.01 -- +- rpminfo(): add option to retrieve error message instead of raising a warning @@ -44,6 +44,7 @@ + diff --git a/rpminfo.c b/rpminfo.c index fcc4041..9749198 100644 --- a/rpminfo.c +++ b/rpminfo.c @@ -45,30 +45,38 @@ static rpmts rpminfo_getts(rpmVSFlags flags) { ZEND_BEGIN_ARG_INFO_EX(arginfo_rpminfo, 0, 0, 1) ZEND_ARG_INFO(0, path) ZEND_ARG_INFO(0, full) + ZEND_ARG_INFO(1, error) ZEND_END_ARG_INFO() -/* {{{ proto array rpminfo(string path [, bool full]) +/* {{{ proto array rpminfo(string path [, bool full [, string &$error]) Retrieve information from a RPM file */ PHP_FUNCTION(rpminfo) { - char *path; + char *path, *e_msg; const char *val; - size_t len; + size_t len, e_len=0; zend_bool full = 0; + zval *error = NULL; FD_t f; - int rc; Header h; HeaderIterator hi; rpmTagVal tag; rpmTagType type; rpmts ts = rpminfo_getts(_RPMVSF_NODIGESTS | _RPMVSF_NOSIGNATURES | RPMVSF_NOHDRCHK); - if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|b", &path, &len, &full) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|bz", &path, &len, &full, &error) == FAILURE) { return; } + if (error) { + ZVAL_DEREF(error); + zval_dtor(error); + ZVAL_NULL(error); + } f = Fopen(path, "r"); if (f) { + int rc; + rc = rpmReadPackageFile(ts, f, "rpminfo", &h); if (rc == RPMRC_OK || rc == RPMRC_NOKEY || rc == RPMRC_NOTTRUSTED) { @@ -125,20 +133,25 @@ PHP_FUNCTION(rpminfo) return; } else if (rc == RPMRC_NOTFOUND) { - php_error_docref(NULL, E_WARNING, "Can't read '%s': Argument is not a RPM file", path); - + e_len = spprintf(&e_msg, 0, "Can't read '%s': Argument is not a RPM file", path); } else if (rc == RPMRC_NOTFOUND) { - php_error_docref(NULL, E_WARNING, "Can't read '%s': Error reading header from package", path); - + e_len = spprintf(&e_msg, 0, "Can't read '%s': Error reading header from package", path); } else { - php_error_docref(NULL, E_WARNING, "Can't read '%s': Unkown error", path); + e_len = spprintf(&e_msg, 0, "Can't read '%s': Unkown error", path); } Fclose(f); } else { - php_error_docref(NULL, E_WARNING, "Can't open '%s': %s", path, Fstrerror(f)); + e_len = spprintf(&e_msg, 0, "Can't open '%s': %s", path, Fstrerror(f)); + } + if (e_len) { + if (error) { + ZVAL_STRINGL(error, e_msg, e_len); + } else { + php_error_docref(NULL, E_WARNING, "%s", e_msg); + } + efree(e_msg); } - RETURN_FALSE; } /* }}} */ diff --git a/tests/003-rpminfo.phpt b/tests/003-rpminfo.phpt index f7cee82..f878c7f 100644 --- a/tests/003-rpminfo.phpt +++ b/tests/003-rpminfo.phpt @@ -5,9 +5,6 @@ Check for rpminfo function --FILE-- Done --EXPECTF-- @@ -21,10 +18,4 @@ array(4) { ["Arch"]=> string(6) "x86_64" } - -Warning: rpminfo(): Can't open '%s/tests/missing.rpm': No such file or directory in %s/003-rpminfo.php on line %d -bool(false) - -Warning: rpminfo(): Can't read '%s/tests/003-rpminfo.php': Argument is not a RPM file in %s/003-rpminfo.php on line %d -bool(false) Done diff --git a/tests/006-rpminfo-errors.phpt b/tests/006-rpminfo-errors.phpt new file mode 100644 index 0000000..78ab21d --- /dev/null +++ b/tests/006-rpminfo-errors.phpt @@ -0,0 +1,36 @@ +--TEST-- +Check for rpminfo function errors +--SKIPIF-- + +--FILE-- + +Done +--EXPECTF-- ++ PHP Warnings + +Warning: rpminfo(): Can't open '%s/tests/missing.rpm': No such file or directory in %s on line %d +bool(false) + +Warning: rpminfo(): Can't read '%s/tests/006-rpminfo-errors.php': Argument is not a RPM file in %s on line %d +bool(false) + ++ PHP Warnings +bool(false) +string(75) "Can't open '%s/tests/missing.rpm': No such file or directory" +bool(false) +string(87) "Can't read '%s/tests/006-rpminfo-errors.php': Argument is not a RPM file" +bool(true) +NULL +Done -- cgit