memcp: fix #83
[awesomized/libmemcached] / clients / memcp.cc
1 /* LibMemcached
2 * Copyright (C) 2011-2013 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 0;
72 }
73
74 if (endptr == nptr)
75 {
76 *error= true;
77 return 0;
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
89 if (optind >= argc)
90 {
91 fprintf(stderr, "Expected argument after options\n");
92 exit(EXIT_FAILURE);
93 }
94
95 initialize_sockets();
96
97 memcached_st *memc= memcached_create(NULL);
98
99 if (opt_udp)
100 {
101 if (opt_verbose)
102 {
103 std::cout << "Enabling UDP" << std::endl;
104 }
105
106 if (memcached_failed(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, opt_udp)))
107 {
108 memcached_free(memc);
109 std::cerr << "Could not enable UDP protocol." << std::endl;
110 return EXIT_FAILURE;
111 }
112 }
113
114 if (opt_buffer)
115 {
116 if (opt_verbose)
117 {
118 std::cout << "Enabling MEMCACHED_BEHAVIOR_BUFFER_REQUESTS" << std::endl;
119 }
120
121 if (memcached_failed(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, opt_buffer)))
122 {
123 memcached_free(memc);
124 std::cerr << "Could not enable MEMCACHED_BEHAVIOR_BUFFER_REQUESTS." << std::endl;
125 return EXIT_FAILURE;
126 }
127 }
128
129 process_hash_option(memc, opt_hash);
130
131 if (opt_servers == NULL)
132 {
133 char *temp;
134
135 if ((temp= getenv("MEMCACHED_SERVERS")))
136 {
137 opt_servers= strdup(temp);
138 }
139 #if 0
140 else if (argc >= 1 and argv[--argc])
141 {
142 opt_servers= strdup(argv[argc]);
143 }
144 #endif
145
146 if (opt_servers == NULL)
147 {
148 std::cerr << "No Servers provided" << std::endl;
149 exit(EXIT_FAILURE);
150 }
151 }
152
153 memcached_server_st* servers= memcached_servers_parse(opt_servers);
154 if (servers == NULL or memcached_server_list_count(servers) == 0)
155 {
156 std::cerr << "Invalid server list provided:" << opt_servers << std::endl;
157 return EXIT_FAILURE;
158 }
159
160 memcached_server_push(memc, servers);
161 memcached_server_list_free(servers);
162 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, opt_binary);
163 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
164 {
165 memcached_free(memc);
166 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
167 return EXIT_FAILURE;
168 }
169
170 if (opt_username)
171 {
172 memcached_return_t ret;
173 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
174 {
175 std::cerr << memcached_last_error_message(memc) << std::endl;
176 memcached_free(memc);
177 return EXIT_FAILURE;
178 }
179 }
180
181 int exit_code= EXIT_SUCCESS;
182 while (optind < argc)
183 {
184 int fd= open(argv[optind], O_RDONLY);
185 if (fd < 0)
186 {
187 std::cerr << "memcp " << argv[optind] << " " << strerror(errno) << std::endl;
188 optind++;
189 exit_code= EXIT_FAILURE;
190 continue;
191 }
192
193 struct stat sbuf;
194 if (fstat(fd, &sbuf) == -1)
195 {
196 std::cerr << "memcp " << argv[optind] << " " << strerror(errno) << std::endl;
197 optind++;
198 exit_code= EXIT_FAILURE;
199 continue;
200 }
201
202 char *ptr= rindex(argv[optind], '/');
203 if (ptr)
204 {
205 ptr++;
206 }
207 else
208 {
209 ptr= argv[optind];
210 }
211
212 if (opt_verbose)
213 {
214 static const char *opstr[] = { "set", "add", "replace" };
215 printf("op: %s\nsource file: %s\nlength: %lu\n"
216 "key: %s\nflags: %x\nexpires: %lu\n",
217 opstr[opt_method - OPT_SET], argv[optind], (unsigned long)sbuf.st_size,
218 ptr, opt_flags, (unsigned long)opt_expires);
219 }
220
221 // The file may be empty
222 char *file_buffer_ptr= NULL;
223 if (sbuf.st_size > 0)
224 {
225 if ((file_buffer_ptr= (char *)malloc(sizeof(char) * (size_t)sbuf.st_size)) == NULL)
226 {
227 std::cerr << "Error allocating file buffer(" << strerror(errno) << ")" << std::endl;
228 close(fd);
229 exit(EXIT_FAILURE);
230 }
231
232 ssize_t read_length;
233 if ((read_length= ::read(fd, file_buffer_ptr, (size_t)sbuf.st_size)) == -1)
234 {
235 std::cerr << "Error while reading file " << file_buffer_ptr << " (" << strerror(errno) << ")" << std::endl;
236 close(fd);
237 free(file_buffer_ptr);
238 exit(EXIT_FAILURE);
239 }
240
241 if (read_length != sbuf.st_size)
242 {
243 std::cerr << "Failure while reading file. Read length was not equal to stat() length" << std::endl;
244 close(fd);
245 free(file_buffer_ptr);
246 exit(EXIT_FAILURE);
247 }
248 }
249
250 memcached_return_t rc;
251 if (opt_method == OPT_ADD)
252 {
253 rc= memcached_add(memc, ptr, strlen(ptr),
254 file_buffer_ptr, (size_t)sbuf.st_size,
255 opt_expires, opt_flags);
256 }
257 else if (opt_method == OPT_REPLACE)
258 {
259 rc= memcached_replace(memc, ptr, strlen(ptr),
260 file_buffer_ptr, (size_t)sbuf.st_size,
261 opt_expires, opt_flags);
262 }
263 else
264 {
265 rc= memcached_set(memc, ptr, strlen(ptr),
266 file_buffer_ptr, (size_t)sbuf.st_size,
267 opt_expires, opt_flags);
268 }
269
270 if (memcached_failed(rc))
271 {
272 std::cerr << "Error occrrured during memcached_set(): " << memcached_last_error_message(memc) << std::endl;
273 exit_code= EXIT_FAILURE;
274 }
275
276 ::free(file_buffer_ptr);
277 ::close(fd);
278 optind++;
279 }
280
281 if (opt_verbose)
282 {
283 std::cout << "Calling memcached_free()" << std::endl;
284 }
285
286 memcached_free(memc);
287
288 if (opt_servers)
289 {
290 free(opt_servers);
291 }
292
293 if (opt_hash)
294 {
295 free(opt_hash);
296 }
297
298 return exit_code;
299 }
300
301 static void options_parse(int argc, char *argv[])
302 {
303 memcached_programs_help_st help_options[]=
304 {
305 {0},
306 };
307
308 static struct option long_options[]=
309 {
310 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
311 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
312 {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
313 {(OPTIONSTRING)"udp", no_argument, NULL, OPT_UDP},
314 {(OPTIONSTRING)"buffer", no_argument, NULL, OPT_BUFFER},
315 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
316 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
317 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
318 {(OPTIONSTRING)"flag", required_argument, NULL, OPT_FLAG},
319 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
320 {(OPTIONSTRING)"set", no_argument, NULL, OPT_SET},
321 {(OPTIONSTRING)"add", no_argument, NULL, OPT_ADD},
322 {(OPTIONSTRING)"replace", no_argument, NULL, OPT_REPLACE},
323 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
324 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
325 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
326 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
327 {0, 0, 0, 0},
328 };
329
330 bool opt_version= false;
331 bool opt_help= false;
332 int option_index= 0;
333
334 while (1)
335 {
336 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
337
338 if (option_rv == -1)
339 break;
340
341 switch (option_rv)
342 {
343 case 0:
344 break;
345
346 case OPT_BINARY:
347 opt_binary= true;
348 break;
349
350 case OPT_VERBOSE: /* --verbose or -v */
351 opt_verbose= OPT_VERBOSE;
352 break;
353
354 case OPT_DEBUG: /* --debug or -d */
355 opt_verbose= OPT_DEBUG;
356 break;
357
358 case OPT_VERSION: /* --version or -V */
359 opt_version= true;
360 break;
361
362 case OPT_HELP: /* --help or -h */
363 opt_help= true;
364 break;
365
366 case OPT_SERVERS: /* --servers or -s */
367 opt_servers= strdup(optarg);
368 break;
369
370 case OPT_FLAG: /* --flag */
371 {
372 bool strtol_error;
373 opt_flags= (uint32_t)strtol_wrapper(optarg, 16, &strtol_error);
374 if (strtol_error == true)
375 {
376 fprintf(stderr, "Bad value passed via --flag\n");
377 exit(1);
378 }
379 }
380 break;
381
382 case OPT_EXPIRE: /* --expire */
383 {
384 bool strtol_error;
385 opt_expires= (time_t)strtol_wrapper(optarg, 10, &strtol_error);
386 if (strtol_error == true)
387 {
388 fprintf(stderr, "Bad value passed via --expire\n");
389 exit(1);
390 }
391 }
392 break;
393
394 case OPT_SET:
395 opt_method= OPT_SET;
396 break;
397
398 case OPT_REPLACE:
399 opt_method= OPT_REPLACE;
400 break;
401
402 case OPT_ADD:
403 opt_method= OPT_ADD;
404 break;
405
406 case OPT_HASH:
407 opt_hash= strdup(optarg);
408 break;
409
410 case OPT_USERNAME:
411 opt_username= optarg;
412 break;
413
414 case OPT_PASSWD:
415 opt_passwd= optarg;
416 break;
417
418 case OPT_QUIET:
419 close_stdio();
420 break;
421
422 case OPT_UDP:
423 opt_udp= true;
424 break;
425
426 case OPT_BUFFER:
427 opt_buffer= true;
428 break;
429
430 case '?':
431 /* getopt_long already printed an error message. */
432 exit(1);
433 default:
434 abort();
435 }
436 }
437
438 if (opt_version)
439 {
440 version_command(PROGRAM_NAME);
441 exit(EXIT_SUCCESS);
442 }
443
444 if (opt_help)
445 {
446 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
447 exit(EXIT_SUCCESS);
448 }
449 }