net: Split out "net afs"
[Samba.git] / source3 / utils / net_cache.c
blob091d8f2e559acbd90da2a8c88b5ce17a1329c6af
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, const char* datastr,
37 const time_t timeout, void* dptr)
39 char *timeout_str;
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);
47 if (!now_tm) {
48 return;
50 memcpy(&timeout_tm, now_tm, sizeof(struct tm));
51 now_tm = localtime(&now_t);
52 if (!now_tm) {
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 asprintf(&alloc_str, "%.2d:%.2d:%.2d", timeout_tm.tm_hour,
68 timeout_tm.tm_min, timeout_tm.tm_sec);
69 if (!alloc_str) {
70 return;
72 timeout_str = alloc_str;
75 d_printf("Key: %s\t Timeout: %s\t Value: %s %s\n", keystr,
76 timeout_str, datastr, timeout > now_t ? "": "(expired)");
78 SAFE_FREE(alloc_str);
81 static void delete_cache_entry(const char* keystr, const char* datastr,
82 const time_t timeout, void* dptr)
84 if (!gencache_del(keystr))
85 d_fprintf(stderr, "Couldn't delete entry! key = %s\n", keystr);
89 /**
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
94 **/
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;
99 time_t timeout;
101 /* sign detection */
102 if (timeout_str[0] == '!' || timeout_str[0] == '+') {
103 sign = timeout_str[0];
104 number_begin = 1;
105 } else {
106 number_begin = 0;
109 /* unit detection */
110 len = strlen(timeout_str);
111 switch (timeout_str[len - 1]) {
112 case 's':
113 case 'm':
114 case 'h':
115 case 'd':
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);
127 switch (unit) {
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 :) */
134 switch (sign) {
135 case '!': timeout = time(NULL) - timeout; break;
136 case '+':
137 default: timeout += time(NULL); break;
140 if (number) SAFE_FREE(number);
141 return timeout;
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;
155 time_t timeout;
157 if (argc < 3) {
158 d_printf("\nUsage: net cache add <key string> <data string> <timeout>\n");
159 return -1;
162 keystr = argv[0];
163 datastr = argv[1];
164 timeout_str = argv[2];
166 /* parse timeout given in command line */
167 timeout = parse_timeout(timeout_str);
168 if (!timeout) {
169 d_fprintf(stderr, "Invalid timeout argument.\n");
170 return -1;
173 if (gencache_set(keystr, datastr, timeout)) {
174 d_printf("New cache entry stored successfully.\n");
175 gencache_shutdown();
176 return 0;
179 d_fprintf(stderr, "Entry couldn't be added. Perhaps there's already such a key.\n");
180 gencache_shutdown();
181 return -1;
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) {
196 d_printf("\nUsage: net cache del <key string>\n");
197 return -1;
200 if(gencache_del(keystr)) {
201 d_printf("Entry deleted.\n");
202 return 0;
205 d_fprintf(stderr, "Couldn't delete specified entry\n");
206 return -1;
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;
221 time_t timeout;
223 if (argc < 1) {
224 d_printf("\nUsage: net cache get <key>\n");
225 return -1;
228 if (gencache_get(keystr, &valuestr, &timeout)) {
229 print_cache_entry(keystr, valuestr, timeout, NULL);
230 return 0;
233 d_fprintf(stderr, "Failed to find entry\n");
234 return -1;
239 * Search an entry/entries in the cache
241 * @param c A net_context structure
242 * @param argv key pattern to match the entries to
243 * @return 0 on success, otherwise failure
245 static int net_cache_search(struct net_context *c, int argc, const char **argv)
247 const char* pattern;
249 if (argc < 1) {
250 d_printf("Usage: net cache search <pattern>\n");
251 return -1;
254 pattern = argv[0];
255 gencache_iterate(print_cache_entry, NULL, pattern);
256 return 0;
261 * List the contents of the cache
263 * @param c A net_context structure
264 * @param argv ignored in this functionailty
265 * @return always returns 0
267 static int net_cache_list(struct net_context *c, int argc, const char **argv)
269 const char* pattern = "*";
270 gencache_iterate(print_cache_entry, NULL, pattern);
271 gencache_shutdown();
272 return 0;
277 * Flush the whole cache
279 * @param c A net_context structure
280 * @param argv ignored in this functionality
281 * @return always returns 0
283 static int net_cache_flush(struct net_context *c, int argc, const char **argv)
285 const char* pattern = "*";
286 gencache_iterate(delete_cache_entry, NULL, pattern);
287 gencache_shutdown();
288 return 0;
293 * Short help
295 * @param c A net_context structure
296 * @param argv ignored in this functionality
297 * @return always returns -1
299 static int net_cache_usage(struct net_context *c, int argc, const char **argv)
301 d_printf(" net cache add \t add add new cache entry\n");
302 d_printf(" net cache del \t delete existing cache entry by key\n");
303 d_printf(" net cache flush \t delete all entries existing in the cache\n");
304 d_printf(" net cache get \t get cache entry by key\n");
305 d_printf(" net cache search \t search for entries in the cache, by given pattern\n");
306 d_printf(" net cache list \t list all cache entries (just like search for \"*\")\n");
307 return -1;
312 * Entry point to 'net cache' subfunctionality
314 * @param c A net_context structure
315 * @param argv arguments passed to further called functions
316 * @return whatever further functions return
318 int net_cache(struct net_context *c, int argc, const char **argv)
320 struct functable func[] = {
321 {"add", net_cache_add},
322 {"del", net_cache_del},
323 {"get", net_cache_get},
324 {"search", net_cache_search},
325 {"list", net_cache_list},
326 {"flush", net_cache_flush},
327 {NULL, NULL}
330 return net_run_function(c, argc, argv, func, net_cache_usage);