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