Merge lp:~tangent-org/libmemcached/1.0-build/ Build: jenkins-Libmemcached-187
[m6w6/libmemcached] / clients / memflush.cc
1 /* LibMemcached
2 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
4 * All rights reserved.
5 *
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
8 *
9 * Summary:
10 *
11 */
12 #include "mem_config.h"
13
14 #include <cstdio>
15 #include <cstring>
16 #include <getopt.h>
17 #include <iostream>
18 #include <unistd.h>
19
20 #include <libmemcached-1.0/memcached.h>
21 #include "client_options.h"
22 #include "utilities.h"
23
24 static int opt_binary= 0;
25 static int opt_verbose= 0;
26 static time_t opt_expire= 0;
27 static char *opt_servers= NULL;
28 static char *opt_username;
29 static char *opt_passwd;
30
31 #define PROGRAM_NAME "memflush"
32 #define PROGRAM_DESCRIPTION "Erase all data in a server of memcached servers."
33
34 /* Prototypes */
35 void options_parse(int argc, char *argv[]);
36
37 int main(int argc, char *argv[])
38 {
39 options_parse(argc, argv);
40
41 if (opt_servers == false)
42 {
43 char *temp;
44
45 if ((temp= getenv("MEMCACHED_SERVERS")))
46 {
47 opt_servers= strdup(temp);
48 }
49 else
50 {
51 std::cerr << "No Servers provided" << std::endl;
52 exit(EXIT_FAILURE);
53 }
54 }
55
56 memcached_st *memc= memcached_create(NULL);
57
58 memcached_server_st *servers= memcached_servers_parse(opt_servers);
59 memcached_server_push(memc, servers);
60 memcached_server_list_free(servers);
61 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
62 (uint64_t) opt_binary);
63
64 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
65 {
66 memcached_free(memc);
67 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
68 return EXIT_FAILURE;
69 }
70
71 if (opt_username)
72 {
73 memcached_return_t ret;
74 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
75 {
76 std::cerr << memcached_last_error_message(memc) << std::endl;
77 memcached_free(memc);
78 return EXIT_FAILURE;
79 }
80 }
81
82 memcached_return_t rc = memcached_flush(memc, opt_expire);
83 if (rc != MEMCACHED_SUCCESS)
84 {
85 std::cerr << memcached_last_error_message(memc) << std::endl;
86 }
87
88 memcached_free(memc);
89
90 free(opt_servers);
91
92 return EXIT_SUCCESS;
93 }
94
95
96 void options_parse(int argc, char *argv[])
97 {
98 static struct option long_options[]=
99 {
100 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
101 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
102 {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
103 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
104 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
105 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
106 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
107 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
108 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
109 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
110 {0, 0, 0, 0},
111 };
112
113 bool opt_version= false;
114 bool opt_help= false;
115 int option_index= 0;
116 while (1)
117 {
118 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
119 if (option_rv == -1) break;
120 switch (option_rv)
121 {
122 case 0:
123 break;
124
125 case OPT_BINARY:
126 opt_binary= true;
127 break;
128
129 case OPT_VERBOSE: /* --verbose or -v */
130 opt_verbose= OPT_VERBOSE;
131 break;
132
133 case OPT_DEBUG: /* --debug or -d */
134 opt_verbose= OPT_DEBUG;
135 break;
136
137 case OPT_VERSION: /* --version or -V */
138 opt_version= true;
139 break;
140
141 case OPT_HELP: /* --help or -h */
142 opt_help= true;
143 break;
144
145 case OPT_SERVERS: /* --servers or -s */
146 opt_servers= strdup(optarg);
147 break;
148
149 case OPT_EXPIRE: /* --expire */
150 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
151 break;
152
153 case OPT_USERNAME:
154 opt_username= optarg;
155 break;
156
157 case OPT_PASSWD:
158 opt_passwd= optarg;
159 break;
160
161 case OPT_QUIET:
162 close_stdio();
163 break;
164
165 case '?':
166 /* getopt_long already printed an error message. */
167 exit(EXIT_FAILURE);
168
169 default:
170 abort();
171 }
172 }
173
174 if (opt_version)
175 {
176 version_command(PROGRAM_NAME);
177 exit(EXIT_SUCCESS);
178 }
179
180 if (opt_help)
181 {
182 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL);
183 exit(EXIT_SUCCESS);
184 }
185 }