version 1.1.1.1, 2013/10/14 10:32:47
|
version 1.1.1.2, 2016/11/02 10:35:00
|
Line 1
|
Line 1
|
|
#include "first.h" |
|
|
#include "buffer.h" |
#include "buffer.h" |
#include "server.h" |
#include "server.h" |
#include "log.h" |
#include "log.h" |
Line 32 typedef char HASHHEX[HASHHEXLEN+1];
|
Line 34 typedef char HASHHEX[HASHHEXLEN+1];
|
|
|
#ifdef HAVE_LUA_H |
#ifdef HAVE_LUA_H |
|
|
|
#include <lauxlib.h> |
|
|
int f_crypto_md5(lua_State *L) { |
int f_crypto_md5(lua_State *L) { |
li_MD5_CTX Md5Ctx; |
li_MD5_CTX Md5Ctx; |
HASH HA1; |
HASH HA1; |
buffer b; |
|
char hex[33]; |
char hex[33]; |
int n = lua_gettop(L); |
int n = lua_gettop(L); |
|
size_t s_len; |
|
const char *s; |
|
|
b.ptr = hex; |
|
b.used = 0; |
|
b.size = sizeof(hex); |
|
|
|
if (n != 1) { |
if (n != 1) { |
lua_pushstring(L, "md5: expected one argument"); |
lua_pushstring(L, "md5: expected one argument"); |
lua_error(L); |
lua_error(L); |
Line 53 int f_crypto_md5(lua_State *L) {
|
Line 54 int f_crypto_md5(lua_State *L) {
|
lua_error(L); |
lua_error(L); |
} |
} |
|
|
|
s = lua_tolstring(L, 1, &s_len); |
|
|
li_MD5_Init(&Md5Ctx); |
li_MD5_Init(&Md5Ctx); |
li_MD5_Update(&Md5Ctx, (unsigned char *)lua_tostring(L, 1), lua_strlen(L, 1)); | li_MD5_Update(&Md5Ctx, (unsigned char *) s, (unsigned int) s_len); |
li_MD5_Final(HA1, &Md5Ctx); |
li_MD5_Final(HA1, &Md5Ctx); |
|
|
buffer_copy_string_hex(&b, (char *)HA1, 16); | li_tohex(hex, sizeof(hex), (const char*) HA1, 16); |
|
|
lua_pushstring(L, b.ptr); | lua_pushstring(L, hex); |
|
|
return 1; |
return 1; |
} |
} |
Line 84 int f_file_mtime(lua_State *L) {
|
Line 87 int f_file_mtime(lua_State *L) {
|
return 1; |
return 1; |
} |
} |
|
|
lua_pushnumber(L, st.st_mtime); | lua_pushinteger(L, st.st_mtime); |
|
|
return 1; |
return 1; |
} |
} |
Line 126 int f_dir_files(lua_State *L) {
|
Line 129 int f_dir_files(lua_State *L) {
|
return 1; |
return 1; |
} |
} |
|
|
/* push d into registry */ | /* push d into userdata */ |
lua_pushlightuserdata(L, d); |
lua_pushlightuserdata(L, d); |
lua_pushcclosure(L, f_dir_files_iter, 1); |
lua_pushcclosure(L, f_dir_files_iter, 1); |
|
|
Line 152 int f_file_isreg(lua_State *L) {
|
Line 155 int f_file_isreg(lua_State *L) {
|
return 1; |
return 1; |
} |
} |
|
|
lua_pushnumber(L, S_ISREG(st.st_mode)); | lua_pushinteger(L, S_ISREG(st.st_mode)); |
|
|
return 1; |
return 1; |
} |
} |
Line 176 int f_file_isdir(lua_State *L) {
|
Line 179 int f_file_isdir(lua_State *L) {
|
return 1; |
return 1; |
} |
} |
|
|
lua_pushnumber(L, S_ISDIR(st.st_mode)); | lua_pushinteger(L, S_ISDIR(st.st_mode)); |
|
|
return 1; |
return 1; |
} |
} |
|
|
|
|
|
|
#ifdef HAVE_MEMCACHE_H | #ifdef USE_MEMCACHED |
int f_memcache_exists(lua_State *L) { |
int f_memcache_exists(lua_State *L) { |
char *r; | size_t key_len; |
int n = lua_gettop(L); | const char *key; |
struct memcache *mc; | memcached_st *memc; |
|
|
if (!lua_islightuserdata(L, lua_upvalueindex(1))) { |
if (!lua_islightuserdata(L, lua_upvalueindex(1))) { |
lua_pushstring(L, "where is my userdata ?"); |
lua_pushstring(L, "where is my userdata ?"); |
lua_error(L); |
lua_error(L); |
} |
} |
|
|
mc = lua_touserdata(L, lua_upvalueindex(1)); | memc = lua_touserdata(L, lua_upvalueindex(1)); |
|
|
if (n != 1) { | if (1 != lua_gettop(L)) { |
lua_pushstring(L, "expected one argument"); |
lua_pushstring(L, "expected one argument"); |
lua_error(L); |
lua_error(L); |
} |
} |
|
|
if (!lua_isstring(L, 1)) { | key = luaL_checklstring(L, 1, &key_len); |
lua_pushstring(L, "argument has to be a string"); | lua_pushboolean(L, (MEMCACHED_SUCCESS == memcached_exist(memc, key, key_len))); |
lua_error(L); | |
} | |
| |
if (NULL == (r = mc_aget(mc, | |
(char*) lua_tostring(L, 1), lua_strlen(L, 1)))) { | |
| |
lua_pushboolean(L, 0); | |
return 1; | |
} | |
| |
free(r); | |
| |
lua_pushboolean(L, 1); | |
return 1; |
return 1; |
} |
} |
|
|
int f_memcache_get_string(lua_State *L) { |
int f_memcache_get_string(lua_State *L) { |
char *r; | size_t key_len, value_len; |
int n = lua_gettop(L); | char *value; |
| const char *key; |
| memcached_st *memc; |
|
|
struct memcache *mc; |
|
|
|
if (!lua_islightuserdata(L, lua_upvalueindex(1))) { |
if (!lua_islightuserdata(L, lua_upvalueindex(1))) { |
lua_pushstring(L, "where is my userdata ?"); |
lua_pushstring(L, "where is my userdata ?"); |
lua_error(L); |
lua_error(L); |
} |
} |
|
|
mc = lua_touserdata(L, lua_upvalueindex(1)); | memc = lua_touserdata(L, lua_upvalueindex(1)); |
|
|
| if (1 != lua_gettop(L)) { |
if (n != 1) { | |
lua_pushstring(L, "expected one argument"); |
lua_pushstring(L, "expected one argument"); |
lua_error(L); |
lua_error(L); |
} |
} |
|
|
if (!lua_isstring(L, 1)) { | key = luaL_checklstring(L, 1, &key_len); |
lua_pushstring(L, "argument has to be a string"); | if (NULL == (value = memcached_get(memc, key, key_len, &value_len, NULL, NULL))) { |
lua_error(L); | |
} | |
| |
if (NULL == (r = mc_aget(mc, | |
(char*) lua_tostring(L, 1), lua_strlen(L, 1)))) { | |
lua_pushnil(L); |
lua_pushnil(L); |
return 1; |
return 1; |
} |
} |
|
|
lua_pushstring(L, r); | lua_pushlstring(L, value, value_len); |
|
|
free(r); | free(value); |
|
|
return 1; |
return 1; |
} |
} |
|
|
int f_memcache_get_long(lua_State *L) { |
int f_memcache_get_long(lua_State *L) { |
char *r; | size_t key_len, value_len; |
int n = lua_gettop(L); | char *value; |
| const char *key; |
| memcached_st *memc; |
| char *endptr; |
| long v; |
|
|
struct memcache *mc; |
|
|
|
if (!lua_islightuserdata(L, lua_upvalueindex(1))) { |
if (!lua_islightuserdata(L, lua_upvalueindex(1))) { |
lua_pushstring(L, "where is my userdata ?"); |
lua_pushstring(L, "where is my userdata ?"); |
lua_error(L); |
lua_error(L); |
} |
} |
|
|
mc = lua_touserdata(L, lua_upvalueindex(1)); | memc = lua_touserdata(L, lua_upvalueindex(1)); |
|
|
| if (1 != lua_gettop(L)) { |
if (n != 1) { | |
lua_pushstring(L, "expected one argument"); |
lua_pushstring(L, "expected one argument"); |
lua_error(L); |
lua_error(L); |
} |
} |
|
|
if (!lua_isstring(L, 1)) { | key = luaL_checklstring(L, 1, &key_len); |
lua_pushstring(L, "argument has to be a string"); | if (NULL == (value = memcached_get(memc, key, key_len, &value_len, NULL, NULL))) { |
lua_error(L); | lua_pushnil(L); |
| return 1; |
} |
} |
|
|
if (NULL == (r = mc_aget(mc, | errno = 0; |
(char*) lua_tostring(L, 1), lua_strlen(L, 1)))) { | v = strtol(value, &endptr, 10); |
| if (0 == errno && *endptr == '\0') { |
| lua_pushinteger(L, v); |
| } else { |
lua_pushnil(L); |
lua_pushnil(L); |
return 1; |
|
} |
} |
|
|
lua_pushnumber(L, strtol(r, NULL, 10)); | free(value); |
| |
free(r); | |
|
|
return 1; |
return 1; |
} |
} |