Merge in sasl update
[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 fprintf(stderr, "No Servers provided\n");
57 exit(1);
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
78 if (opt_username and initialize_sasl(memc, opt_username, opt_passwd) == false)
79 {
80 std::cerr << "Failed to initialize SASL support." << std::endl;
81
82 memcached_free(memc);
83 return EXIT_FAILURE;
84 }
85
86 while (optind < argc)
87 {
88 if (opt_verbose)
89 printf("key: %s\nexpires: %llu\n", argv[optind], (unsigned long long)opt_expire);
90 rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire);
91
92 if (rc != MEMCACHED_SUCCESS)
93 {
94 fprintf(stderr, "memrm: %s: memcache error %s",
95 argv[optind], memcached_strerror(memc, rc));
96 if (memcached_last_error_errno(memc))
97 fprintf(stderr, " system error %s", strerror(memcached_last_error_errno(memc)));
98 fprintf(stderr, "\n");
99
100 return_code= -1;
101 }
102
103 optind++;
104 }
105
106 memcached_free(memc);
107
108 if (opt_servers)
109 free(opt_servers);
110
111 if (opt_hash)
112 free(opt_hash);
113
114 shutdown_sasl();
115
116 return return_code;
117 }
118
119
120 static void options_parse(int argc, char *argv[])
121 {
122 memcached_programs_help_st help_options[]=
123 {
124 {0},
125 };
126
127 static struct option long_options[]=
128 {
129 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
130 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
131 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
132 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
133 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
134 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
135 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
136 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
137 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
138 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
139 {0, 0, 0, 0},
140 };
141 int option_index= 0;
142 int option_rv;
143
144 while (1)
145 {
146 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
147 if (option_rv == -1) break;
148 switch (option_rv)
149 {
150 case 0:
151 break;
152 case OPT_BINARY:
153 opt_binary = 1;
154 break;
155 case OPT_VERBOSE: /* --verbose or -v */
156 opt_verbose = OPT_VERBOSE;
157 break;
158 case OPT_DEBUG: /* --debug or -d */
159 opt_verbose = OPT_DEBUG;
160 break;
161 case OPT_VERSION: /* --version or -V */
162 version_command(PROGRAM_NAME);
163 break;
164 case OPT_HELP: /* --help or -h */
165 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
166 break;
167 case OPT_SERVERS: /* --servers or -s */
168 opt_servers= strdup(optarg);
169 break;
170 case OPT_EXPIRE: /* --expire */
171 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
172 break;
173 case OPT_HASH:
174 opt_hash= strdup(optarg);
175 break;
176 case OPT_USERNAME:
177 opt_username= optarg;
178 break;
179 case OPT_PASSWD:
180 opt_passwd= optarg;
181 break;
182 case '?':
183 /* getopt_long already printed an error message. */
184 exit(1);
185 default:
186 abort();
187 }
188 }
189 }