7bd351329937f596dc596f3c4bd80a93af47780d
[m6w6/libmemcached] / src / bin / memrm.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "mem_config.h"
17
18 #include <cerrno>
19 #include <cstdio>
20 #include <cstring>
21 #include <getopt.h>
22 #include <iostream>
23 #include <unistd.h>
24
25 #include "libmemcached-1.0/memcached.h"
26 #include "client_options.h"
27 #include "utilities.h"
28
29 static int opt_binary = 0;
30 static int opt_verbose = 0;
31 static time_t opt_expire = 0;
32 static char *opt_servers = NULL;
33 static char *opt_hash = NULL;
34 static char *opt_username;
35 static char *opt_passwd;
36
37 #define PROGRAM_NAME "memrm"
38 #define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached cluster."
39
40 /* Prototypes */
41 static void options_parse(int argc, char *argv[]);
42
43 int main(int argc, char *argv[]) {
44 options_parse(argc, argv);
45 initialize_sockets();
46
47 if (opt_servers == NULL) {
48 char *temp;
49
50 if ((temp = getenv("MEMCACHED_SERVERS"))) {
51 opt_servers = strdup(temp);
52 }
53
54 if (opt_servers == NULL) {
55 std::cerr << "No Servers provided" << std::endl;
56 exit(EXIT_FAILURE);
57 }
58 }
59
60 memcached_server_st *servers = memcached_servers_parse(opt_servers);
61 if (servers == NULL or memcached_server_list_count(servers) == 0) {
62 std::cerr << "Invalid server list provided:" << opt_servers << std::endl;
63 return EXIT_FAILURE;
64 }
65
66 memcached_st *memc = memcached_create(NULL);
67 process_hash_option(memc, opt_hash);
68
69 memcached_server_push(memc, servers);
70 memcached_server_list_free(servers);
71 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t) opt_binary);
72
73 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) {
74 memcached_free(memc);
75 std::cerr << "--username was supplied, but binary was not built with SASL support."
76 << std::endl;
77 return EXIT_FAILURE;
78 }
79
80 if (opt_username) {
81 memcached_return_t ret;
82 if (memcached_failed(ret = memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) {
83 std::cerr << memcached_last_error_message(memc) << std::endl;
84 memcached_free(memc);
85 return EXIT_FAILURE;
86 }
87 }
88
89 int return_code = EXIT_SUCCESS;
90
91 while (optind < argc) {
92 memcached_return_t rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire);
93
94 if (rc == MEMCACHED_NOTFOUND) {
95 if (opt_verbose) {
96 std::cerr << "Could not find key \"" << argv[optind] << "\"" << std::endl;
97 }
98 } else if (memcached_fatal(rc)) {
99 if (opt_verbose) {
100 std::cerr << "Failed to delete key \"" << argv[optind]
101 << "\" :" << memcached_last_error_message(memc) << std::endl;
102 }
103
104 return_code = EXIT_FAILURE;
105 } else // success
106 {
107 if (opt_verbose) {
108 std::cout << "Deleted key " << argv[optind];
109 if (opt_expire) {
110 std::cout << " expires: " << opt_expire << std::endl;
111 }
112 std::cout << std::endl;
113 }
114 }
115
116 optind++;
117 }
118
119 memcached_free(memc);
120
121 if (opt_servers) {
122 free(opt_servers);
123 }
124
125 if (opt_hash) {
126 free(opt_hash);
127 }
128
129 return return_code;
130 }
131
132 static void options_parse(int argc, char *argv[]) {
133 memcached_programs_help_st help_options[] = {
134 {0},
135 };
136
137 static struct option long_options[] = {
138 {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION},
139 {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP},
140 {(OPTIONSTRING) "quiet", no_argument, NULL, OPT_QUIET},
141 {(OPTIONSTRING) "verbose", no_argument, &opt_verbose, OPT_VERBOSE},
142 {(OPTIONSTRING) "debug", no_argument, &opt_verbose, OPT_DEBUG},
143 {(OPTIONSTRING) "servers", required_argument, NULL, OPT_SERVERS},
144 {(OPTIONSTRING) "expire", required_argument, NULL, OPT_EXPIRE},
145 {(OPTIONSTRING) "hash", required_argument, NULL, OPT_HASH},
146 {(OPTIONSTRING) "binary", no_argument, NULL, OPT_BINARY},
147 {(OPTIONSTRING) "username", required_argument, NULL, OPT_USERNAME},
148 {(OPTIONSTRING) "password", required_argument, NULL, OPT_PASSWD},
149 {0, 0, 0, 0},
150 };
151
152 bool opt_version = false;
153 bool opt_help = false;
154 int option_index = 0;
155
156 while (1) {
157 int option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
158 if (option_rv == -1) {
159 break;
160 }
161
162 switch (option_rv) {
163 case 0: break;
164
165 case OPT_BINARY: opt_binary = 1; break;
166
167 case OPT_VERBOSE: /* --verbose or -v */ opt_verbose = OPT_VERBOSE; break;
168
169 case OPT_DEBUG: /* --debug or -d */ opt_verbose = OPT_DEBUG; break;
170
171 case OPT_VERSION: /* --version or -V */ opt_version = true; break;
172
173 case OPT_HELP: /* --help or -h */ opt_help = true; break;
174
175 case OPT_SERVERS: /* --servers or -s */ opt_servers = strdup(optarg); break;
176
177 case OPT_EXPIRE: /* --expire */
178 errno = 0;
179 opt_expire = (time_t) strtoll(optarg, (char **) NULL, 10);
180 if (errno != 0) {
181 std::cerr << "Incorrect value passed to --expire: `" << optarg << "`" << std::endl;
182 exit(EXIT_FAILURE);
183 }
184 break;
185
186 case OPT_HASH: opt_hash = strdup(optarg); break;
187
188 case OPT_USERNAME: opt_username = optarg; break;
189
190 case OPT_PASSWD: opt_passwd = optarg; break;
191
192 case OPT_QUIET: close_stdio(); break;
193
194 case '?':
195 /* getopt_long already printed an error message. */
196 exit(EXIT_SUCCESS);
197
198 default: abort();
199 }
200 }
201
202 if (opt_version) {
203 version_command(PROGRAM_NAME);
204 exit(EXIT_SUCCESS);
205 }
206
207 if (opt_help) {
208 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
209 exit(EXIT_SUCCESS);
210 }
211 }