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.
19 #include <sys/types.h>
21 #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
__attribute__((unused
)),
47 const char *key
, size_t key_length
,
48 void *context
__attribute__((unused
)))
50 printf("%.*s\n", (uint32_t)key_length
, key
);
52 return MEMCACHED_SUCCESS
;
55 int main(int argc
, char *argv
[])
58 memcached_return_t rc
;
59 memcached_server_st
*servers
;
60 memcached_dump_fn callbacks
[1];
62 callbacks
[0]= &key_printer
;
64 options_parse(argc
, argv
);
66 memc
= memcached_create(NULL
);
67 process_hash_option(memc
, opt_hash
);
73 if ((temp
= getenv("MEMCACHED_SERVERS")))
74 opt_servers
= strdup(temp
);
77 fprintf(stderr
, "No Servers provided\n");
83 servers
= memcached_servers_parse(opt_servers
);
85 servers
= memcached_servers_parse(argv
[--argc
]);
87 memcached_server_push(memc
, servers
);
88 memcached_server_list_free(servers
);
89 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
90 (uint64_t)opt_binary
);
91 if (!initialize_sasl(memc
, opt_username
, opt_passwd
))
97 rc
= memcached_dump(memc
, callbacks
, NULL
, 1);
99 if (rc
!= MEMCACHED_SUCCESS
)
101 fprintf(stderr
, "memdump: memcache error %s", memcached_strerror(memc
, rc
));
102 if (memc
->cached_errno
)
103 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
104 fprintf(stderr
, "\n");
107 memcached_free(memc
);
119 static void options_parse(int argc
, char *argv
[])
124 static struct option long_options
[]=
126 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
127 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
128 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
129 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
130 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
131 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
132 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
133 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
134 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
140 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
142 if (option_rv
== -1) break;
151 case OPT_VERBOSE
: /* --verbose or -v */
152 opt_verbose
= OPT_VERBOSE
;
154 case OPT_DEBUG
: /* --debug or -d */
155 opt_verbose
= OPT_DEBUG
;
157 case OPT_VERSION
: /* --version or -V */
158 version_command(PROGRAM_NAME
);
160 case OPT_HELP
: /* --help or -h */
161 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, NULL
);
163 case OPT_SERVERS
: /* --servers or -s */
164 opt_servers
= strdup(optarg
);
167 opt_hash
= strdup(optarg
);
170 opt_username
= optarg
;
176 /* getopt_long already printed an error message. */