Merge Trond, this is the preliminary support for Windows.
[m6w6/libmemcached] / clients / memdump.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12 #include "config.h"
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <inttypes.h>
17 #include <unistd.h>
18 #include <getopt.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <strings.h>
25 #include <string.h>
26
27 #include <libmemcached/memcached.h>
28
29 #include "client_options.h"
30 #include "utilities.h"
31
32 #define PROGRAM_NAME "memdump"
33 #define PROGRAM_DESCRIPTION "Dump all values from one or many servers."
34
35 /* Prototypes */
36 static void options_parse(int argc, char *argv[]);
37
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;
44
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)))
49 {
50 printf("%.*s\n", (uint32_t)key_length, key);
51
52 return MEMCACHED_SUCCESS;
53 }
54
55 int main(int argc, char *argv[])
56 {
57 memcached_st *memc;
58 memcached_return_t rc;
59 memcached_server_st *servers;
60 memcached_dump_fn callbacks[1];
61
62 callbacks[0]= &key_printer;
63
64 options_parse(argc, argv);
65
66 memc= memcached_create(NULL);
67 process_hash_option(memc, opt_hash);
68
69 if (!opt_servers)
70 {
71 char *temp;
72
73 if ((temp= getenv("MEMCACHED_SERVERS")))
74 opt_servers= strdup(temp);
75 else
76 {
77 fprintf(stderr, "No Servers provided\n");
78 exit(1);
79 }
80 }
81
82 if (opt_servers)
83 servers= memcached_servers_parse(opt_servers);
84 else
85 servers= memcached_servers_parse(argv[--argc]);
86
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))
92 {
93 memcached_free(memc);
94 return 1;
95 }
96
97 rc= memcached_dump(memc, callbacks, NULL, 1);
98
99 if (rc != MEMCACHED_SUCCESS)
100 {
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");
105 }
106
107 memcached_free(memc);
108
109 if (opt_servers)
110 free(opt_servers);
111 if (opt_hash)
112 free(opt_hash);
113
114 shutdown_sasl();
115
116 return 0;
117 }
118
119 static void options_parse(int argc, char *argv[])
120 {
121 int option_index= 0;
122 int option_rv;
123
124 static struct option long_options[]=
125 {
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},
135 {0, 0, 0, 0}
136 };
137
138 while (1)
139 {
140 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
141
142 if (option_rv == -1) break;
143
144 switch (option_rv)
145 {
146 case 0:
147 break;
148 case OPT_BINARY:
149 opt_binary = 1;
150 break;
151 case OPT_VERBOSE: /* --verbose or -v */
152 opt_verbose = OPT_VERBOSE;
153 break;
154 case OPT_DEBUG: /* --debug or -d */
155 opt_verbose = OPT_DEBUG;
156 break;
157 case OPT_VERSION: /* --version or -V */
158 version_command(PROGRAM_NAME);
159 break;
160 case OPT_HELP: /* --help or -h */
161 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL);
162 break;
163 case OPT_SERVERS: /* --servers or -s */
164 opt_servers= strdup(optarg);
165 break;
166 case OPT_HASH:
167 opt_hash= strdup(optarg);
168 break;
169 case OPT_USERNAME:
170 opt_username= optarg;
171 break;
172 case OPT_PASSWD:
173 opt_passwd= optarg;
174 break;
175 case '?':
176 /* getopt_long already printed an error message. */
177 exit(1);
178 default:
179 abort();
180 }
181 }
182 }