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 +--------------------------------------------------------------------+
16 #include "mem_config.h"
25 #include "libmemcached-1.0/memcached.h"
26 #include "client_options.h"
27 #include "utilities.h"
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
;
37 #define PROGRAM_NAME "memrm"
38 #define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached cluster."
41 static void options_parse(int argc
, char *argv
[]);
43 int main(int argc
, char *argv
[]) {
44 options_parse(argc
, argv
);
47 if (opt_servers
== NULL
) {
50 if ((temp
= getenv("MEMCACHED_SERVERS"))) {
51 opt_servers
= strdup(temp
);
54 if (opt_servers
== NULL
) {
55 std::cerr
<< "No Servers provided" << std::endl
;
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
;
66 memcached_st
*memc
= memcached_create(NULL
);
67 process_hash_option(memc
, opt_hash
);
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
);
73 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0) {
75 std::cerr
<< "--username was supplied, but binary was not built with SASL support."
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
;
89 int return_code
= EXIT_SUCCESS
;
91 while (optind
< argc
) {
92 memcached_return_t rc
= memcached_delete(memc
, argv
[optind
], strlen(argv
[optind
]), opt_expire
);
94 if (rc
== MEMCACHED_NOTFOUND
) {
96 std::cerr
<< "Could not find key \"" << argv
[optind
] << "\"" << std::endl
;
98 } else if (memcached_fatal(rc
)) {
100 std::cerr
<< "Failed to delete key \"" << argv
[optind
]
101 << "\" :" << memcached_last_error_message(memc
) << std::endl
;
104 return_code
= EXIT_FAILURE
;
108 std::cout
<< "Deleted key " << argv
[optind
];
110 std::cout
<< " expires: " << opt_expire
<< std::endl
;
112 std::cout
<< std::endl
;
119 memcached_free(memc
);
132 static void options_parse(int argc
, char *argv
[]) {
133 memcached_programs_help_st help_options
[] = {
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
},
152 bool opt_version
= false;
153 bool opt_help
= false;
154 int option_index
= 0;
157 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
158 if (option_rv
== -1) {
170 case OPT_VERBOSE
: /* --verbose or -v */
171 opt_verbose
= OPT_VERBOSE
;
174 case OPT_DEBUG
: /* --debug or -d */
175 opt_verbose
= OPT_DEBUG
;
178 case OPT_VERSION
: /* --version or -V */
182 case OPT_HELP
: /* --help or -h */
186 case OPT_SERVERS
: /* --servers or -s */
187 opt_servers
= strdup(optarg
);
190 case OPT_EXPIRE
: /* --expire */
192 opt_expire
= (time_t) strtoll(optarg
, (char **) NULL
, 10);
194 std::cerr
<< "Incorrect value passed to --expire: `" << optarg
<< "`" << std::endl
;
200 opt_hash
= strdup(optarg
);
204 opt_username
= optarg
;
216 /* getopt_long already printed an error message. */
225 version_command(PROGRAM_NAME
);
230 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);