From 2804d01d89ca79082f15e217e46269d6ef7e5c1f Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Sat, 19 Feb 2011 09:41:15 +0000 Subject: lua: implement exists/is_installed Tests whether given package string is installed --- src/lua-apk.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/lua-apk.c b/src/lua-apk.c index 835d379..779dfd1 100644 --- a/src/lua-apk.c +++ b/src/lua-apk.c @@ -193,12 +193,45 @@ static int Papk_who_owns(lua_State *L) return push_package(L, pkg); } +static int Papk_exists(lua_State *L) +{ + struct apk_database *db = checkdb(L, 1); + const char *depstr = luaL_checkstring(L, 2); + struct apk_dependency dep; + struct apk_name *name; + struct apk_package *pkg; + int i, r; + + r = apk_dep_from_blob(&dep, db, APK_BLOB_STR(depstr)); + if (r != 0) + goto ret_nil; + + name = dep.name; + for (i = 0; i < name->pkgs->num; i++) { + pkg = name->pkgs->item[i]; + if (pkg->ipkg != NULL) + break; + } + if (i >= name->pkgs->num) + goto ret_nil; + + if (!apk_dep_is_satisfied(&dep, pkg)) + goto ret_nil; + + return push_package(L, pkg); +ret_nil: + lua_pushnil(L); + return 1; +} + static const luaL_reg reg_apk_methods[] = { {"version_validate", Pversion_validate}, {"version_compare", Pversion_compare}, {"version_is_less", Pversion_is_less}, {"db_open", Papk_db_open}, {"who_owns", Papk_who_owns}, + {"exists", Papk_exists}, + {"is_installed", Papk_exists}, {NULL, NULL} }; -- cgit v1.2.3