Convert storage over to use vector better.
[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 int opt_verbose= 0;
46 static char *opt_servers= NULL;
47 static char *opt_hash= NULL;
48 static int opt_method= OPT_SET;
49 static uint32_t opt_flags= 0;
50 static time_t opt_expires= 0;
51 static char *opt_username;
52 static char *opt_passwd;
53
54 static long strtol_wrapper(const char *nptr, int base, bool *error)
55 {
56 long val;
57 char *endptr;
58
59 errno= 0; /* To distinguish success/failure after call */
60 val= strtol(nptr, &endptr, base);
61
62 /* Check for various possible errors */
63
64 if ((errno == ERANGE and (val == LONG_MAX or val == LONG_MIN))
65 or (errno != 0 && val == 0))
66 {
67 *error= true;
68 return EXIT_SUCCESS;
69 }
70
71 if (endptr == nptr)
72 {
73 *error= true;
74 return EXIT_SUCCESS;
75 }
76
77 *error= false;
78 return val;
79 }
80
81 int main(int argc, char *argv[])
82 {
83
84 options_parse(argc, argv);
85 initialize_sockets();
86
87 memcached_st *memc= memcached_create(NULL);
88 process_hash_option(memc, opt_hash);
89
90 if (opt_servers == NULL)
91 {
92 char *temp;
93
94 if ((temp= getenv("MEMCACHED_SERVERS")))
95 {
96 opt_servers= strdup(temp);
97 }
98 else
99 {
100 std::cerr << "No Servers provided" << std::endl;
101 exit(EXIT_FAILURE);
102 }
103 }
104
105 memcached_server_st *servers;
106 if (opt_servers)
107 {
108 servers= memcached_servers_parse(opt_servers);
109 }
110 else
111 {
112 servers= memcached_servers_parse(argv[--argc]);
113 }
114
115 memcached_server_push(memc, servers);
116 memcached_server_list_free(servers);
117 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
118 (uint64_t)opt_binary);
119
120 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
121 {
122 memcached_free(memc);
123 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
124 return EXIT_FAILURE;
125 }
126
127 if (opt_username)
128 {
129 memcached_return_t ret;
130 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
131 {
132 std::cerr << memcached_last_error_message(memc) << std::endl;
133 memcached_free(memc);
134 return EXIT_FAILURE;
135 }
136 }
137
138 int exit_code= EXIT_SUCCESS;
139 while (optind < argc)
140 {
141 int fd= open(argv[optind], O_RDONLY);
142 if (fd < 0)
143 {
144 if (opt_verbose)
145 {
146 std::cerr << "memcp " << argv[optind] << " " << strerror(errno) << std::endl;
147 optind++;
148 }
149 exit_code= EXIT_FAILURE;
150 continue;
151 }
152
153 struct stat sbuf;
154 (void)fstat(fd, &sbuf);
155
156 char *ptr= rindex(argv[optind], '/');
157 if (ptr)
158 {
159 ptr++;
160 }
161 else
162 {
163 ptr= argv[optind];
164 }
165
166 if (opt_verbose)
167 {
168 static const char *opstr[] = { "set", "add", "replace" };
169 printf("op: %s\nsource file: %s\nlength: %lu\n"
170 "key: %s\nflags: %x\nexpires: %lu\n",
171 opstr[opt_method - OPT_SET], argv[optind], (unsigned long)sbuf.st_size,
172 ptr, opt_flags, (unsigned long)opt_expires);
173 }
174
175 char *file_buffer_ptr;
176 if ((file_buffer_ptr= (char *)malloc(sizeof(char) * (size_t)sbuf.st_size)) == NULL)
177 {
178 std::cerr << "Error allocating file buffer(" << strerror(errno) << ")" << std::endl;
179 exit(EXIT_FAILURE);
180 }
181
182 ssize_t read_length;
183 if ((read_length= read(fd, file_buffer_ptr, (size_t)sbuf.st_size)) == -1)
184 {
185 std::cerr << "Error while reading file " << file_buffer_ptr << " (" << strerror(errno) << ")" << std::endl;
186 exit(EXIT_FAILURE);
187 }
188
189 if (read_length != sbuf.st_size)
190 {
191 std::cerr << "Failure while reading file. Read length was not equal to stat() length" << std::endl;
192 exit(EXIT_FAILURE);
193 }
194
195 memcached_return_t rc;
196 if (opt_method == OPT_ADD)
197 {
198 rc= memcached_add(memc, ptr, strlen(ptr),
199 file_buffer_ptr, (size_t)sbuf.st_size,
200 opt_expires, opt_flags);
201 }
202 else if (opt_method == OPT_REPLACE)
203 {
204 rc= memcached_replace(memc, ptr, strlen(ptr),
205 file_buffer_ptr, (size_t)sbuf.st_size,
206 opt_expires, opt_flags);
207 }
208 else
209 {
210 rc= memcached_set(memc, ptr, strlen(ptr),
211 file_buffer_ptr, (size_t)sbuf.st_size,
212 opt_expires, opt_flags);
213 }
214
215 if (rc != MEMCACHED_SUCCESS)
216 {
217 std::cerr << "Error occrrured during operation: " << memcached_last_error_message(memc) << std::endl;
218 exit_code= EXIT_FAILURE;
219 }
220
221 free(file_buffer_ptr);
222 close(fd);
223 optind++;
224 }
225
226 memcached_free(memc);
227
228 if (opt_servers)
229 {
230 free(opt_servers);
231 }
232
233 if (opt_hash)
234 {
235 free(opt_hash);
236 }
237
238 return exit_code;
239 }
240
241 static void options_parse(int argc, char *argv[])
242 {
243 memcached_programs_help_st help_options[]=
244 {
245 {0},
246 };
247
248 static struct option long_options[]=
249 {
250 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
251 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
252 {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
253 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
254 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
255 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
256 {(OPTIONSTRING)"flag", required_argument, NULL, OPT_FLAG},
257 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
258 {(OPTIONSTRING)"set", no_argument, NULL, OPT_SET},
259 {(OPTIONSTRING)"add", no_argument, NULL, OPT_ADD},
260 {(OPTIONSTRING)"replace", no_argument, NULL, OPT_REPLACE},
261 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
262 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
263 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
264 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
265 {0, 0, 0, 0},
266 };
267
268 bool opt_version= false;
269 bool opt_help= false;
270 int option_index= 0;
271
272 while (1)
273 {
274 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
275
276 if (option_rv == -1)
277 break;
278
279 switch (option_rv)
280 {
281 case 0:
282 break;
283
284 case OPT_BINARY:
285 opt_binary= true;
286 break;
287
288 case OPT_VERBOSE: /* --verbose or -v */
289 opt_verbose= OPT_VERBOSE;
290 break;
291
292 case OPT_DEBUG: /* --debug or -d */
293 opt_verbose= OPT_DEBUG;
294 break;
295
296 case OPT_VERSION: /* --version or -V */
297 opt_version= true;
298 break;
299
300 case OPT_HELP: /* --help or -h */
301 opt_help= true;
302 break;
303
304 case OPT_SERVERS: /* --servers or -s */
305 opt_servers= strdup(optarg);
306 break;
307
308 case OPT_FLAG: /* --flag */
309 {
310 bool strtol_error;
311 opt_flags= (uint32_t)strtol_wrapper(optarg, 16, &strtol_error);
312 if (strtol_error == true)
313 {
314 fprintf(stderr, "Bad value passed via --flag\n");
315 exit(1);
316 }
317 }
318 break;
319
320 case OPT_EXPIRE: /* --expire */
321 {
322 bool strtol_error;
323 opt_expires= (time_t)strtol_wrapper(optarg, 16, &strtol_error);
324 if (strtol_error == true)
325 {
326 fprintf(stderr, "Bad value passed via --flag\n");
327 exit(1);
328 }
329 }
330 break;
331
332 case OPT_SET:
333 opt_method= OPT_SET;
334 break;
335
336 case OPT_REPLACE:
337 opt_method= OPT_REPLACE;
338 break;
339
340 case OPT_ADD:
341 opt_method= OPT_ADD;
342 break;
343
344 case OPT_HASH:
345 opt_hash= strdup(optarg);
346 break;
347
348 case OPT_USERNAME:
349 opt_username= optarg;
350 break;
351
352 case OPT_PASSWD:
353 opt_passwd= optarg;
354 break;
355
356 case OPT_QUIET:
357 close_stdio();
358 break;
359
360 case '?':
361 /* getopt_long already printed an error message. */
362 exit(1);
363 default:
364 abort();
365 }
366 }
367
368 if (opt_version)
369 {
370 version_command(PROGRAM_NAME);
371 exit(EXIT_SUCCESS);
372 }
373
374 if (opt_help)
375 {
376 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
377 exit(EXIT_SUCCESS);
378 }
379 }