Deprecate the old callback interface to set the memory allocators
[awesomized/libmemcached] / clients / memdump.c
1 #include "libmemcached/common.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <inttypes.h>
5 #include <unistd.h>
6 #include <getopt.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <sys/types.h>
10 #include <fcntl.h>
11 #include <errno.h>
12 #include <strings.h>
13 #include <string.h>
14
15 #include <libmemcached/memcached.h>
16
17 #include "client_options.h"
18 #include "utilities.h"
19
20 #define PROGRAM_NAME "memdump"
21 #define PROGRAM_DESCRIPTION "Dump all values from one or many servers."
22
23 /* Prototypes */
24 void options_parse(int argc, char *argv[]);
25
26 static int opt_binary=0;
27 static int opt_verbose= 0;
28 static char *opt_servers= NULL;
29 static char *opt_hash= NULL;
30
31 /* Print the keys and counter how many were found */
32 static memcached_return key_printer(memcached_st *ptr __attribute__((unused)),
33 const char *key, size_t key_length,
34 void *context __attribute__((unused)))
35 {
36 printf("%.*s\n", (uint32_t)key_length, key);
37
38 return MEMCACHED_SUCCESS;
39 }
40
41 int main(int argc, char *argv[])
42 {
43 memcached_st *memc;
44 memcached_return rc;
45 memcached_server_st *servers;
46 memcached_dump_func callbacks[1];
47
48 callbacks[0]= &key_printer;
49
50 options_parse(argc, argv);
51
52 memc= memcached_create(NULL);
53 process_hash_option(memc, opt_hash);
54
55 if (!opt_servers)
56 {
57 char *temp;
58
59 if ((temp= getenv("MEMCACHED_SERVERS")))
60 opt_servers= strdup(temp);
61 else
62 {
63 fprintf(stderr, "No Servers provided\n");
64 exit(1);
65 }
66 }
67
68 if (opt_servers)
69 servers= memcached_servers_parse(opt_servers);
70 else
71 servers= memcached_servers_parse(argv[--argc]);
72
73 memcached_server_push(memc, servers);
74 memcached_server_list_free(servers);
75 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, opt_binary);
76
77 rc= memcached_dump(memc, callbacks, NULL, 1);
78
79 if (rc != MEMCACHED_SUCCESS)
80 {
81 fprintf(stderr, "memdump: memcache error %s", memcached_strerror(memc, rc));
82 if (memc->cached_errno)
83 fprintf(stderr, " system error %s", strerror(memc->cached_errno));
84 fprintf(stderr, "\n");
85 }
86
87 memcached_free(memc);
88
89 if (opt_servers)
90 free(opt_servers);
91 if (opt_hash)
92 free(opt_hash);
93
94 return 0;
95 }
96
97 void options_parse(int argc, char *argv[])
98 {
99 int option_index= 0;
100 int option_rv;
101
102 static struct option long_options[]=
103 {
104 {"version", no_argument, NULL, OPT_VERSION},
105 {"help", no_argument, NULL, OPT_HELP},
106 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
107 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
108 {"servers", required_argument, NULL, OPT_SERVERS},
109 {"hash", required_argument, NULL, OPT_HASH},
110 {"binary", no_argument, NULL, OPT_BINARY},
111 {0, 0, 0, 0}
112 };
113
114 while (1)
115 {
116 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
117
118 if (option_rv == -1) break;
119
120 switch (option_rv)
121 {
122 case 0:
123 break;
124 case OPT_BINARY:
125 opt_binary = 1;
126 break;
127 case OPT_VERBOSE: /* --verbose or -v */
128 opt_verbose = OPT_VERBOSE;
129 break;
130 case OPT_DEBUG: /* --debug or -d */
131 opt_verbose = OPT_DEBUG;
132 break;
133 case OPT_VERSION: /* --version or -V */
134 version_command(PROGRAM_NAME);
135 break;
136 case OPT_HELP: /* --help or -h */
137 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL);
138 break;
139 case OPT_SERVERS: /* --servers or -s */
140 opt_servers= strdup(optarg);
141 break;
142 case OPT_HASH:
143 opt_hash= strdup(optarg);
144 break;
145 case '?':
146 /* getopt_long already printed an error message. */
147 exit(1);
148 default:
149 abort();
150 }
151 }
152 }