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