)
set(LIBCLI_SOURCES
+ expire.c
cache.c
prot.c
cynagora.c
/* see cache.h */
int
-cache_put(
- cache_t *cache,
+cache_put(cache_t *cache,
const cynagora_key_t *key,
int value,
- time_t expire
+ time_t expire,
+ bool absolute
) {
uint16_t length;
item_t *item;
stpcpy(1 + stpcpy(1 + stpcpy(1 + stpcpy(item->strings, key->client), key->session), key->user), key->permission);
cache->used += (uint32_t)size;
}
- item->expire = expire;
+ item->expire = !expire ? 0 : absolute ? expire : expire + time(NULL);
item->hit = 255;
item->value = (int8_t)value;
return 0;
cache_t *cache,
const cynagora_key_t *key,
int value,
- time_t expire
+ time_t expire,
+ bool absolute
);
/**
#include "cyn-server.h"
#include "socket.h"
#include "pollitem.h"
+#include "expire.h"
#define MAX_PUTX_ITEMS 15
if (expire < 0)
return "-"; /* no cache */
- /* TODO: check size */
- snprintf(buffer, bufsz, "%lld", (long long)expire);
+ if (exp2txt(expire, true, buffer, bufsz) >= bufsz)
+ return "-"; /* no cache */
+
return buffer;
}
if (!expire)
return NULL;
- if (expire < 0) {
- expire = -(expire + 1);
- if (!expire)
- return "-";
- *buffer++ = '-';
- bufsz--;
- }
+ if (exp2txt(expire, true, buffer, bufsz) >= bufsz)
+ return "-"; /* no cache */
- snprintf(buffer, bufsz, "%lld", (long long)expire);
return buffer;
}
if (count == 6)
value.expire = 0;
else
- value.expire = strtoll(args[6], NULL, 10);
+ txt2exp(args[6], &value.expire, true);
+
key.client = args[1];
key.session = args[2];
key.user = args[3];
#include "cynagora.h"
#include "cache.h"
#include "socket.h"
+#include "expire.h"
#define MIN_CACHE_SIZE 400
#define CACHESIZE(x) ((x) >= MIN_CACHE_SIZE ? (x) : (x) ? MIN_CACHE_SIZE : 0)
if (!optval->expire)
text[0] = 0;
else
- snprintf(text, sizeof text, "%lld", (long long)optval->expire);
+ exp2txt(optval->expire, true, text, sizeof text);
rc = prot_put_field(prot, text);
}
}
else if (cynagora->reply.fields[1][0] == '-')
*expire = -1;
else
- *expire = strtoll(cynagora->reply.fields[1], NULL, 10);
+ txt2exp(cynagora->reply.fields[1], expire, true);
return rc;
}
if (rc >= 0) {
rc = status_check(cynagora, &expire);
if (rc >= 0 && action == _check_)
- cache_put(cynagora->cache, key, rc, expire);
+ cache_put(cynagora->cache, key, rc, expire, true);
}
}
return rc;
k.user = cynagora->reply.fields[3];
k.permission = cynagora->reply.fields[4];
v.value = cynagora->reply.fields[5];
- v.expire = rc == 6 ? 0 : (time_t)strtoll(cynagora->reply.fields[6], NULL, 10);
+ if (rc == 6)
+ v.expire = 0;
+ else if (!txt2exp(cynagora->reply.fields[6], &v.expire, true))
+ v.expire = -1;
callback(closure, &k, &v);
rc = wait_reply(cynagora, true);
}
key.session = &key.client[1 + strlen(key.client)];
key.user = &key.session[1 + strlen(key.session)];
key.permission = &key.user[1 + strlen(key.user)];
- cache_put(cynagora->cache, &key, rc, expire);
+ cache_put(cynagora->cache, &key, rc, expire, true);
}
ar->callback(ar->closure, rc);
free(ar);
key.user = item[2];
key.permission = item[3];
value.value = item[4];
- if (!txt2exp(item[5], &value.expire)) {
+ if (!txt2exp(item[5], &value.expire, true)) {
fprintf(stderr, "bad expiration %s (%s:%d)\n", item[5], path, lino);
rc = -EINVAL;
goto error2;
/* see expire.h */
-bool txt2exp(const char *txt, time_t *time_out)
+bool txt2exp(const char *txt, time_t *time_out, bool absolute)
{
bool nocache;
time_t r;
/* parse */
if (!parse_time_spec(txt, &r))
return false;
- /* relative time */
- r = pt_add(r, time(NULL));
+ if (absolute) {
+ /* absolute time */
+ r = pt_add(r, time(NULL));
+ }
}
*time_out = nocache ? -(r + 1) : r;
}
/* see expire.h */
-size_t exp2txt(time_t expire, char *buffer, size_t buflen)
+size_t exp2txt(time_t expire, bool absolute, char *buffer, size_t buflen)
{
char b[100];
size_t l, n;
if (!n)
strncpy(b, "forever", sizeof b);
} else {
- expire -= time(NULL);
+ if (absolute)
+ expire -= time(NULL);
#define ADD(C,U) \
if (expire >= U) { \
n += (size_t)snprintf(&b[n], sizeof b - (size_t)n, "%lld" #C, (long long)(expire / U)); \
*
* @param txt the text to convert
* @param time_out where to store the result
+ * @param absolute return the expiration in epoch
* @return true if valid false otherwise
*/
extern
bool
txt2exp(
const char *txt,
- time_t *time_out
+ time_t *time_out,
+ bool absolute
);
/**
* Converts the expiration in to its relative string representation
*
* @param expire the epiration to convert
+ * @param expire is expiration absolute?
* @param buffer the buffer where to store the converted string
* @param buflen length of the buffer
* @return the length of the resulting string, can be greater than buflen but
size_t
exp2txt(
time_t expire,
+ bool absolute,
char *buffer,
size_t buflen
);
value.value = n > 5 ? av[5] : "no";
if (n <= 6)
value.expire = 0;
- else if (!txt2exp(av[6], &value.expire))
+ else if (!txt2exp(av[6], &value.expire, true))
return -EINVAL;
return key.client && key.session && key.user && key.permission && value.value ? 0 : -EINVAL;
size_t s, as;
int i;
- exp2txt(v->expire, buffer, sizeof buffer);
+ exp2txt(v->expire, true, buffer, sizeof buffer);
items[0] = k->client;
items[1] = k->session;
items[2] = k->user;