Fix build for non-sasl-enabled builds
[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 "libmemcached/common.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <inttypes.h>
16 #include <unistd.h>
17 #include <getopt.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <fcntl.h>
22 #include <errno.h>
23 #include <strings.h>
24 #include <string.h>
25
26 #include <libmemcached/memcached.h>
27
28 #include "client_options.h"
29 #include "utilities.h"
30
31 #define PROGRAM_NAME "memdump"
32 #define PROGRAM_DESCRIPTION "Dump all values from one or many servers."
33
34 /* Prototypes */
35 static void options_parse(int argc, char *argv[]);
36
37 static int opt_binary=0;
38 static int opt_verbose= 0;
39 static char *opt_servers= NULL;
40 static char *opt_hash= NULL;
41 static char *opt_username;
42 static char *opt_passwd;
43
44 /* Print the keys and counter how many were found */
45 static memcached_return_t key_printer(const memcached_st *ptr __attribute__((unused)),
46 const char *key, size_t key_length,
47 void *context __attribute__((unused)))
48 {
49 printf("%.*s\n", (uint32_t)key_length, key);
50
51 return MEMCACHED_SUCCESS;
52 }
53
54 int main(int argc, char *argv[])
55 {
56 memcached_st *memc;
57 memcached_return_t rc;
58 memcached_server_st *servers;
59 memcached_dump_fn callbacks[1];
60
61 callbacks[0]= &key_printer;
62
63 options_parse(argc, argv);
64
65 memc= memcached_create(NULL);
66 process_hash_option(memc, opt_hash);
67
68 if (!opt_servers)
69 {
70 char *temp;
71
72 if ((temp= getenv("MEMCACHED_SERVERS")))
73 opt_servers= strdup(temp);
74 else
75 {
76 fprintf(stderr, "No Servers provided\n");
77 exit(1);
78 }
79 }
80
81 if (opt_servers)
82 servers= memcached_servers_parse(opt_servers);
83 else
84 servers= memcached_servers_parse(argv[--argc]);
85
86 memcached_server_push(memc, servers);
87 memcached_server_list_free(servers);
88 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
89 (uint64_t)opt_binary);
90 if (!initialize_sasl(memc, opt_username, opt_passwd))
91 {
92 memcached_free(memc);
93 return 1;
94 }
95
96 rc= memcached_dump(memc, callbacks, NULL, 1);
97
98 if (rc != MEMCACHED_SUCCESS)
99 {
100 fprintf(stderr, "memdump: memcache error %s", memcached_strerror(memc, rc));
101 if (memc->cached_errno)
102 fprintf(stderr, " system error %s", strerror(memc->cached_errno));
103 fprintf(stderr, "\n");
104 }
105
106 memcached_free(memc);
107
108 if (opt_servers)
109 free(opt_servers);
110 if (opt_hash)
111 free(opt_hash);
112
113 shutdown_sasl();
114
115 return 0;
116 }
117
118 static void options_parse(int argc, char *argv[])
119 {
120 int option_index= 0;
121 int option_rv;
122
123 static struct option long_options[]=
124 {
125 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
126 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
127 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
128 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
129 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
130 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
131 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
132 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
133 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
134 {0, 0, 0, 0}
135 };
136
137 while (1)
138 {
139 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
140
141 if (option_rv == -1) break;
142
143 switch (option_rv)
144 {
145 case 0:
146 break;
147 case OPT_BINARY:
148 opt_binary = 1;
149 break;
150 case OPT_VERBOSE: /* --verbose or -v */
151 opt_verbose = OPT_VERBOSE;
152 break;
153 case OPT_DEBUG: /* --debug or -d */
154 opt_verbose = OPT_DEBUG;
155 break;
156 case OPT_VERSION: /* --version or -V */
157 version_command(PROGRAM_NAME);
158 break;
159 case OPT_HELP: /* --help or -h */
160 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL);
161 break;
162 case OPT_SERVERS: /* --servers or -s */
163 opt_servers= strdup(optarg);
164 break;
165 case OPT_HASH:
166 opt_hash= strdup(optarg);
167 break;
168 case OPT_USERNAME:
169 opt_username= optarg;
170 break;
171 case OPT_PASSWD:
172 opt_passwd= optarg;
173 break;
174 case '?':
175 /* getopt_long already printed an error message. */
176 exit(1);
177 default:
178 abort();
179 }
180 }
181 }