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