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