2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
19 #include <libmemcached/memcached.h>
21 #include "utilities.h"
23 #define PROGRAM_NAME "memtouch"
24 #define PROGRAM_DESCRIPTION "Update the expiration value of an alreasy existing value in the sever"
28 void options_parse(int argc
, char *argv
[]);
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
;
39 int main(int argc
, char *argv
[])
41 int return_code
= EXIT_SUCCESS
;
43 options_parse(argc
, argv
);
46 if (opt_servers
== NULL
)
50 if ((temp
= getenv("MEMCACHED_SERVERS")))
52 opt_servers
= strdup(temp
);
56 std::cerr
<< "No Servers provided" << std::endl
;
61 memcached_st
*memc
= memcached_create(NULL
);
62 process_hash_option(memc
, opt_hash
);
64 memcached_server_st
*servers
= memcached_servers_parse(opt_servers
);
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
);
71 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
74 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
80 memcached_return_t ret
;
81 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
83 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
91 memcached_return_t rc
= memcached_touch(memc
, argv
[optind
], strlen(argv
[optind
]), expiration
);
92 if (rc
== MEMCACHED_NOTFOUND
)
96 std::cout
<< "Could not find key \"" << argv
[optind
] << "\"" << std::endl
;
99 return_code
= EXIT_FAILURE
;
101 else if (memcached_failed(rc
))
105 std::cerr
<< "Fatal error for key \"" << argv
[optind
] << "\" :" << memcached_last_error_message(memc
) << std::endl
;
108 return_code
= EXIT_FAILURE
;
114 std::cout
<< "Found key " << argv
[optind
] << std::endl
;
121 memcached_free(memc
);
137 void options_parse(int argc
, char *argv
[])
139 memcached_programs_help_st help_options
[]=
144 static struct option long_options
[]=
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
},
160 bool opt_version
= false;
161 bool opt_help
= false;
166 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
181 case OPT_VERBOSE
: /* --verbose or -v */
182 opt_verbose
= OPT_VERBOSE
;
185 case OPT_DEBUG
: /* --debug or -d */
186 opt_verbose
= OPT_DEBUG
;
189 case OPT_VERSION
: /* --version or -V */
193 case OPT_HELP
: /* --help or -h */
197 case OPT_SERVERS
: /* --servers or -s */
198 opt_servers
= strdup(optarg
);
202 opt_hash
= strdup(optarg
);
206 opt_username
= optarg
;
214 expiration
= time_t(strtoul(optarg
, (char **)NULL
, 10));
222 /* getopt_long already printed an error message. */
232 version_command(PROGRAM_NAME
);
238 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);