source: vendor/current/source3/utils/net_cache.c

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

File size: 10.0 KB
Line 
1/*
2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) Rafal Szczesniak 2002
5
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.
10
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.
15
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/>.
18*/
19
20#include "includes.h"
21#include "net.h"
22
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)
27 *
28 **/
29
30
31/*
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.
35 */
36static void print_cache_entry(const char* keystr, DATA_BLOB value,
37 const time_t timeout, void* dptr)
38{
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;
46
47 ptimeout_tm = localtime_r(&timeout, &timeout_tm);
48 if (ptimeout_tm == NULL) {
49 return;
50 }
51 pnow_tm = localtime_r(&now_t, &now_tm);
52 if (pnow_tm == NULL) {
53 return;
54 }
55
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) {
60
61 timeout_str = asctime(&timeout_tm);
62 if (!timeout_str) {
63 return;
64 }
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;
70 }
71 timeout_str = alloc_str;
72 }
73
74 datastr = (char *)value.data;
75
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>";
83 }
84 }
85
86 d_printf(_("Key: %s\t Timeout: %s\t Value: %s %s\n"), keystr,
87 timeout_str, datastr, timeout > now_t ? "": _("(expired)"));
88
89 SAFE_FREE(alloc_str);
90}
91
92static void delete_cache_entry(const char* keystr, const char* datastr,
93 const time_t timeout, void* dptr)
94{
95 if (!gencache_del(keystr))
96 d_fprintf(stderr, _("Couldn't delete entry! key = %s\n"),
97 keystr);
98}
99
100
101/**
102 * Parse text representation of timeout value
103 *
104 * @param timeout_str string containing text representation of the timeout
105 * @return numeric timeout of time_t type
106 **/
107static time_t parse_timeout(const char* timeout_str)
108{
109 char sign = '\0', *number = NULL, unit = '\0';
110 int len, number_begin, number_end;
111 time_t timeout;
112
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;
119 }
120
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];
129 }
130
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);
135
136 /* calculate actual timeout value */
137 timeout = (time_t)atoi(number);
138
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 :) */
144 }
145
146 switch (sign) {
147 case '!': timeout = time(NULL) - timeout; break;
148 case '+':
149 default: timeout += time(NULL); break;
150 }
151
152 if (number) SAFE_FREE(number);
153 return timeout;
154}
155
156
157/**
158 * Add an entry to the cache. If it does exist, then set it.
159 *
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
163 **/
164static int net_cache_add(struct net_context *c, int argc, const char **argv)
165{
166 const char *keystr, *datastr, *timeout_str;
167 time_t timeout;
168
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;
175 }
176
177 keystr = argv[0];
178 datastr = argv[1];
179 timeout_str = argv[2];
180
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;
186 }
187
188 if (gencache_set(keystr, datastr, timeout)) {
189 d_printf(_("New cache entry stored successfully.\n"));
190 return 0;
191 }
192
193 d_fprintf(stderr, _("Entry couldn't be added. Perhaps there's already such a key.\n"));
194 return -1;
195}
196
197/**
198 * Delete an entry in the cache
199 *
200 * @param c A net_context structure
201 * @param argv key to delete an entry of
202 * @return 0 on success, otherwise failure
203 **/
204static int net_cache_del(struct net_context *c, int argc, const char **argv)
205{
206 const char *keystr = argv[0];
207
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;
213 }
214
215 if(gencache_del(keystr)) {
216 d_printf(_("Entry deleted.\n"));
217 return 0;
218 }
219
220 d_fprintf(stderr, _("Couldn't delete specified entry\n"));
221 return -1;
222}
223
224
225/**
226 * Get and display an entry from the cache
227 *
228 * @param c A net_context structure
229 * @param argv key to search an entry of
230 * @return 0 on success, otherwise failure
231 **/
232static int net_cache_get(struct net_context *c, int argc, const char **argv)
233{
234 const char* keystr = argv[0];
235 DATA_BLOB value;
236 time_t timeout;
237
238 if (argc < 1 || c->display_usage) {
239 d_printf("%s\n%s",
240 _("Usage:"),
241 _(" net cache get <key>\n"));
242 return -1;
243 }
244
245 if (gencache_get_data_blob(keystr, NULL, &value, &timeout, NULL)) {
246 print_cache_entry(keystr, value, timeout, NULL);
247 data_blob_free(&value);
248 return 0;
249 }
250
251 d_fprintf(stderr, _("Failed to find entry\n"));
252 return -1;
253}
254
255
256/**
257 * Search an entry/entries in the cache
258 *
259 * @param c A net_context structure
260 * @param argv key pattern to match the entries to
261 * @return 0 on success, otherwise failure
262 **/
263static int net_cache_search(struct net_context *c, int argc, const char **argv)
264{
265 const char* pattern;
266
267 if (argc < 1 || c->display_usage) {
268 d_printf("%s\n%s",
269 _("Usage:"),
270 _(" net cache search <pattern>\n"));
271 return -1;
272 }
273
274 pattern = argv[0];
275 gencache_iterate_blobs(print_cache_entry, NULL, pattern);
276 return 0;
277}
278
279
280/**
281 * List the contents of the cache
282 *
283 * @param c A net_context structure
284 * @param argv ignored in this functionailty
285 * @return always returns 0
286 **/
287static int net_cache_list(struct net_context *c, int argc, const char **argv)
288{
289 const char* pattern = "*";
290
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;
298 }
299 gencache_iterate_blobs(print_cache_entry, NULL, pattern);
300 return 0;
301}
302
303
304/**
305 * Flush the whole cache
306 *
307 * @param c A net_context structure
308 * @param argv ignored in this functionality
309 * @return always returns 0
310 **/
311static int net_cache_flush(struct net_context *c, int argc, const char **argv)
312{
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;
321 }
322 gencache_iterate(delete_cache_entry, NULL, pattern);
323 return 0;
324}
325
326static int net_cache_stabilize(struct net_context *c, int argc,
327 const char **argv)
328{
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;
336 }
337
338 if (!gencache_stabilize()) {
339 return -1;
340 }
341 return 0;
342}
343/**
344 * Entry point to 'net cache' subfunctionality
345 *
346 * @param c A net_context structure
347 * @param argv arguments passed to further called functions
348 * @return whatever further functions return
349 **/
350int net_cache(struct net_context *c, int argc, const char **argv)
351{
352 struct functable func[] = {
353 {
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.")
363 },
364 {
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.")
372 },
373 {
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.")
381
382 },
383 {
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.")
391 },
392 {
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")
399 },
400 {
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")
407 },
408 {
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")
415 },
416 {NULL, NULL, 0, NULL, NULL}
417 };
418
419 return net_run_function(c, argc, argv, "net cache", func);
420}
Note: See TracBrowser for help on using the repository browser.