summaryrefslogtreecommitdiff
path: root/source3/utils/net_cache.c
diff options
context:
space:
mode:
Diffstat (limited to 'source3/utils/net_cache.c')
-rw-r--r--source3/utils/net_cache.c56
1 files changed, 38 insertions, 18 deletions
diff --git a/source3/utils/net_cache.c b/source3/utils/net_cache.c
index 359c06d1aa..93c4f1aa1d 100644
--- a/source3/utils/net_cache.c
+++ b/source3/utils/net_cache.c
@@ -34,15 +34,34 @@
* (print_cache_entry) and to flush it (delete_cache_entry).
* Both of them are defined by first arg of gencache_iterate() routine.
*/
-static void print_cache_entry(const char* keystr, const char* datastr, const time_t timeout)
+static void print_cache_entry(const char* keystr, const char* datastr,
+ const time_t timeout, void* dptr)
{
- char* timeout_str = ctime(&timeout);
- timeout_str[strlen(timeout_str) - 1] = '\0';
- d_printf("Key: %s\t\t Value: %s\t\t Timeout: %s %s\n", keystr, datastr,
- timeout_str, timeout > time(NULL) ? "": "(expired)");
+ char* timeout_str;
+ time_t now_t = time(NULL);
+ struct tm timeout_tm, *now_tm;
+ /* localtime returns statically allocated pointer, so timeout_tm
+ has to be copied somewhere else */
+ memcpy(&timeout_tm, localtime(&timeout), sizeof(struct tm));
+ now_tm = localtime(&now_t);
+
+ /* form up timeout string depending whether it's today's date or not */
+ if (timeout_tm.tm_year != now_tm->tm_year ||
+ timeout_tm.tm_mon != now_tm->tm_mon ||
+ timeout_tm.tm_mday != now_tm->tm_mday) {
+
+ timeout_str = asctime(&timeout_tm);
+ timeout_str[strlen(timeout_str) - 1] = '\0'; /* remove tailing CR */
+ } else
+ asprintf(&timeout_str, "%.2d:%.2d:%.2d", timeout_tm.tm_hour,
+ timeout_tm.tm_min, timeout_tm.tm_sec);
+
+ d_printf("Key: %s\t Timeout: %s\t Value: %s %s\n", keystr,
+ timeout_str, datastr, timeout > now_t ? "": "(expired)");
}
-static void delete_cache_entry(const char* keystr, const char* datastr, const time_t timeout)
+static void delete_cache_entry(const char* keystr, const char* datastr,
+ const time_t timeout, void* dptr)
{
if (!gencache_del(keystr))
d_printf("Couldn't delete entry! key = %s", keystr);
@@ -106,7 +125,7 @@ static time_t parse_timeout(const char* timeout_str)
/**
- * Add an entry to the cache
+ * Add an entry to the cache. If it does exist, then set it.
*
* @param argv key, value and timeout are passed in command line
* @return 0 on success, otherwise failure
@@ -132,12 +151,12 @@ static int net_cache_add(int argc, const char **argv)
return -1;
}
- if (gencache_add(keystr, datastr, timeout)) {
+ if (gencache_set(keystr, datastr, timeout)) {
d_printf("New cache entry stored successfully.\n");
gencache_shutdown();
return 0;
- }
-
+ }
+
d_printf("Entry couldn't be added. Perhaps there's already such a key.\n");
gencache_shutdown();
return -1;
@@ -145,7 +164,8 @@ static int net_cache_add(int argc, const char **argv)
/**
- * Set new value of an existing entry in the cache
+ * 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
@@ -171,7 +191,7 @@ static int net_cache_set(int argc, const char **argv)
return -1;
}
- if (gencache_set(keystr, datastr, timeout)) {
+ if (gencache_set_only(keystr, datastr, timeout)) {
d_printf("Cache entry set successfully.\n");
gencache_shutdown();
return 0;
@@ -201,7 +221,7 @@ static int net_cache_del(int argc, const char **argv)
if(gencache_del(keystr)) {
d_printf("Entry deleted.\n");
return 0;
- }
+ }
d_printf("Couldn't delete specified entry\n");
return -1;
@@ -226,9 +246,9 @@ static int net_cache_get(int argc, const char **argv)
}
if (gencache_get(keystr, &valuestr, &timeout)) {
- print_cache_entry(keystr, valuestr, timeout);
+ print_cache_entry(keystr, valuestr, timeout, NULL);
return 0;
- }
+ }
d_printf("Failed to find entry\n");
return -1;
@@ -251,7 +271,7 @@ static int net_cache_search(int argc, const char **argv)
}
pattern = argv[0];
- gencache_iterate(print_cache_entry, pattern);
+ gencache_iterate(print_cache_entry, NULL, pattern);
return 0;
}
@@ -265,7 +285,7 @@ static int net_cache_search(int argc, const char **argv)
static int net_cache_list(int argc, const char **argv)
{
const char* pattern = "*";
- gencache_iterate(print_cache_entry, pattern);
+ gencache_iterate(print_cache_entry, NULL, pattern);
gencache_shutdown();
return 0;
}
@@ -280,7 +300,7 @@ static int net_cache_list(int argc, const char **argv)
static int net_cache_flush(int argc, const char **argv)
{
const char* pattern = "*";
- gencache_iterate(delete_cache_entry, pattern);
+ gencache_iterate(delete_cache_entry, NULL, pattern);
gencache_shutdown();
return 0;
}