summaryrefslogtreecommitdiff
path: root/src/lua-apk.c
diff options
context:
space:
mode:
authorNatanael Copa <ncopa@alpinelinux.org>2011-02-21 14:35:00 +0000
committerNatanael Copa <ncopa@alpinelinux.org>2011-09-15 17:13:08 +0000
commit391cd01b419e6ba4dbcd19d902a947126391bb26 (patch)
tree333e141d2ac67c9e3282c9278931b906cd15d331 /src/lua-apk.c
parent9e60a72b92c34d0bd6ce3791da68a37cccfe999b (diff)
downloadapk-tools-391cd01b419e6ba4dbcd19d902a947126391bb26.tar.gz
apk-tools-391cd01b419e6ba4dbcd19d902a947126391bb26.tar.bz2
apk-tools-391cd01b419e6ba4dbcd19d902a947126391bb26.tar.xz
apk-tools-391cd01b419e6ba4dbcd19d902a947126391bb26.zip
lua: implement iterator with installed packages
Diffstat (limited to 'src/lua-apk.c')
-rw-r--r--src/lua-apk.c48
1 files changed, 43 insertions, 5 deletions
diff --git a/src/lua-apk.c b/src/lua-apk.c
index 8bd9a7b..845d67a 100644
--- a/src/lua-apk.c
+++ b/src/lua-apk.c
@@ -9,7 +9,8 @@
#include "apk_version.h"
#define LIBNAME "apk"
-#define APKDB_META "apk_database"
+#define APK_DB_META "apk_database"
+#define APK_IPKG_META "apk_installed_package"
struct flagmap {
const char *name;
@@ -134,9 +135,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;
}
@@ -155,7 +156,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);
@@ -227,6 +228,42 @@ ret_nil:
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},
@@ -235,6 +272,7 @@ static const luaL_reg reg_apk_methods[] = {
{"who_owns", Papk_who_owns},
{"exists", Papk_exists},
{"is_installed", Papk_exists},
+ {"installed", Pinstalled},
{NULL, NULL}
};
@@ -250,7 +288,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);