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