2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
16 #include "mem_config.h"
25 #include "libmemcached-1.0/memcached.h"
27 #include "utilities.h"
29 #define PROGRAM_NAME "memtouch"
30 #define PROGRAM_DESCRIPTION "Update the expiration value of an already existing value in the server"
33 void options_parse(int argc
, char *argv
[]);
35 static int opt_binary
= 0;
36 static int opt_verbose
= 0;
37 static char *opt_servers
= NULL
;
38 static char *opt_hash
= NULL
;
39 static char *opt_username
;
40 static char *opt_passwd
;
42 time_t expiration
= 0;
44 int main(int argc
, char *argv
[]) {
45 int return_code
= EXIT_SUCCESS
;
47 options_parse(argc
, argv
);
50 if (opt_servers
== NULL
) {
53 if ((temp
= getenv("MEMCACHED_SERVERS"))) {
54 opt_servers
= strdup(temp
);
57 if (opt_servers
== NULL
) {
58 std::cerr
<< "No Servers provided" << std::endl
;
63 memcached_server_st
*servers
= memcached_servers_parse(opt_servers
);
64 if (servers
== NULL
or memcached_server_list_count(servers
) == 0) {
65 std::cerr
<< "Invalid server list provided:" << opt_servers
<< std::endl
;
69 memcached_st
*memc
= memcached_create(NULL
);
70 process_hash_option(memc
, opt_hash
);
72 memcached_server_push(memc
, servers
);
73 memcached_server_list_free(servers
);
74 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, (uint64_t) opt_binary
);
76 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0) {
78 std::cerr
<< "--username was supplied, but binary was not built with SASL support."
84 memcached_return_t ret
;
85 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
))) {
86 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
92 while (optind
< argc
) {
93 memcached_return_t rc
= memcached_touch(memc
, argv
[optind
], strlen(argv
[optind
]), expiration
);
94 if (rc
== MEMCACHED_NOTFOUND
) {
96 std::cout
<< "Could not find key \"" << argv
[optind
] << "\"" << std::endl
;
99 return_code
= EXIT_FAILURE
;
100 } else if (memcached_failed(rc
)) {
102 std::cerr
<< "Fatal error for key \"" << argv
[optind
]
103 << "\" :" << memcached_last_error_message(memc
) << std::endl
;
106 return_code
= EXIT_FAILURE
;
110 std::cout
<< "Found key " << argv
[optind
] << std::endl
;
117 memcached_free(memc
);
130 void options_parse(int argc
, char *argv
[]) {
131 memcached_programs_help_st help_options
[] = {
135 static struct option long_options
[] = {
136 {(OPTIONSTRING
) "version", no_argument
, NULL
, OPT_VERSION
},
137 {(OPTIONSTRING
) "help", no_argument
, NULL
, OPT_HELP
},
138 {(OPTIONSTRING
) "quiet", no_argument
, NULL
, OPT_QUIET
},
139 {(OPTIONSTRING
) "verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
140 {(OPTIONSTRING
) "debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
141 {(OPTIONSTRING
) "servers", required_argument
, NULL
, OPT_SERVERS
},
142 {(OPTIONSTRING
) "hash", required_argument
, NULL
, OPT_HASH
},
143 {(OPTIONSTRING
) "binary", no_argument
, NULL
, OPT_BINARY
},
144 {(OPTIONSTRING
) "username", required_argument
, NULL
, OPT_USERNAME
},
145 {(OPTIONSTRING
) "password", required_argument
, NULL
, OPT_PASSWD
},
146 {(OPTIONSTRING
) "expire", required_argument
, NULL
, OPT_EXPIRE
},
150 bool opt_version
= false;
151 bool opt_help
= false;
152 int option_index
= 0;
155 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
156 if (option_rv
== -1) {
168 case OPT_VERBOSE
: /* --verbose or -v */
169 opt_verbose
= OPT_VERBOSE
;
172 case OPT_DEBUG
: /* --debug or -d */
173 opt_verbose
= OPT_DEBUG
;
176 case OPT_VERSION
: /* --version or -V */
180 case OPT_HELP
: /* --help or -h */
184 case OPT_SERVERS
: /* --servers or -s */
185 opt_servers
= strdup(optarg
);
189 opt_hash
= strdup(optarg
);
193 opt_username
= optarg
;
202 expiration
= time_t(strtoul(optarg
, (char **) NULL
, 10));
204 fprintf(stderr
, "Invalid value for --expire: %s\n", optarg
);
214 /* getopt_long already printed an error message. */
223 version_command(PROGRAM_NAME
);
228 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);