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, DATA_BLOB value,
37 const time_t timeout, void* dptr)
40 char *alloc_str = NULL;
42 char *datastr_free = NULL;
43 time_t now_t = time(NULL);
44 struct tm timeout_tm, now_tm;
45 struct tm *ptimeout_tm, *pnow_tm;
47 ptimeout_tm = localtime_r(&timeout, &timeout_tm);
48 if (ptimeout_tm == NULL) {
51 pnow_tm = localtime_r(&now_t, &now_tm);
52 if (pnow_tm == NULL) {
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 datastr = (char *)value.data;
76 if ((value.length > 0) && (value.data[value.length-1] != '\0')) {
77 datastr_free = talloc_asprintf(
78 talloc_tos(), "<binary length %d>",
80 datastr = datastr_free;
81 if (datastr == NULL) {
86 d_printf(_("Key: %s\t Timeout: %s\t Value: %s %s\n"), keystr,
87 timeout_str, datastr, timeout > now_t ? "": _("(expired)"));
92 static void delete_cache_entry(const char* keystr, const char* datastr,
93 const time_t timeout, void* dptr)
95 if (!gencache_del(keystr))
96 d_fprintf(stderr, _("Couldn't delete entry! key = %s\n"),
102 * Parse text representation of timeout value
104 * @param timeout_str string containing text representation of the timeout
105 * @return numeric timeout of time_t type
107 static time_t parse_timeout(const char* timeout_str)
109 char sign = '\0', *number = NULL, unit = '\0';
110 int len, number_begin, number_end;
114 if (timeout_str[0] == '!' || timeout_str[0] == '+') {
115 sign = timeout_str[0];
122 len = strlen(timeout_str);
123 switch (timeout_str[len - 1]) {
128 case 'w': unit = timeout_str[len - 1];
131 /* number detection */
132 len = (sign) ? strlen(&timeout_str[number_begin]) : len;
133 number_end = (unit) ? len - 1 : len;
134 number = SMB_STRNDUP(&timeout_str[number_begin], number_end);
136 /* calculate actual timeout value */
137 timeout = (time_t)atoi(number);
140 case 'm': timeout *= 60; break;
141 case 'h': timeout *= 60*60; break;
142 case 'd': timeout *= 60*60*24; break;
143 case 'w': timeout *= 60*60*24*7; break; /* that's fair enough, I think :) */
147 case '!': timeout = time(NULL) - timeout; break;
149 default: timeout += time(NULL); break;
152 if (number) SAFE_FREE(number);
158 * Add an entry to the cache. If it does exist, then set it.
160 * @param c A net_context structure
161 * @param argv key, value and timeout are passed in command line
162 * @return 0 on success, otherwise failure
164 static int net_cache_add(struct net_context *c, int argc, const char **argv)
166 const char *keystr, *datastr, *timeout_str;
169 if (argc < 3 || c->display_usage) {
172 _("net cache add <key string> <data string> "
179 timeout_str = argv[2];
181 /* parse timeout given in command line */
182 timeout = parse_timeout(timeout_str);
184 d_fprintf(stderr, _("Invalid timeout argument.\n"));
188 if (gencache_set(keystr, datastr, timeout)) {
189 d_printf(_("New cache entry stored successfully.\n"));
193 d_fprintf(stderr, _("Entry couldn't be added. Perhaps there's already such a key.\n"));
198 * Delete an entry in the cache
200 * @param c A net_context structure
201 * @param argv key to delete an entry of
202 * @return 0 on success, otherwise failure
204 static int net_cache_del(struct net_context *c, int argc, const char **argv)
206 const char *keystr = argv[0];
208 if (argc < 1 || c->display_usage) {
211 _(" net cache del <key string>\n"));
215 if(gencache_del(keystr)) {
216 d_printf(_("Entry deleted.\n"));
220 d_fprintf(stderr, _("Couldn't delete specified entry\n"));
226 * Get and display an entry from the cache
228 * @param c A net_context structure
229 * @param argv key to search an entry of
230 * @return 0 on success, otherwise failure
232 static int net_cache_get(struct net_context *c, int argc, const char **argv)
234 const char* keystr = argv[0];
238 if (argc < 1 || c->display_usage) {
241 _(" net cache get <key>\n"));
245 if (gencache_get_data_blob(keystr, &value, &timeout, NULL)) {
246 print_cache_entry(keystr, value, timeout, NULL);
247 SAFE_FREE(value.data);
251 d_fprintf(stderr, _("Failed to find entry\n"));
257 * Search an entry/entries in the cache
259 * @param c A net_context structure
260 * @param argv key pattern to match the entries to
261 * @return 0 on success, otherwise failure
263 static int net_cache_search(struct net_context *c, int argc, const char **argv)
267 if (argc < 1 || c->display_usage) {
270 _(" net cache search <pattern>\n"));
275 gencache_iterate_blobs(print_cache_entry, NULL, pattern);
281 * List the contents of the cache
283 * @param c A net_context structure
284 * @param argv ignored in this functionailty
285 * @return always returns 0
287 static int net_cache_list(struct net_context *c, int argc, const char **argv)
289 const char* pattern = "*";
291 if (c->display_usage) {
296 _("List all cache entries."));
299 gencache_iterate_blobs(print_cache_entry, NULL, pattern);
305 * Flush the whole cache
307 * @param c A net_context structure
308 * @param argv ignored in this functionality
309 * @return always returns 0
311 static int net_cache_flush(struct net_context *c, int argc, const char **argv)
313 const char* pattern = "*";
314 if (c->display_usage) {
319 _("Delete all cache entries."));
322 gencache_iterate(delete_cache_entry, NULL, pattern);
326 static int net_cache_stabilize(struct net_context *c, int argc,
329 if (c->display_usage) {
331 "net cache stabilize\n"
334 _("Move transient cache content to stable storage"));
338 if (!gencache_stabilize()) {
344 * Entry point to 'net cache' subfunctionality
346 * @param c A net_context structure
347 * @param argv arguments passed to further called functions
348 * @return whatever further functions return
350 int net_cache(struct net_context *c, int argc, const char **argv)
352 struct functable func[] = {
357 N_("Add new cache entry"),
358 N_("net cache add <key string> <data string> <timeout>\n"
359 " Add new cache entry.\n"
360 " key string\tKey string to add cache data under.\n"
361 " data string\tData to store under given key.\n"
362 " timeout\tTimeout for cache data.")
368 N_("Delete existing cache entry by key"),
369 N_("net cache del <key string>\n"
370 " Delete existing cache entry by key.\n"
371 " key string\tKey string to delete.")
377 N_("Get cache entry by key"),
378 N_("net cache get <key string>\n"
379 " Get cache entry by key.\n"
380 " key string\tKey string to look up cache entry for.")
387 N_("Search entry by pattern"),
388 N_("net cache search <pattern>\n"
389 " Search entry by pattern.\n"
390 " pattern\tPattern to search for in cache.")
396 N_("List all cache entries"),
397 N_("net cache list\n"
398 " List all cache entries")
404 N_("Delete all cache entries"),
405 N_("net cache flush\n"
406 " Delete all cache entries")
412 N_("Move transient cache content to stable storage"),
413 N_("net cache stabilize\n"
414 " Move transient cache content to stable storage")
416 {NULL, NULL, 0, NULL, NULL}
419 return net_run_function(c, argc, argv, "net cache", func);