diff options
-rw-r--r-- | README.md | 11 | ||||
-rw-r--r-- | REFLECTION | 9 | ||||
-rw-r--r-- | package.xml | 3 | ||||
-rw-r--r-- | rpminfo.c | 37 | ||||
-rw-r--r-- | tests/003-rpminfo.phpt | 9 | ||||
-rw-r--r-- | tests/006-rpminfo-errors.phpt | 36 |
6 files changed, 81 insertions, 24 deletions
@@ -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 + ---- @@ -1,11 +1,16 @@ -Extension [ <persistent> extension #15 rpminfo version 0.1.0 ] { +Extension [ <persistent> extension #15 rpminfo version 0.1.2-dev ] { + + - Constants [1] { + Constant [ string RPMVERSION ] { 4.14.0 } + } - Functions { Function [ <internal:rpminfo> function rpminfo ] { - - Parameters [2] { + - Parameters [3] { Parameter #0 [ <required> $path ] Parameter #1 [ <optional> $full ] + Parameter #2 [ <optional> &$error ] } } Function [ <internal:rpminfo> function rpmvercmp ] { diff --git a/package.xml b/package.xml index d806c47..57ef292 100644 --- a/package.xml +++ b/package.xml @@ -21,7 +21,7 @@ </stability> <license>PHP 3.01</license> <notes> -- +- rpminfo(): add option to retrieve error message instead of raising a warning </notes> <contents> <dir name="/"> @@ -44,6 +44,7 @@ <file name="003-rpminfo.phpt" role="test"/> <file name="004-constants.phpt" role="test"/> <file name="005-rpminfo-full.phpt" role="test"/> + <file name="006-rpminfo-errors.phpt" role="test"/> <file name="bidon.rpm" role="test"/> <file name="bidon-src.rpm" role="test"/> </dir> @@ -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-- <?php var_dump(rpminfo(__DIR__ . "/bidon.rpm")); -// Errors -var_dump(rpminfo(__DIR__ . "/missing.rpm")); -var_dump(rpminfo(__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-- +<?php if (!extension_loaded("rpminfo")) print "skip"; ?> +--FILE-- +<?php +echo "+ PHP Warnings\n"; +var_dump(rpminfo(__DIR__ . "/missing.rpm")); +var_dump(rpminfo(__FILE__)); + +echo "\n+ PHP Warnings\n"; +var_dump(rpminfo(__DIR__ . "/missing.rpm", true, $error), + $error); +var_dump(rpminfo(__FILE__, false, $error), + $error); +var_dump(is_array(rpminfo(__DIR__ . "/bidon.rpm", false, $error)), + $error); +?> +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 |