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