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