Merge in updates for sasl.
[awesomized/libmemcached] / clients / memrm.cc
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 #include "config.h"
12
13 #include <cstdio>
14 #include <cstring>
15 #include <getopt.h>
16 #include <iostream>
17 #include <unistd.h>
18
19 #include <libmemcached/memcached.h>
20 #include "client_options.h"
21 #include "utilities.h"
22
23 static int opt_binary= 0;
24 static int opt_verbose= 0;
25 static time_t opt_expire= 0;
26 static char *opt_servers= NULL;
27 static char *opt_hash= NULL;
28 static char *opt_username;
29 static char *opt_passwd;
30
31 #define PROGRAM_NAME "memrm"
32 #define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached cluster."
33
34 /* Prototypes */
35 static void options_parse(int argc, char *argv[]);
36
37 int main(int argc, char *argv[])
38 {
39 memcached_st *memc;
40 memcached_return_t rc;
41 memcached_server_st *servers;
42
43 int return_code= 0;
44
45 options_parse(argc, argv);
46 initialize_sockets();
47
48 if (opt_servers == 0)
49 {
50 char *temp;
51
52 if ((temp= getenv("MEMCACHED_SERVERS")))
53 opt_servers= strdup(temp);
54 else
55 {
56 std::cerr << "No Servers provided" << std::endl;
57 return EXIT_FAILURE;
58 }
59 }
60
61 memc= memcached_create(NULL);
62 process_hash_option(memc, opt_hash);
63
64 servers= memcached_servers_parse(opt_servers);
65 memcached_server_push(memc, servers);
66 memcached_server_list_free(servers);
67 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
68 (uint64_t) opt_binary);
69
70 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
71 {
72 memcached_free(memc);
73 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
74 return EXIT_FAILURE;
75 }
76
77 if (opt_username)
78 {
79 memcached_return_t ret;
80 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
81 {
82 std::cerr << memcached_last_error_message(memc) << std::endl;
83 memcached_free(memc);
84 return EXIT_FAILURE;
85 }
86 }
87
88 while (optind < argc)
89 {
90 if (opt_verbose)
91 {
92 std::cout << "key: " << argv[optind] << std::endl;
93 std::cout << "expires: " << opt_expire << std::endl;
94 }
95 rc= memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire);
96
97 if (memcached_failed(rc))
98 {
99 std::cerr << PROGRAM_NAME << ": " << argv[optind] << ": error " << memcached_strerror(memc, rc) << std::endl;
100
101 if (memcached_last_error_errno(memc))
102 {
103 std::cerr << " system error " << strerror(memcached_last_error_errno(memc));
104 }
105 std::cerr << std::endl;
106
107 return_code= -1;
108 }
109
110 optind++;
111 }
112
113 memcached_free(memc);
114
115 if (opt_servers)
116 free(opt_servers);
117
118 if (opt_hash)
119 free(opt_hash);
120
121 return return_code;
122 }
123
124
125 static void options_parse(int argc, char *argv[])
126 {
127 memcached_programs_help_st help_options[]=
128 {
129 {0},
130 };
131
132 static struct option long_options[]=
133 {
134 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
135 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
136 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
137 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
138 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
139 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
140 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
141 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
142 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
143 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
144 {0, 0, 0, 0},
145 };
146 int option_index= 0;
147 int option_rv;
148
149 while (1)
150 {
151 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
152 if (option_rv == -1) break;
153 switch (option_rv)
154 {
155 case 0:
156 break;
157 case OPT_BINARY:
158 opt_binary = 1;
159 break;
160 case OPT_VERBOSE: /* --verbose or -v */
161 opt_verbose = OPT_VERBOSE;
162 break;
163 case OPT_DEBUG: /* --debug or -d */
164 opt_verbose = OPT_DEBUG;
165 break;
166 case OPT_VERSION: /* --version or -V */
167 version_command(PROGRAM_NAME);
168 break;
169 case OPT_HELP: /* --help or -h */
170 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
171 break;
172 case OPT_SERVERS: /* --servers or -s */
173 opt_servers= strdup(optarg);
174 break;
175 case OPT_EXPIRE: /* --expire */
176 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
177 break;
178 case OPT_HASH:
179 opt_hash= strdup(optarg);
180 break;
181 case OPT_USERNAME:
182 opt_username= optarg;
183 break;
184 case OPT_PASSWD:
185 opt_passwd= optarg;
186 break;
187 case '?':
188 /* getopt_long already printed an error message. */
189 exit(1);
190 default:
191 abort();
192 }
193 }
194 }