src/bin: apply clang-format
[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: break;
162
163 case OPT_BINARY: opt_binary = true; break;
164
165 case OPT_VERBOSE: /* --verbose or -v */ opt_verbose = OPT_VERBOSE; break;
166
167 case OPT_DEBUG: /* --debug or -d */ opt_verbose = OPT_DEBUG; break;
168
169 case OPT_VERSION: /* --version or -V */ opt_version = true; break;
170
171 case OPT_HELP: /* --help or -h */ opt_help = true; break;
172
173 case OPT_SERVERS: /* --servers or -s */ opt_servers = strdup(optarg); break;
174
175 case OPT_HASH: opt_hash = strdup(optarg); break;
176
177 case OPT_USERNAME: opt_username = optarg; break;
178
179 case OPT_PASSWD: opt_passwd = optarg; break;
180
181 case OPT_EXPIRE:
182 errno = 0;
183 expiration = time_t(strtoul(optarg, (char **) NULL, 10));
184 if (errno != 0) {
185 fprintf(stderr, "Invalid value for --expire: %s\n", optarg);
186 exit(EXIT_FAILURE);
187 }
188 break;
189
190 case OPT_QUIET: close_stdio(); break;
191
192 case '?':
193 /* getopt_long already printed an error message. */
194 exit(EXIT_FAILURE);
195
196 default: abort();
197 }
198 }
199
200 if (opt_version) {
201 version_command(PROGRAM_NAME);
202 exit(EXIT_SUCCESS);
203 }
204
205 if (opt_help) {
206 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
207 exit(EXIT_SUCCESS);
208 }
209 }