Fix bug #8458] - IE9 on Windows 7 cannot download files to samba 3.5.11 share
[Samba.git] / source3 / utils / net_cache.c
blobafcb7a1874f2ca76ee5d82a71e8b09796f143c60
1 /*
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/>.
20 #include "includes.h"
21 #include "net.h"
23 /**
24 * @file net_cache.c
25 * @brief This is part of the net tool which is basically command
26 * line wrapper for gencache.c functions (mainly for testing)
28 **/
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)
39 char *timeout_str;
40 char *alloc_str = NULL;
41 const char *datastr;
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) {
49 return;
51 pnow_tm = localtime_r(&now_t, &now_tm);
52 if (pnow_tm == NULL) {
53 return;
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);
62 if (!timeout_str) {
63 return;
65 timeout_str[strlen(timeout_str) - 1] = '\0'; /* remove tailing CR */
66 } else {
67 if (asprintf(&alloc_str, "%.2d:%.2d:%.2d", timeout_tm.tm_hour,
68 timeout_tm.tm_min, timeout_tm.tm_sec) == -1) {
69 return;
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>",
79 (int)value.length);
80 datastr = datastr_free;
81 if (datastr == NULL) {
82 datastr = "<binary>";
86 d_printf(_("Key: %s\t Timeout: %s\t Value: %s %s\n"), keystr,
87 timeout_str, datastr, timeout > now_t ? "": _("(expired)"));
89 SAFE_FREE(alloc_str);
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"),
97 keystr);
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;
111 time_t timeout;
113 /* sign detection */
114 if (timeout_str[0] == '!' || timeout_str[0] == '+') {
115 sign = timeout_str[0];
116 number_begin = 1;
117 } else {
118 number_begin = 0;
121 /* unit detection */
122 len = strlen(timeout_str);
123 switch (timeout_str[len - 1]) {
124 case 's':
125 case 'm':
126 case 'h':
127 case 'd':
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);
139 switch (unit) {
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 :) */
146 switch (sign) {
147 case '!': timeout = time(NULL) - timeout; break;
148 case '+':
149 default: timeout += time(NULL); break;
152 if (number) SAFE_FREE(number);
153 return timeout;
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;
167 time_t timeout;
169 if (argc < 3 || c->display_usage) {
170 d_printf("%s\n%s",
171 _("Usage:"),
172 _("net cache add <key string> <data string> "
173 "<timeout>\n"));
174 return -1;
177 keystr = argv[0];
178 datastr = argv[1];
179 timeout_str = argv[2];
181 /* parse timeout given in command line */
182 timeout = parse_timeout(timeout_str);
183 if (!timeout) {
184 d_fprintf(stderr, _("Invalid timeout argument.\n"));
185 return -1;
188 if (gencache_set(keystr, datastr, timeout)) {
189 d_printf(_("New cache entry stored successfully.\n"));
190 return 0;
193 d_fprintf(stderr, _("Entry couldn't be added. Perhaps there's already such a key.\n"));
194 return -1;
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) {
209 d_printf("%s\n%s",
210 _("Usage:"),
211 _(" net cache del <key string>\n"));
212 return -1;
215 if(gencache_del(keystr)) {
216 d_printf(_("Entry deleted.\n"));
217 return 0;
220 d_fprintf(stderr, _("Couldn't delete specified entry\n"));
221 return -1;
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];
235 DATA_BLOB value;
236 time_t timeout;
238 if (argc < 1 || c->display_usage) {
239 d_printf("%s\n%s",
240 _("Usage:"),
241 _(" net cache get <key>\n"));
242 return -1;
245 if (gencache_get_data_blob(keystr, &value, &timeout, NULL)) {
246 print_cache_entry(keystr, value, timeout, NULL);
247 data_blob_free(&value);
248 return 0;
251 d_fprintf(stderr, _("Failed to find entry\n"));
252 return -1;
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)
265 const char* pattern;
267 if (argc < 1 || c->display_usage) {
268 d_printf("%s\n%s",
269 _("Usage:"),
270 _(" net cache search <pattern>\n"));
271 return -1;
274 pattern = argv[0];
275 gencache_iterate_blobs(print_cache_entry, NULL, pattern);
276 return 0;
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) {
292 d_printf( "%s\n"
293 "net cache list\n"
294 " %s\n",
295 _("Usage:"),
296 _("List all cache entries."));
297 return 0;
299 gencache_iterate_blobs(print_cache_entry, NULL, pattern);
300 return 0;
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) {
315 d_printf( "%s\n"
316 "net cache flush\n"
317 " %s",
318 _("Usage:"),
319 _("Delete all cache entries."));
320 return 0;
322 gencache_iterate(delete_cache_entry, NULL, pattern);
323 return 0;
326 static int net_cache_stabilize(struct net_context *c, int argc,
327 const char **argv)
329 if (c->display_usage) {
330 d_printf( "%s\n"
331 "net cache stabilize\n"
332 " %s\n",
333 _("Usage:"),
334 _("Move transient cache content to stable storage"));
335 return 0;
338 if (!gencache_stabilize()) {
339 return -1;
341 return 0;
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[] = {
354 "add",
355 net_cache_add,
356 NET_TRANSPORT_LOCAL,
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.")
365 "del",
366 net_cache_del,
367 NET_TRANSPORT_LOCAL,
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.")
374 "get",
375 net_cache_get,
376 NET_TRANSPORT_LOCAL,
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.")
384 "search",
385 net_cache_search,
386 NET_TRANSPORT_LOCAL,
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.")
393 "list",
394 net_cache_list,
395 NET_TRANSPORT_LOCAL,
396 N_("List all cache entries"),
397 N_("net cache list\n"
398 " List all cache entries")
401 "flush",
402 net_cache_flush,
403 NET_TRANSPORT_LOCAL,
404 N_("Delete all cache entries"),
405 N_("net cache flush\n"
406 " Delete all cache entries")
409 "stabilize",
410 net_cache_stabilize,
411 NET_TRANSPORT_LOCAL,
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);