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 +--------------------------------------------------------------------+
17 #include "mem_config.h"
19 #define PROGRAM_NAME "memdump"
20 #define PROGRAM_DESCRIPTION "Dump all values from one or many servers."
21 #define PROGRAM_VERSION "1.1"
23 #include "common/options.hpp"
24 #include "common/checks.hpp"
26 static memcached_return_t
print(const memcached_st
*, const char *k
, size_t l
, void *ctx
) {
27 auto out
= static_cast<std::ostream
*>(ctx
);
30 return MEMCACHED_SUCCESS
;
33 int main(int argc
, char *argv
[]) {
34 client_options opt
{PROGRAM_NAME
, PROGRAM_VERSION
, PROGRAM_DESCRIPTION
};
36 for (const auto &def
: opt
.defaults
) {
37 switch (def
.opt
.val
) {
38 case 'H': // no need for --hash
39 case 'b': // binary proto not available
46 opt
.add("file", 'f', required_argument
, "Output to file instead of standard output.");
48 if (!opt
.parse(argc
, argv
)) {
52 if (opt
.isset("quiet") && !opt
.isset("file")) {
53 std::cerr
<< "--quiet operation was requested, but --file was not set.\n";
58 if (!check_memcached(opt
, memc
)) {
62 if (!opt
.apply(&memc
)) {
66 memcached_dump_fn cb
[1] = {&print
};
67 std::ofstream outfile
{};
68 std::ostream
*outstream
= check_ostream(opt
, opt
.argof("file"), outfile
);
70 auto rc
= memcached_dump(&memc
, cb
, outstream
, 1);
73 if (opt
.isset("debug")) {
74 std::cerr
<< "Flushing " << opt
.argof("file") << ".\n";
79 if (MEMCACHED_SUCCESS
!= rc
) {
80 if (!opt
.isset("quiet")) {
81 std::cerr
<< "Failed to dump keys:" << memcached_last_error_message(&memc
) << "\n";
83 memcached_free(&memc
);
87 memcached_free(&memc
);