2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
23 #include <sys/types.h>
24 #include <sys/types.h>
27 #include <libmemcached/memcached.h>
29 #include "client_options.h"
30 #include "utilities.h"
32 #define PROGRAM_NAME "memdump"
33 #define PROGRAM_DESCRIPTION "Dump all values from one or many servers."
36 static void options_parse(int argc
, char *argv
[]);
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
;
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
,
50 (void)ptr
;(void)context
;
51 printf("%.*s\n", (uint32_t)key_length
, key
);
53 return MEMCACHED_SUCCESS
;
56 int main(int argc
, char *argv
[])
59 memcached_return_t rc
;
60 memcached_server_st
*servers
;
61 memcached_dump_fn callbacks
[1];
63 callbacks
[0]= &key_printer
;
65 options_parse(argc
, argv
);
67 memc
= memcached_create(NULL
);
68 process_hash_option(memc
, opt_hash
);
74 if ((temp
= getenv("MEMCACHED_SERVERS")))
75 opt_servers
= strdup(temp
);
78 fprintf(stderr
, "No Servers provided\n");
84 servers
= memcached_servers_parse(opt_servers
);
86 servers
= memcached_servers_parse(argv
[--argc
]);
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
);
93 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
96 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
100 if (opt_username
and initialize_sasl(memc
, opt_username
, opt_passwd
) == false)
102 std::cerr
<< "Failed to initialize SASL support." << std::endl
;
104 memcached_free(memc
);
108 rc
= memcached_dump(memc
, callbacks
, NULL
, 1);
110 if (rc
!= MEMCACHED_SUCCESS
)
112 fprintf(stderr
, "memdump: memcache error %s", memcached_strerror(memc
, rc
));
113 if (memcached_last_error_errno(memc
))
114 fprintf(stderr
, " system error %s", strerror(memcached_last_error_errno(memc
)));
115 fprintf(stderr
, "\n");
118 memcached_free(memc
);
130 static void options_parse(int argc
, char *argv
[])
135 static struct option long_options
[]=
137 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
138 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
139 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
140 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
141 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
142 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
143 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
144 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
145 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
151 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
153 if (option_rv
== -1) break;
162 case OPT_VERBOSE
: /* --verbose or -v */
163 opt_verbose
= OPT_VERBOSE
;
165 case OPT_DEBUG
: /* --debug or -d */
166 opt_verbose
= OPT_DEBUG
;
168 case OPT_VERSION
: /* --version or -V */
169 version_command(PROGRAM_NAME
);
171 case OPT_HELP
: /* --help or -h */
172 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, NULL
);
174 case OPT_SERVERS
: /* --servers or -s */
175 opt_servers
= strdup(optarg
);
178 opt_hash
= strdup(optarg
);
181 opt_username
= optarg
;
187 /* getopt_long already printed an error message. */