Fix for lp:1123153 (poor use of strtol).
[awesomized/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 alreasy existing value in the sever"
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 else
58 {
59 std::cerr << "No Servers provided" << std::endl;
60 return EXIT_FAILURE;
61 }
62 }
63
64 memcached_st *memc= memcached_create(NULL);
65 process_hash_option(memc, opt_hash);
66
67 memcached_server_st *servers= memcached_servers_parse(opt_servers);
68
69 memcached_server_push(memc, servers);
70 memcached_server_list_free(servers);
71 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
72 (uint64_t)opt_binary);
73
74 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
75 {
76 memcached_free(memc);
77 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
78 return EXIT_FAILURE;
79 }
80
81 if (opt_username)
82 {
83 memcached_return_t ret;
84 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
85 {
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 {
94 memcached_return_t rc= memcached_touch(memc, argv[optind], strlen(argv[optind]), expiration);
95 if (rc == MEMCACHED_NOTFOUND)
96 {
97 if (opt_verbose)
98 {
99 std::cout << "Could not find key \"" << argv[optind] << "\"" << std::endl;
100 }
101
102 return_code= EXIT_FAILURE;
103 }
104 else if (memcached_failed(rc))
105 {
106 if (opt_verbose)
107 {
108 std::cerr << "Fatal error for key \"" << argv[optind] << "\" :" << memcached_last_error_message(memc) << std::endl;
109 }
110
111 return_code= EXIT_FAILURE;
112 }
113 else // success
114 {
115 if (opt_verbose)
116 {
117 std::cout << "Found key " << argv[optind] << std::endl;
118 }
119 }
120
121 optind++;
122 }
123
124 memcached_free(memc);
125
126 if (opt_servers)
127 {
128 free(opt_servers);
129 }
130
131 if (opt_hash)
132 {
133 free(opt_hash);
134 }
135
136 return return_code;
137 }
138
139
140 void options_parse(int argc, char *argv[])
141 {
142 memcached_programs_help_st help_options[]=
143 {
144 {0},
145 };
146
147 static struct option long_options[]=
148 {
149 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
150 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
151 {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
152 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
153 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
154 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
155 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
156 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
157 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
158 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
159 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
160 {0, 0, 0, 0},
161 };
162
163 bool opt_version= false;
164 bool opt_help= false;
165 int option_index= 0;
166
167 while (1)
168 {
169 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
170 if (option_rv == -1)
171 {
172 break;
173 }
174
175 switch (option_rv)
176 {
177 case 0:
178 break;
179
180 case OPT_BINARY:
181 opt_binary = true;
182 break;
183
184 case OPT_VERBOSE: /* --verbose or -v */
185 opt_verbose = OPT_VERBOSE;
186 break;
187
188 case OPT_DEBUG: /* --debug or -d */
189 opt_verbose = OPT_DEBUG;
190 break;
191
192 case OPT_VERSION: /* --version or -V */
193 opt_version= true;
194 break;
195
196 case OPT_HELP: /* --help or -h */
197 opt_help= true;
198 break;
199
200 case OPT_SERVERS: /* --servers or -s */
201 opt_servers= strdup(optarg);
202 break;
203
204 case OPT_HASH:
205 opt_hash= strdup(optarg);
206 break;
207
208 case OPT_USERNAME:
209 opt_username= optarg;
210 break;
211
212 case OPT_PASSWD:
213 opt_passwd= optarg;
214 break;
215
216 case OPT_EXPIRE:
217 errno= 0;
218 expiration= time_t(strtoul(optarg, (char **)NULL, 10));
219 if (errno != 0)
220 {
221 fprintf(stderr, "Invalid value for --expire: %s\n", optarg);
222 exit(EXIT_FAILURE);
223 }
224 break;
225
226 case OPT_QUIET:
227 close_stdio();
228 break;
229
230 case '?':
231 /* getopt_long already printed an error message. */
232 exit(EXIT_FAILURE);
233
234 default:
235 abort();
236 }
237 }
238
239 if (opt_version)
240 {
241 version_command(PROGRAM_NAME);
242 exit(EXIT_SUCCESS);
243 }
244
245 if (opt_help)
246 {
247 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
248 exit(EXIT_SUCCESS);
249 }
250 }