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