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