summaryrefslogtreecommitdiff
path: root/source/utils/net_cache.c
diff options
context:
space:
mode:
Diffstat (limited to 'source/utils/net_cache.c')
-rw-r--r--source/utils/net_cache.c190
1 files changed, 70 insertions, 120 deletions
diff --git a/source/utils/net_cache.c b/source/utils/net_cache.c
index 4e9ae18c0d..8e0f98ef7e 100644
--- a/source/utils/net_cache.c
+++ b/source/utils/net_cache.c
@@ -1,21 +1,21 @@
-/*
- Samba Unix/Linux SMB client library
- Distributed SMB/CIFS Server Management Utility
+/*
+ Samba Unix/Linux SMB client library
+ Distributed SMB/CIFS Server Management Utility
Copyright (C) Rafal Szczesniak 2002
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
-
+
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
-
+
You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-*/
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
#include "includes.h"
#include "net.h"
@@ -57,11 +57,11 @@ static void print_cache_entry(const char* keystr, const char* datastr,
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);
if (!timeout_str) {
return;
- }
+ }
timeout_str[strlen(timeout_str) - 1] = '\0'; /* remove tailing CR */
} else {
asprintf(&alloc_str, "%.2d:%.2d:%.2d", timeout_tm.tm_hour,
@@ -71,7 +71,7 @@ static void print_cache_entry(const char* keystr, const char* datastr,
}
timeout_str = alloc_str;
}
-
+
d_printf("Key: %s\t Timeout: %s\t Value: %s %s\n", keystr,
timeout_str, datastr, timeout > now_t ? "": "(expired)");
@@ -105,7 +105,7 @@ static time_t parse_timeout(const char* timeout_str)
} else {
number_begin = 0;
}
-
+
/* unit detection */
len = strlen(timeout_str);
switch (timeout_str[len - 1]) {
@@ -115,12 +115,12 @@ static time_t parse_timeout(const char* timeout_str)
case 'd':
case 'w': unit = timeout_str[len - 1];
}
-
+
/* number detection */
len = (sign) ? strlen(&timeout_str[number_begin]) : len;
number_end = (unit) ? len - 1 : len;
number = SMB_STRNDUP(&timeout_str[number_begin], number_end);
-
+
/* calculate actual timeout value */
timeout = (time_t)atoi(number);
@@ -130,52 +130,51 @@ static time_t parse_timeout(const char* timeout_str)
case 'd': timeout *= 60*60*24; break;
case 'w': timeout *= 60*60*24*7; break; /* that's fair enough, I think :) */
}
-
+
switch (sign) {
case '!': timeout = time(NULL) - timeout; break;
case '+':
default: timeout += time(NULL); break;
}
-
+
if (number) SAFE_FREE(number);
- return timeout;
+ return timeout;
}
/**
* Add an entry to the cache. If it does exist, then set it.
- *
- * @param c A net_context structure
+ *
* @param argv key, value and timeout are passed in command line
* @return 0 on success, otherwise failure
**/
-static int net_cache_add(struct net_context *c, int argc, const char **argv)
+static int net_cache_add(int argc, const char **argv)
{
const char *keystr, *datastr, *timeout_str;
time_t timeout;
-
- if (argc < 3 || c->display_usage) {
+
+ if (argc < 3) {
d_printf("\nUsage: net cache add <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(keystr, datastr, timeout)) {
d_printf("New cache entry stored successfully.\n");
gencache_shutdown();
return 0;
}
-
+
d_fprintf(stderr, "Entry couldn't be added. Perhaps there's already such a key.\n");
gencache_shutdown();
return -1;
@@ -183,20 +182,19 @@ static int net_cache_add(struct net_context *c, int argc, const char **argv)
/**
* Delete an entry in the cache
- *
- * @param c A net_context structure
+ *
* @param argv key to delete an entry of
* @return 0 on success, otherwise failure
**/
-static int net_cache_del(struct net_context *c, int argc, const char **argv)
+static int net_cache_del(int argc, const char **argv)
{
const char *keystr = argv[0];
-
- if (argc < 1 || c->display_usage) {
+
+ if (argc < 1) {
d_printf("\nUsage: net cache del <key string>\n");
return -1;
}
-
+
if(gencache_del(keystr)) {
d_printf("Entry deleted.\n");
return 0;
@@ -209,22 +207,21 @@ static int net_cache_del(struct net_context *c, int argc, const char **argv)
/**
* Get and display an entry from the cache
- *
- * @param c A net_context structure
+ *
* @param argv key to search an entry of
* @return 0 on success, otherwise failure
**/
-static int net_cache_get(struct net_context *c, int argc, const char **argv)
+static int net_cache_get(int argc, const char **argv)
{
const char* keystr = argv[0];
char* valuestr;
time_t timeout;
- if (argc < 1 || c->display_usage) {
+ if (argc < 1) {
d_printf("\nUsage: net cache get <key>\n");
return -1;
}
-
+
if (gencache_get(keystr, &valuestr, &timeout)) {
print_cache_entry(keystr, valuestr, timeout, NULL);
return 0;
@@ -237,20 +234,19 @@ static int net_cache_get(struct net_context *c, int argc, const char **argv)
/**
* Search an entry/entries in the cache
- *
- * @param c A net_context structure
+ *
* @param argv key pattern to match the entries to
* @return 0 on success, otherwise failure
**/
-static int net_cache_search(struct net_context *c, int argc, const char **argv)
+static int net_cache_search(int argc, const char **argv)
{
const char* pattern;
-
- if (argc < 1 || c->display_usage) {
+
+ if (argc < 1) {
d_printf("Usage: net cache search <pattern>\n");
return -1;
}
-
+
pattern = argv[0];
gencache_iterate(print_cache_entry, NULL, pattern);
return 0;
@@ -259,21 +255,13 @@ static int net_cache_search(struct net_context *c, int argc, const char **argv)
/**
* List the contents of the cache
- *
- * @param c A net_context structure
+ *
* @param argv ignored in this functionailty
* @return always returns 0
**/
-static int net_cache_list(struct net_context *c, int argc, const char **argv)
+static int net_cache_list(int argc, const char **argv)
{
const char* pattern = "*";
-
- if (c->display_usage) {
- d_printf("Usage:\n"
- "net cache list\n"
- " List all cache entries.\n");
- return 0;
- }
gencache_iterate(print_cache_entry, NULL, pattern);
gencache_shutdown();
return 0;
@@ -282,92 +270,54 @@ static int net_cache_list(struct net_context *c, int argc, const char **argv)
/**
* Flush the whole cache
- *
- * @param c A net_context structure
+ *
* @param argv ignored in this functionality
* @return always returns 0
**/
-static int net_cache_flush(struct net_context *c, int argc, const char **argv)
+static int net_cache_flush(int argc, const char **argv)
{
const char* pattern = "*";
- if (c->display_usage) {
- d_printf("Usage:\n"
- "net cache flush\n"
- " Delete all cache entries.\n");
- return 0;
- }
gencache_iterate(delete_cache_entry, NULL, pattern);
gencache_shutdown();
return 0;
}
+
+/**
+ * Short help
+ *
+ * @param argv ignored in this functionality
+ * @return always returns -1
+ **/
+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 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");
+ d_printf(" net cache search \t search for entries in the cache, by given pattern\n");
+ d_printf(" net cache list \t list all cache entries (just like search for \"*\")\n");
+ return -1;
+}
+
+
/**
* Entry point to 'net cache' subfunctionality
*
- * @param c A net_context structure
* @param argv arguments passed to further called functions
* @return whatever further functions return
**/
-int net_cache(struct net_context *c, int argc, const char **argv)
+int net_cache(int argc, const char **argv)
{
struct functable func[] = {
- {
- "add",
- net_cache_add,
- NET_TRANSPORT_LOCAL,
- "Add new cache entry",
- "net cache add <key string> <data string> <timeout>\n"
- " Add new cache entry.\n"
- " key string\tKey string to add cache data under.\n"
- " data string\tData to store under given key.\n"
- " timeout\tTimeout for cache data."
- },
- {
- "del",
- net_cache_del,
- NET_TRANSPORT_LOCAL,
- "Delete existing cache entry by key",
- "net cache del <key string>\n"
- " Delete existing cache entry by key.\n"
- " key string\tKey string to delete."
- },
- {
- "get",
- net_cache_get,
- NET_TRANSPORT_LOCAL,
- "Get cache entry by key",
- "net cache get <key string>\n"
- " Get cache entry by key.\n"
- " key string\tKey string to look up cache entry for."
-
- },
- {
- "search",
- net_cache_search,
- NET_TRANSPORT_LOCAL,
- "Search entry by pattern",
- "net cache search <pattern>\n"
- " Search entry by pattern.\n"
- " pattern\tPattern to search for in cache."
- },
- {
- "list",
- net_cache_list,
- NET_TRANSPORT_LOCAL,
- "List all cache entries",
- "net cache list\n"
- " List all cache entries"
- },
- {
- "flush",
- net_cache_flush,
- NET_TRANSPORT_LOCAL,
- "Delete all cache entries",
- "net cache flush\n"
- " Delete all cache entries"
- },
- {NULL, NULL, 0, NULL, NULL}
+ {"add", net_cache_add},
+ {"del", net_cache_del},
+ {"get", net_cache_get},
+ {"search", net_cache_search},
+ {"list", net_cache_list},
+ {"flush", net_cache_flush},
+ {NULL, NULL}
};
- return net_run_function(c, argc, argv, "net cache", func);
+ return net_run_function(argc, argv, func, net_cache_usage);
}