Merge in updates for sasl.
[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 <iostream>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <inttypes.h>
18 #include <unistd.h>
19 #include <getopt.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <strings.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <stdlib.h>
29 #include <limits.h>
30
31
32 #include <libmemcached/memcached.h>
33
34 #include "client_options.h"
35 #include "utilities.h"
36
37 #define PROGRAM_NAME "memcp"
38 #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
39
40 /* Prototypes */
41 static void options_parse(int argc, char *argv[]);
42
43 static int opt_binary=0;
44 static int opt_verbose= 0;
45 static char *opt_servers= NULL;
46 static char *opt_hash= NULL;
47 static int opt_method= OPT_SET;
48 static uint32_t opt_flags= 0;
49 static time_t opt_expires= 0;
50 static char *opt_username;
51 static char *opt_passwd;
52
53 static long strtol_wrapper(const char *nptr, int base, bool *error)
54 {
55 long val;
56 char *endptr;
57
58 errno= 0; /* To distinguish success/failure after call */
59 val= strtol(nptr, &endptr, base);
60
61 /* Check for various possible errors */
62
63 if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
64 || (errno != 0 && val == 0))
65 {
66 *error= true;
67 return EXIT_SUCCESS;
68 }
69
70 if (endptr == nptr)
71 {
72 *error= true;
73 return EXIT_SUCCESS;
74 }
75
76 *error= false;
77 return val;
78 }
79
80 int main(int argc, char *argv[])
81 {
82 memcached_st *memc;
83 memcached_return_t rc;
84 memcached_server_st *servers;
85
86 int return_code= 0;
87
88 options_parse(argc, argv);
89 initialize_sockets();
90
91 memc= memcached_create(NULL);
92 process_hash_option(memc, opt_hash);
93
94 if (!opt_servers)
95 {
96 char *temp;
97
98 if ((temp= getenv("MEMCACHED_SERVERS")))
99 {
100 opt_servers= strdup(temp);
101 }
102 else
103 {
104 fprintf(stderr, "No Servers provided\n");
105 exit(1);
106 }
107 }
108
109 if (opt_servers)
110 servers= memcached_servers_parse(opt_servers);
111 else
112 servers= memcached_servers_parse(argv[--argc]);
113
114 memcached_server_push(memc, servers);
115 memcached_server_list_free(servers);
116 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
117 (uint64_t)opt_binary);
118
119 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
120 {
121 memcached_free(memc);
122 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
123 return EXIT_FAILURE;
124 }
125
126 if (opt_username)
127 {
128 memcached_return_t ret;
129 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
130 {
131 std::cerr << memcached_last_error_message(memc) << std::endl;
132 memcached_free(memc);
133 return EXIT_FAILURE;
134 }
135 }
136
137 while (optind < argc)
138 {
139 struct stat sbuf;
140 int fd;
141 char *ptr;
142 ssize_t read_length;
143 char *file_buffer_ptr;
144
145 fd= open(argv[optind], O_RDONLY);
146 if (fd < 0)
147 {
148 fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno));
149 optind++;
150 continue;
151 }
152
153 (void)fstat(fd, &sbuf);
154
155 ptr= rindex(argv[optind], '/');
156 if (ptr)
157 ptr++;
158 else
159 ptr= argv[optind];
160
161 if (opt_verbose)
162 {
163 static const char *opstr[] = { "set", "add", "replace" };
164 printf("op: %s\nsource file: %s\nlength: %lu\n"
165 "key: %s\nflags: %x\nexpires: %lu\n",
166 opstr[opt_method - OPT_SET], argv[optind], (unsigned long)sbuf.st_size,
167 ptr, opt_flags, (unsigned long)opt_expires);
168 }
169
170 if ((file_buffer_ptr= (char *)malloc(sizeof(char) * (size_t)sbuf.st_size)) == NULL)
171 {
172 fprintf(stderr, "malloc: %s\n", strerror(errno));
173 exit(1);
174 }
175
176 if ((read_length= read(fd, file_buffer_ptr, (size_t)sbuf.st_size)) == -1)
177 {
178 fprintf(stderr, "read: %s\n", strerror(errno));
179 exit(1);
180 }
181
182 if (read_length != sbuf.st_size)
183 {
184 fprintf(stderr, "Failure reading from file\n");
185 exit(1);
186 }
187
188 if (opt_method == OPT_ADD)
189 rc= memcached_add(memc, ptr, strlen(ptr),
190 file_buffer_ptr, (size_t)sbuf.st_size,
191 opt_expires, opt_flags);
192 else if (opt_method == OPT_REPLACE)
193 rc= memcached_replace(memc, ptr, strlen(ptr),
194 file_buffer_ptr, (size_t)sbuf.st_size,
195 opt_expires, opt_flags);
196 else
197 rc= memcached_set(memc, ptr, strlen(ptr),
198 file_buffer_ptr, (size_t)sbuf.st_size,
199 opt_expires, opt_flags);
200
201 if (rc != MEMCACHED_SUCCESS)
202 {
203 fprintf(stderr, "memcp: %s: memcache error %s",
204 ptr, memcached_strerror(memc, rc));
205 if (memcached_last_error_errno(memc))
206 fprintf(stderr, " system error %s", strerror(memcached_last_error_errno(memc)));
207 fprintf(stderr, "\n");
208
209 return_code= -1;
210 }
211
212 free(file_buffer_ptr);
213 close(fd);
214 optind++;
215 }
216
217 memcached_free(memc);
218
219 if (opt_servers)
220 free(opt_servers);
221 if (opt_hash)
222 free(opt_hash);
223
224 return return_code;
225 }
226
227 static void options_parse(int argc, char *argv[])
228 {
229 int option_index= 0;
230 int option_rv;
231
232 memcached_programs_help_st help_options[]=
233 {
234 {0},
235 };
236
237 static struct option long_options[]=
238 {
239 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
240 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
241 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
242 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
243 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
244 {(OPTIONSTRING)"flag", required_argument, NULL, OPT_FLAG},
245 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
246 {(OPTIONSTRING)"set", no_argument, NULL, OPT_SET},
247 {(OPTIONSTRING)"add", no_argument, NULL, OPT_ADD},
248 {(OPTIONSTRING)"replace", no_argument, NULL, OPT_REPLACE},
249 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
250 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
251 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
252 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
253 {0, 0, 0, 0},
254 };
255
256 while (1)
257 {
258 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
259
260 if (option_rv == -1) break;
261
262 switch (option_rv)
263 {
264 case 0:
265 break;
266 case OPT_BINARY:
267 opt_binary = 1;
268 break;
269 case OPT_VERBOSE: /* --verbose or -v */
270 opt_verbose = OPT_VERBOSE;
271 break;
272 case OPT_DEBUG: /* --debug or -d */
273 opt_verbose = OPT_DEBUG;
274 break;
275 case OPT_VERSION: /* --version or -V */
276 version_command(PROGRAM_NAME);
277 break;
278 case OPT_HELP: /* --help or -h */
279 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
280 break;
281 case OPT_SERVERS: /* --servers or -s */
282 opt_servers= strdup(optarg);
283 break;
284 case OPT_FLAG: /* --flag */
285 {
286 bool strtol_error;
287 opt_flags= (uint32_t)strtol_wrapper(optarg, 16, &strtol_error);
288 if (strtol_error == true)
289 {
290 fprintf(stderr, "Bad value passed via --flag\n");
291 exit(1);
292 }
293 break;
294 }
295 case OPT_EXPIRE: /* --expire */
296 {
297 bool strtol_error;
298 opt_expires= (time_t)strtol_wrapper(optarg, 16, &strtol_error);
299 if (strtol_error == true)
300 {
301 fprintf(stderr, "Bad value passed via --flag\n");
302 exit(1);
303 }
304 }
305 case OPT_SET:
306 opt_method= OPT_SET;
307 break;
308 case OPT_REPLACE:
309 opt_method= OPT_REPLACE;
310 break;
311 case OPT_ADD:
312 opt_method= OPT_ADD;
313 break;
314 case OPT_HASH:
315 opt_hash= strdup(optarg);
316 break;
317 case OPT_USERNAME:
318 opt_username= optarg;
319 break;
320 case OPT_PASSWD:
321 opt_passwd= optarg;
322 break;
323 case '?':
324 /* getopt_long already printed an error message. */
325 exit(1);
326 default:
327 abort();
328 }
329 }
330 }