Add in missing files.
[m6w6/libmemcached] / clients / memtouch.cc
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12 #include <config.h>
13
14 #include <cstdio>
15 #include <cstring>
16 #include <getopt.h>
17 #include <iostream>
18 #include <unistd.h>
19 #include <libmemcached/memcached.h>
20
21 #include "utilities.h"
22
23 #define PROGRAM_NAME "memtouch"
24 #define PROGRAM_DESCRIPTION "Update the expiration value of an alreasy existing value in the sever"
25
26
27 /* Prototypes */
28 void options_parse(int argc, char *argv[]);
29
30 static int opt_binary= 0;
31 static int opt_verbose= 0;
32 static char *opt_servers= NULL;
33 static char *opt_hash= NULL;
34 static char *opt_username;
35 static char *opt_passwd;
36
37 time_t expiration= 0;
38
39 int main(int argc, char *argv[])
40 {
41 int return_code= EXIT_SUCCESS;
42
43 options_parse(argc, argv);
44 initialize_sockets();
45
46 if (opt_servers == NULL)
47 {
48 char *temp;
49
50 if ((temp= getenv("MEMCACHED_SERVERS")))
51 {
52 opt_servers= strdup(temp);
53 }
54 else
55 {
56 std::cerr << "No Servers provided" << std::endl;
57 return EXIT_FAILURE;
58 }
59 }
60
61 memcached_st *memc= memcached_create(NULL);
62 process_hash_option(memc, opt_hash);
63
64 memcached_server_st *servers= memcached_servers_parse(opt_servers);
65
66 memcached_server_push(memc, servers);
67 memcached_server_list_free(servers);
68 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
69 (uint64_t)opt_binary);
70
71 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
72 {
73 memcached_free(memc);
74 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
75 return EXIT_FAILURE;
76 }
77
78 if (opt_username)
79 {
80 memcached_return_t ret;
81 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
82 {
83 std::cerr << memcached_last_error_message(memc) << std::endl;
84 memcached_free(memc);
85 return EXIT_FAILURE;
86 }
87 }
88
89 while (optind < argc)
90 {
91 memcached_return_t rc= memcached_touch(memc, argv[optind], strlen(argv[optind]), expiration);
92 if (rc == MEMCACHED_NOTFOUND)
93 {
94 if (opt_verbose)
95 {
96 std::cout << "Could not find key \"" << argv[optind] << "\"" << std::endl;
97 }
98
99 return_code= EXIT_FAILURE;
100 }
101 else if (memcached_failed(rc))
102 {
103 if (opt_verbose)
104 {
105 std::cerr << "Fatal error for key \"" << argv[optind] << "\" :" << memcached_last_error_message(memc) << std::endl;
106 }
107
108 return_code= EXIT_FAILURE;
109 }
110 else // success
111 {
112 if (opt_verbose)
113 {
114 std::cout << "Found key " << argv[optind] << std::endl;
115 }
116 }
117
118 optind++;
119 }
120
121 memcached_free(memc);
122
123 if (opt_servers)
124 {
125 free(opt_servers);
126 }
127
128 if (opt_hash)
129 {
130 free(opt_hash);
131 }
132
133 return return_code;
134 }
135
136
137 void options_parse(int argc, char *argv[])
138 {
139 memcached_programs_help_st help_options[]=
140 {
141 {0},
142 };
143
144 static struct option long_options[]=
145 {
146 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
147 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
148 {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
149 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
150 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
151 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
152 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
153 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
154 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
155 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
156 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
157 {0, 0, 0, 0},
158 };
159
160 bool opt_version= false;
161 bool opt_help= false;
162 int option_index= 0;
163
164 while (1)
165 {
166 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
167 if (option_rv == -1)
168 {
169 break;
170 }
171
172 switch (option_rv)
173 {
174 case 0:
175 break;
176
177 case OPT_BINARY:
178 opt_binary = true;
179 break;
180
181 case OPT_VERBOSE: /* --verbose or -v */
182 opt_verbose = OPT_VERBOSE;
183 break;
184
185 case OPT_DEBUG: /* --debug or -d */
186 opt_verbose = OPT_DEBUG;
187 break;
188
189 case OPT_VERSION: /* --version or -V */
190 opt_version= true;
191 break;
192
193 case OPT_HELP: /* --help or -h */
194 opt_help= true;
195 break;
196
197 case OPT_SERVERS: /* --servers or -s */
198 opt_servers= strdup(optarg);
199 break;
200
201 case OPT_HASH:
202 opt_hash= strdup(optarg);
203 break;
204
205 case OPT_USERNAME:
206 opt_username= optarg;
207 break;
208
209 case OPT_PASSWD:
210 opt_passwd= optarg;
211 break;
212
213 case OPT_EXPIRE:
214 expiration= time_t(strtoul(optarg, (char **)NULL, 10));
215 break;
216
217 case OPT_QUIET:
218 close_stdio();
219 break;
220
221 case '?':
222 /* getopt_long already printed an error message. */
223 exit(EXIT_FAILURE);
224
225 default:
226 abort();
227 }
228 }
229
230 if (opt_version)
231 {
232 version_command(PROGRAM_NAME);
233 exit(EXIT_SUCCESS);
234 }
235
236 if (opt_help)
237 {
238 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
239 exit(EXIT_SUCCESS);
240 }
241 }