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