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