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