Merge in fixes for SASL.
[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 <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 (initialize_sasl(memc, opt_username, opt_passwd) == false)
127 {
128 std::cerr << "Failed to initialize SASL support." << std::endl;
129 memcached_free(memc);
130 return EXIT_FAILURE;
131 }
132
133 while (optind < argc)
134 {
135 struct stat sbuf;
136 int fd;
137 char *ptr;
138 ssize_t read_length;
139 char *file_buffer_ptr;
140
141 fd= open(argv[optind], O_RDONLY);
142 if (fd < 0)
143 {
144 fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno));
145 optind++;
146 continue;
147 }
148
149 (void)fstat(fd, &sbuf);
150
151 ptr= rindex(argv[optind], '/');
152 if (ptr)
153 ptr++;
154 else
155 ptr= argv[optind];
156
157 if (opt_verbose)
158 {
159 static const char *opstr[] = { "set", "add", "replace" };
160 printf("op: %s\nsource file: %s\nlength: %lu\n"
161 "key: %s\nflags: %x\nexpires: %lu\n",
162 opstr[opt_method - OPT_SET], argv[optind], (unsigned long)sbuf.st_size,
163 ptr, opt_flags, (unsigned long)opt_expires);
164 }
165
166 if ((file_buffer_ptr= (char *)malloc(sizeof(char) * (size_t)sbuf.st_size)) == NULL)
167 {
168 fprintf(stderr, "malloc: %s\n", strerror(errno));
169 exit(1);
170 }
171
172 if ((read_length= read(fd, file_buffer_ptr, (size_t)sbuf.st_size)) == -1)
173 {
174 fprintf(stderr, "read: %s\n", strerror(errno));
175 exit(1);
176 }
177
178 if (read_length != sbuf.st_size)
179 {
180 fprintf(stderr, "Failure reading from file\n");
181 exit(1);
182 }
183
184 if (opt_method == OPT_ADD)
185 rc= memcached_add(memc, ptr, strlen(ptr),
186 file_buffer_ptr, (size_t)sbuf.st_size,
187 opt_expires, opt_flags);
188 else if (opt_method == OPT_REPLACE)
189 rc= memcached_replace(memc, ptr, strlen(ptr),
190 file_buffer_ptr, (size_t)sbuf.st_size,
191 opt_expires, opt_flags);
192 else
193 rc= memcached_set(memc, ptr, strlen(ptr),
194 file_buffer_ptr, (size_t)sbuf.st_size,
195 opt_expires, opt_flags);
196
197 if (rc != MEMCACHED_SUCCESS)
198 {
199 fprintf(stderr, "memcp: %s: memcache error %s",
200 ptr, memcached_strerror(memc, rc));
201 if (memcached_last_error_errno(memc))
202 fprintf(stderr, " system error %s", strerror(memcached_last_error_errno(memc)));
203 fprintf(stderr, "\n");
204
205 return_code= -1;
206 }
207
208 free(file_buffer_ptr);
209 close(fd);
210 optind++;
211 }
212
213 memcached_free(memc);
214
215 if (opt_servers)
216 free(opt_servers);
217 if (opt_hash)
218 free(opt_hash);
219 shutdown_sasl();
220
221 return return_code;
222 }
223
224 static void options_parse(int argc, char *argv[])
225 {
226 int option_index= 0;
227 int option_rv;
228
229 memcached_programs_help_st help_options[]=
230 {
231 {0},
232 };
233
234 static struct option long_options[]=
235 {
236 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
237 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
238 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
239 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
240 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
241 {(OPTIONSTRING)"flag", required_argument, NULL, OPT_FLAG},
242 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
243 {(OPTIONSTRING)"set", no_argument, NULL, OPT_SET},
244 {(OPTIONSTRING)"add", no_argument, NULL, OPT_ADD},
245 {(OPTIONSTRING)"replace", no_argument, NULL, OPT_REPLACE},
246 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
247 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
248 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
249 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
250 {0, 0, 0, 0},
251 };
252
253 while (1)
254 {
255 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
256
257 if (option_rv == -1) break;
258
259 switch (option_rv)
260 {
261 case 0:
262 break;
263 case OPT_BINARY:
264 opt_binary = 1;
265 break;
266 case OPT_VERBOSE: /* --verbose or -v */
267 opt_verbose = OPT_VERBOSE;
268 break;
269 case OPT_DEBUG: /* --debug or -d */
270 opt_verbose = OPT_DEBUG;
271 break;
272 case OPT_VERSION: /* --version or -V */
273 version_command(PROGRAM_NAME);
274 break;
275 case OPT_HELP: /* --help or -h */
276 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
277 break;
278 case OPT_SERVERS: /* --servers or -s */
279 opt_servers= strdup(optarg);
280 break;
281 case OPT_FLAG: /* --flag */
282 {
283 bool strtol_error;
284 opt_flags= (uint32_t)strtol_wrapper(optarg, 16, &strtol_error);
285 if (strtol_error == true)
286 {
287 fprintf(stderr, "Bad value passed via --flag\n");
288 exit(1);
289 }
290 break;
291 }
292 case OPT_EXPIRE: /* --expire */
293 {
294 bool strtol_error;
295 opt_expires= (time_t)strtol_wrapper(optarg, 16, &strtol_error);
296 if (strtol_error == true)
297 {
298 fprintf(stderr, "Bad value passed via --flag\n");
299 exit(1);
300 }
301 }
302 case OPT_SET:
303 opt_method= OPT_SET;
304 break;
305 case OPT_REPLACE:
306 opt_method= OPT_REPLACE;
307 break;
308 case OPT_ADD:
309 opt_method= OPT_ADD;
310 break;
311 case OPT_HASH:
312 opt_hash= strdup(optarg);
313 break;
314 case OPT_USERNAME:
315 opt_username= optarg;
316 break;
317 case OPT_PASSWD:
318 opt_passwd= optarg;
319 break;
320 case '?':
321 /* getopt_long already printed an error message. */
322 exit(1);
323 default:
324 abort();
325 }
326 }
327 }