2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) Rafal Szczesniak 2002
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
26 * @brief This is part of the net tool which is basically command
27 * line wrapper for gencache.c functions (mainly for testing)
33 * These routines are used via gencache_iterate() to display the cache's contents
34 * (print_cache_entry) and to flush it (delete_cache_entry).
35 * Both of them are defined by first arg of gencache_iterate() routine.
37 static void print_cache_entry(const char* keystr, const char* datastr, const time_t timeout)
39 char* timeout_str = ctime(&timeout);
40 timeout_str[strlen(timeout_str) - 1] = '\0';
41 d_printf("Key: %s\t\t Value: %s\t\t Timeout: %s %s\n", keystr, datastr,
42 timeout_str, timeout > time(NULL) ? "": "(expired)");
45 static void delete_cache_entry(const char* keystr, const char* datastr, const time_t timeout)
47 if (!gencache_del(keystr))
48 d_printf("Couldn't delete entry! key = %s", keystr);
53 * Parse text representation of timeout value
55 * @param timeout_str string containing text representation of the timeout
56 * @return numeric timeout of time_t type
58 static time_t parse_timeout(const char* timeout_str)
60 char sign = '\0', *number = NULL, unit = '\0';
61 int len, number_begin, number_end;
65 if (timeout_str[0] == '!' || timeout_str[0] == '+') {
66 sign = timeout_str[0];
73 len = strlen(timeout_str);
74 switch (timeout_str[len - 1]) {
79 case 'w': unit = timeout_str[len - 1];
82 /* number detection */
83 len = (sign) ? strlen(&timeout_str[number_begin]) : len;
84 number_end = (unit) ? len - 1 : len;
85 number = strndup(&timeout_str[number_begin], number_end);
87 /* calculate actual timeout value */
88 timeout = (time_t)atoi(number);
91 case 'm': timeout *= 60; break;
92 case 'h': timeout *= 60*60; break;
93 case 'd': timeout *= 60*60*24; break;
94 case 'w': timeout *= 60*60*24*7; break; /* that's fair enough, I think :) */
98 case '!': timeout = time(NULL) - timeout; break;
100 default: timeout += time(NULL); break;
103 if (number) SAFE_FREE(number);
109 * Add an entry to the cache
111 * @param argv key, value and timeout are passed in command line
112 * @return 0 on success, otherwise failure
114 static int net_cache_add(int argc, const char **argv)
116 const char *keystr, *datastr, *timeout_str;
120 d_printf("\nUsage: net cache add <key string> <data string> <timeout>\n");
126 timeout_str = argv[2];
128 /* parse timeout given in command line */
129 timeout = parse_timeout(timeout_str);
131 d_printf("Invalid timeout argument.\n");
135 if (gencache_add(keystr, datastr, timeout)) {
136 d_printf("New cache entry stored successfully.\n");
141 d_printf("Entry couldn't be added. Perhaps there's already such a key.\n");
148 * Set new value of an existing entry in the cache
150 * @param argv key being searched and new value and timeout to set in the entry
151 * @return 0 on success, otherwise failure
153 static int net_cache_set(int argc, const char **argv)
155 const char *keystr, *datastr, *timeout_str;
159 d_printf("\nUsage: net cache set <key string> <data string> <timeout>\n");
165 timeout_str = argv[2];
167 /* parse timeout given in command line */
168 timeout = parse_timeout(timeout_str);
170 d_printf("Invalid timeout argument.\n");
174 if (gencache_set(keystr, datastr, timeout)) {
175 d_printf("Cache entry set successfully.\n");
180 d_printf("Entry couldn't be set. Perhaps there's no such a key.\n");
187 * Delete an entry in the cache
189 * @param argv key to delete an entry of
190 * @return 0 on success, otherwise failure
192 static int net_cache_del(int argc, const char **argv)
194 const char *keystr = argv[0];
197 d_printf("\nUsage: net cache add <key string>\n");
201 if(gencache_del(keystr)) {
202 d_printf("Entry deleted.\n");
206 d_printf("Couldn't delete specified entry\n");
212 * Get and display an entry from the cache
214 * @param argv key to search an entry of
215 * @return 0 on success, otherwise failure
217 static int net_cache_get(int argc, const char **argv)
219 const char* keystr = argv[0];
224 d_printf("\nUsage: net cache get <key>\n");
228 if (gencache_get(keystr, &valuestr, &timeout)) {
229 print_cache_entry(keystr, valuestr, timeout);
233 d_printf("Failed to find entry\n");
239 * Search an entry/entries in the cache
241 * @param argv key pattern to match the entries to
242 * @return 0 on success, otherwise failure
244 static int net_cache_search(int argc, const char **argv)
249 d_printf("Usage: net cache search <pattern>\n");
254 gencache_iterate(print_cache_entry, pattern);
260 * List the contents of the cache
262 * @param argv ignored in this functionailty
263 * @return always returns 0
265 static int net_cache_list(int argc, const char **argv)
267 const char* pattern = "*";
268 gencache_iterate(print_cache_entry, pattern);
275 * Flush the whole cache
277 * @param argv ignored in this functionality
278 * @return always returns 0
280 static int net_cache_flush(int argc, const char **argv)
282 const char* pattern = "*";
283 gencache_iterate(delete_cache_entry, pattern);
292 * @param argv ignored in this functionality
293 * @return always returns -1
295 static int net_cache_usage(int argc, const char **argv)
297 d_printf(" net cache add \t add add new cache entry\n");
298 d_printf(" net cache set \t set new value for existing cache entry\n");
299 d_printf(" net cache del \t delete existing cache entry by key\n");
300 d_printf(" net cache flush \t delete all entries existing in the cache\n");
301 d_printf(" net cache get \t get cache entry by key\n");
302 d_printf(" net cache search \t search for entries in the cache, by given pattern\n");
303 d_printf(" net cache list \t list all cache entries (just like search for \"*\")\n");
309 * Entry point to 'net cache' subfunctionality
311 * @param argv arguments passed to further called functions
312 * @return whatever further functions return
314 int net_cache(int argc, const char **argv)
316 struct functable func[] = {
317 {"add", net_cache_add},
318 {"set", net_cache_set},
319 {"del", net_cache_del},
320 {"get", net_cache_get},
321 {"search", net_cache_search},
322 {"list", net_cache_list},
323 {"flush", net_cache_flush},
327 return net_run_function(argc, argv, func, net_cache_usage);