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,
38 const time_t timeout, void* dptr)
41 time_t now_t = time(NULL);
42 struct tm timeout_tm, *now_tm;
43 /* localtime returns statically allocated pointer, so timeout_tm
44 has to be copied somewhere else */
45 memcpy(&timeout_tm, localtime(&timeout), sizeof(struct tm));
46 now_tm = localtime(&now_t);
48 /* form up timeout string depending whether it's today's date or not */
49 if (timeout_tm.tm_year != now_tm->tm_year ||
50 timeout_tm.tm_mon != now_tm->tm_mon ||
51 timeout_tm.tm_mday != now_tm->tm_mday) {
53 timeout_str = asctime(&timeout_tm);
54 timeout_str[strlen(timeout_str) - 1] = '\0'; /* remove tailing CR */
56 asprintf(&timeout_str, "%.2d:%.2d:%.2d", timeout_tm.tm_hour,
57 timeout_tm.tm_min, timeout_tm.tm_sec);
59 d_printf("Key: %s\t Timeout: %s\t Value: %s %s\n", keystr,
60 timeout_str, datastr, timeout > now_t ? "": "(expired)");
63 static void delete_cache_entry(const char* keystr, const char* datastr,
64 const time_t timeout, void* dptr)
66 if (!gencache_del(keystr))
67 d_printf("Couldn't delete entry! key = %s", keystr);
72 * Parse text representation of timeout value
74 * @param timeout_str string containing text representation of the timeout
75 * @return numeric timeout of time_t type
77 static time_t parse_timeout(const char* timeout_str)
79 char sign = '\0', *number = NULL, unit = '\0';
80 int len, number_begin, number_end;
84 if (timeout_str[0] == '!' || timeout_str[0] == '+') {
85 sign = timeout_str[0];
92 len = strlen(timeout_str);
93 switch (timeout_str[len - 1]) {
98 case 'w': unit = timeout_str[len - 1];
101 /* number detection */
102 len = (sign) ? strlen(&timeout_str[number_begin]) : len;
103 number_end = (unit) ? len - 1 : len;
104 number = strndup(&timeout_str[number_begin], number_end);
106 /* calculate actual timeout value */
107 timeout = (time_t)atoi(number);
110 case 'm': timeout *= 60; break;
111 case 'h': timeout *= 60*60; break;
112 case 'd': timeout *= 60*60*24; break;
113 case 'w': timeout *= 60*60*24*7; break; /* that's fair enough, I think :) */
117 case '!': timeout = time(NULL) - timeout; break;
119 default: timeout += time(NULL); break;
122 if (number) SAFE_FREE(number);
128 * Add an entry to the cache. If it does exist, then set it.
130 * @param argv key, value and timeout are passed in command line
131 * @return 0 on success, otherwise failure
133 static int net_cache_add(int argc, const char **argv)
135 const char *keystr, *datastr, *timeout_str;
139 d_printf("\nUsage: net cache add <key string> <data string> <timeout>\n");
145 timeout_str = argv[2];
147 /* parse timeout given in command line */
148 timeout = parse_timeout(timeout_str);
150 d_printf("Invalid timeout argument.\n");
154 if (gencache_set(keystr, datastr, timeout)) {
155 d_printf("New cache entry stored successfully.\n");
160 d_printf("Entry couldn't be added. Perhaps there's already such a key.\n");
167 * Set new value of an existing entry in the cache. Fail If the entry doesn't
170 * @param argv key being searched and new value and timeout to set in the entry
171 * @return 0 on success, otherwise failure
173 static int net_cache_set(int argc, const char **argv)
175 const char *keystr, *datastr, *timeout_str;
179 d_printf("\nUsage: net cache set <key string> <data string> <timeout>\n");
185 timeout_str = argv[2];
187 /* parse timeout given in command line */
188 timeout = parse_timeout(timeout_str);
190 d_printf("Invalid timeout argument.\n");
194 if (gencache_set_only(keystr, datastr, timeout)) {
195 d_printf("Cache entry set successfully.\n");
200 d_printf("Entry couldn't be set. Perhaps there's no such a key.\n");
207 * Delete an entry in the cache
209 * @param argv key to delete an entry of
210 * @return 0 on success, otherwise failure
212 static int net_cache_del(int argc, const char **argv)
214 const char *keystr = argv[0];
217 d_printf("\nUsage: net cache add <key string>\n");
221 if(gencache_del(keystr)) {
222 d_printf("Entry deleted.\n");
226 d_printf("Couldn't delete specified entry\n");
232 * Get and display an entry from the cache
234 * @param argv key to search an entry of
235 * @return 0 on success, otherwise failure
237 static int net_cache_get(int argc, const char **argv)
239 const char* keystr = argv[0];
244 d_printf("\nUsage: net cache get <key>\n");
248 if (gencache_get(keystr, &valuestr, &timeout)) {
249 print_cache_entry(keystr, valuestr, timeout, NULL);
253 d_printf("Failed to find entry\n");
259 * Search an entry/entries in the cache
261 * @param argv key pattern to match the entries to
262 * @return 0 on success, otherwise failure
264 static int net_cache_search(int argc, const char **argv)
269 d_printf("Usage: net cache search <pattern>\n");
274 gencache_iterate(print_cache_entry, NULL, pattern);
280 * List the contents of the cache
282 * @param argv ignored in this functionailty
283 * @return always returns 0
285 static int net_cache_list(int argc, const char **argv)
287 const char* pattern = "*";
288 gencache_iterate(print_cache_entry, NULL, pattern);
295 * Flush the whole cache
297 * @param argv ignored in this functionality
298 * @return always returns 0
300 static int net_cache_flush(int argc, const char **argv)
302 const char* pattern = "*";
303 gencache_iterate(delete_cache_entry, NULL, pattern);
312 * @param argv ignored in this functionality
313 * @return always returns -1
315 static int net_cache_usage(int argc, const char **argv)
317 d_printf(" net cache add \t add add new cache entry\n");
318 d_printf(" net cache set \t set new value for existing cache entry\n");
319 d_printf(" net cache del \t delete existing cache entry by key\n");
320 d_printf(" net cache flush \t delete all entries existing in the cache\n");
321 d_printf(" net cache get \t get cache entry by key\n");
322 d_printf(" net cache search \t search for entries in the cache, by given pattern\n");
323 d_printf(" net cache list \t list all cache entries (just like search for \"*\")\n");
329 * Entry point to 'net cache' subfunctionality
331 * @param argv arguments passed to further called functions
332 * @return whatever further functions return
334 int net_cache(int argc, const char **argv)
336 struct functable func[] = {
337 {"add", net_cache_add},
338 {"set", net_cache_set},
339 {"del", net_cache_del},
340 {"get", net_cache_get},
341 {"search", net_cache_search},
342 {"list", net_cache_list},
343 {"flush", net_cache_flush},
347 return net_run_function(argc, argv, func, net_cache_usage);