2 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
13 #include <mem_config.h>
22 #include <libmemcached-1.0/memcached.h>
24 #include "utilities.h"
26 #define PROGRAM_NAME "memtouch"
27 #define PROGRAM_DESCRIPTION "Update the expiration value of an already existing value in the server"
31 void options_parse(int argc
, char *argv
[]);
33 static int opt_binary
= 0;
34 static int opt_verbose
= 0;
35 static char *opt_servers
= NULL
;
36 static char *opt_hash
= NULL
;
37 static char *opt_username
;
38 static char *opt_passwd
;
42 int main(int argc
, char *argv
[])
44 int return_code
= EXIT_SUCCESS
;
46 options_parse(argc
, argv
);
49 if (opt_servers
== NULL
)
53 if ((temp
= getenv("MEMCACHED_SERVERS")))
55 opt_servers
= strdup(temp
);
58 if (opt_servers
== NULL
)
60 std::cerr
<< "No Servers provided" << std::endl
;
65 memcached_server_st
* servers
= memcached_servers_parse(opt_servers
);
66 if (servers
== NULL
or memcached_server_list_count(servers
) == 0)
68 std::cerr
<< "Invalid server list provided:" << opt_servers
<< std::endl
;
72 memcached_st
*memc
= memcached_create(NULL
);
73 process_hash_option(memc
, opt_hash
);
75 memcached_server_push(memc
, servers
);
76 memcached_server_list_free(servers
);
77 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
78 (uint64_t)opt_binary
);
80 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
83 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
89 memcached_return_t ret
;
90 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
92 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
100 memcached_return_t rc
= memcached_touch(memc
, argv
[optind
], strlen(argv
[optind
]), expiration
);
101 if (rc
== MEMCACHED_NOTFOUND
)
105 std::cout
<< "Could not find key \"" << argv
[optind
] << "\"" << std::endl
;
108 return_code
= EXIT_FAILURE
;
110 else if (memcached_failed(rc
))
114 std::cerr
<< "Fatal error for key \"" << argv
[optind
] << "\" :" << memcached_last_error_message(memc
) << std::endl
;
117 return_code
= EXIT_FAILURE
;
123 std::cout
<< "Found key " << argv
[optind
] << std::endl
;
130 memcached_free(memc
);
146 void options_parse(int argc
, char *argv
[])
148 memcached_programs_help_st help_options
[]=
153 static struct option long_options
[]=
155 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
156 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
157 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
158 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
159 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
160 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
161 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
162 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
163 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
164 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
165 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
169 bool opt_version
= false;
170 bool opt_help
= false;
175 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
190 case OPT_VERBOSE
: /* --verbose or -v */
191 opt_verbose
= OPT_VERBOSE
;
194 case OPT_DEBUG
: /* --debug or -d */
195 opt_verbose
= OPT_DEBUG
;
198 case OPT_VERSION
: /* --version or -V */
202 case OPT_HELP
: /* --help or -h */
206 case OPT_SERVERS
: /* --servers or -s */
207 opt_servers
= strdup(optarg
);
211 opt_hash
= strdup(optarg
);
215 opt_username
= optarg
;
224 expiration
= time_t(strtoul(optarg
, (char **)NULL
, 10));
227 fprintf(stderr
, "Invalid value for --expire: %s\n", optarg
);
237 /* getopt_long already printed an error message. */
247 version_command(PROGRAM_NAME
);
253 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);