utils/net_cache.c

This is part of the net tool which is basically command line wrapper for gencache.c functions (mainly for testing) [詳細]

ソースコードを見る。

関数

static void print_cache_entry (const char *keystr, const char *datastr, const time_t timeout, void *dptr)
static void delete_cache_entry (const char *keystr, const char *datastr, const time_t timeout, void *dptr)
static time_t parse_timeout (const char *timeout_str)
 Parse text representation of timeout value
static int net_cache_add (int argc, const char **argv)
 Add an entry to the cache.
static int net_cache_del (int argc, const char **argv)
 Delete an entry in the cache
static int net_cache_get (int argc, const char **argv)
 Get and display an entry from the cache
static int net_cache_search (int argc, const char **argv)
 Search an entry/entries in the cache
static int net_cache_list (int argc, const char **argv)
 List the contents of the cache
static int net_cache_flush (int argc, const char **argv)
 Flush the whole cache
static int net_cache_usage (int argc, const char **argv)
 Short help
int net_cache (int argc, const char **argv)
 Entry point to 'net cache' subfunctionality


説明

This is part of the net tool which is basically command line wrapper for gencache.c functions (mainly for testing)

net_cache.c で定義されています。


関数

static void print_cache_entry ( const char *  keystr,
const char *  datastr,
const time_t  timeout,
void *  dptr 
) [static]

net_cache.c37 行で定義されています。

参照先 asprintf()d_printf()localtime().

参照元 net_cache_get()net_cache_list()net_cache_search().

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 }

static void delete_cache_entry ( const char *  keystr,
const char *  datastr,
const time_t  timeout,
void *  dptr 
) [static]

net_cache.c82 行で定義されています。

参照先 d_fprintf()gencache_del().

参照元 net_cache_flush().

00084 {
00085         if (!gencache_del(keystr))
00086                 d_fprintf(stderr, "Couldn't delete entry! key = %s\n", keystr);
00087 }

static time_t parse_timeout ( const char *  timeout_str  )  [static]

Parse text representation of timeout value

引数:
timeout_str string containing text representation of the timeout
戻り値:
numeric timeout of time_t type

net_cache.c96 行で定義されています。

参照先 lentimeout.

参照元 net_cache_add().

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 }

static int net_cache_add ( int  argc,
const char **  argv 
) [static]

Add an entry to the cache.

If it does exist, then set it.

引数:
argv key, value and timeout are passed in command line
戻り値:
0 on success, otherwise failure

net_cache.c152 行で定義されています。

参照先 d_fprintf()d_printf()gencache_set()gencache_shutdown()parse_timeout()timeout.

参照元 net_cache().

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 }

static int net_cache_del ( int  argc,
const char **  argv 
) [static]

Delete an entry in the cache

引数:
argv key to delete an entry of
戻り値:
0 on success, otherwise failure

net_cache.c190 行で定義されています。

参照先 d_fprintf()d_printf()gencache_del().

参照元 net_cache().

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 }

static int net_cache_get ( int  argc,
const char **  argv 
) [static]

Get and display an entry from the cache

引数:
argv key to search an entry of
戻り値:
0 on success, otherwise failure

net_cache.c215 行で定義されています。

参照先 d_fprintf()d_printf()gencache_get()print_cache_entry()timeout.

参照元 net_cache().

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 }

static int net_cache_search ( int  argc,
const char **  argv 
) [static]

Search an entry/entries in the cache

引数:
argv key pattern to match the entries to
戻り値:
0 on success, otherwise failure

net_cache.c242 行で定義されています。

参照先 d_printf()gencache_iterate()print_cache_entry().

参照元 net_cache().

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 }

static int net_cache_list ( int  argc,
const char **  argv 
) [static]

List the contents of the cache

引数:
argv ignored in this functionailty
戻り値:
always returns 0

net_cache.c263 行で定義されています。

参照先 gencache_iterate()gencache_shutdown()print_cache_entry().

参照元 net_cache().

00264 {
00265         const char* pattern = "*";
00266         gencache_iterate(print_cache_entry, NULL, pattern);
00267         gencache_shutdown();
00268         return 0;
00269 }

static int net_cache_flush ( int  argc,
const char **  argv 
) [static]

Flush the whole cache

引数:
argv ignored in this functionality
戻り値:
always returns 0

net_cache.c278 行で定義されています。

参照先 delete_cache_entry()gencache_iterate()gencache_shutdown().

参照元 net_cache().

00279 {
00280         const char* pattern = "*";
00281         gencache_iterate(delete_cache_entry, NULL, pattern);
00282         gencache_shutdown();
00283         return 0;
00284 }

static int net_cache_usage ( int  argc,
const char **  argv 
) [static]

Short help

引数:
argv ignored in this functionality
戻り値:
always returns -1

net_cache.c293 行で定義されています。

参照先 d_printf().

参照元 net_cache().

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 }

int net_cache ( int  argc,
const char **  argv 
)

Entry point to 'net cache' subfunctionality

引数:
argv arguments passed to further called functions
戻り値:
whatever further functions return

net_cache.c311 行で定義されています。

参照先 net_cache_add()net_cache_del()net_cache_flush()net_cache_get()net_cache_list()net_cache_search()net_cache_usage()net_run_function().

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:24:33 2009に生成されました。  doxygen 1.4.7