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 3 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, see <http://www.gnu.org/licenses/>.
25 * @brief This is part of the net tool which is basically command
26 * line wrapper for gencache.c functions (mainly for testing)
32 * These routines are used via gencache_iterate() to display the cache's contents
33 * (print_cache_entry) and to flush it (delete_cache_entry).
34 * Both of them are defined by first arg of gencache_iterate() routine.
36 static void print_cache_entry(const char* keystr, const char* datastr,
37 const time_t timeout, void* dptr)
40 char *alloc_str = NULL;
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 */
46 now_tm = localtime(&timeout);
50 memcpy(&timeout_tm, now_tm, sizeof(struct tm));
51 now_tm = localtime(&now_t);
56 /* form up timeout string depending whether it's today's date or not */
57 if (timeout_tm.tm_year != now_tm->tm_year ||
58 timeout_tm.tm_mon != now_tm->tm_mon ||
59 timeout_tm.tm_mday != now_tm->tm_mday) {
61 timeout_str = asctime(&timeout_tm);
65 timeout_str[strlen(timeout_str) - 1] = '\0'; /* remove tailing CR */
67 if (asprintf(&alloc_str, "%.2d:%.2d:%.2d", timeout_tm.tm_hour,
68 timeout_tm.tm_min, timeout_tm.tm_sec) == -1) {
71 timeout_str = alloc_str;
74 d_printf(_("Key: %s\t Timeout: %s\t Value: %s %s\n"), keystr,
75 timeout_str, datastr, timeout > now_t ? "": _("(expired)"));
80 static void delete_cache_entry(const char* keystr, const char* datastr,
81 const time_t timeout, void* dptr)
83 if (!gencache_del(keystr))
84 d_fprintf(stderr, _("Couldn't delete entry! key = %s\n"),
90 * Parse text representation of timeout value
92 * @param timeout_str string containing text representation of the timeout
93 * @return numeric timeout of time_t type
95 static time_t parse_timeout(const char* timeout_str)
97 char sign = '\0', *number = NULL, unit = '\0';
98 int len, number_begin, number_end;
102 if (timeout_str[0] == '!' || timeout_str[0] == '+') {
103 sign = timeout_str[0];
110 len = strlen(timeout_str);
111 switch (timeout_str[len - 1]) {
116 case 'w': unit = timeout_str[len - 1];
119 /* number detection */
120 len = (sign) ? strlen(&timeout_str[number_begin]) : len;
121 number_end = (unit) ? len - 1 : len;
122 number = SMB_STRNDUP(&timeout_str[number_begin], number_end);
124 /* calculate actual timeout value */
125 timeout = (time_t)atoi(number);
128 case 'm': timeout *= 60; break;
129 case 'h': timeout *= 60*60; break;
130 case 'd': timeout *= 60*60*24; break;
131 case 'w': timeout *= 60*60*24*7; break; /* that's fair enough, I think :) */
135 case '!': timeout = time(NULL) - timeout; break;
137 default: timeout += time(NULL); break;
140 if (number) SAFE_FREE(number);
146 * Add an entry to the cache. If it does exist, then set it.
148 * @param c A net_context structure
149 * @param argv key, value and timeout are passed in command line
150 * @return 0 on success, otherwise failure
152 static int net_cache_add(struct net_context *c, int argc, const char **argv)
154 const char *keystr, *datastr, *timeout_str;
157 if (argc < 3 || c->display_usage) {
158 d_printf("\n",_("Usage:\n"),
159 _("net cache add <key string> <data string> "
166 timeout_str = argv[2];
168 /* parse timeout given in command line */
169 timeout = parse_timeout(timeout_str);
171 d_fprintf(stderr, _("Invalid timeout argument.\n"));
175 if (gencache_set(keystr, datastr, timeout)) {
176 d_printf(_("New cache entry stored successfully.\n"));
180 d_fprintf(stderr, _("Entry couldn't be added. Perhaps there's already such a key.\n"));
185 * Delete an entry in the cache
187 * @param c A net_context structure
188 * @param argv key to delete an entry of
189 * @return 0 on success, otherwise failure
191 static int net_cache_del(struct net_context *c, int argc, const char **argv)
193 const char *keystr = argv[0];
195 if (argc < 1 || c->display_usage) {
196 d_printf("\n",_("Usage:"), _(" net cache del <key string>\n"));
200 if(gencache_del(keystr)) {
201 d_printf(_("Entry deleted.\n"));
205 d_fprintf(stderr, _("Couldn't delete specified entry\n"));
211 * Get and display an entry from the cache
213 * @param c A net_context structure
214 * @param argv key to search an entry of
215 * @return 0 on success, otherwise failure
217 static int net_cache_get(struct net_context *c, int argc, const char **argv)
219 const char* keystr = argv[0];
220 char* valuestr = NULL;
223 if (argc < 1 || c->display_usage) {
224 d_printf("\n", _("Usage:"), _(" net cache get <key>\n"));
228 if (gencache_get(keystr, &valuestr, &timeout)) {
229 print_cache_entry(keystr, valuestr, timeout, NULL);
234 d_fprintf(stderr, _("Failed to find entry\n"));
240 * Search an entry/entries in the cache
242 * @param c A net_context structure
243 * @param argv key pattern to match the entries to
244 * @return 0 on success, otherwise failure
246 static int net_cache_search(struct net_context *c, int argc, const char **argv)
250 if (argc < 1 || c->display_usage) {
251 d_printf(_("Usage:"), _(" net cache search <pattern>\n"));
256 gencache_iterate(print_cache_entry, NULL, pattern);
262 * List the contents of the cache
264 * @param c A net_context structure
265 * @param argv ignored in this functionailty
266 * @return always returns 0
268 static int net_cache_list(struct net_context *c, int argc, const char **argv)
270 const char* pattern = "*";
272 if (c->display_usage) {
273 d_printf(_("Usage:\n"),
275 " ", _("List all cache entries.\n"));
278 gencache_iterate(print_cache_entry, NULL, pattern);
284 * Flush the whole cache
286 * @param c A net_context structure
287 * @param argv ignored in this functionality
288 * @return always returns 0
290 static int net_cache_flush(struct net_context *c, int argc, const char **argv)
292 const char* pattern = "*";
293 if (c->display_usage) {
294 d_printf(_("Usage:\n"),
296 " ", _("Delete all cache entries.\n"));
299 gencache_iterate(delete_cache_entry, NULL, pattern);
303 static int net_cache_stabilize(struct net_context *c, int argc,
306 if (c->display_usage) {
307 d_printf(_("Usage:\n"),
309 " ", _("Delete all cache entries.\n"));
313 if (!gencache_stabilize()) {
319 * Entry point to 'net cache' subfunctionality
321 * @param c A net_context structure
322 * @param argv arguments passed to further called functions
323 * @return whatever further functions return
325 int net_cache(struct net_context *c, int argc, const char **argv)
327 struct functable func[] = {
332 N_("Add new cache entry"),
333 N_("net cache add <key string> <data string> <timeout>\n"
334 " Add new cache entry.\n"
335 " key string\tKey string to add cache data under.\n"
336 " data string\tData to store under given key.\n"
337 " timeout\tTimeout for cache data.")
343 N_("Delete existing cache entry by key"),
344 N_("net cache del <key string>\n"
345 " Delete existing cache entry by key.\n"
346 " key string\tKey string to delete.")
352 N_("Get cache entry by key"),
353 N_("net cache get <key string>\n"
354 " Get cache entry by key.\n"
355 " key string\tKey string to look up cache entry for.")
362 N_("Search entry by pattern"),
363 N_("net cache search <pattern>\n"
364 " Search entry by pattern.\n"
365 " pattern\tPattern to search for in cache.")
371 N_("List all cache entries"),
372 N_("net cache list\n"
373 " List all cache entries")
379 N_("Delete all cache entries"),
380 N_("net cache flush\n"
381 " Delete all cache entries")
387 N_("Move transient cache content to stable storage"),
388 N_("net cache stabilize\n"
389 " Move transient cache content to stable storage")
391 {NULL, NULL, 0, NULL, NULL}
394 return net_run_function(c, argc, argv, "net cache", func);