Update for mavericks
[m6w6/libmemcached] / clients / memexist.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 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 "memexist"
32 #define PROGRAM_DESCRIPTION "Check for the existance of a key within a cluster."
33
34 /* Prototypes */
35 static void options_parse(int argc, char *argv[]);
36
37 int main(int argc, char *argv[])
38 {
39 options_parse(argc, argv);
40 initialize_sockets();
41
42 if (opt_servers == NULL)
43 {
44 char *temp;
45
46 if ((temp= getenv("MEMCACHED_SERVERS")))
47 {
48 opt_servers= strdup(temp);
49 }
50
51 if (opt_servers == NULL)
52 {
53 std::cerr << "No Servers provided" << std::endl;
54 exit(EXIT_FAILURE);
55 }
56 }
57
58 memcached_server_st* servers= memcached_servers_parse(opt_servers);
59 if (servers == NULL or memcached_server_list_count(servers) == 0)
60 {
61 std::cerr << "Invalid server list provided:" << opt_servers << std::endl;
62 return EXIT_FAILURE;
63 }
64
65 memcached_st* memc= memcached_create(NULL);
66 process_hash_option(memc, opt_hash);
67
68 memcached_server_push(memc, servers);
69 memcached_server_list_free(servers);
70 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
71 (uint64_t) opt_binary);
72
73 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
74 {
75 memcached_free(memc);
76 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
77 return EXIT_FAILURE;
78 }
79
80 if (opt_username)
81 {
82 memcached_return_t ret;
83 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
84 {
85 std::cerr << memcached_last_error_message(memc) << std::endl;
86 memcached_free(memc);
87 return EXIT_FAILURE;
88 }
89 }
90
91 int return_code= EXIT_SUCCESS;
92
93 while (optind < argc)
94 {
95 memcached_return_t rc= memcached_exist(memc, argv[optind], strlen(argv[optind]));
96
97 if (rc == MEMCACHED_NOTFOUND)
98 {
99 if (opt_verbose)
100 {
101 std::cout << "Could not find key \"" << argv[optind] << "\"" << std::endl;
102 }
103
104 return_code= EXIT_FAILURE;
105 }
106 else if (memcached_failed(rc))
107 {
108 if (opt_verbose)
109 {
110 std::cerr << "Fatal error for 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 << "Found key " << argv[optind] << 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)"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_HASH:
206 opt_hash= strdup(optarg);
207 break;
208
209 case OPT_USERNAME:
210 opt_username= optarg;
211 break;
212
213 case OPT_PASSWD:
214 opt_passwd= optarg;
215 break;
216
217 case OPT_QUIET:
218 close_stdio();
219 break;
220
221 case '?':
222 /* getopt_long already printed an error message. */
223 exit(EXIT_SUCCESS);
224
225 default:
226 abort();
227 }
228 }
229
230 if (opt_version)
231 {
232 version_command(PROGRAM_NAME);
233 exit(EXIT_SUCCESS);
234 }
235
236 if (opt_help)
237 {
238 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
239 exit(EXIT_SUCCESS);
240 }
241 }