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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
26 * @brief This is part of the net tool which is basically command
27 * line wrapper for gencache.c functions (mainly for testing)
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.
37 static void print_cache_entry(const char* keystr
, const char* datastr
,
38 const time_t timeout
, void* dptr
)
41 char *alloc_str
= NULL
;
42 time_t now_t
= time(NULL
);
43 struct tm timeout_tm
, *now_tm
;
44 /* localtime returns statically allocated pointer, so timeout_tm
45 has to be copied somewhere else */
47 now_tm
= localtime(&timeout
);
51 memcpy(&timeout_tm
, now_tm
, sizeof(struct tm
));
52 now_tm
= localtime(&now_t
);
57 /* form up timeout string depending whether it's today's date or not */
58 if (timeout_tm
.tm_year
!= now_tm
->tm_year
||
59 timeout_tm
.tm_mon
!= now_tm
->tm_mon
||
60 timeout_tm
.tm_mday
!= now_tm
->tm_mday
) {
62 timeout_str
= asctime(&timeout_tm
);
66 timeout_str
[strlen(timeout_str
) - 1] = '\0'; /* remove tailing CR */
68 asprintf(&alloc_str
, "%.2d:%.2d:%.2d", timeout_tm
.tm_hour
,
69 timeout_tm
.tm_min
, timeout_tm
.tm_sec
);
73 timeout_str
= alloc_str
;
76 d_printf("Key: %s\t Timeout: %s\t Value: %s %s\n", keystr
,
77 timeout_str
, datastr
, timeout
> now_t
? "": "(expired)");
82 static void delete_cache_entry(const char* keystr
, const char* datastr
,
83 const time_t timeout
, void* dptr
)
85 if (!gencache_del(keystr
))
86 d_fprintf(stderr
, "Couldn't delete entry! key = %s\n", keystr
);
91 * Parse text representation of timeout value
93 * @param timeout_str string containing text representation of the timeout
94 * @return numeric timeout of time_t type
96 static time_t parse_timeout(const char* timeout_str
)
98 char sign
= '\0', *number
= NULL
, unit
= '\0';
99 int len
, number_begin
, number_end
;
103 if (timeout_str
[0] == '!' || timeout_str
[0] == '+') {
104 sign
= timeout_str
[0];
111 len
= strlen(timeout_str
);
112 switch (timeout_str
[len
- 1]) {
117 case 'w': unit
= timeout_str
[len
- 1];
120 /* number detection */
121 len
= (sign
) ? strlen(&timeout_str
[number_begin
]) : len
;
122 number_end
= (unit
) ? len
- 1 : len
;
123 number
= SMB_STRNDUP(&timeout_str
[number_begin
], number_end
);
125 /* calculate actual timeout value */
126 timeout
= (time_t)atoi(number
);
129 case 'm': timeout
*= 60; break;
130 case 'h': timeout
*= 60*60; break;
131 case 'd': timeout
*= 60*60*24; break;
132 case 'w': timeout
*= 60*60*24*7; break; /* that's fair enough, I think :) */
136 case '!': timeout
= time(NULL
) - timeout
; break;
138 default: timeout
+= time(NULL
); break;
141 if (number
) SAFE_FREE(number
);
147 * Add an entry to the cache. If it does exist, then set it.
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(int argc
, const char **argv
)
154 const char *keystr
, *datastr
, *timeout_str
;
158 d_printf("\nUsage: net cache add <key string> <data string> <timeout>\n");
164 timeout_str
= argv
[2];
166 /* parse timeout given in command line */
167 timeout
= parse_timeout(timeout_str
);
169 d_fprintf(stderr
, "Invalid timeout argument.\n");
173 if (gencache_set(keystr
, datastr
, timeout
)) {
174 d_printf("New cache entry stored successfully.\n");
179 d_fprintf(stderr
, "Entry couldn't be added. Perhaps there's already such a key.\n");
185 * Delete an entry in the cache
187 * @param argv key to delete an entry of
188 * @return 0 on success, otherwise failure
190 static int net_cache_del(int argc
, const char **argv
)
192 const char *keystr
= argv
[0];
195 d_printf("\nUsage: net cache del <key string>\n");
199 if(gencache_del(keystr
)) {
200 d_printf("Entry deleted.\n");
204 d_fprintf(stderr
, "Couldn't delete specified entry\n");
210 * Get and display an entry from the cache
212 * @param argv key to search an entry of
213 * @return 0 on success, otherwise failure
215 static int net_cache_get(int argc
, const char **argv
)
217 const char* keystr
= argv
[0];
222 d_printf("\nUsage: net cache get <key>\n");
226 if (gencache_get(keystr
, &valuestr
, &timeout
)) {
227 print_cache_entry(keystr
, valuestr
, timeout
, NULL
);
231 d_fprintf(stderr
, "Failed to find entry\n");
237 * Search an entry/entries in the cache
239 * @param argv key pattern to match the entries to
240 * @return 0 on success, otherwise failure
242 static int net_cache_search(int argc
, const char **argv
)
247 d_printf("Usage: net cache search <pattern>\n");
252 gencache_iterate(print_cache_entry
, NULL
, pattern
);
258 * List the contents of the cache
260 * @param argv ignored in this functionailty
261 * @return always returns 0
263 static int net_cache_list(int argc
, const char **argv
)
265 const char* pattern
= "*";
266 gencache_iterate(print_cache_entry
, NULL
, pattern
);
273 * Flush the whole cache
275 * @param argv ignored in this functionality
276 * @return always returns 0
278 static int net_cache_flush(int argc
, const char **argv
)
280 const char* pattern
= "*";
281 gencache_iterate(delete_cache_entry
, NULL
, pattern
);
290 * @param argv ignored in this functionality
291 * @return always returns -1
293 static int net_cache_usage(int argc
, const char **argv
)
295 d_printf(" net cache add \t add add new cache entry\n");
296 d_printf(" net cache del \t delete existing cache entry by key\n");
297 d_printf(" net cache flush \t delete all entries existing in the cache\n");
298 d_printf(" net cache get \t get cache entry by key\n");
299 d_printf(" net cache search \t search for entries in the cache, by given pattern\n");
300 d_printf(" net cache list \t list all cache entries (just like search for \"*\")\n");
306 * Entry point to 'net cache' subfunctionality
308 * @param argv arguments passed to further called functions
309 * @return whatever further functions return
311 int net_cache(int argc
, const char **argv
)
313 struct functable func
[] = {
314 {"add", net_cache_add
},
315 {"del", net_cache_del
},
316 {"get", net_cache_get
},
317 {"search", net_cache_search
},
318 {"list", net_cache_list
},
319 {"flush", net_cache_flush
},
323 return net_run_function(argc
, argv
, func
, net_cache_usage
);