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.
12 #include "libmemcached/common.h"
18 #include <sys/types.h>
20 #include <sys/types.h>
26 #include <libmemcached/memcached.h>
28 #include "client_options.h"
29 #include "utilities.h"
31 #define PROGRAM_NAME "memdump"
32 #define PROGRAM_DESCRIPTION "Dump all values from one or many servers."
35 static void options_parse(int argc
, char *argv
[]);
37 static int opt_binary
=0;
38 static int opt_verbose
= 0;
39 static char *opt_servers
= NULL
;
40 static char *opt_hash
= NULL
;
41 static char *opt_username
;
42 static char *opt_passwd
;
44 /* Print the keys and counter how many were found */
45 static memcached_return_t
key_printer(const memcached_st
*ptr
__attribute__((unused
)),
46 const char *key
, size_t key_length
,
47 void *context
__attribute__((unused
)))
49 printf("%.*s\n", (uint32_t)key_length
, key
);
51 return MEMCACHED_SUCCESS
;
54 int main(int argc
, char *argv
[])
57 memcached_return_t rc
;
58 memcached_server_st
*servers
;
59 memcached_dump_fn callbacks
[1];
61 callbacks
[0]= &key_printer
;
63 options_parse(argc
, argv
);
65 memc
= memcached_create(NULL
);
66 process_hash_option(memc
, opt_hash
);
72 if ((temp
= getenv("MEMCACHED_SERVERS")))
73 opt_servers
= strdup(temp
);
76 fprintf(stderr
, "No Servers provided\n");
82 servers
= memcached_servers_parse(opt_servers
);
84 servers
= memcached_servers_parse(argv
[--argc
]);
86 memcached_server_push(memc
, servers
);
87 memcached_server_list_free(servers
);
88 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
89 (uint64_t)opt_binary
);
90 if (!initialize_sasl(memc
, opt_username
, opt_passwd
))
96 rc
= memcached_dump(memc
, callbacks
, NULL
, 1);
98 if (rc
!= MEMCACHED_SUCCESS
)
100 fprintf(stderr
, "memdump: memcache error %s", memcached_strerror(memc
, rc
));
101 if (memc
->cached_errno
)
102 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
103 fprintf(stderr
, "\n");
106 memcached_free(memc
);
118 static void options_parse(int argc
, char *argv
[])
123 static struct option long_options
[]=
125 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
126 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
127 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
128 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
129 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
130 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
131 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
132 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
133 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
139 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
141 if (option_rv
== -1) break;
150 case OPT_VERBOSE
: /* --verbose or -v */
151 opt_verbose
= OPT_VERBOSE
;
153 case OPT_DEBUG
: /* --debug or -d */
154 opt_verbose
= OPT_DEBUG
;
156 case OPT_VERSION
: /* --version or -V */
157 version_command(PROGRAM_NAME
);
159 case OPT_HELP
: /* --help or -h */
160 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, NULL
);
162 case OPT_SERVERS
: /* --servers or -s */
163 opt_servers
= strdup(optarg
);
166 opt_hash
= strdup(optarg
);
169 opt_username
= optarg
;
175 /* getopt_long already printed an error message. */