2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include "mod_magnet_cache.h"
|
2019-12-05 08:16:25 +00:00
|
|
|
#include "log.h"
|
2009-10-11 14:31:42 +00:00
|
|
|
#include "stat_cache.h"
|
|
|
|
|
2006-09-10 22:01:43 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2006-09-15 14:56:41 +00:00
|
|
|
#include <lualib.h>
|
|
|
|
#include <lauxlib.h>
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
static script *script_init() {
|
2006-09-10 22:01:43 +00:00
|
|
|
script *sc;
|
|
|
|
|
|
|
|
sc = calloc(1, sizeof(*sc));
|
|
|
|
sc->name = buffer_init();
|
|
|
|
sc->etag = buffer_init();
|
|
|
|
|
|
|
|
return sc;
|
|
|
|
}
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
static void script_free(script *sc) {
|
2006-09-10 22:01:43 +00:00
|
|
|
if (!sc) return;
|
|
|
|
|
|
|
|
lua_pop(sc->L, 1); /* the function copy */
|
|
|
|
|
|
|
|
buffer_free(sc->name);
|
|
|
|
buffer_free(sc->etag);
|
|
|
|
|
|
|
|
lua_close(sc->L);
|
|
|
|
|
|
|
|
free(sc);
|
|
|
|
}
|
|
|
|
|
2019-10-26 06:00:09 +00:00
|
|
|
#if 0
|
2006-09-10 22:01:43 +00:00
|
|
|
script_cache *script_cache_init() {
|
2019-10-26 06:00:09 +00:00
|
|
|
script_cache *p = calloc(1, sizeof(script_cache));
|
|
|
|
force_assert(p);
|
2006-09-10 22:01:43 +00:00
|
|
|
return p;
|
|
|
|
}
|
2019-10-26 06:00:09 +00:00
|
|
|
#endif
|
2006-09-10 22:01:43 +00:00
|
|
|
|
2019-10-26 06:00:09 +00:00
|
|
|
void script_cache_free_data(script_cache *p) {
|
2006-09-10 22:01:43 +00:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!p) return;
|
|
|
|
|
|
|
|
for (i = 0; i < p->used; i++) {
|
|
|
|
script_free(p->ptr[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(p->ptr);
|
|
|
|
}
|
|
|
|
|
2019-12-05 08:16:25 +00:00
|
|
|
lua_State *script_cache_get_script(script_cache *cache, buffer *name, int etag_flags) {
|
2006-09-10 22:01:43 +00:00
|
|
|
script *sc = NULL;
|
|
|
|
stat_cache_entry *sce;
|
|
|
|
|
2019-12-05 08:16:25 +00:00
|
|
|
for (uint32_t i = 0; i < cache->used; ++i, sc = NULL) {
|
2006-09-10 22:01:43 +00:00
|
|
|
sc = cache->ptr[i];
|
2019-12-05 08:16:25 +00:00
|
|
|
if (!buffer_is_equal(name, sc->name)) continue;
|
2006-09-10 22:01:43 +00:00
|
|
|
|
2019-12-05 08:16:25 +00:00
|
|
|
sc->last_used = log_epoch_secs;
|
2006-09-10 22:01:43 +00:00
|
|
|
|
|
|
|
/* oops, the script failed last time */
|
|
|
|
|
|
|
|
if (lua_gettop(sc->L) == 0) break;
|
2016-01-03 14:48:09 +00:00
|
|
|
force_assert(lua_gettop(sc->L) == 1);
|
2006-09-10 22:01:43 +00:00
|
|
|
|
2019-12-05 08:16:25 +00:00
|
|
|
sce = stat_cache_get_entry(sc->name);
|
|
|
|
if (NULL == sce) {
|
2006-09-10 22:01:43 +00:00
|
|
|
lua_pop(sc->L, 1); /* pop the old function */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-12-05 08:16:25 +00:00
|
|
|
const buffer *etag = stat_cache_etag_get(sce, etag_flags);
|
|
|
|
if (NULL == etag || !buffer_is_equal(sc->etag, etag)) {
|
2006-09-10 22:01:43 +00:00
|
|
|
/* the etag is outdated, reload the function */
|
|
|
|
lua_pop(sc->L, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-16 13:08:20 +00:00
|
|
|
force_assert(lua_isfunction(sc->L, -1));
|
2006-09-10 22:01:43 +00:00
|
|
|
|
|
|
|
return sc->L;
|
|
|
|
}
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
/* if the script was script already loaded but either got changed or
|
2006-09-10 22:01:43 +00:00
|
|
|
* failed to load last time */
|
|
|
|
if (sc == NULL) {
|
|
|
|
sc = script_init();
|
|
|
|
|
2019-02-13 03:36:04 +00:00
|
|
|
if (cache->used == cache->size) {
|
2006-09-10 22:01:43 +00:00
|
|
|
cache->size += 16;
|
|
|
|
cache->ptr = realloc(cache->ptr, cache->size * sizeof(*(cache->ptr)));
|
|
|
|
}
|
|
|
|
|
|
|
|
cache->ptr[cache->used++] = sc;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_copy_buffer(sc->name, name);
|
2006-09-10 22:01:43 +00:00
|
|
|
|
|
|
|
sc->L = luaL_newstate();
|
|
|
|
luaL_openlibs(sc->L);
|
|
|
|
}
|
|
|
|
|
2019-12-05 08:16:25 +00:00
|
|
|
sc->last_used = log_epoch_secs;
|
2006-09-10 22:01:43 +00:00
|
|
|
|
|
|
|
if (0 != luaL_loadfile(sc->L, name->ptr)) {
|
|
|
|
/* oops, an error, return it */
|
|
|
|
return sc->L;
|
|
|
|
}
|
|
|
|
|
2019-12-05 08:16:25 +00:00
|
|
|
sce = stat_cache_get_entry(sc->name);
|
|
|
|
if (sce) {
|
|
|
|
buffer_copy_buffer(sc->etag, stat_cache_etag_get(sce, etag_flags));
|
2006-09-10 22:01:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-16 13:08:20 +00:00
|
|
|
force_assert(lua_isfunction(sc->L, -1));
|
2006-09-10 22:01:43 +00:00
|
|
|
|
|
|
|
return sc->L;
|
|
|
|
}
|