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