added gencache implementation from mimir - thanks!
[nivanova/samba-autobuild/.git] / source3 / utils / net_cache.c
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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19  
20
21 #include "includes.h"
22 #include "net.h"
23
24 /**
25  * @file net_cache.c
26  * @brief This is part of the net tool which is basically command
27  *        line wrapper for gencache.c functions (mainly for testing)
28  *
29  **/
30
31
32 /*
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.
36  */
37 static void print_cache_entry(const char* keystr, const char* datastr, const time_t timeout)
38 {
39         char* timeout_str = ctime(&timeout);
40         timeout_str[strlen(timeout_str) - 1] = '\0';
41         d_printf("Key: %s\t\t Value: %s\t\t Timeout: %s %s\n", keystr, datastr,
42                  timeout_str, timeout > time(NULL) ? "": "(expired)");
43 }
44
45 static void delete_cache_entry(const char* keystr, const char* datastr, const time_t timeout)
46 {
47         if (!gencache_del(keystr))
48                 d_printf("Couldn't delete entry! key = %s", keystr);
49 }
50
51
52 /**
53  * Parse text representation of timeout value
54  *
55  * @param timeout_str string containing text representation of the timeout
56  * @return numeric timeout of time_t type
57  **/
58 static time_t parse_timeout(const char* timeout_str)
59 {
60         char sign = '\0', *number = NULL, unit = '\0';
61         int len, number_begin, number_end;
62         time_t timeout;
63
64         /* sign detection */
65         if (timeout_str[0] == '!' || timeout_str[0] == '+') {
66                 sign = timeout_str[0];
67                 number_begin = 1;
68         } else {
69                 number_begin = 0;
70         }
71         
72         /* unit detection */
73         len = strlen(timeout_str);
74         switch (timeout_str[len - 1]) {
75         case 's':
76         case 'm':
77         case 'h':
78         case 'd':
79         case 'w': unit = timeout_str[len - 1];
80         }
81         
82         /* number detection */
83         len = (sign) ? strlen(&timeout_str[number_begin]) : len;
84         number_end = (unit) ? len - 1 : len;
85         number = strndup(&timeout_str[number_begin], number_end);
86         
87         /* calculate actual timeout value */
88         timeout = (time_t)atoi(number);
89
90         switch (unit) {
91         case 'm': timeout *= 60; break;
92         case 'h': timeout *= 60*60; break;
93         case 'd': timeout *= 60*60*24; break;
94         case 'w': timeout *= 60*60*24*7; break;  /* that's fair enough, I think :) */
95         }
96         
97         switch (sign) {
98         case '!': timeout = time(NULL) - timeout; break;
99         case '+':
100         default:  timeout += time(NULL); break;
101         }
102         
103         if (number) SAFE_FREE(number);
104         return timeout; 
105 }
106
107
108 /**
109  * Add an entry to the cache
110  * 
111  * @param argv key, value and timeout are passed in command line
112  * @return 0 on success, otherwise failure
113  **/
114 static int net_cache_add(int argc, const char **argv)
115 {
116         const char *keystr, *datastr, *timeout_str;
117         time_t timeout;
118         
119         if (argc < 3) {
120                 d_printf("\nUsage: net cache add <key string> <data string> <timeout>\n");
121                 return -1;
122         }
123         
124         keystr = argv[0];
125         datastr = argv[1];
126         timeout_str = argv[2];
127         
128         /* parse timeout given in command line */
129         timeout = parse_timeout(timeout_str);
130         if (!timeout) {
131                 d_printf("Invalid timeout argument.\n");
132                 return -1;
133         }
134         
135         if (gencache_add(keystr, datastr, timeout)) {
136                 d_printf("New cache entry stored successfully.\n");
137                 gencache_shutdown();
138                 return 0;
139         } 
140
141         d_printf("Entry couldn't be added. Perhaps there's already such a key.\n");
142         gencache_shutdown();
143         return -1;
144 }
145
146
147 /**
148  * Set new value of an existing entry in the cache
149  * 
150  * @param argv key being searched and new value and timeout to set in the entry
151  * @return 0 on success, otherwise failure
152  **/
153 static int net_cache_set(int argc, const char **argv)
154 {
155         const char *keystr, *datastr, *timeout_str;
156         time_t timeout;
157         
158         if (argc < 3) {
159                 d_printf("\nUsage: net cache set <key string> <data string> <timeout>\n");
160                 return -1;
161         }
162         
163         keystr = argv[0];
164         datastr = argv[1];
165         timeout_str = argv[2];
166         
167         /* parse timeout given in command line */
168         timeout = parse_timeout(timeout_str);
169         if (!timeout) {
170                 d_printf("Invalid timeout argument.\n");
171                 return -1;
172         }
173         
174         if (gencache_set(keystr, datastr, timeout)) {
175                 d_printf("Cache entry set successfully.\n");
176                 gencache_shutdown();
177                 return 0;
178         }
179
180         d_printf("Entry couldn't be set. Perhaps there's no such a key.\n");
181         gencache_shutdown();
182         return -1;
183 }
184
185
186 /**
187  * Delete an entry in the cache
188  * 
189  * @param argv key to delete an entry of
190  * @return 0 on success, otherwise failure
191  **/
192 static int net_cache_del(int argc, const char **argv)
193 {
194         const char *keystr = argv[0];
195         
196         if (argc < 1) {
197                 d_printf("\nUsage: net cache add <key string>\n");
198                 return -1;
199         }
200         
201         if(gencache_del(keystr)) {
202                 d_printf("Entry deleted.\n");
203                 return 0;
204         } 
205
206         d_printf("Couldn't delete specified entry\n");
207         return -1;
208 }
209
210
211 /**
212  * Get and display an entry from the cache
213  * 
214  * @param argv key to search an entry of
215  * @return 0 on success, otherwise failure
216  **/
217 static int net_cache_get(int argc, const char **argv)
218 {
219         const char* keystr = argv[0];
220         char* valuestr;
221         time_t timeout;
222
223         if (argc < 1) {
224                 d_printf("\nUsage: net cache get <key>\n");
225                 return -1;
226         }
227         
228         if (gencache_get(keystr, &valuestr, &timeout)) {
229                 print_cache_entry(keystr, valuestr, timeout);
230                 return 0;
231         } 
232
233         d_printf("Failed to find entry\n");
234         return -1;
235 }
236
237
238 /**
239  * Search an entry/entries in the cache
240  * 
241  * @param argv key pattern to match the entries to
242  * @return 0 on success, otherwise failure
243  **/
244 static int net_cache_search(int argc, const char **argv)
245 {
246         const char* pattern;
247         
248         if (argc < 1) {
249                 d_printf("Usage: net cache search <pattern>\n");
250                 return -1;
251         }
252         
253         pattern = argv[0];
254         gencache_iterate(print_cache_entry, pattern);
255         return 0;
256 }
257
258
259 /**
260  * List the contents of the cache
261  * 
262  * @param argv ignored in this functionailty
263  * @return always returns 0
264  **/
265 static int net_cache_list(int argc, const char **argv)
266 {
267         const char* pattern = "*";
268         gencache_iterate(print_cache_entry, pattern);
269         gencache_shutdown();
270         return 0;
271 }
272
273
274 /**
275  * Flush the whole cache
276  * 
277  * @param argv ignored in this functionality
278  * @return always returns 0
279  **/
280 static int net_cache_flush(int argc, const char **argv)
281 {
282         const char* pattern = "*";
283         gencache_iterate(delete_cache_entry, pattern);
284         gencache_shutdown();
285         return 0;
286 }
287
288
289 /**
290  * Short help
291  *
292  * @param argv ignored in this functionality
293  * @return always returns -1
294  **/
295 static int net_cache_usage(int argc, const char **argv)
296 {
297         d_printf("  net cache add \t add add new cache entry\n");
298         d_printf("  net cache set \t set new value for existing cache entry\n");
299         d_printf("  net cache del \t delete existing cache entry by key\n");
300         d_printf("  net cache flush \t delete all entries existing in the cache\n");
301         d_printf("  net cache get \t get cache entry by key\n");
302         d_printf("  net cache search \t search for entries in the cache, by given pattern\n");
303         d_printf("  net cache list \t list all cache entries (just like search for \"*\")\n");
304         return -1;
305 }
306
307
308 /**
309  * Entry point to 'net cache' subfunctionality
310  *
311  * @param argv arguments passed to further called functions
312  * @return whatever further functions return
313  **/
314 int net_cache(int argc, const char **argv)
315 {
316         struct functable func[] = {
317                 {"add", net_cache_add},
318                 {"set", net_cache_set},
319                 {"del", net_cache_del},
320                 {"get", net_cache_get},
321                 {"search", net_cache_search},
322                 {"list", net_cache_list},
323                 {"flush", net_cache_flush},
324                 {NULL, NULL}
325         };
326
327         return net_run_function(argc, argv, func, net_cache_usage);
328 }