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>
20 #include <libmemcached-1.0/memcached.h>
22 #include "utilities.h"
24 #define PROGRAM_NAME "memtouch"
25 #define PROGRAM_DESCRIPTION "Update the expiration value of an alreasy existing value in the sever"
29 void options_parse(int argc
, char *argv
[]);
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
;
40 int main(int argc
, char *argv
[])
42 int return_code
= EXIT_SUCCESS
;
44 options_parse(argc
, argv
);
47 if (opt_servers
== NULL
)
51 if ((temp
= getenv("MEMCACHED_SERVERS")))
53 opt_servers
= strdup(temp
);
57 std::cerr
<< "No Servers provided" << std::endl
;
62 memcached_st
*memc
= memcached_create(NULL
);
63 process_hash_option(memc
, opt_hash
);
65 memcached_server_st
*servers
= memcached_servers_parse(opt_servers
);
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
);
72 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
75 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
81 memcached_return_t ret
;
82 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
84 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
92 memcached_return_t rc
= memcached_touch(memc
, argv
[optind
], strlen(argv
[optind
]), expiration
);
93 if (rc
== MEMCACHED_NOTFOUND
)
97 std::cout
<< "Could not find key \"" << argv
[optind
] << "\"" << std::endl
;
100 return_code
= EXIT_FAILURE
;
102 else if (memcached_failed(rc
))
106 std::cerr
<< "Fatal error for key \"" << argv
[optind
] << "\" :" << memcached_last_error_message(memc
) << std::endl
;
109 return_code
= EXIT_FAILURE
;
115 std::cout
<< "Found key " << argv
[optind
] << std::endl
;
122 memcached_free(memc
);
138 void options_parse(int argc
, char *argv
[])
140 memcached_programs_help_st help_options
[]=
145 static struct option long_options
[]=
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
},
161 bool opt_version
= false;
162 bool opt_help
= false;
167 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
182 case OPT_VERBOSE
: /* --verbose or -v */
183 opt_verbose
= OPT_VERBOSE
;
186 case OPT_DEBUG
: /* --debug or -d */
187 opt_verbose
= OPT_DEBUG
;
190 case OPT_VERSION
: /* --version or -V */
194 case OPT_HELP
: /* --help or -h */
198 case OPT_SERVERS
: /* --servers or -s */
199 opt_servers
= strdup(optarg
);
203 opt_hash
= strdup(optarg
);
207 opt_username
= optarg
;
215 expiration
= time_t(strtoul(optarg
, (char **)NULL
, 10));
223 /* getopt_long already printed an error message. */
233 version_command(PROGRAM_NAME
);
239 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);