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