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