Include config.h before any system header to avoid problems with FILE_OFFSET_BITS...
[m6w6/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,
76 (uint64_t)opt_binary);
77
78 rc= memcached_dump(memc, callbacks, NULL, 1);
79
80 if (rc != MEMCACHED_SUCCESS)
81 {
82 fprintf(stderr, "memdump: memcache error %s", memcached_strerror(memc, rc));
83 if (memc->cached_errno)
84 fprintf(stderr, " system error %s", strerror(memc->cached_errno));
85 fprintf(stderr, "\n");
86 }
87
88 memcached_free(memc);
89
90 if (opt_servers)
91 free(opt_servers);
92 if (opt_hash)
93 free(opt_hash);
94
95 return 0;
96 }
97
98 void options_parse(int argc, char *argv[])
99 {
100 int option_index= 0;
101 int option_rv;
102
103 static struct option long_options[]=
104 {
105 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
106 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
107 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
108 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
109 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
110 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
111 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
112 {0, 0, 0, 0}
113 };
114
115 while (1)
116 {
117 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
118
119 if (option_rv == -1) break;
120
121 switch (option_rv)
122 {
123 case 0:
124 break;
125 case OPT_BINARY:
126 opt_binary = 1;
127 break;
128 case OPT_VERBOSE: /* --verbose or -v */
129 opt_verbose = OPT_VERBOSE;
130 break;
131 case OPT_DEBUG: /* --debug or -d */
132 opt_verbose = OPT_DEBUG;
133 break;
134 case OPT_VERSION: /* --version or -V */
135 version_command(PROGRAM_NAME);
136 break;
137 case OPT_HELP: /* --help or -h */
138 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL);
139 break;
140 case OPT_SERVERS: /* --servers or -s */
141 opt_servers= strdup(optarg);
142 break;
143 case OPT_HASH:
144 opt_hash= strdup(optarg);
145 break;
146 case '?':
147 /* getopt_long already printed an error message. */
148 exit(1);
149 default:
150 abort();
151 }
152 }
153 }