59bd7478bb780a89d447d9c8949b1381c7342baa
[m6w6/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 exit(EXIT_FAILURE);
211 }
212
213 ssize_t read_length;
214 if ((read_length= ::read(fd, file_buffer_ptr, (size_t)sbuf.st_size)) == -1)
215 {
216 std::cerr << "Error while reading file " << file_buffer_ptr << " (" << strerror(errno) << ")" << std::endl;
217 exit(EXIT_FAILURE);
218 }
219
220 if (read_length != sbuf.st_size)
221 {
222 std::cerr << "Failure while reading file. Read length was not equal to stat() length" << std::endl;
223 exit(EXIT_FAILURE);
224 }
225
226 memcached_return_t rc;
227 if (opt_method == OPT_ADD)
228 {
229 rc= memcached_add(memc, ptr, strlen(ptr),
230 file_buffer_ptr, (size_t)sbuf.st_size,
231 opt_expires, opt_flags);
232 }
233 else if (opt_method == OPT_REPLACE)
234 {
235 rc= memcached_replace(memc, ptr, strlen(ptr),
236 file_buffer_ptr, (size_t)sbuf.st_size,
237 opt_expires, opt_flags);
238 }
239 else
240 {
241 rc= memcached_set(memc, ptr, strlen(ptr),
242 file_buffer_ptr, (size_t)sbuf.st_size,
243 opt_expires, opt_flags);
244 }
245
246 if (memcached_failed(rc))
247 {
248 std::cerr << "Error occrrured during memcached_set(): " << memcached_last_error_message(memc) << std::endl;
249 exit_code= EXIT_FAILURE;
250 }
251
252 ::free(file_buffer_ptr);
253 ::close(fd);
254 optind++;
255 }
256
257 if (opt_verbose)
258 {
259 std::cout << "Calling memcached_free()" << std::endl;
260 }
261
262 memcached_free(memc);
263
264 if (opt_servers)
265 {
266 free(opt_servers);
267 }
268
269 if (opt_hash)
270 {
271 free(opt_hash);
272 }
273
274 return exit_code;
275 }
276
277 static void options_parse(int argc, char *argv[])
278 {
279 memcached_programs_help_st help_options[]=
280 {
281 {0},
282 };
283
284 static struct option long_options[]=
285 {
286 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
287 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
288 {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
289 {(OPTIONSTRING)"udp", no_argument, NULL, OPT_UDP},
290 {(OPTIONSTRING)"buffer", no_argument, NULL, OPT_BUFFER},
291 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
292 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
293 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
294 {(OPTIONSTRING)"flag", required_argument, NULL, OPT_FLAG},
295 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
296 {(OPTIONSTRING)"set", no_argument, NULL, OPT_SET},
297 {(OPTIONSTRING)"add", no_argument, NULL, OPT_ADD},
298 {(OPTIONSTRING)"replace", no_argument, NULL, OPT_REPLACE},
299 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
300 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
301 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
302 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
303 {0, 0, 0, 0},
304 };
305
306 bool opt_version= false;
307 bool opt_help= false;
308 int option_index= 0;
309
310 while (1)
311 {
312 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
313
314 if (option_rv == -1)
315 break;
316
317 switch (option_rv)
318 {
319 case 0:
320 break;
321
322 case OPT_BINARY:
323 opt_binary= true;
324 break;
325
326 case OPT_VERBOSE: /* --verbose or -v */
327 opt_verbose= OPT_VERBOSE;
328 break;
329
330 case OPT_DEBUG: /* --debug or -d */
331 opt_verbose= OPT_DEBUG;
332 break;
333
334 case OPT_VERSION: /* --version or -V */
335 opt_version= true;
336 break;
337
338 case OPT_HELP: /* --help or -h */
339 opt_help= true;
340 break;
341
342 case OPT_SERVERS: /* --servers or -s */
343 opt_servers= strdup(optarg);
344 break;
345
346 case OPT_FLAG: /* --flag */
347 {
348 bool strtol_error;
349 opt_flags= (uint32_t)strtol_wrapper(optarg, 16, &strtol_error);
350 if (strtol_error == true)
351 {
352 fprintf(stderr, "Bad value passed via --flag\n");
353 exit(1);
354 }
355 }
356 break;
357
358 case OPT_EXPIRE: /* --expire */
359 {
360 bool strtol_error;
361 opt_expires= (time_t)strtol_wrapper(optarg, 16, &strtol_error);
362 if (strtol_error == true)
363 {
364 fprintf(stderr, "Bad value passed via --flag\n");
365 exit(1);
366 }
367 }
368 break;
369
370 case OPT_SET:
371 opt_method= OPT_SET;
372 break;
373
374 case OPT_REPLACE:
375 opt_method= OPT_REPLACE;
376 break;
377
378 case OPT_ADD:
379 opt_method= OPT_ADD;
380 break;
381
382 case OPT_HASH:
383 opt_hash= strdup(optarg);
384 break;
385
386 case OPT_USERNAME:
387 opt_username= optarg;
388 break;
389
390 case OPT_PASSWD:
391 opt_passwd= optarg;
392 break;
393
394 case OPT_QUIET:
395 close_stdio();
396 break;
397
398 case OPT_UDP:
399 opt_udp= true;
400 break;
401
402 case OPT_BUFFER:
403 opt_buffer= true;
404 break;
405
406 case '?':
407 /* getopt_long already printed an error message. */
408 exit(1);
409 default:
410 abort();
411 }
412 }
413
414 if (opt_version)
415 {
416 version_command(PROGRAM_NAME);
417 exit(EXIT_SUCCESS);
418 }
419
420 if (opt_help)
421 {
422 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
423 exit(EXIT_SUCCESS);
424 }
425 }