summaryrefslogtreecommitdiffstats
path: root/src/lua-apk.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua-apk.c')
-rw-r--r--src/lua-apk.c82
1 files changed, 76 insertions, 6 deletions
diff --git a/src/lua-apk.c b/src/lua-apk.c
index 835d379..cb36784 100644
--- a/src/lua-apk.c
+++ b/src/lua-apk.c
@@ -9,7 +9,8 @@
#include "apk_database.h"
#define LIBNAME "apk"
-#define APKDB_META "apk_database"
+#define APK_DB_META "apk_database"
+#define APK_IPKG_META "apk_installed_package"
int apk_verbosity;
unsigned int apk_flags;
@@ -131,9 +132,9 @@ static struct apk_database *checkdb(lua_State *L, int index)
{
struct apk_database *db;
luaL_checktype(L, index, LUA_TUSERDATA);
- db = (struct apk_database *) luaL_checkudata(L, index, APKDB_META);
+ db = (struct apk_database *) luaL_checkudata(L, index, APK_DB_META);
if (db == NULL)
- luaL_typerror(L, index, APKDB_META);
+ luaL_typerror(L, index, APK_DB_META);
return db;
}
@@ -152,7 +153,7 @@ static int Papk_db_open(lua_State *L)
opts.open_flags |= APK_OPENF_READ;
db = lua_newuserdata(L, sizeof(struct apk_database));
- luaL_getmetatable(L, APKDB_META);
+ luaL_getmetatable(L, APK_DB_META);
lua_setmetatable(L, -2);
r = apk_db_open(db, &opts);
@@ -168,7 +169,6 @@ static int Papk_db_close(lua_State *L)
return 0;
}
-
static int push_package(lua_State *L, struct apk_package *pkg)
{
if (pkg == NULL) {
@@ -193,12 +193,82 @@ 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;
+}
+
+// Iterator of all installed packages
+struct apk_installed_package_iterator {
+ struct list_head *end;
+ struct apk_installed_package *node;
+};
+
+static int iterate_installed(lua_State *L)
+{
+ struct apk_installed_package_iterator *i;
+ struct apk_installed_package *ipkg;
+ i = (struct apk_installed_package_iterator *)lua_touserdata(L, lua_upvalueindex(1));
+ ipkg = i->node;
+
+ if (&ipkg->installed_pkgs_list == i->end)
+ return 0;
+
+ i->node = list_entry(ipkg->installed_pkgs_list.next,
+ typeof(*ipkg), installed_pkgs_list);
+ return push_package(L, ipkg->pkg);
+
+}
+static int Pinstalled(lua_State *L)
+{
+ struct apk_database *db = checkdb(L, 1);
+ struct apk_installed_package_iterator *i;
+
+ i = (struct apk_installed_package_iterator *) lua_newuserdata(L, sizeof(*i));
+ i->end = &db->installed.packages;
+ i->node = list_entry((&db->installed.packages)->next,
+ struct apk_installed_package,
+ installed_pkgs_list);
+
+ lua_pushcclosure(L, iterate_installed, 1);
+ 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},
+ {"installed", Pinstalled},
{NULL, NULL}
};
@@ -214,7 +284,7 @@ LUALIB_API int luaopen_apk(lua_State *L)
lua_pushliteral(L, APK_VERSION);
lua_settable(L, -3);
- luaL_newmetatable(L, APKDB_META);
+ luaL_newmetatable(L, APK_DB_META);
luaL_register(L, NULL, reg_apk_db_meta_methods);
lua_pushliteral(L, "__index");
lua_pushvalue(L, -3);