When returning CLIENT_ERROR do not close the socket connection (i.e. it should not...
[awesomized/libmemcached] / clients / memcp.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 <climits>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <cstdlib>
20 #include <cstring>
21 #include <fcntl.h>
22 #include <getopt.h>
23 #include <iostream>
24 #ifdef HAVE_STRINGS_H
25 #include <strings.h>
26 #endif
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <sys/types.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32
33
34 #include <libmemcached-1.0/memcached.h>
35
36 #include "client_options.h"
37 #include "utilities.h"
38
39 #define PROGRAM_NAME "memcp"
40 #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
41
42 /* Prototypes */
43 static void options_parse(int argc, char *argv[]);
44
45 static bool opt_binary= false;
46 static bool opt_udp= false;
47 static bool opt_buffer= false;
48 static int opt_verbose= 0;
49 static char *opt_servers= NULL;
50 static char *opt_hash= NULL;
51 static int opt_method= OPT_SET;
52 static uint32_t opt_flags= 0;
53 static time_t opt_expires= 0;
54 static char *opt_username;
55 static char *opt_passwd;
56
57 static long strtol_wrapper(const char *nptr, int base, bool *error)
58 {
59 long val;
60 char *endptr;
61
62 errno= 0; /* To distinguish success/failure after call */
63 val= strtol(nptr, &endptr, base);
64
65 /* Check for various possible errors */
66
67 if ((errno == ERANGE and (val == LONG_MAX or val == LONG_MIN))
68 or (errno != 0 && val == 0))
69 {
70 *error= true;
71 return EXIT_SUCCESS;
72 }
73
74 if (endptr == nptr)
75 {
76 *error= true;
77 return EXIT_SUCCESS;
78 }
79
80 *error= false;
81 return val;
82 }
83
84 int main(int argc, char *argv[])
85 {
86
87 options_parse(argc, argv);
88 initialize_sockets();
89
90 memcached_st *memc= memcached_create(NULL);
91
92 if (opt_udp)
93 {
94 if (opt_verbose)
95 {
96 std::cout << "Enabling UDP" << std::endl;
97 }
98
99 if (memcached_failed(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, opt_udp)))
100 {
101 memcached_free(memc);
102 std::cerr << "Could not enable UDP protocol." << std::endl;
103 return EXIT_FAILURE;
104 }
105 }
106
107 if (opt_buffer)
108 {
109 if (opt_verbose)
110 {
111 std::cout << "Enabling MEMCACHED_BEHAVIOR_BUFFER_REQUESTS" << std::endl;
112 }
113
114 if (memcached_failed(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, opt_buffer)))
115 {
116 memcached_free(memc);
117 std::cerr << "Could not enable MEMCACHED_BEHAVIOR_BUFFER_REQUESTS." << std::endl;
118 return EXIT_FAILURE;
119 }
120 }
121
122 process_hash_option(memc, opt_hash);
123
124 if (opt_servers == NULL)
125 {
126 char *temp;
127
128 if ((temp= getenv("MEMCACHED_SERVERS")))
129 {
130 opt_servers= strdup(temp);
131 }
132 else
133 {
134 std::cerr << "No Servers provided" << std::endl;
135 exit(EXIT_FAILURE);
136 }
137 }
138
139 memcached_server_st *servers;
140 if (opt_servers)
141 {
142 servers= memcached_servers_parse(opt_servers);
143 }
144 else
145 {
146 servers= memcached_servers_parse(argv[--argc]);
147 }
148
149 memcached_server_push(memc, servers);
150 memcached_server_list_free(servers);
151 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, opt_binary);
152 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
153 {
154 memcached_free(memc);
155 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
156 return EXIT_FAILURE;
157 }
158
159 if (opt_username)
160 {
161 memcached_return_t ret;
162 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
163 {
164 std::cerr << memcached_last_error_message(memc) << std::endl;
165 memcached_free(memc);
166 return EXIT_FAILURE;
167 }
168 }
169
170 int exit_code= EXIT_SUCCESS;
171 while (optind < argc)
172 {
173 int fd= open(argv[optind], O_RDONLY);
174 if (fd < 0)
175 {
176 if (opt_verbose)
177 {
178 std::cerr << "memcp " << argv[optind] << " " << strerror(errno) << std::endl;
179 optind++;
180 }
181 exit_code= EXIT_FAILURE;
182 continue;
183 }
184
185 struct stat sbuf;
186 (void)fstat(fd, &sbuf);
187
188 char *ptr= rindex(argv[optind], '/');
189 if (ptr)
190 {
191 ptr++;
192 }
193 else
194 {
195 ptr= argv[optind];
196 }
197
198 if (opt_verbose)
199 {
200 static const char *opstr[] = { "set", "add", "replace" };
201 printf("op: %s\nsource file: %s\nlength: %lu\n"
202 "key: %s\nflags: %x\nexpires: %lu\n",
203 opstr[opt_method - OPT_SET], argv[optind], (unsigned long)sbuf.st_size,
204 ptr, opt_flags, (unsigned long)opt_expires);
205 }
206
207 char *file_buffer_ptr;
208 if ((file_buffer_ptr= (char *)malloc(sizeof(char) * (size_t)sbuf.st_size)) == NULL)
209 {
210 std::cerr << "Error allocating file buffer(" << strerror(errno) << ")" << std::endl;
211 close(fd);
212 exit(EXIT_FAILURE);
213 }
214
215 ssize_t read_length;
216 if ((read_length= ::read(fd, file_buffer_ptr, (size_t)sbuf.st_size)) == -1)
217 {
218 std::cerr << "Error while reading file " << file_buffer_ptr << " (" << strerror(errno) << ")" << std::endl;
219 close(fd);
220 exit(EXIT_FAILURE);
221 }
222
223 if (read_length != sbuf.st_size)
224 {
225 std::cerr << "Failure while reading file. Read length was not equal to stat() length" << std::endl;
226 close(fd);
227 exit(EXIT_FAILURE);
228 }
229
230 memcached_return_t rc;
231 if (opt_method == OPT_ADD)
232 {
233 rc= memcached_add(memc, ptr, strlen(ptr),
234 file_buffer_ptr, (size_t)sbuf.st_size,
235 opt_expires, opt_flags);
236 }
237 else if (opt_method == OPT_REPLACE)
238 {
239 rc= memcached_replace(memc, ptr, strlen(ptr),
240 file_buffer_ptr, (size_t)sbuf.st_size,
241 opt_expires, opt_flags);
242 }
243 else
244 {
245 rc= memcached_set(memc, ptr, strlen(ptr),
246 file_buffer_ptr, (size_t)sbuf.st_size,
247 opt_expires, opt_flags);
248 }
249
250 if (memcached_failed(rc))
251 {
252 std::cerr << "Error occrrured during memcached_set(): " << memcached_last_error_message(memc) << std::endl;
253 exit_code= EXIT_FAILURE;
254 }
255
256 ::free(file_buffer_ptr);
257 ::close(fd);
258 optind++;
259 }
260
261 if (opt_verbose)
262 {
263 std::cout << "Calling memcached_free()" << std::endl;
264 }
265
266 memcached_free(memc);
267
268 if (opt_servers)
269 {
270 free(opt_servers);
271 }
272
273 if (opt_hash)
274 {
275 free(opt_hash);
276 }
277
278 return exit_code;
279 }
280
281 static void options_parse(int argc, char *argv[])
282 {
283 memcached_programs_help_st help_options[]=
284 {
285 {0},
286 };
287
288 static struct option long_options[]=
289 {
290 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
291 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
292 {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
293 {(OPTIONSTRING)"udp", no_argument, NULL, OPT_UDP},
294 {(OPTIONSTRING)"buffer", no_argument, NULL, OPT_BUFFER},
295 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
296 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
297 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
298 {(OPTIONSTRING)"flag", required_argument, NULL, OPT_FLAG},
299 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
300 {(OPTIONSTRING)"set", no_argument, NULL, OPT_SET},
301 {(OPTIONSTRING)"add", no_argument, NULL, OPT_ADD},
302 {(OPTIONSTRING)"replace", no_argument, NULL, OPT_REPLACE},
303 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
304 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
305 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
306 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
307 {0, 0, 0, 0},
308 };
309
310 bool opt_version= false;
311 bool opt_help= false;
312 int option_index= 0;
313
314 while (1)
315 {
316 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
317
318 if (option_rv == -1)
319 break;
320
321 switch (option_rv)
322 {
323 case 0:
324 break;
325
326 case OPT_BINARY:
327 opt_binary= true;
328 break;
329
330 case OPT_VERBOSE: /* --verbose or -v */
331 opt_verbose= OPT_VERBOSE;
332 break;
333
334 case OPT_DEBUG: /* --debug or -d */
335 opt_verbose= OPT_DEBUG;
336 break;
337
338 case OPT_VERSION: /* --version or -V */
339 opt_version= true;
340 break;
341
342 case OPT_HELP: /* --help or -h */
343 opt_help= true;
344 break;
345
346 case OPT_SERVERS: /* --servers or -s */
347 opt_servers= strdup(optarg);
348 break;
349
350 case OPT_FLAG: /* --flag */
351 {
352 bool strtol_error;
353 opt_flags= (uint32_t)strtol_wrapper(optarg, 16, &strtol_error);
354 if (strtol_error == true)
355 {
356 fprintf(stderr, "Bad value passed via --flag\n");
357 exit(1);
358 }
359 }
360 break;
361
362 case OPT_EXPIRE: /* --expire */
363 {
364 bool strtol_error;
365 opt_expires= (time_t)strtol_wrapper(optarg, 16, &strtol_error);
366 if (strtol_error == true)
367 {
368 fprintf(stderr, "Bad value passed via --flag\n");
369 exit(1);
370 }
371 }
372 break;
373
374 case OPT_SET:
375 opt_method= OPT_SET;
376 break;
377
378 case OPT_REPLACE:
379 opt_method= OPT_REPLACE;
380 break;
381
382 case OPT_ADD:
383 opt_method= OPT_ADD;
384 break;
385
386 case OPT_HASH:
387 opt_hash= strdup(optarg);
388 break;
389
390 case OPT_USERNAME:
391 opt_username= optarg;
392 break;
393
394 case OPT_PASSWD:
395 opt_passwd= optarg;
396 break;
397
398 case OPT_QUIET:
399 close_stdio();
400 break;
401
402 case OPT_UDP:
403 opt_udp= true;
404 break;
405
406 case OPT_BUFFER:
407 opt_buffer= true;
408 break;
409
410 case '?':
411 /* getopt_long already printed an error message. */
412 exit(1);
413 default:
414 abort();
415 }
416 }
417
418 if (opt_version)
419 {
420 version_command(PROGRAM_NAME);
421 exit(EXIT_SUCCESS);
422 }
423
424 if (opt_help)
425 {
426 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
427 exit(EXIT_SUCCESS);
428 }
429 }