From 06a93ae1423434b65592f3a91bbbf13fccf475b8 Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Wed, 11 Mar 2020 18:08:53 +0100 Subject: revert previous and add rpmdbsearch function instead --- README.md | 33 ++++++++++++++------- package.xml | 6 ++-- rpminfo.c | 72 ++++++++++++++++++++++++++++++++++++++++------ tests/008-rpmdbinfo2.phpt | 16 ----------- tests/008-rpmdbsearch.phpt | 22 ++++++++++++++ 5 files changed, 112 insertions(+), 37 deletions(-) delete mode 100644 tests/008-rpmdbinfo2.phpt create mode 100644 tests/008-rpmdbsearch.phpt diff --git a/README.md b/README.md index 8d42ae9..9f5e430 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ Retrieve information from rpm database about installed packages using glob or re The return value is an array of hash tables, or false if it fails. $ php -a - php > print_r(rpmdbinfo("php-pecl-r*", false, RPM_MATCH_GLOB)); + php > print_r(rpmdbsearch("php-pecl-r*", RPM_TAG_NAME, RPM_MATCH_GLOB)); Array ( [0] => Array @@ -121,10 +121,10 @@ The return value is an array of hash tables, or false if it fails. ) [1] => Array ( - [Name] => php-pecl-request - [Version] => 1.0.0 - [Release] => 0.11.b2.fc31.remi.7.3 - [Summary] => Server-side request and response objects + [Name] => php-pecl-redis5 + [Version] => 5.2.0 + [Release] => 1.fc31.remi.7.3 + [Summary] => Extension for communicating with the Redis key-value store [Arch] => x86_64 ) [2] => Array @@ -138,7 +138,7 @@ The return value is an array of hash tables, or false if it fails. ) $ php -a - php > print_r(rpmdbinfo("^php-pecl-r", false, RPM_MATCH_REGEX)); + php > print_r(rpmdbsearch("^php-pecl-r", RPM_TAG_NAME, RPM_MATCH_REGEX)); Array ( [0] => Array @@ -151,10 +151,10 @@ The return value is an array of hash tables, or false if it fails. ) [1] => Array ( - [Name] => php-pecl-request - [Version] => 1.0.0 - [Release] => 0.11.b2.fc31.remi.7.3 - [Summary] => Server-side request and response objects + [Name] => php-pecl-redis5 + [Version] => 5.2.0 + [Release] => 1.fc31.remi.7.3 + [Summary] => Extension for communicating with the Redis key-value store [Arch] => x86_64 ) [2] => Array @@ -167,6 +167,19 @@ The return value is an array of hash tables, or false if it fails. ) ) + $ php -a + php > print_r(rpmdbsearch(PHP_BINARY, RPM_TAG_INSTFILENAMES)); + Array + ( + [0] => Array + ( + [Name] => php-cli + [Version] => 7.3.15 + [Release] => 1.fc31.remi + [Summary] => Command-line interface for PHP + [Arch] => x86_64 + ) + ) ---- diff --git a/package.xml b/package.xml index 41ff33f..2b3d729 100644 --- a/package.xml +++ b/package.xml @@ -26,8 +26,8 @@ Available functions: PHP 3.01 -- add match_mode parameters to rpmdbinfo function allowing - to search packages with name matching a glob or a regex. +- add rpmdbsearch function to search packages using + name, owned files, requires, provides... @@ -53,7 +53,7 @@ Available functions: - + diff --git a/rpminfo.c b/rpminfo.c index f20d605..2244609 100644 --- a/rpminfo.c +++ b/rpminfo.c @@ -250,37 +250,86 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_rpmdbinfo, 0, 0, 1) ZEND_ARG_INFO(0, match_mode) ZEND_END_ARG_INFO() -/* {{{ proto array rpmdbinfo(string name [, bool full [, string &$error]) - Retrieve information from a RPM file */ +/* {{{ proto array rpmdbinfo(string name [, bool full]) + Retrieve information from an installed RPM */ PHP_FUNCTION(rpmdbinfo) { char *name; size_t len; zend_bool full = 0; + Header h; + rpmdb db; + rpmdbMatchIterator di; + rpmts ts = rpminfo_getts(_RPMVSF_NODIGESTS | _RPMVSF_NOSIGNATURES | RPMVSF_NOHDRCHK); + + if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|b", &name, &len, &full) == FAILURE) { + return; + } + + rpmtsOpenDB(ts, O_RDONLY); + db = rpmtsGetRdb(ts); + di = rpmdbInitIterator(db, RPMTAG_NAME, name, len); + if (!di) { + // Not found + rpmtsCloseDB(ts); + RETURN_FALSE; + } + + array_init(return_value); + while ((h = rpmdbNextIterator(di)) != NULL) { + zval tmp; + rpm_header_to_zval(&tmp, h, full); + add_next_index_zval(return_value, &tmp); + } + + rpmdbFreeIterator(di); + rpmtsCloseDB(ts); +} +/* }}} */ + +ZEND_BEGIN_ARG_INFO_EX(arginfo_rpmdbsearch, 0, 0, 2) + ZEND_ARG_INFO(0, tag_name) + ZEND_ARG_INFO(0, pattern) + ZEND_ARG_INFO(0, mode) +ZEND_END_ARG_INFO() + +/* {{{ proto array rpmdbsearch(string pattern [, integer tag_name = RPM_TAG_NAME [, integer mode]]) + Search information from installed RPMs */ +PHP_FUNCTION(rpmdbsearch) +{ + char *name; + size_t len; + zend_long crit = RPMTAG_NAME; zend_long mode = 0; Header h; rpmdb db; rpmdbMatchIterator di; rpmts ts = rpminfo_getts(_RPMVSF_NODIGESTS | _RPMVSF_NOSIGNATURES | RPMVSF_NOHDRCHK); - if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|bl", &name, &len, &full, &mode) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|ll", &name, &len, &crit, &mode) == FAILURE) { return; } + if (mode && crit != RPMTAG_NAME) { + php_error_docref(NULL, E_WARNING, "Mode only allowed for name"); + mode = 0; + } + rpmtsOpenDB(ts, O_RDONLY); db = rpmtsGetRdb(ts); if (mode) { - di = rpmdbInitIterator(db, RPMTAG_NAME, NULL, 0); + di = rpmdbInitIterator(db, crit, NULL, len); } else { - di = rpmdbInitIterator(db, RPMTAG_NAME, name, len); + di = rpmdbInitIterator(db, crit, name, len); } if (!di) { // Not found rpmtsCloseDB(ts); RETURN_FALSE; } + if (mode) { - if (rpmdbSetIteratorRE(di, RPMTAG_NAME, mode, name)) { + if (rpmdbSetIteratorRE(di, crit, mode, name)) { php_error_docref(NULL, E_WARNING, "Can't set filter"); rpmtsCloseDB(ts); RETURN_FALSE; @@ -290,7 +339,7 @@ PHP_FUNCTION(rpmdbinfo) array_init(return_value); while ((h = rpmdbNextIterator(di)) != NULL) { zval tmp; - rpm_header_to_zval(&tmp, h, full); + rpm_header_to_zval(&tmp, h, 0); add_next_index_zval(return_value, &tmp); } @@ -403,10 +452,16 @@ PHP_MINIT_FUNCTION(rpminfo) REGISTER_LONG_CONSTANT("RPMSENSE_KEYRING", RPMSENSE_KEYRING, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("RPMSENSE_CONFIG", RPMSENSE_CONFIG, CONST_CS | CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("RPM_MATCH_EQUAL", 0, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("RPM_MATCH_DEFAULT", RPMMIRE_DEFAULT, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("RPM_MATCH_STRCMP", RPMMIRE_STRCMP, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("RPM_MATCH_REGEX", RPMMIRE_REGEX, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("RPM_MATCH_GLOB", RPMMIRE_GLOB, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("RPM_TAG_NAME", RPMTAG_NAME, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("RPM_TAG_INSTFILENAMES", RPMTAG_INSTFILENAMES, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("RPM_TAG_REQUIRENAME", RPMTAG_REQUIRENAME, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("RPM_TAG_PROVIDENAME", RPMTAG_PROVIDENAME, CONST_CS | CONST_PERSISTENT); + return SUCCESS; } /* }}} */ @@ -466,6 +521,7 @@ PHP_GSHUTDOWN_FUNCTION(rpminfo) */ const zend_function_entry rpminfo_functions[] = { PHP_FE(rpmdbinfo, arginfo_rpmdbinfo) + PHP_FE(rpmdbsearch, arginfo_rpmdbsearch) PHP_FE(rpminfo, arginfo_rpminfo) PHP_FE(rpmvercmp, arginfo_rpmvercmp) PHP_FE_END diff --git a/tests/008-rpmdbinfo2.phpt b/tests/008-rpmdbinfo2.phpt deleted file mode 100644 index 940ad61..0000000 --- a/tests/008-rpmdbinfo2.phpt +++ /dev/null @@ -1,16 +0,0 @@ ---TEST-- -Check for rpmdbinfo function ---SKIPIF-- - ---FILE-- - 1); -$a = rpmdbinfo('^php', false, RPM_MATCH_REGEX); -var_dump(count($a) > 1); -?> -Done ---EXPECTF-- -bool(true) -bool(true) -Done diff --git a/tests/008-rpmdbsearch.phpt b/tests/008-rpmdbsearch.phpt new file mode 100644 index 0000000..09800a6 --- /dev/null +++ b/tests/008-rpmdbsearch.phpt @@ -0,0 +1,22 @@ +--TEST-- +Check for rpmdbinfo function +--SKIPIF-- + +--FILE-- + 1); + +$a = rpmdbsearch('^php', RPM_TAG_NAME, RPM_MATCH_REGEX); +var_dump(count($a) > 1); + +$a = rpmdbsearch(PHP_BINARY, RPM_TAG_INSTFILENAMES); +var_dump(count($a) == 1); + +?> +Done +--EXPECTF-- +bool(true) +bool(true) +bool(true) +Done -- cgit