1 #include "libmemcached/common.h"
15 #include <libmemcached/memcached.h>
17 #include "client_options.h"
18 #include "utilities.h"
20 #define PROGRAM_NAME "memdump"
21 #define PROGRAM_DESCRIPTION "Dump all values from one or many servers."
24 void options_parse(int argc
, char *argv
[]);
26 static int opt_binary
=0;
27 static int opt_verbose
= 0;
28 static char *opt_servers
= NULL
;
29 static char *opt_hash
= NULL
;
31 /* Print the keys and counter how many were found */
32 static memcached_return
key_printer(memcached_st
*ptr
__attribute__((unused
)),
33 const char *key
, size_t key_length
,
34 void *context
__attribute__((unused
)))
36 printf("%.*s\n", (uint32_t)key_length
, key
);
38 return MEMCACHED_SUCCESS
;
41 int main(int argc
, char *argv
[])
45 memcached_server_st
*servers
;
46 memcached_dump_func callbacks
[1];
48 callbacks
[0]= &key_printer
;
50 options_parse(argc
, argv
);
52 memc
= memcached_create(NULL
);
53 process_hash_option(memc
, opt_hash
);
59 if ((temp
= getenv("MEMCACHED_SERVERS")))
60 opt_servers
= strdup(temp
);
63 fprintf(stderr
, "No Servers provided\n");
69 servers
= memcached_servers_parse(opt_servers
);
71 servers
= memcached_servers_parse(argv
[--argc
]);
73 memcached_server_push(memc
, servers
);
74 memcached_server_list_free(servers
);
75 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
76 (uint64_t)opt_binary
);
78 rc
= memcached_dump(memc
, callbacks
, NULL
, 1);
80 if (rc
!= MEMCACHED_SUCCESS
)
82 fprintf(stderr
, "memdump: memcache error %s", memcached_strerror(memc
, rc
));
83 if (memc
->cached_errno
)
84 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
85 fprintf(stderr
, "\n");
98 void options_parse(int argc
, char *argv
[])
103 static struct option long_options
[]=
105 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
106 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
107 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
108 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
109 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
110 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
111 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
117 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
119 if (option_rv
== -1) break;
128 case OPT_VERBOSE
: /* --verbose or -v */
129 opt_verbose
= OPT_VERBOSE
;
131 case OPT_DEBUG
: /* --debug or -d */
132 opt_verbose
= OPT_DEBUG
;
134 case OPT_VERSION
: /* --version or -V */
135 version_command(PROGRAM_NAME
);
137 case OPT_HELP
: /* --help or -h */
138 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, NULL
);
140 case OPT_SERVERS
: /* --servers or -s */
141 opt_servers
= strdup(optarg
);
144 opt_hash
= strdup(optarg
);
147 /* getopt_long already printed an error message. */