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
, opt_binary
);
77 rc
= memcached_dump(memc
, callbacks
, NULL
, 1);
79 if (rc
!= MEMCACHED_SUCCESS
)
81 fprintf(stderr
, "memdump: memcache error %s", memcached_strerror(memc
, rc
));
82 if (memc
->cached_errno
)
83 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
84 fprintf(stderr
, "\n");
97 void options_parse(int argc
, char *argv
[])
102 static struct option long_options
[]=
104 {"version", no_argument
, NULL
, OPT_VERSION
},
105 {"help", no_argument
, NULL
, OPT_HELP
},
106 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
107 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
108 {"servers", required_argument
, NULL
, OPT_SERVERS
},
109 {"hash", required_argument
, NULL
, OPT_HASH
},
110 {"binary", no_argument
, NULL
, OPT_BINARY
},
116 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
118 if (option_rv
== -1) break;
127 case OPT_VERBOSE
: /* --verbose or -v */
128 opt_verbose
= OPT_VERBOSE
;
130 case OPT_DEBUG
: /* --debug or -d */
131 opt_verbose
= OPT_DEBUG
;
133 case OPT_VERSION
: /* --version or -V */
134 version_command(PROGRAM_NAME
);
136 case OPT_HELP
: /* --help or -h */
137 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, NULL
);
139 case OPT_SERVERS
: /* --servers or -s */
140 opt_servers
= strdup(optarg
);
143 opt_hash
= strdup(optarg
);
146 /* getopt_long already printed an error message. */