Merge in updates for sasl.
[awesomized/libmemcached] / clients / memdump.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
12 #include "config.h"
13
14 #include <cerrno>
15 #include <cstdio>
16 #include <cstdlib>
17 #include <cstring>
18 #include <fcntl.h>
19 #include <getopt.h>
20 #include <inttypes.h>
21 #include <iostream>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26
27 #include <libmemcached/memcached.h>
28
29 #include "client_options.h"
30 #include "utilities.h"
31
32 #define PROGRAM_NAME "memdump"
33 #define PROGRAM_DESCRIPTION "Dump all values from one or many servers."
34
35 /* Prototypes */
36 static void options_parse(int argc, char *argv[]);
37
38 static int opt_binary=0;
39 static int opt_verbose= 0;
40 static char *opt_servers= NULL;
41 static char *opt_hash= NULL;
42 static char *opt_username;
43 static char *opt_passwd;
44
45 /* Print the keys and counter how many were found */
46 static memcached_return_t key_printer(const memcached_st *ptr,
47 const char *key, size_t key_length,
48 void *context)
49 {
50 (void)ptr;(void)context;
51 printf("%.*s\n", (uint32_t)key_length, key);
52
53 return MEMCACHED_SUCCESS;
54 }
55
56 int main(int argc, char *argv[])
57 {
58 memcached_st *memc;
59 memcached_return_t rc;
60 memcached_server_st *servers;
61 memcached_dump_fn callbacks[1];
62
63 callbacks[0]= &key_printer;
64
65 options_parse(argc, argv);
66
67 memc= memcached_create(NULL);
68 process_hash_option(memc, opt_hash);
69
70 if (!opt_servers)
71 {
72 char *temp;
73
74 if ((temp= getenv("MEMCACHED_SERVERS")))
75 opt_servers= strdup(temp);
76 else
77 {
78 fprintf(stderr, "No Servers provided\n");
79 exit(1);
80 }
81 }
82
83 if (opt_servers)
84 servers= memcached_servers_parse(opt_servers);
85 else
86 servers= memcached_servers_parse(argv[--argc]);
87
88 memcached_server_push(memc, servers);
89 memcached_server_list_free(servers);
90 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
91 (uint64_t)opt_binary);
92
93 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
94 {
95 memcached_free(memc);
96 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
97 return EXIT_FAILURE;
98 }
99
100 if (opt_username)
101 {
102 memcached_return_t ret;
103 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
104 {
105 std::cerr << memcached_last_error_message(memc) << std::endl;
106 memcached_free(memc);
107 return EXIT_FAILURE;
108 }
109 }
110
111 rc= memcached_dump(memc, callbacks, NULL, 1);
112
113 if (rc != MEMCACHED_SUCCESS)
114 {
115 fprintf(stderr, "memdump: memcache error %s", memcached_strerror(memc, rc));
116 if (memcached_last_error_errno(memc))
117 fprintf(stderr, " system error %s", strerror(memcached_last_error_errno(memc)));
118 fprintf(stderr, "\n");
119 }
120
121 memcached_free(memc);
122
123 if (opt_servers)
124 free(opt_servers);
125 if (opt_hash)
126 free(opt_hash);
127
128 return EXIT_SUCCESS;
129 }
130
131 static void options_parse(int argc, char *argv[])
132 {
133 int option_index= 0;
134 int option_rv;
135
136 static struct option long_options[]=
137 {
138 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
139 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
140 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
141 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
142 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
143 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
144 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
145 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
146 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
147 {0, 0, 0, 0}
148 };
149
150 while (1)
151 {
152 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
153
154 if (option_rv == -1) break;
155
156 switch (option_rv)
157 {
158 case 0:
159 break;
160 case OPT_BINARY:
161 opt_binary = 1;
162 break;
163 case OPT_VERBOSE: /* --verbose or -v */
164 opt_verbose = OPT_VERBOSE;
165 break;
166 case OPT_DEBUG: /* --debug or -d */
167 opt_verbose = OPT_DEBUG;
168 break;
169 case OPT_VERSION: /* --version or -V */
170 version_command(PROGRAM_NAME);
171 break;
172 case OPT_HELP: /* --help or -h */
173 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL);
174 break;
175 case OPT_SERVERS: /* --servers or -s */
176 opt_servers= strdup(optarg);
177 break;
178 case OPT_HASH:
179 opt_hash= strdup(optarg);
180 break;
181 case OPT_USERNAME:
182 opt_username= optarg;
183 break;
184 case OPT_PASSWD:
185 opt_passwd= optarg;
186 break;
187 case '?':
188 /* getopt_long already printed an error message. */
189 exit(1);
190 default:
191 abort();
192 }
193 }
194 }