clang-format: no single-line case
[awesomized/libmemcached] / src / bin / memexist.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 <cstdio>
19 #include <cstring>
20 #include <getopt.h>
21 #include <iostream>
22 #include <unistd.h>
23
24 #include "libmemcached-1.0/memcached.h"
25 #include "client_options.h"
26 #include "utilities.h"
27
28 static int opt_binary = 0;
29 static int opt_verbose = 0;
30 static char *opt_servers = NULL;
31 static char *opt_hash = NULL;
32 static char *opt_username;
33 static char *opt_passwd;
34
35 #define PROGRAM_NAME "memexist"
36 #define PROGRAM_DESCRIPTION "Check for the existance of a key within a cluster."
37
38 /* Prototypes */
39 static void options_parse(int argc, char *argv[]);
40
41 int main(int argc, char *argv[]) {
42 options_parse(argc, argv);
43 initialize_sockets();
44
45 if (opt_servers == NULL) {
46 char *temp;
47
48 if ((temp = getenv("MEMCACHED_SERVERS"))) {
49 opt_servers = strdup(temp);
50 }
51
52 if (opt_servers == NULL) {
53 std::cerr << "No Servers provided" << std::endl;
54 exit(EXIT_FAILURE);
55 }
56 }
57
58 memcached_server_st *servers = memcached_servers_parse(opt_servers);
59 if (servers == NULL or memcached_server_list_count(servers) == 0) {
60 std::cerr << "Invalid server list provided:" << opt_servers << std::endl;
61 return EXIT_FAILURE;
62 }
63
64 memcached_st *memc = memcached_create(NULL);
65 process_hash_option(memc, opt_hash);
66
67 memcached_server_push(memc, servers);
68 memcached_server_list_free(servers);
69 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t) opt_binary);
70
71 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) {
72 memcached_free(memc);
73 std::cerr << "--username was supplied, but binary was not built with SASL support."
74 << std::endl;
75 return EXIT_FAILURE;
76 }
77
78 if (opt_username) {
79 memcached_return_t ret;
80 if (memcached_failed(ret = memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) {
81 std::cerr << memcached_last_error_message(memc) << std::endl;
82 memcached_free(memc);
83 return EXIT_FAILURE;
84 }
85 }
86
87 int return_code = EXIT_SUCCESS;
88
89 while (optind < argc) {
90 memcached_return_t rc = memcached_exist(memc, argv[optind], strlen(argv[optind]));
91
92 if (rc == MEMCACHED_NOTFOUND) {
93 if (opt_verbose) {
94 std::cout << "Could not find key \"" << argv[optind] << "\"" << std::endl;
95 }
96
97 return_code = EXIT_FAILURE;
98 } else if (memcached_failed(rc)) {
99 if (opt_verbose) {
100 std::cerr << "Fatal error for 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 << "Found key " << argv[optind] << std::endl;
109 }
110 }
111
112 optind++;
113 }
114
115 memcached_free(memc);
116
117 if (opt_servers) {
118 free(opt_servers);
119 }
120
121 if (opt_hash) {
122 free(opt_hash);
123 }
124
125 return return_code;
126 }
127
128 static void options_parse(int argc, char *argv[]) {
129 memcached_programs_help_st help_options[] = {
130 {0},
131 };
132
133 static struct option long_options[] = {
134 {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION},
135 {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP},
136 {(OPTIONSTRING) "quiet", no_argument, NULL, OPT_QUIET},
137 {(OPTIONSTRING) "verbose", no_argument, &opt_verbose, OPT_VERBOSE},
138 {(OPTIONSTRING) "debug", no_argument, &opt_verbose, OPT_DEBUG},
139 {(OPTIONSTRING) "servers", required_argument, NULL, OPT_SERVERS},
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
147 bool opt_version = false;
148 bool opt_help = false;
149 int option_index = 0;
150
151 while (1) {
152 int option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
153 if (option_rv == -1) {
154 break;
155 }
156
157 switch (option_rv) {
158 case 0:
159 break;
160
161 case OPT_BINARY:
162 opt_binary = 1;
163 break;
164
165 case OPT_VERBOSE: /* --verbose or -v */
166 opt_verbose = OPT_VERBOSE;
167 break;
168
169 case OPT_DEBUG: /* --debug or -d */
170 opt_verbose = OPT_DEBUG;
171 break;
172
173 case OPT_VERSION: /* --version or -V */
174 opt_version = true;
175 break;
176
177 case OPT_HELP: /* --help or -h */
178 opt_help = true;
179 break;
180
181 case OPT_SERVERS: /* --servers or -s */
182 opt_servers = strdup(optarg);
183 break;
184
185 case OPT_HASH:
186 opt_hash = strdup(optarg);
187 break;
188
189 case OPT_USERNAME:
190 opt_username = optarg;
191 break;
192
193 case OPT_PASSWD:
194 opt_passwd = optarg;
195 break;
196
197 case OPT_QUIET:
198 close_stdio();
199 break;
200
201 case '?':
202 /* getopt_long already printed an error message. */
203 exit(EXIT_SUCCESS);
204
205 default:
206 abort();
207 }
208 }
209
210 if (opt_version) {
211 version_command(PROGRAM_NAME);
212 exit(EXIT_SUCCESS);
213 }
214
215 if (opt_help) {
216 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
217 exit(EXIT_SUCCESS);
218 }
219 }