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