Merge lp:~tangent-org/libmemcached/1.0-build/ Build: jenkins-Libmemcached-187
[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 memcached_st *memc= memcached_create(NULL);
66 process_hash_option(memc, opt_hash);
67
68 if (opt_servers == NULL)
69 {
70 char *temp;
71
72 if ((temp= getenv("MEMCACHED_SERVERS")))
73 {
74 opt_servers= strdup(temp);
75 }
76 else
77 {
78 std::cerr << "No Servers provided" << std::endl;
79 exit(EXIT_FAILURE);
80 }
81 }
82
83 memcached_server_st *servers;
84 if (opt_servers)
85 {
86 servers= memcached_servers_parse(opt_servers);
87 }
88 else
89 {
90 servers= memcached_servers_parse(argv[--argc]);
91 }
92
93 memcached_server_push(memc, servers);
94 memcached_server_list_free(servers);
95 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
96 (uint64_t)opt_binary);
97
98 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
99 {
100 memcached_free(memc);
101 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
102 return EXIT_FAILURE;
103 }
104
105 if (opt_username)
106 {
107 memcached_return_t ret;
108 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
109 {
110 std::cerr << memcached_last_error_message(memc) << std::endl;
111 memcached_free(memc);
112 return EXIT_FAILURE;
113 }
114 }
115
116 memcached_return_t rc= memcached_dump(memc, callbacks, NULL, 1);
117
118 int exit_code= EXIT_SUCCESS;
119 if (memcached_failed(rc))
120 {
121 if (opt_verbose)
122 {
123 std::cerr << "Failed to dump keys: " << memcached_last_error_message(memc) << std::endl;
124 }
125 exit_code= EXIT_FAILURE;
126 }
127
128 memcached_free(memc);
129
130 if (opt_servers)
131 {
132 free(opt_servers);
133 }
134 if (opt_hash)
135 {
136 free(opt_hash);
137 }
138
139 return exit_code;
140 }
141
142 static void options_parse(int argc, char *argv[])
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 int option_index= 0;
160 bool opt_version= false;
161 bool opt_help= false;
162 while (1)
163 {
164 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
165
166 if (option_rv == -1) break;
167
168 switch (option_rv)
169 {
170 case 0:
171 break;
172
173 case OPT_BINARY:
174 opt_binary= true;
175 break;
176
177 case OPT_VERBOSE: /* --verbose or -v */
178 opt_verbose= OPT_VERBOSE;
179 break;
180
181 case OPT_DEBUG: /* --debug or -d */
182 opt_verbose= OPT_DEBUG;
183 break;
184
185 case OPT_VERSION: /* --version or -V */
186 opt_verbose= true;
187 break;
188
189 case OPT_HELP: /* --help or -h */
190 opt_help= true;
191 break;
192
193 case OPT_SERVERS: /* --servers or -s */
194 opt_servers= strdup(optarg);
195 break;
196
197 case OPT_HASH:
198 opt_hash= strdup(optarg);
199 break;
200
201 case OPT_USERNAME:
202 opt_username= optarg;
203 break;
204
205 case OPT_PASSWD:
206 opt_passwd= optarg;
207 break;
208
209 case OPT_QUIET:
210 close_stdio();
211 break;
212
213 case '?':
214 /* getopt_long already printed an error message. */
215 exit(1);
216 default:
217 abort();
218 }
219 }
220
221 if (opt_version)
222 {
223 version_command(PROGRAM_NAME);
224 exit(EXIT_SUCCESS);
225 }
226
227 if (opt_help)
228 {
229 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL);
230 exit(EXIT_SUCCESS);
231 }
232 }