2 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
13 #include "mem_config.h"
24 #include <sys/types.h>
25 #include <sys/types.h>
28 #include <libmemcached-1.0/memcached.h>
30 #include "client_options.h"
31 #include "utilities.h"
33 #define PROGRAM_NAME "memdump"
34 #define PROGRAM_DESCRIPTION "Dump all values from one or many servers."
37 static void options_parse(int argc
, char *argv
[]);
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
;
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
,
51 std::cout
.write(key
, key_length
);
52 std::cout
<< std::endl
;
54 return MEMCACHED_SUCCESS
;
57 int main(int argc
, char *argv
[])
59 memcached_dump_fn callbacks
[1];
61 callbacks
[0]= &key_printer
;
63 options_parse(argc
, argv
);
65 if (opt_servers
== NULL
)
69 if ((temp
= getenv("MEMCACHED_SERVERS")))
71 opt_servers
= strdup(temp
);
73 else if (argc
>= 1 and argv
[--argc
])
75 opt_servers
= strdup(argv
[argc
]);
78 if (opt_servers
== NULL
)
80 std::cerr
<< "No Servers provided" << std::endl
;
85 memcached_server_st
* servers
= memcached_servers_parse(opt_servers
);
86 if (servers
== NULL
or memcached_server_list_count(servers
) == 0)
88 std::cerr
<< "Invalid server list provided:" << opt_servers
<< std::endl
;
92 memcached_st
*memc
= memcached_create(NULL
);
95 std::cerr
<< "Could not allocate a memcached_st structure.\n" << std::endl
;
98 process_hash_option(memc
, opt_hash
);
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
);
105 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
107 memcached_free(memc
);
108 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
114 memcached_return_t ret
;
115 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
117 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
118 memcached_free(memc
);
123 memcached_return_t rc
= memcached_dump(memc
, callbacks
, NULL
, 1);
125 int exit_code
= EXIT_SUCCESS
;
126 if (memcached_failed(rc
))
130 std::cerr
<< "Failed to dump keys: " << memcached_last_error_message(memc
) << std::endl
;
132 exit_code
= EXIT_FAILURE
;
135 memcached_free(memc
);
149 static void options_parse(int argc
, char *argv
[])
151 static struct option long_options
[]=
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
},
167 bool opt_version
= false;
168 bool opt_help
= false;
171 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
173 if (option_rv
== -1) break;
184 case OPT_VERBOSE
: /* --verbose or -v */
185 opt_verbose
= OPT_VERBOSE
;
188 case OPT_DEBUG
: /* --debug or -d */
189 opt_verbose
= OPT_DEBUG
;
192 case OPT_VERSION
: /* --version or -V */
196 case OPT_HELP
: /* --help or -h */
200 case OPT_SERVERS
: /* --servers or -s */
201 opt_servers
= strdup(optarg
);
205 opt_hash
= strdup(optarg
);
209 opt_username
= optarg
;
221 /* getopt_long already printed an error message. */
230 version_command(PROGRAM_NAME
);
236 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, NULL
);