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