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