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