Fixed semi colon bug in memcp.
[awesomized/libmemcached] / clients / 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
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_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
109 if (read_length != sbuf.st_size)
110 {
111 fprintf(stderr, "Failure reading from file\n");
112 exit(1);
113 }
114
115 if (opt_method == OPT_ADD)
116 rc= memcached_add(memc, ptr, strlen(ptr),
117 file_buffer_ptr, sbuf.st_size,
118 opt_expires, opt_flags);
119 else if (opt_method == OPT_REPLACE)
120 rc= memcached_replace(memc, ptr, strlen(ptr),
121 file_buffer_ptr, sbuf.st_size,
122 opt_expires, opt_flags);
123 else
124 rc= memcached_set(memc, ptr, strlen(ptr),
125 file_buffer_ptr, sbuf.st_size,
126 opt_expires, opt_flags);
127
128 if (rc != MEMCACHED_SUCCESS)
129 {
130 fprintf(stderr, "memcp: %s: memcache error %s",
131 ptr, memcached_strerror(memc, rc));
132 if (memc->cached_errno)
133 fprintf(stderr, " system error %s", strerror(memc->cached_errno));
134 fprintf(stderr, "\n");
135 }
136
137 free(file_buffer_ptr);
138 close(fd);
139 optind++;
140 }
141
142 memcached_free(memc);
143
144 if (opt_servers)
145 free(opt_servers);
146 if (opt_hash)
147 free(opt_hash);
148
149 return 0;
150 }
151
152 void options_parse(int argc, char *argv[])
153 {
154 int option_index= 0;
155 int option_rv;
156
157 memcached_programs_help_st help_options[]=
158 {
159 {0},
160 };
161
162 static struct option long_options[]=
163 {
164 {"version", no_argument, NULL, OPT_VERSION},
165 {"help", no_argument, NULL, OPT_HELP},
166 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
167 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
168 {"servers", required_argument, NULL, OPT_SERVERS},
169 {"flag", required_argument, NULL, OPT_FLAG},
170 {"expire", required_argument, NULL, OPT_EXPIRE},
171 {"set", no_argument, NULL, OPT_SET},
172 {"add", no_argument, NULL, OPT_ADD},
173 {"replace", no_argument, NULL, OPT_REPLACE},
174 {"hash", required_argument, NULL, OPT_HASH},
175 {0, 0, 0, 0},
176 };
177
178 while (1)
179 {
180 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
181
182 if (option_rv == -1) break;
183
184 switch (option_rv)
185 {
186 case 0:
187 break;
188 case OPT_VERBOSE: /* --verbose or -v */
189 opt_verbose = OPT_VERBOSE;
190 break;
191 case OPT_DEBUG: /* --debug or -d */
192 opt_verbose = OPT_DEBUG;
193 break;
194 case OPT_VERSION: /* --version or -V */
195 version_command(PROGRAM_NAME);
196 break;
197 case OPT_HELP: /* --help or -h */
198 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
199 break;
200 case OPT_SERVERS: /* --servers or -s */
201 opt_servers= strdup(optarg);
202 break;
203 case OPT_FLAG: /* --flag */
204 opt_flags= (uint32_t)strtol(optarg, (char **)NULL, 16);
205 break;
206 case OPT_EXPIRE: /* --expire */
207 opt_expires= (time_t)strtoll(optarg, (char **)NULL, 10);
208 break;
209 case OPT_SET:
210 opt_method= OPT_SET;
211 break;
212 case OPT_REPLACE:
213 opt_method= OPT_REPLACE;
214 break;
215 case OPT_ADD:
216 opt_method= OPT_ADD;
217 case OPT_HASH:
218 opt_hash= strdup(optarg);
219 break;
220 case '?':
221 /* getopt_long already printed an error message. */
222 exit(1);
223 default:
224 abort();
225 }
226 }
227 }