utils/net_cache.c

説明を見る。
00001 /* 
00002    Samba Unix/Linux SMB client library 
00003    Distributed SMB/CIFS Server Management Utility 
00004    Copyright (C) Rafal Szczesniak    2002
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015    
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
00019  
00020 
00021 #include "includes.h"
00022 #include "net.h"
00023 
00024 /**
00025  * @file net_cache.c
00026  * @brief This is part of the net tool which is basically command
00027  *        line wrapper for gencache.c functions (mainly for testing)
00028  *
00029  **/
00030 
00031 
00032 /*
00033  * These routines are used via gencache_iterate() to display the cache's contents
00034  * (print_cache_entry) and to flush it (delete_cache_entry).
00035  * Both of them are defined by first arg of gencache_iterate() routine.
00036  */
00037 static void print_cache_entry(const char* keystr, const char* datastr,
00038                               const time_t timeout, void* dptr)
00039 {
00040         char *timeout_str;
00041         char *alloc_str = NULL;
00042         time_t now_t = time(NULL);
00043         struct tm timeout_tm, *now_tm;
00044         /* localtime returns statically allocated pointer, so timeout_tm
00045            has to be copied somewhere else */
00046 
00047         now_tm = localtime(&timeout);
00048         if (!now_tm) {
00049                 return;
00050         }
00051         memcpy(&timeout_tm, now_tm, sizeof(struct tm));
00052         now_tm = localtime(&now_t);
00053         if (!now_tm) {
00054                 return;
00055         }
00056 
00057         /* form up timeout string depending whether it's today's date or not */
00058         if (timeout_tm.tm_year != now_tm->tm_year ||
00059                         timeout_tm.tm_mon != now_tm->tm_mon ||
00060                         timeout_tm.tm_mday != now_tm->tm_mday) {
00061             
00062                 timeout_str = asctime(&timeout_tm);
00063                 if (!timeout_str) {
00064                         return;
00065                 }       
00066                 timeout_str[strlen(timeout_str) - 1] = '\0';    /* remove tailing CR */
00067         } else {
00068                 asprintf(&alloc_str, "%.2d:%.2d:%.2d", timeout_tm.tm_hour,
00069                          timeout_tm.tm_min, timeout_tm.tm_sec);
00070                 if (!alloc_str) {
00071                         return;
00072                 }
00073                 timeout_str = alloc_str;
00074         }
00075         
00076         d_printf("Key: %s\t Timeout: %s\t Value: %s  %s\n", keystr,
00077                  timeout_str, datastr, timeout > now_t ? "": "(expired)");
00078 
00079         SAFE_FREE(alloc_str);
00080 }
00081 
00082 static void delete_cache_entry(const char* keystr, const char* datastr,
00083                                const time_t timeout, void* dptr)
00084 {
00085         if (!gencache_del(keystr))
00086                 d_fprintf(stderr, "Couldn't delete entry! key = %s\n", keystr);
00087 }
00088 
00089 
00090 /**
00091  * Parse text representation of timeout value
00092  *
00093  * @param timeout_str string containing text representation of the timeout
00094  * @return numeric timeout of time_t type
00095  **/
00096 static time_t parse_timeout(const char* timeout_str)
00097 {
00098         char sign = '\0', *number = NULL, unit = '\0';
00099         int len, number_begin, number_end;
00100         time_t timeout;
00101 
00102         /* sign detection */
00103         if (timeout_str[0] == '!' || timeout_str[0] == '+') {
00104                 sign = timeout_str[0];
00105                 number_begin = 1;
00106         } else {
00107                 number_begin = 0;
00108         }
00109         
00110         /* unit detection */
00111         len = strlen(timeout_str);
00112         switch (timeout_str[len - 1]) {
00113         case 's':
00114         case 'm':
00115         case 'h':
00116         case 'd':
00117         case 'w': unit = timeout_str[len - 1];
00118         }
00119         
00120         /* number detection */
00121         len = (sign) ? strlen(&timeout_str[number_begin]) : len;
00122         number_end = (unit) ? len - 1 : len;
00123         number = SMB_STRNDUP(&timeout_str[number_begin], number_end);
00124         
00125         /* calculate actual timeout value */
00126         timeout = (time_t)atoi(number);
00127 
00128         switch (unit) {
00129         case 'm': timeout *= 60; break;
00130         case 'h': timeout *= 60*60; break;
00131         case 'd': timeout *= 60*60*24; break;
00132         case 'w': timeout *= 60*60*24*7; break;  /* that's fair enough, I think :) */
00133         }
00134         
00135         switch (sign) {
00136         case '!': timeout = time(NULL) - timeout; break;
00137         case '+':
00138         default:  timeout += time(NULL); break;
00139         }
00140         
00141         if (number) SAFE_FREE(number);
00142         return timeout; 
00143 }
00144 
00145 
00146 /**
00147  * Add an entry to the cache. If it does exist, then set it.
00148  * 
00149  * @param argv key, value and timeout are passed in command line
00150  * @return 0 on success, otherwise failure
00151  **/
00152 static int net_cache_add(int argc, const char **argv)
00153 {
00154         const char *keystr, *datastr, *timeout_str;
00155         time_t timeout;
00156         
00157         if (argc < 3) {
00158                 d_printf("\nUsage: net cache add <key string> <data string> <timeout>\n");
00159                 return -1;
00160         }
00161         
00162         keystr = argv[0];
00163         datastr = argv[1];
00164         timeout_str = argv[2];
00165         
00166         /* parse timeout given in command line */
00167         timeout = parse_timeout(timeout_str);
00168         if (!timeout) {
00169                 d_fprintf(stderr, "Invalid timeout argument.\n");
00170                 return -1;
00171         }
00172         
00173         if (gencache_set(keystr, datastr, timeout)) {
00174                 d_printf("New cache entry stored successfully.\n");
00175                 gencache_shutdown();
00176                 return 0;
00177         }
00178         
00179         d_fprintf(stderr, "Entry couldn't be added. Perhaps there's already such a key.\n");
00180         gencache_shutdown();
00181         return -1;
00182 }
00183 
00184 /**
00185  * Delete an entry in the cache
00186  * 
00187  * @param argv key to delete an entry of
00188  * @return 0 on success, otherwise failure
00189  **/
00190 static int net_cache_del(int argc, const char **argv)
00191 {
00192         const char *keystr = argv[0];
00193         
00194         if (argc < 1) {
00195                 d_printf("\nUsage: net cache del <key string>\n");
00196                 return -1;
00197         }
00198         
00199         if(gencache_del(keystr)) {
00200                 d_printf("Entry deleted.\n");
00201                 return 0;
00202         }
00203 
00204         d_fprintf(stderr, "Couldn't delete specified entry\n");
00205         return -1;
00206 }
00207 
00208 
00209 /**
00210  * Get and display an entry from the cache
00211  * 
00212  * @param argv key to search an entry of
00213  * @return 0 on success, otherwise failure
00214  **/
00215 static int net_cache_get(int argc, const char **argv)
00216 {
00217         const char* keystr = argv[0];
00218         char* valuestr;
00219         time_t timeout;
00220 
00221         if (argc < 1) {
00222                 d_printf("\nUsage: net cache get <key>\n");
00223                 return -1;
00224         }
00225         
00226         if (gencache_get(keystr, &valuestr, &timeout)) {
00227                 print_cache_entry(keystr, valuestr, timeout, NULL);
00228                 return 0;
00229         }
00230 
00231         d_fprintf(stderr, "Failed to find entry\n");
00232         return -1;
00233 }
00234 
00235 
00236 /**
00237  * Search an entry/entries in the cache
00238  * 
00239  * @param argv key pattern to match the entries to
00240  * @return 0 on success, otherwise failure
00241  **/
00242 static int net_cache_search(int argc, const char **argv)
00243 {
00244         const char* pattern;
00245         
00246         if (argc < 1) {
00247                 d_printf("Usage: net cache search <pattern>\n");
00248                 return -1;
00249         }
00250         
00251         pattern = argv[0];
00252         gencache_iterate(print_cache_entry, NULL, pattern);
00253         return 0;
00254 }
00255 
00256 
00257 /**
00258  * List the contents of the cache
00259  * 
00260  * @param argv ignored in this functionailty
00261  * @return always returns 0
00262  **/
00263 static int net_cache_list(int argc, const char **argv)
00264 {
00265         const char* pattern = "*";
00266         gencache_iterate(print_cache_entry, NULL, pattern);
00267         gencache_shutdown();
00268         return 0;
00269 }
00270 
00271 
00272 /**
00273  * Flush the whole cache
00274  * 
00275  * @param argv ignored in this functionality
00276  * @return always returns 0
00277  **/
00278 static int net_cache_flush(int argc, const char **argv)
00279 {
00280         const char* pattern = "*";
00281         gencache_iterate(delete_cache_entry, NULL, pattern);
00282         gencache_shutdown();
00283         return 0;
00284 }
00285 
00286 
00287 /**
00288  * Short help
00289  *
00290  * @param argv ignored in this functionality
00291  * @return always returns -1
00292  **/
00293 static int net_cache_usage(int argc, const char **argv)
00294 {
00295         d_printf("  net cache add \t add add new cache entry\n");
00296         d_printf("  net cache del \t delete existing cache entry by key\n");
00297         d_printf("  net cache flush \t delete all entries existing in the cache\n");
00298         d_printf("  net cache get \t get cache entry by key\n");
00299         d_printf("  net cache search \t search for entries in the cache, by given pattern\n");
00300         d_printf("  net cache list \t list all cache entries (just like search for \"*\")\n");
00301         return -1;
00302 }
00303 
00304 
00305 /**
00306  * Entry point to 'net cache' subfunctionality
00307  *
00308  * @param argv arguments passed to further called functions
00309  * @return whatever further functions return
00310  **/
00311 int net_cache(int argc, const char **argv)
00312 {
00313         struct functable func[] = {
00314                 {"add", net_cache_add},
00315                 {"del", net_cache_del},
00316                 {"get", net_cache_get},
00317                 {"search", net_cache_search},
00318                 {"list", net_cache_list},
00319                 {"flush", net_cache_flush},
00320                 {NULL, NULL}
00321         };
00322 
00323         return net_run_function(argc, argv, func, net_cache_usage);
00324 }

Sambaに対してSat Aug 29 21:23:28 2009に生成されました。  doxygen 1.4.7