Allows zero as a value for retry.
[m6w6/libmemcached] / clients / memrm.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 <cerrno>
15 #include <cstdio>
16 #include <cstring>
17 #include <getopt.h>
18 #include <iostream>
19 #include <unistd.h>
20
21 #include <libmemcached-1.0/memcached.h>
22 #include "client_options.h"
23 #include "utilities.h"
24
25 static int opt_binary= 0;
26 static int opt_verbose= 0;
27 static time_t opt_expire= 0;
28 static char *opt_servers= NULL;
29 static char *opt_hash= NULL;
30 static char *opt_username;
31 static char *opt_passwd;
32
33 #define PROGRAM_NAME "memrm"
34 #define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached cluster."
35
36 /* Prototypes */
37 static void options_parse(int argc, char *argv[]);
38
39 int main(int argc, char *argv[])
40 {
41 options_parse(argc, argv);
42 initialize_sockets();
43
44 if (opt_servers == NULL)
45 {
46 char *temp;
47
48 if ((temp= getenv("MEMCACHED_SERVERS")))
49 {
50 opt_servers= strdup(temp);
51 }
52
53 if (opt_servers == NULL)
54 {
55 std::cerr << "No Servers provided" << std::endl;
56 exit(EXIT_FAILURE);
57 }
58 }
59
60 memcached_server_st* servers= memcached_servers_parse(opt_servers);
61 if (servers == NULL or memcached_server_list_count(servers) == 0)
62 {
63 std::cerr << "Invalid server list provided:" << opt_servers << std::endl;
64 return EXIT_FAILURE;
65 }
66
67 memcached_st* memc= memcached_create(NULL);
68 process_hash_option(memc, opt_hash);
69
70 memcached_server_push(memc, servers);
71 memcached_server_list_free(servers);
72 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
73 (uint64_t) opt_binary);
74
75 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
76 {
77 memcached_free(memc);
78 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
79 return EXIT_FAILURE;
80 }
81
82 if (opt_username)
83 {
84 memcached_return_t ret;
85 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
86 {
87 std::cerr << memcached_last_error_message(memc) << std::endl;
88 memcached_free(memc);
89 return EXIT_FAILURE;
90 }
91 }
92
93 int return_code= EXIT_SUCCESS;
94
95 while (optind < argc)
96 {
97 memcached_return_t rc= memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire);
98
99 if (rc == MEMCACHED_NOTFOUND)
100 {
101 if (opt_verbose)
102 {
103 std::cerr << "Could not find key \"" << argv[optind] << "\"" << std::endl;
104 }
105 }
106 else if (memcached_fatal(rc))
107 {
108 if (opt_verbose)
109 {
110 std::cerr << "Failed to delete key \"" << argv[optind] << "\" :" << memcached_last_error_message(memc) << std::endl;
111 }
112
113 return_code= EXIT_FAILURE;
114 }
115 else // success
116 {
117 if (opt_verbose)
118 {
119 std::cout << "Deleted key " << argv[optind];
120 if (opt_expire)
121 {
122 std::cout << " expires: " << opt_expire << std::endl;
123 }
124 std::cout << std::endl;
125 }
126 }
127
128 optind++;
129 }
130
131 memcached_free(memc);
132
133 if (opt_servers)
134 {
135 free(opt_servers);
136 }
137
138 if (opt_hash)
139 {
140 free(opt_hash);
141 }
142
143 return return_code;
144 }
145
146
147 static void options_parse(int argc, char *argv[])
148 {
149 memcached_programs_help_st help_options[]=
150 {
151 {0},
152 };
153
154 static struct option long_options[]=
155 {
156 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
157 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
158 {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
159 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
160 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
161 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
162 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
163 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
164 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
165 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
166 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
167 {0, 0, 0, 0},
168 };
169
170 bool opt_version= false;
171 bool opt_help= false;
172 int option_index= 0;
173
174 while (1)
175 {
176 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
177 if (option_rv == -1)
178 {
179 break;
180 }
181
182 switch (option_rv)
183 {
184 case 0:
185 break;
186
187 case OPT_BINARY:
188 opt_binary = 1;
189 break;
190
191 case OPT_VERBOSE: /* --verbose or -v */
192 opt_verbose = OPT_VERBOSE;
193 break;
194
195 case OPT_DEBUG: /* --debug or -d */
196 opt_verbose = OPT_DEBUG;
197 break;
198
199 case OPT_VERSION: /* --version or -V */
200 opt_version= true;
201 break;
202
203 case OPT_HELP: /* --help or -h */
204 opt_help= true;
205 break;
206
207 case OPT_SERVERS: /* --servers or -s */
208 opt_servers= strdup(optarg);
209 break;
210
211 case OPT_EXPIRE: /* --expire */
212 errno= 0;
213 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
214 if (errno != 0)
215 {
216 std::cerr << "Incorrect value passed to --expire: `" << optarg << "`" << std::endl;
217 exit(EXIT_FAILURE);
218 }
219 break;
220
221 case OPT_HASH:
222 opt_hash= strdup(optarg);
223 break;
224
225 case OPT_USERNAME:
226 opt_username= optarg;
227 break;
228
229 case OPT_PASSWD:
230 opt_passwd= optarg;
231 break;
232
233 case OPT_QUIET:
234 close_stdio();
235 break;
236
237 case '?':
238 /* getopt_long already printed an error message. */
239 exit(EXIT_SUCCESS);
240
241 default:
242 abort();
243 }
244 }
245
246 if (opt_version)
247 {
248 version_command(PROGRAM_NAME);
249 exit(EXIT_SUCCESS);
250 }
251
252 if (opt_help)
253 {
254 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
255 exit(EXIT_SUCCESS);
256 }
257 }