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