lua-apk.c 7.79 KB
Newer Older
1 2 3 4 5 6 7 8 9
/* src/lua-apk.c - Alpine Package Keeper (APK)
 *
 * Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
 * Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi>
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-only
 */

Natanael Copa's avatar
Natanael Copa committed
10
#include <features.h>
Natanael Copa's avatar
Natanael Copa committed
11 12 13 14
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

Natanael Copa's avatar
Natanael Copa committed
15 16
#include "apk_blob.h"
#include "apk_database.h"
Natanael Copa's avatar
Natanael Copa committed
17 18 19 20
#include "apk_defines.h"
#include "apk_version.h"

#define LIBNAME "apk"
21 22
#define APK_DB_META "apk_database"
#define APK_IPKG_META "apk_installed_package"
Natanael Copa's avatar
Natanael Copa committed
23

Natanael Copa's avatar
Natanael Copa committed
24 25 26 27
#if LUA_VERSION_NUM < 502
# define luaL_newlib(L,l) (lua_newtable(L), luaL_register(L,NULL,l))
#endif

Natanael Copa's avatar
Natanael Copa committed
28
struct flagmap {
29 30
	const char *name;
	int flag;
Natanael Copa's avatar
Natanael Copa committed
31 32 33
};

struct flagmap opendb_flagmap[] = {
34 35 36 37 38 39 40 41 42 43 44 45 46
	{"read",                APK_OPENF_READ},
	{"write",               APK_OPENF_WRITE},
	{"create",              APK_OPENF_CREATE},
	{"no_installed",        APK_OPENF_NO_INSTALLED},
	{"no_scripts",          APK_OPENF_NO_SCRIPTS},
	{"no_world",            APK_OPENF_NO_WORLD},
	{"no_sys_repos",        APK_OPENF_NO_SYS_REPOS},
	{"no_installed_repo",   APK_OPENF_NO_INSTALLED_REPO},
	{"no_repos",            APK_OPENF_NO_REPOS},
	{"no_state",            APK_OPENF_NO_STATE},
	{"no_scripts",          APK_OPENF_NO_SCRIPTS},
	{"no_world",            APK_OPENF_NO_WORLD},
	{NULL, 0}
Natanael Copa's avatar
Natanael Copa committed
47 48
};

Natanael Copa's avatar
Natanael Copa committed
49 50 51 52 53 54 55 56 57
/* implemented as luaL_typerror until lua 5.1, dropped in 5.2
 * (C) 1994-2012 Lua.org, PUC-Rio. MIT license
 */
static int typerror (lua_State *L, int narg, const char *tname) {
	const char *msg = lua_pushfstring(L, "%s expected, got %s",
					  tname, luaL_typename(L, narg));
	return luaL_argerror(L, narg, msg);
}

Natanael Copa's avatar
Natanael Copa committed
58 59 60 61 62 63 64
static apk_blob_t check_blob(lua_State *L, int index)
{
	apk_blob_t blob;
	blob.ptr = (char *)luaL_checklstring(L, index, (size_t *)&blob.len);
	return blob;
}

Natanael Copa's avatar
Natanael Copa committed
65 66 67 68
/* version_validate(verstr) */
/* returns boolean */
static int Pversion_validate(lua_State *L)
{
Natanael Copa's avatar
Natanael Copa committed
69
	apk_blob_t ver = check_blob(L, 1);
Natanael Copa's avatar
Natanael Copa committed
70 71 72 73 74 75 76 77 78 79
	lua_pushboolean(L, apk_version_validate(ver));
	return 1;
}

/* version_compare(verstr1, verstr2)
   returns either '<', '=' or '>'
*/
static int Pversion_compare(lua_State *L)
{
	apk_blob_t a, b;
Natanael Copa's avatar
Natanael Copa committed
80 81
	a = check_blob(L, 1);
	b = check_blob(L, 2);
Natanael Copa's avatar
Natanael Copa committed
82 83 84 85 86
	lua_pushstring(L, apk_version_op_string(apk_version_compare_blob(a, b)));
	return 1;
}

/* version_is_less(verstr1, verstr2)
87
   returns whether version is '<'
Natanael Copa's avatar
Natanael Copa committed
88 89 90 91
*/
static int Pversion_is_less(lua_State *L)
{
	apk_blob_t a, b;
Natanael Copa's avatar
Natanael Copa committed
92 93
	a = check_blob(L, 1);
	b = check_blob(L, 2);
Natanael Copa's avatar
Natanael Copa committed
94 95 96 97
	lua_pushboolean(L, apk_version_compare_blob(a, b) == APK_VERSION_LESS);
	return 1;
}

Natanael Copa's avatar
Natanael Copa committed
98 99 100 101 102 103 104 105 106 107 108 109
//static getfield(lua_State *L, const char *key)
//{
static const char *get_opt_string_field(lua_State *L, int index,
				        const char *key, const char *def)
{
	const char *value;
	lua_getfield(L, index, key);
	value = luaL_optstring(L, -1, def);
	lua_pop(L, 1);
	return value;
}

Natanael Copa's avatar
Natanael Copa committed
110 111 112 113 114 115 116 117
static void set_string_field(lua_State *L, int index, const char *key,
			     const char *value)
{
	lua_pushstring(L, key);
	lua_pushstring(L, value);
	lua_settable(L, index);
}

Natanael Copa's avatar
Natanael Copa committed
118 119 120 121 122 123 124 125 126
static int get_opt_int_field(lua_State *L, int index, const char *key, int def)
{
	int value;
	lua_getfield(L, index, key);
	value = luaL_optinteger(L, -1, def);
	lua_pop(L, 1);
	return value;
}

Natanael Copa's avatar
Natanael Copa committed
127 128 129 130 131 132 133
static void set_int_field(lua_State *L, int index, const char *key, int value)
{
	lua_pushstring(L, key);
	lua_pushinteger(L, value);
	lua_settable(L, index);
}

Natanael Copa's avatar
Natanael Copa committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
static int get_boolean_field(lua_State *L, int index, const char *key)
{
	int value;
	lua_getfield(L, index, key);
	value = lua_toboolean(L, -1);
	lua_pop(L, 1);
	return value;
}

static int get_dbopts(lua_State *L, int i, struct apk_db_options *o)
{
	struct flagmap *f;
	o->root = (char *)get_opt_string_field(L, i, "root", NULL);
	o->repositories_file = (char *)get_opt_string_field(L, i, "repositories_file", NULL);
	o->keys_dir = (char *)get_opt_string_field(L, i, "keys_dir", NULL);
	o->lock_wait = get_opt_int_field(L, i, "lock_wait", 0);
	for (f = opendb_flagmap; f->name != NULL; f++)
		if (get_boolean_field(L, i, f->name))
			o->open_flags |= f->flag;
	return 0;
}

Natanael Copa's avatar
Natanael Copa committed
156 157 158 159
static struct apk_database *checkdb(lua_State *L, int index)
{
	struct apk_database *db;
	luaL_checktype(L, index, LUA_TUSERDATA);
160
	db = (struct apk_database  *) luaL_checkudata(L, index, APK_DB_META);
Natanael Copa's avatar
Natanael Copa committed
161
	if (db == NULL)
Natanael Copa's avatar
Natanael Copa committed
162
		typerror(L, index, APK_DB_META);
Natanael Copa's avatar
Natanael Copa committed
163 164 165
	return db;
}

Natanael Copa's avatar
Natanael Copa committed
166 167 168
static int Papk_db_open(lua_State *L)
{
	struct apk_db_options opts;
Natanael Copa's avatar
Natanael Copa committed
169 170 171
	struct apk_database *db;
	int r;

Natanael Copa's avatar
Natanael Copa committed
172
	memset(&opts, 0, sizeof(opts));
Natanael Copa's avatar
Natanael Copa committed
173
	list_init(&opts.repository_list);
Natanael Copa's avatar
Natanael Copa committed
174
	apk_atom_init();
Natanael Copa's avatar
Natanael Copa committed
175 176
	if (lua_istable(L, 1))
		get_dbopts(L, 1, &opts);
Natanael Copa's avatar
Natanael Copa committed
177 178
	else
		opts.open_flags |= APK_OPENF_READ;
Natanael Copa's avatar
Natanael Copa committed
179 180

	db = lua_newuserdata(L, sizeof(struct apk_database));
181
	luaL_getmetatable(L, APK_DB_META);
Natanael Copa's avatar
Natanael Copa committed
182 183
	lua_setmetatable(L, -2);

Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
184
	apk_db_init(db);
Natanael Copa's avatar
Natanael Copa committed
185 186 187 188 189 190 191 192 193 194
	r = apk_db_open(db, &opts);
	if (r != 0)
		luaL_error(L, "apk_db_open() failed");
	return 1;
}

static int Papk_db_close(lua_State *L)
{
	struct apk_database *db = checkdb(L, 1);
	apk_db_close(db);
Natanael Copa's avatar
Natanael Copa committed
195 196
	return 0;
}
Natanael Copa's avatar
Natanael Copa committed
197

Natanael Copa's avatar
Natanael Copa committed
198

Natanael Copa's avatar
Natanael Copa committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
static int push_package(lua_State *L, struct apk_package *pkg)
{
	if (pkg == NULL) {
		lua_pushnil(L);
		return 1;
	}
	lua_newtable(L);
	set_string_field(L, -3, "name", pkg->name->name);
	set_string_field(L, -3, "version", apk_blob_cstr(*pkg->version));
	set_string_field(L, -3, "url", pkg->url);
	set_string_field(L, -3, "license", apk_blob_cstr(*pkg->license));
	set_string_field(L, -3, "description", pkg->description);
	set_string_field(L, -3, "filename", pkg->filename);
	set_int_field(L, -3, "size", pkg->size);
	return 1;
}
static int Papk_who_owns(lua_State *L)
{
	struct apk_database *db = checkdb(L, 1);
	const char *path = luaL_checkstring(L, 2);
	struct apk_package *pkg = apk_db_get_file_owner(db, APK_BLOB_STR(path));
	return push_package(L, pkg);
}

223 224 225 226 227 228
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_package *pkg;
Natanael Copa's avatar
Natanael Copa committed
229 230
	apk_blob_t blob = APK_BLOB_STR(depstr);
	apk_blob_pull_dep(&blob, db, &dep);
231

Timo Teräs's avatar
Timo Teräs committed
232
	if (APK_BLOB_IS_NULL(blob) || blob.len > 0)
233 234
		goto ret_nil;

235 236
	pkg = apk_pkg_get_installed(dep.name);
	if (pkg == NULL)
237 238
		goto ret_nil;

Timo Teräs's avatar
Timo Teräs committed
239 240
	if (apk_dep_analyze(&dep, pkg) & APK_DEP_SATISFIES)
		return push_package(L, pkg);
241 242 243 244 245 246

ret_nil:
	lua_pushnil(L);
	return 1;
}

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
// 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;
}

Natanael Copa's avatar
Natanael Copa committed
283
static const luaL_Reg reg_apk_methods[] = {
Natanael Copa's avatar
Natanael Copa committed
284 285 286
	{"version_validate",	Pversion_validate},
	{"version_compare",	Pversion_compare},
	{"version_is_less",	Pversion_is_less},
Natanael Copa's avatar
Natanael Copa committed
287
	{"db_open",		Papk_db_open},
Natanael Copa's avatar
Natanael Copa committed
288
	{"who_owns",		Papk_who_owns},
289 290
	{"exists",		Papk_exists},
	{"is_installed",	Papk_exists},
291
	{"installed",		Pinstalled},
Natanael Copa's avatar
Natanael Copa committed
292 293 294
	{NULL,		NULL}
};

Natanael Copa's avatar
Natanael Copa committed
295 296 297 298 299 300 301 302 303 304 305
static int db_create_meta(lua_State *L)
{
	luaL_newmetatable(L, APK_DB_META);
	lua_newtable(L);

	lua_setfield(L, -2, "__index");
	lua_pushcfunction(L, Papk_db_close);
	lua_setfield(L, -2, "__gc");
	return 1;
}

Natanael Copa's avatar
Natanael Copa committed
306 307
LUALIB_API int luaopen_apk(lua_State *L)
{
Natanael Copa's avatar
Natanael Copa committed
308 309 310 311 312
	db_create_meta(L);
	luaL_newlib(L, reg_apk_methods);
	lua_pushvalue(L, -1);
	lua_setglobal(L, LIBNAME);

Natanael Copa's avatar
Natanael Copa committed
313 314 315
	lua_pushliteral(L, "version");
	lua_pushliteral(L, APK_VERSION);
	lua_settable(L, -3);
Natanael Copa's avatar
Natanael Copa committed
316

Natanael Copa's avatar
Natanael Copa committed
317 318 319
	return 1;
}