Merge lp:~tangent-org/libmemcached/1.0-build/ Build: jenkins-Libmemcached-187
[m6w6/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 ::close(fd);
254 exit_code= EXIT_FAILURE;
255 }
256
257 ::free(file_buffer_ptr);
258 ::close(fd);
259 optind++;
260 }
261
262 if (opt_verbose)
263 {
264 std::cout << "Calling memcached_free()" << std::endl;
265 }
266
267 memcached_free(memc);
268
269 if (opt_servers)
270 {
271 free(opt_servers);
272 }
273
274 if (opt_hash)
275 {
276 free(opt_hash);
277 }
278
279 return exit_code;
280 }
281
282 static void options_parse(int argc, char *argv[])
283 {
284 memcached_programs_help_st help_options[]=
285 {
286 {0},
287 };
288
289 static struct option long_options[]=
290 {
291 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
292 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
293 {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
294 {(OPTIONSTRING)"udp", no_argument, NULL, OPT_UDP},
295 {(OPTIONSTRING)"buffer", no_argument, NULL, OPT_BUFFER},
296 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
297 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
298 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
299 {(OPTIONSTRING)"flag", required_argument, NULL, OPT_FLAG},
300 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
301 {(OPTIONSTRING)"set", no_argument, NULL, OPT_SET},
302 {(OPTIONSTRING)"add", no_argument, NULL, OPT_ADD},
303 {(OPTIONSTRING)"replace", no_argument, NULL, OPT_REPLACE},
304 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
305 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
306 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
307 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
308 {0, 0, 0, 0},
309 };
310
311 bool opt_version= false;
312 bool opt_help= false;
313 int option_index= 0;
314
315 while (1)
316 {
317 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
318
319 if (option_rv == -1)
320 break;
321
322 switch (option_rv)
323 {
324 case 0:
325 break;
326
327 case OPT_BINARY:
328 opt_binary= true;
329 break;
330
331 case OPT_VERBOSE: /* --verbose or -v */
332 opt_verbose= OPT_VERBOSE;
333 break;
334
335 case OPT_DEBUG: /* --debug or -d */
336 opt_verbose= OPT_DEBUG;
337 break;
338
339 case OPT_VERSION: /* --version or -V */
340 opt_version= true;
341 break;
342
343 case OPT_HELP: /* --help or -h */
344 opt_help= true;
345 break;
346
347 case OPT_SERVERS: /* --servers or -s */
348 opt_servers= strdup(optarg);
349 break;
350
351 case OPT_FLAG: /* --flag */
352 {
353 bool strtol_error;
354 opt_flags= (uint32_t)strtol_wrapper(optarg, 16, &strtol_error);
355 if (strtol_error == true)
356 {
357 fprintf(stderr, "Bad value passed via --flag\n");
358 exit(1);
359 }
360 }
361 break;
362
363 case OPT_EXPIRE: /* --expire */
364 {
365 bool strtol_error;
366 opt_expires= (time_t)strtol_wrapper(optarg, 16, &strtol_error);
367 if (strtol_error == true)
368 {
369 fprintf(stderr, "Bad value passed via --flag\n");
370 exit(1);
371 }
372 }
373 break;
374
375 case OPT_SET:
376 opt_method= OPT_SET;
377 break;
378
379 case OPT_REPLACE:
380 opt_method= OPT_REPLACE;
381 break;
382
383 case OPT_ADD:
384 opt_method= OPT_ADD;
385 break;
386
387 case OPT_HASH:
388 opt_hash= strdup(optarg);
389 break;
390
391 case OPT_USERNAME:
392 opt_username= optarg;
393 break;
394
395 case OPT_PASSWD:
396 opt_passwd= optarg;
397 break;
398
399 case OPT_QUIET:
400 close_stdio();
401 break;
402
403 case OPT_UDP:
404 opt_udp= true;
405 break;
406
407 case OPT_BUFFER:
408 opt_buffer= true;
409 break;
410
411 case '?':
412 /* getopt_long already printed an error message. */
413 exit(1);
414 default:
415 abort();
416 }
417 }
418
419 if (opt_version)
420 {
421 version_command(PROGRAM_NAME);
422 exit(EXIT_SUCCESS);
423 }
424
425 if (opt_help)
426 {
427 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
428 exit(EXIT_SUCCESS);
429 }
430 }