885aa6c85089708dcabd6058c58ee20f90e1c9da
[m6w6/libmemcached] / clients / memflush.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12 #include "libmemcached/common.h"
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <getopt.h>
17 #include <libmemcached/memcached.h>
18 #include "client_options.h"
19 #include "utilities.h"
20
21 static int opt_binary= 0;
22 static int opt_verbose= 0;
23 static time_t opt_expire= 0;
24 static char *opt_servers= NULL;
25
26 #define PROGRAM_NAME "memflush"
27 #define PROGRAM_DESCRIPTION "Erase all data in a server of memcached servers."
28
29 /* Prototypes */
30 void options_parse(int argc, char *argv[]);
31
32 int main(int argc, char *argv[])
33 {
34 memcached_st *memc;
35 memcached_return_t rc;
36 memcached_server_st *servers;
37
38 options_parse(argc, argv);
39
40 if (!opt_servers)
41 {
42 char *temp;
43
44 if ((temp= getenv("MEMCACHED_SERVERS")))
45 opt_servers= strdup(temp);
46 else
47 {
48 fprintf(stderr, "No Servers provided\n");
49 exit(1);
50 }
51 }
52
53 memc= memcached_create(NULL);
54
55 servers= memcached_servers_parse(opt_servers);
56 memcached_server_push(memc, servers);
57 memcached_server_list_free(servers);
58 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
59 (uint64_t) opt_binary);
60
61 rc = memcached_flush(memc, opt_expire);
62 if (rc != MEMCACHED_SUCCESS)
63 {
64 fprintf(stderr, "memflush: memcache error %s",
65 memcached_strerror(memc, rc));
66 if (memc->cached_errno)
67 fprintf(stderr, " system error %s", strerror(memc->cached_errno));
68 fprintf(stderr, "\n");
69 }
70
71 memcached_free(memc);
72
73 free(opt_servers);
74
75 return 0;
76 }
77
78
79 void options_parse(int argc, char *argv[])
80 {
81 memcached_programs_help_st help_options[]=
82 {
83 {0},
84 };
85
86 static struct option long_options[]=
87 {
88 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
89 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
90 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
91 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
92 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
93 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
94 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
95 {0, 0, 0, 0},
96 };
97 int option_index= 0;
98 int option_rv;
99
100 while (1)
101 {
102 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
103 if (option_rv == -1) break;
104 switch (option_rv)
105 {
106 case 0:
107 break;
108 case OPT_BINARY:
109 opt_binary = 1;
110 break;
111 case OPT_VERBOSE: /* --verbose or -v */
112 opt_verbose = OPT_VERBOSE;
113 break;
114 case OPT_DEBUG: /* --debug or -d */
115 opt_verbose = OPT_DEBUG;
116 break;
117 case OPT_VERSION: /* --version or -V */
118 version_command(PROGRAM_NAME);
119 break;
120 case OPT_HELP: /* --help or -h */
121 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
122 break;
123 case OPT_SERVERS: /* --servers or -s */
124 opt_servers= strdup(optarg);
125 break;
126 case OPT_EXPIRE: /* --expire */
127 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
128 break;
129 case '?':
130 /* getopt_long already printed an error message. */
131 exit(1);
132 default:
133 abort();
134 }
135 }
136 }