Allows zero as a value for retry.
[m6w6/libmemcached] / clients / memtouch.cc
1 /* LibMemcached
2 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
4 * All rights reserved.
5 *
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
8 *
9 * Summary:
10 *
11 */
12
13 #include <mem_config.h>
14
15 #include <cerrno>
16 #include <cstdio>
17 #include <cstring>
18 #include <getopt.h>
19 #include <iostream>
20 #include <unistd.h>
21
22 #include <libmemcached-1.0/memcached.h>
23
24 #include "utilities.h"
25
26 #define PROGRAM_NAME "memtouch"
27 #define PROGRAM_DESCRIPTION "Update the expiration value of an already existing value in the server"
28
29
30 /* Prototypes */
31 void options_parse(int argc, char *argv[]);
32
33 static int opt_binary= 0;
34 static int opt_verbose= 0;
35 static char *opt_servers= NULL;
36 static char *opt_hash= NULL;
37 static char *opt_username;
38 static char *opt_passwd;
39
40 time_t expiration= 0;
41
42 int main(int argc, char *argv[])
43 {
44 int return_code= EXIT_SUCCESS;
45
46 options_parse(argc, argv);
47 initialize_sockets();
48
49 if (opt_servers == NULL)
50 {
51 char *temp;
52
53 if ((temp= getenv("MEMCACHED_SERVERS")))
54 {
55 opt_servers= strdup(temp);
56 }
57
58 if (opt_servers == NULL)
59 {
60 std::cerr << "No Servers provided" << std::endl;
61 exit(EXIT_FAILURE);
62 }
63 }
64
65 memcached_server_st* servers= memcached_servers_parse(opt_servers);
66 if (servers == NULL or memcached_server_list_count(servers) == 0)
67 {
68 std::cerr << "Invalid server list provided:" << opt_servers << std::endl;
69 return EXIT_FAILURE;
70 }
71
72 memcached_st *memc= memcached_create(NULL);
73 process_hash_option(memc, opt_hash);
74
75 memcached_server_push(memc, servers);
76 memcached_server_list_free(servers);
77 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
78 (uint64_t)opt_binary);
79
80 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
81 {
82 memcached_free(memc);
83 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
84 return EXIT_FAILURE;
85 }
86
87 if (opt_username)
88 {
89 memcached_return_t ret;
90 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
91 {
92 std::cerr << memcached_last_error_message(memc) << std::endl;
93 memcached_free(memc);
94 return EXIT_FAILURE;
95 }
96 }
97
98 while (optind < argc)
99 {
100 memcached_return_t rc= memcached_touch(memc, argv[optind], strlen(argv[optind]), expiration);
101 if (rc == MEMCACHED_NOTFOUND)
102 {
103 if (opt_verbose)
104 {
105 std::cout << "Could not find key \"" << argv[optind] << "\"" << std::endl;
106 }
107
108 return_code= EXIT_FAILURE;
109 }
110 else if (memcached_failed(rc))
111 {
112 if (opt_verbose)
113 {
114 std::cerr << "Fatal error for key \"" << argv[optind] << "\" :" << memcached_last_error_message(memc) << std::endl;
115 }
116
117 return_code= EXIT_FAILURE;
118 }
119 else // success
120 {
121 if (opt_verbose)
122 {
123 std::cout << "Found key " << argv[optind] << std::endl;
124 }
125 }
126
127 optind++;
128 }
129
130 memcached_free(memc);
131
132 if (opt_servers)
133 {
134 free(opt_servers);
135 }
136
137 if (opt_hash)
138 {
139 free(opt_hash);
140 }
141
142 return return_code;
143 }
144
145
146 void options_parse(int argc, char *argv[])
147 {
148 memcached_programs_help_st help_options[]=
149 {
150 {0},
151 };
152
153 static struct option long_options[]=
154 {
155 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
156 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
157 {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
158 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
159 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
160 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
161 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
162 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
163 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
164 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
165 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
166 {0, 0, 0, 0},
167 };
168
169 bool opt_version= false;
170 bool opt_help= false;
171 int option_index= 0;
172
173 while (1)
174 {
175 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
176 if (option_rv == -1)
177 {
178 break;
179 }
180
181 switch (option_rv)
182 {
183 case 0:
184 break;
185
186 case OPT_BINARY:
187 opt_binary = true;
188 break;
189
190 case OPT_VERBOSE: /* --verbose or -v */
191 opt_verbose = OPT_VERBOSE;
192 break;
193
194 case OPT_DEBUG: /* --debug or -d */
195 opt_verbose = OPT_DEBUG;
196 break;
197
198 case OPT_VERSION: /* --version or -V */
199 opt_version= true;
200 break;
201
202 case OPT_HELP: /* --help or -h */
203 opt_help= true;
204 break;
205
206 case OPT_SERVERS: /* --servers or -s */
207 opt_servers= strdup(optarg);
208 break;
209
210 case OPT_HASH:
211 opt_hash= strdup(optarg);
212 break;
213
214 case OPT_USERNAME:
215 opt_username= optarg;
216 break;
217
218 case OPT_PASSWD:
219 opt_passwd= optarg;
220 break;
221
222 case OPT_EXPIRE:
223 errno= 0;
224 expiration= time_t(strtoul(optarg, (char **)NULL, 10));
225 if (errno != 0)
226 {
227 fprintf(stderr, "Invalid value for --expire: %s\n", optarg);
228 exit(EXIT_FAILURE);
229 }
230 break;
231
232 case OPT_QUIET:
233 close_stdio();
234 break;
235
236 case '?':
237 /* getopt_long already printed an error message. */
238 exit(EXIT_FAILURE);
239
240 default:
241 abort();
242 }
243 }
244
245 if (opt_version)
246 {
247 version_command(PROGRAM_NAME);
248 exit(EXIT_SUCCESS);
249 }
250
251 if (opt_help)
252 {
253 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
254 exit(EXIT_SUCCESS);
255 }
256 }