diff options
author | Remi Collet <remi@remirepo.net> | 2020-03-12 14:45:49 +0100 |
---|---|---|
committer | Remi Collet <remi@remirepo.net> | 2020-03-12 14:45:49 +0100 |
commit | d80ca1bb5098a8ee32c642e28cda591b451d47c5 (patch) | |
tree | 90ce30bb8baea3545e4f482eb941beaf7ee42da8 | |
parent | 43fd291ac199efdea4addbf3ec45a16dd3962b7f (diff) |
fix search for various tags
-rw-r--r-- | package.xml | 3 | ||||
-rw-r--r-- | rpminfo.c | 75 | ||||
-rw-r--r-- | tests/008-rpmdbsearch.phpt | 33 |
3 files changed, 104 insertions, 7 deletions
diff --git a/package.xml b/package.xml index c6dc892..e684c9e 100644 --- a/package.xml +++ b/package.xml @@ -27,7 +27,8 @@ Available functions: </stability> <license>PHP 3.01</license> <notes> -- +- allow search by Pkgid, Hdrid, Installtid with specific input +- fix search with various other tags (Version, ...) </notes> <contents> <dir name="/"> @@ -286,6 +286,48 @@ PHP_FUNCTION(rpmdbinfo) } /* }}} */ +static unsigned char nibble(char c) { + if (c >= '0' && c <= '9') { + return (c - '0'); + } + if (c >= 'a' && c <= 'f') { + return (c - 'a') + 10; + } + if (c >= 'A' && c <= 'F') { + return (c - 'A') + 10; + } + return 0; +} + +static int hex2bin(const char *hex, char *bin, int len) { + int i; + + for (i=0 ; (i+1)<len ; i+=2, hex+=2, bin++) { + *bin = nibble(hex[0]) << 4 | nibble(hex[1]); + } + + return i/2; +} + +static int canUseRe(zend_long tag) { + if (tag == RPMTAG_GROUP || + tag == RPMTAG_TRIGGERNAME || + tag == RPMTAG_PKGID || + tag == RPMTAG_HDRID || + tag == RPMTAG_INSTALLTID || + tag == RPMTAG_CONFLICTNAME || + tag == RPMTAG_REQUIRENAME || + tag == RPMTAG_PROVIDENAME || + tag == RPMTAG_SUGGESTNAME || + tag == RPMTAG_SUPPLEMENTNAME || + tag == RPMTAG_RECOMMENDNAME || + tag == RPMTAG_ENHANCENAME || + tag == RPMTAG_INSTFILENAMES) { + return 0; + } + return 1; +} + ZEND_BEGIN_ARG_INFO_EX(arginfo_rpmdbsearch, 0, 0, 1) ZEND_ARG_INFO(0, pattern) ZEND_ARG_INFO(0, rpmtag) @@ -296,6 +338,8 @@ ZEND_END_ARG_INFO() Search information from installed RPMs */ PHP_FUNCTION(rpmdbsearch) { + char MD5[16]; + rpm_tid_t tid; char *name; size_t len; zend_long crit = RPMTAG_NAME; @@ -303,21 +347,41 @@ PHP_FUNCTION(rpmdbsearch) Header h; rpmdb db; rpmdbMatchIterator di; + int usere; rpmts ts = rpminfo_getts(_RPMVSF_NODIGESTS | _RPMVSF_NOSIGNATURES | RPMVSF_NOHDRCHK); 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"); + usere = canUseRe(crit); + if (mode && !usere) { + php_error_docref(NULL, E_WARNING, "Mode not allowed for this criterion"); mode = 0; } + if (crit == RPMTAG_PKGID) { + if (len != 32) { + php_error_docref(NULL, E_WARNING, "Bad length for PKGID, 32 expected"); + RETURN_FALSE; + } + len = hex2bin(name, MD5, len); + name = MD5; + } else if (crit == RPMTAG_HDRID) { + if (len != 40) { + php_error_docref(NULL, E_WARNING, "Bad length for HDRID, 40 expected"); + RETURN_FALSE; + } + } else if (crit == RPMTAG_INSTALLTID) { + tid = atol(name); + name = (char *)&tid; + len = sizeof(tid); + } + rpmtsOpenDB(ts, O_RDONLY); db = rpmtsGetRdb(ts); - if (mode) { - di = rpmdbInitIterator(db, crit, NULL, len); + if (usere) { + di = rpmdbInitIterator(db, RPMDBI_PACKAGES, NULL, len); } else { di = rpmdbInitIterator(db, crit, name, len); } @@ -326,8 +390,7 @@ PHP_FUNCTION(rpmdbsearch) rpmtsCloseDB(ts); RETURN_FALSE; } - - if (mode) { + if (usere) { if (rpmdbSetIteratorRE(di, crit, mode, name)) { php_error_docref(NULL, E_WARNING, "Can't set filter"); rpmtsCloseDB(ts); diff --git a/tests/008-rpmdbsearch.phpt b/tests/008-rpmdbsearch.phpt index 28357e0..d22a4bd 100644 --- a/tests/008-rpmdbsearch.phpt +++ b/tests/008-rpmdbsearch.phpt @@ -4,19 +4,52 @@ Check for rpmdbinfo function <?php if (!extension_loaded("rpminfo")) print "skip"; ?> --FILE-- <?php +echo "Name / glob\n"; $a = rpmdbsearch('php*', RPMTAG_NAME , RPMMIRE_GLOB); var_dump(count($a) > 1); +echo "Name / regex\n"; $a = rpmdbsearch('^php', RPMTAG_NAME, RPMMIRE_REGEX); var_dump(count($a) > 1); +echo "Installed file\n"; $a = rpmdbsearch(PHP_BINARY, RPMTAG_INSTFILENAMES); var_dump(count($a) == 1); +$phprpm = $a[0]['Name']; +$p = rpmdbinfo($phprpm, 1); + +echo "Pkgid\n"; +$a = rpmdbsearch($p[0]['Sigmd5'], RPMTAG_PKGID); +var_dump($a[0]['Name'] == $phprpm); + +echo "Hdrid\n"; +$a = rpmdbsearch($p[0]['Sha1header'], RPMTAG_HDRID); +var_dump($a[0]['Name'] == $phprpm); + +echo "Installtid\n"; +$a = rpmdbsearch($p[0]['Installtid'], RPMTAG_INSTALLTID); +var_dump(count($a) >= 1); + +echo "Version\n"; +$a = rpmdbsearch($p[0]['Version'], RPMTAG_VERSION); +var_dump(count($a) > 1); + ?> Done --EXPECTF-- +Name / glob +bool(true) +Name / regex +bool(true) +Installed file +bool(true) +Pkgid +bool(true) +Hdrid bool(true) +Installtid bool(true) +Version bool(true) Done |