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