1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-24 21:34:56 +03:00

r18310: Add a little test for some gencache routines

Remove unused gencache_set_only
Use CONST_DISCARD instead of SMB_STRDUP

Volker
(This used to be commit 651e7e44e2)
This commit is contained in:
Volker Lendecke 2006-09-09 21:05:51 +00:00 committed by Gerald (Jerry) Carter
parent 415aa96f09
commit 96c72e2f81
3 changed files with 67 additions and 110 deletions

View File

@ -56,13 +56,13 @@ BOOL gencache_init(void)
if (cache) return True;
asprintf(&cache_fname, "%s/%s", lp_lockdir(), "gencache.tdb");
if (cache_fname)
DEBUG(5, ("Opening cache file at %s\n", cache_fname));
else {
if (cache_fname == NULL) {
DEBUG(0, ("Filename allocation failed.\n"));
return False;
}
DEBUG(5, ("Opening cache file at %s\n", cache_fname));
cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
O_RDWR|O_CREAT, 0644);
@ -121,9 +121,9 @@ BOOL gencache_set(const char *keystr, const char *value, time_t timeout)
if (!valstr)
return False;
keybuf.dptr = SMB_STRDUP(keystr);
keybuf.dptr = CONST_DISCARD(char *, keystr);
keybuf.dsize = strlen(keystr)+1;
databuf.dptr = SMB_STRDUP(valstr);
databuf.dptr = valstr;
databuf.dsize = strlen(valstr)+1;
DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
" %s (%d seconds %s)\n", keybuf.dptr, value,ctime(&timeout),
@ -132,68 +132,10 @@ BOOL gencache_set(const char *keystr, const char *value, time_t timeout)
ret = tdb_store(cache, keybuf, databuf, 0);
SAFE_FREE(valstr);
SAFE_FREE(keybuf.dptr);
SAFE_FREE(databuf.dptr);
return ret == 0;
}
/**
* Set existing entry to the cache file.
*
* @param keystr string that represents a key of this entry
* @param valstr text representation value being cached
* @param timeout time when the value is expired
*
* @retval true when entry is successfuly set
* @retval false on failure
**/
BOOL gencache_set_only(const char *keystr, const char *valstr, time_t timeout)
{
int ret = -1;
TDB_DATA keybuf, databuf;
char *old_valstr, *datastr;
time_t old_timeout;
/* fail completely if get null pointers passed */
SMB_ASSERT(keystr && valstr);
if (!gencache_init()) return False;
/*
* Check whether entry exists in the cache
* Don't verify gencache_get exit code, since the entry may be expired
*/
gencache_get(keystr, &old_valstr, &old_timeout);
if (!(old_valstr && old_timeout)) return False;
DEBUG(10, ("Setting cache entry with key = %s; old value = %s and old timeout \
= %s\n", keystr, old_valstr, ctime(&old_timeout)));
asprintf(&datastr, CACHE_DATA_FMT, (int)timeout, valstr);
keybuf.dptr = SMB_STRDUP(keystr);
keybuf.dsize = strlen(keystr)+1;
databuf.dptr = SMB_STRDUP(datastr);
databuf.dsize = strlen(datastr)+1;
DEBUGADD(10, ("New value = %s, new timeout = %s (%d seconds %s)", valstr,
ctime(&timeout), (int)(timeout - time(NULL)),
timeout > time(NULL) ? "ahead" : "in the past"));
ret = tdb_store(cache, keybuf, databuf, TDB_REPLACE);
SAFE_FREE(datastr);
SAFE_FREE(old_valstr);
SAFE_FREE(keybuf.dptr);
SAFE_FREE(databuf.dptr);
return ret == 0;
}
/**
* Delete one entry from the cache file.
*
@ -213,12 +155,11 @@ BOOL gencache_del(const char *keystr)
if (!gencache_init()) return False;
keybuf.dptr = SMB_STRDUP(keystr);
keybuf.dptr = CONST_DISCARD(char *, keystr);
keybuf.dsize = strlen(keystr)+1;
DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr));
ret = tdb_delete(cache, keybuf);
SAFE_FREE(keybuf.dptr);
return ret == 0;
}
@ -247,10 +188,9 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout)
return False;
}
keybuf.dptr = SMB_STRDUP(keystr);
keybuf.dptr = CONST_DISCARD(char *, keystr);
keybuf.dsize = strlen(keystr)+1;
databuf = tdb_fetch(cache, keybuf);
SAFE_FREE(keybuf.dptr);
if (databuf.dptr && databuf.dsize > TIMEOUT_LEN) {
char* entry_buf = SMB_STRNDUP(databuf.dptr, databuf.dsize);

View File

@ -4805,6 +4805,65 @@ static BOOL run_local_substitute(int dummy)
return (diff == 0);
}
static BOOL run_local_gencache(int dummy)
{
char *val;
time_t tm;
if (!gencache_init()) {
d_printf("%s: gencache_init() failed\n", __location__);
return False;
}
if (!gencache_set("foo", "bar", time(NULL) + 1000)) {
d_printf("%s: gencache_set() failed\n", __location__);
return False;
}
if (!gencache_get("foo", &val, &tm)) {
d_printf("%s: gencache_get() failed\n", __location__);
return False;
}
if (strcmp(val, "bar") != 0) {
d_printf("%s: gencache_get() returned %s, expected %s\n",
__location__, val, "bar");
SAFE_FREE(val);
return False;
}
SAFE_FREE(val);
if (!gencache_del("foo")) {
d_printf("%s: gencache_del() failed\n", __location__);
return False;
}
if (gencache_del("foo")) {
d_printf("%s: second gencache_del() succeeded\n",
__location__);
return False;
}
if (gencache_get("foo", &val, &tm)) {
d_printf("%s: gencache_get() on deleted entry "
"succeeded\n", __location__);
return False;
}
if (!gencache_shutdown()) {
d_printf("%s: gencache_shutdown() failed\n", __location__);
return False;
}
if (gencache_shutdown()) {
d_printf("%s: second gencache_shutdown() succeeded\n",
__location__);
return False;
}
return True;
}
static double create_procs(BOOL (*fn)(int), BOOL *result)
{
int i, status;
@ -4956,6 +5015,7 @@ static struct {
{"FDSESS", run_fdsesstest, 0},
{ "EATEST", run_eatest, 0},
{ "LOCAL-SUBSTITUTE", run_local_substitute, 0},
{ "LOCAL-GENCACHE", run_local_gencache, 0},
{NULL, NULL, 0}};

View File

@ -181,47 +181,6 @@ static int net_cache_add(int argc, const char **argv)
return -1;
}
/**
* Set new value of an existing entry in the cache. Fail If the entry doesn't
* exist.
*
* @param argv key being searched and new value and timeout to set in the entry
* @return 0 on success, otherwise failure
**/
static int net_cache_set(int argc, const char **argv)
{
const char *keystr, *datastr, *timeout_str;
time_t timeout;
if (argc < 3) {
d_printf("\nUsage: net cache set <key string> <data string> <timeout>\n");
return -1;
}
keystr = argv[0];
datastr = argv[1];
timeout_str = argv[2];
/* parse timeout given in command line */
timeout = parse_timeout(timeout_str);
if (!timeout) {
d_fprintf(stderr, "Invalid timeout argument.\n");
return -1;
}
if (gencache_set_only(keystr, datastr, timeout)) {
d_printf("Cache entry set successfully.\n");
gencache_shutdown();
return 0;
}
d_fprintf(stderr, "Entry couldn't be set. Perhaps there's no such a key.\n");
gencache_shutdown();
return -1;
}
/**
* Delete an entry in the cache
*
@ -334,7 +293,6 @@ static int net_cache_flush(int argc, const char **argv)
static int net_cache_usage(int argc, const char **argv)
{
d_printf(" net cache add \t add add new cache entry\n");
d_printf(" net cache set \t set new value for existing cache entry\n");
d_printf(" net cache del \t delete existing cache entry by key\n");
d_printf(" net cache flush \t delete all entries existing in the cache\n");
d_printf(" net cache get \t get cache entry by key\n");
@ -354,7 +312,6 @@ int net_cache(int argc, const char **argv)
{
struct functable func[] = {
{"add", net_cache_add},
{"set", net_cache_set},
{"del", net_cache_del},
{"get", net_cache_get},
{"search", net_cache_search},