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
;
42 /* Print the keys and counter how many were found */
43 static memcached_return_t
key_printer(memcached_st
*ptr
__attribute__((unused
)),
44 const char *key
, size_t key_length
,
45 void *context
__attribute__((unused
)))
47 printf("%.*s\n", (uint32_t)key_length
, key
);
49 return MEMCACHED_SUCCESS
;
52 int main(int argc
, char *argv
[])
55 memcached_return_t rc
;
56 memcached_server_st
*servers
;
57 memcached_dump_fn callbacks
[1];
59 callbacks
[0]= &key_printer
;
61 options_parse(argc
, argv
);
63 memc
= memcached_create(NULL
);
64 process_hash_option(memc
, opt_hash
);
70 if ((temp
= getenv("MEMCACHED_SERVERS")))
71 opt_servers
= strdup(temp
);
74 fprintf(stderr
, "No Servers provided\n");
80 servers
= memcached_servers_parse(opt_servers
);
82 servers
= memcached_servers_parse(argv
[--argc
]);
84 memcached_server_push(memc
, servers
);
85 memcached_server_list_free(servers
);
86 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
87 (uint64_t)opt_binary
);
89 rc
= memcached_dump(memc
, callbacks
, NULL
, 1);
91 if (rc
!= MEMCACHED_SUCCESS
)
93 fprintf(stderr
, "memdump: memcache error %s", memcached_strerror(memc
, rc
));
94 if (memc
->cached_errno
)
95 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
96 fprintf(stderr
, "\n");
109 static void options_parse(int argc
, char *argv
[])
114 static struct option long_options
[]=
116 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
117 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
118 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
119 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
120 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
121 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
122 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
128 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
130 if (option_rv
== -1) break;
139 case OPT_VERBOSE
: /* --verbose or -v */
140 opt_verbose
= OPT_VERBOSE
;
142 case OPT_DEBUG
: /* --debug or -d */
143 opt_verbose
= OPT_DEBUG
;
145 case OPT_VERSION
: /* --version or -V */
146 version_command(PROGRAM_NAME
);
148 case OPT_HELP
: /* --help or -h */
149 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, NULL
);
151 case OPT_SERVERS
: /* --servers or -s */
152 opt_servers
= strdup(optarg
);
155 opt_hash
= strdup(optarg
);
158 /* getopt_long already printed an error message. */