Branch merge for fixes in reconnect.
[m6w6/libmemcached] / src / memcp.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <unistd.h>
5 #include <getopt.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <fcntl.h>
10 #include <errno.h>
11 #include <strings.h>
12 #include <string.h>
13 #include <assert.h>
14
15 #include <memcached.h>
16 #include "client_options.h"
17 #include "utilities.h"
18
19 #define PROGRAM_NAME "memcp"
20 #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
21
22 /* Prototypes */
23 void options_parse(int argc, char *argv[]);
24
25 static int opt_verbose= 0;
26 static char *opt_servers= NULL;
27 static char *opt_hash= NULL;
28 static int opt_method= OPT_SET;
29 static uint32_t opt_flags= 0;
30 static time_t opt_expires= 0;
31
32 int main(int argc, char *argv[])
33 {
34 memcached_st *memc;
35 memcached_return rc;
36 memcached_server_st *servers;
37
38 options_parse(argc, argv);
39
40 memc= memcached_create(NULL);
41 process_hash_option(memc, opt_hash);
42
43 if (!opt_servers)
44 {
45 char *temp;
46
47 if ((temp= getenv("MEMCACHED_SERVERS")))
48 opt_servers= strdup(temp);
49 else
50 {
51 fprintf(stderr, "No Servers provided\n");
52 exit(1);
53 }
54 }
55
56 if (opt_servers)
57 servers= memcached_servers_parse(opt_servers);
58 else
59 servers= memcached_servers_parse(argv[--argc]);
60
61 memcached_server_push(memc, servers);
62 memcached_server_list_free(servers);
63
64 while (optind < argc)
65 {
66 struct stat sbuf;
67 int fd;
68 char *ptr;
69 ssize_t read_length;
70 char *file_buffer_ptr;
71
72 fd= open(argv[optind], O_RDONLY);
73 if (fd < 0)
74 {
75 fprintf(stderr, "memcp: %s: %s\n", argv[optind], strerror(errno));
76 optind++;
77 continue;
78 }
79
80 (void)fstat(fd, &sbuf);
81
82 ptr= rindex(argv[optind], '/');
83 if (ptr)
84 ptr++;
85 else
86 ptr= argv[optind];
87
88 if (opt_verbose)
89 {
90 static char *opstr[] = { "set", "add", "replace" };
91 printf("op: %s\nsource file: %s\nlength: %zu\n"
92 "key: %s\nflags: %x\nexpires: %llu\n",
93 opstr[opt_method - OPT_SET], argv[optind], (size_t)sbuf.st_size,
94 ptr, opt_flags, (unsigned long long)opt_expires);
95 }
96
97 if ((file_buffer_ptr= (char *)malloc(sizeof(char) * sbuf.st_size)) == NULL)
98 {
99 fprintf(stderr, "malloc: %s\n", strerror(errno));
100 exit(1);
101 }
102
103 if ((read_length= read(fd, file_buffer_ptr, sbuf.st_size)) == -1)
104 {
105 fprintf(stderr, "read: %s\n", strerror(errno));
106 exit(1);
107 }
108 assert(read_length == sbuf.st_size);
109
110 if (opt_method == OPT_ADD)
111 rc= memcached_add(memc, ptr, strlen(ptr),
112 file_buffer_ptr, sbuf.st_size,
113 opt_expires, opt_flags);
114 else if (opt_method == OPT_REPLACE)
115 rc= memcached_replace(memc, ptr, strlen(ptr),
116 file_buffer_ptr, sbuf.st_size,
117 opt_expires, opt_flags);
118 else
119 rc= memcached_set(memc, ptr, strlen(ptr),
120 file_buffer_ptr, sbuf.st_size,
121 opt_expires, opt_flags);
122
123 if (rc != MEMCACHED_SUCCESS)
124 {
125 fprintf(stderr, "memcp: %s: memcache error %s",
126 ptr, memcached_strerror(memc, rc));
127 if (memc->cached_errno)
128 fprintf(stderr, " system error %s", strerror(memc->cached_errno));
129 fprintf(stderr, "\n");
130 }
131
132 free(file_buffer_ptr);
133 close(fd);
134 optind++;
135 }
136
137 memcached_free(memc);
138
139 if (opt_servers)
140 free(opt_servers);
141 if (opt_hash)
142 free(opt_hash);
143
144 return 0;
145 }
146
147 void options_parse(int argc, char *argv[])
148 {
149 int option_index= 0;
150 int option_rv;
151
152 memcached_programs_help_st help_options[]=
153 {
154 {0},
155 };
156
157 static struct option long_options[]=
158 {
159 {"version", no_argument, NULL, OPT_VERSION},
160 {"help", no_argument, NULL, OPT_HELP},
161 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
162 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
163 {"servers", required_argument, NULL, OPT_SERVERS},
164 {"flag", required_argument, NULL, OPT_FLAG},
165 {"expire", required_argument, NULL, OPT_EXPIRE},
166 {"set", no_argument, NULL, OPT_SET},
167 {"add", no_argument, NULL, OPT_ADD},
168 {"replace", no_argument, NULL, OPT_REPLACE},
169 {"hash", required_argument, NULL, OPT_HASH},
170 {0, 0, 0, 0},
171 };
172
173 while (1)
174 {
175 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
176
177 if (option_rv == -1) break;
178
179 switch (option_rv)
180 {
181 case 0:
182 break;
183 case OPT_VERBOSE: /* --verbose or -v */
184 opt_verbose = OPT_VERBOSE;
185 break;
186 case OPT_DEBUG: /* --debug or -d */
187 opt_verbose = OPT_DEBUG;
188 break;
189 case OPT_VERSION: /* --version or -V */
190 version_command(PROGRAM_NAME);
191 break;
192 case OPT_HELP: /* --help or -h */
193 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
194 break;
195 case OPT_SERVERS: /* --servers or -s */
196 opt_servers= strdup(optarg);
197 break;
198 case OPT_FLAG: /* --flag */
199 opt_flags= (uint32_t)strtol(optarg, (char **)NULL, 16);
200 break;
201 case OPT_EXPIRE: /* --expire */
202 opt_expires= (time_t)strtoll(optarg, (char **)NULL, 10);
203 break;
204 case OPT_SET:
205 opt_method= OPT_SET;
206 break;
207 case OPT_REPLACE:
208 opt_method= OPT_REPLACE;
209 break;
210 case OPT_ADD:
211 opt_method= OPT_ADD;
212 case OPT_HASH:
213 opt_hash= strdup(optarg);
214 break;
215 case '?':
216 /* getopt_long already printed an error message. */
217 exit(1);
218 default:
219 abort();
220 }
221 }
222 }