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"
27 #include <sys/types.h>
28 #include <sys/types.h>
31 #include "libmemcached-1.0/memcached.h"
33 #include "client_options.h"
34 #include "utilities.h"
36 #define PROGRAM_NAME "memdump"
37 #define PROGRAM_DESCRIPTION "Dump all values from one or many servers."
40 static void options_parse(int argc
, char *argv
[]);
42 static bool opt_binary
= 0;
43 static int opt_verbose
= 0;
44 static char *opt_servers
= NULL
;
45 static char *opt_hash
= NULL
;
46 static char *opt_username
;
47 static char *opt_passwd
;
49 /* Print the keys and counter how many were found */
50 static memcached_return_t
key_printer(const memcached_st
*, const char *key
, size_t key_length
,
52 std::cout
.write(key
, key_length
);
53 std::cout
<< std::endl
;
55 return MEMCACHED_SUCCESS
;
58 int main(int argc
, char *argv
[]) {
59 memcached_dump_fn callbacks
[1];
61 callbacks
[0] = &key_printer
;
63 options_parse(argc
, argv
);
65 if (opt_servers
== NULL
) {
68 if ((temp
= getenv("MEMCACHED_SERVERS"))) {
69 opt_servers
= strdup(temp
);
70 } else if (argc
> 1 and argv
[--argc
]) {
71 opt_servers
= strdup(argv
[argc
]);
74 if (opt_servers
== NULL
) {
75 std::cerr
<< "No Servers provided" << std::endl
;
80 memcached_server_st
*servers
= memcached_servers_parse(opt_servers
);
81 if (servers
== NULL
or memcached_server_list_count(servers
) == 0) {
82 std::cerr
<< "Invalid server list provided:" << opt_servers
<< std::endl
;
86 memcached_st
*memc
= memcached_create(NULL
);
88 std::cerr
<< "Could not allocate a memcached_st structure.\n" << std::endl
;
91 process_hash_option(memc
, opt_hash
);
93 memcached_server_push(memc
, servers
);
94 memcached_server_list_free(servers
);
95 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, (uint64_t) opt_binary
);
97 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0) {
99 std::cerr
<< "--username was supplied, but binary was not built with SASL support."
105 memcached_return_t ret
;
106 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
))) {
107 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
108 memcached_free(memc
);
113 memcached_return_t rc
= memcached_dump(memc
, callbacks
, NULL
, 1);
115 int exit_code
= EXIT_SUCCESS
;
116 if (memcached_failed(rc
)) {
118 std::cerr
<< "Failed to dump keys: " << memcached_last_error_message(memc
) << std::endl
;
120 exit_code
= EXIT_FAILURE
;
123 memcached_free(memc
);
135 static void options_parse(int argc
, char *argv
[]) {
136 static struct option long_options
[] = {
137 {(OPTIONSTRING
) "version", no_argument
, NULL
, OPT_VERSION
},
138 {(OPTIONSTRING
) "help", no_argument
, NULL
, OPT_HELP
},
139 {(OPTIONSTRING
) "quiet", no_argument
, NULL
, OPT_QUIET
},
140 {(OPTIONSTRING
) "verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
141 {(OPTIONSTRING
) "debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
142 {(OPTIONSTRING
) "servers", required_argument
, NULL
, OPT_SERVERS
},
143 {(OPTIONSTRING
) "hash", required_argument
, NULL
, OPT_HASH
},
144 {(OPTIONSTRING
) "binary", no_argument
, NULL
, OPT_BINARY
},
145 {(OPTIONSTRING
) "username", required_argument
, NULL
, OPT_USERNAME
},
146 {(OPTIONSTRING
) "password", required_argument
, NULL
, OPT_PASSWD
},
149 int option_index
= 0;
150 bool opt_version
= false;
151 bool opt_help
= false;
153 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
166 case OPT_VERBOSE
: /* --verbose or -v */
167 opt_verbose
= OPT_VERBOSE
;
170 case OPT_DEBUG
: /* --debug or -d */
171 opt_verbose
= OPT_DEBUG
;
174 case OPT_VERSION
: /* --version or -V */
178 case OPT_HELP
: /* --help or -h */
182 case OPT_SERVERS
: /* --servers or -s */
183 opt_servers
= strdup(optarg
);
187 opt_hash
= strdup(optarg
);
191 opt_username
= optarg
;
203 /* getopt_long already printed an error message. */
211 version_command(PROGRAM_NAME
);
216 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, NULL
);