Merge trunk
[awesomized/libmemcached] / clients / memflush.cc
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 #include "config.h"
12
13 #include <cstdio>
14 #include <cstring>
15 #include <getopt.h>
16 #include <iostream>
17 #include <unistd.h>
18
19 #include <libmemcached/memcached.h>
20 #include "client_options.h"
21 #include "utilities.h"
22
23 static int opt_binary= 0;
24 static int opt_verbose= 0;
25 static time_t opt_expire= 0;
26 static char *opt_servers= NULL;
27 static char *opt_username;
28 static char *opt_passwd;
29
30 #define PROGRAM_NAME "memflush"
31 #define PROGRAM_DESCRIPTION "Erase all data in a server of memcached servers."
32
33 /* Prototypes */
34 void options_parse(int argc, char *argv[]);
35
36 int main(int argc, char *argv[])
37 {
38 options_parse(argc, argv);
39
40 if (opt_servers == false)
41 {
42 char *temp;
43
44 if ((temp= getenv("MEMCACHED_SERVERS")))
45 {
46 opt_servers= strdup(temp);
47 }
48 else
49 {
50 std::cerr << "No Servers provided" << std::endl;
51 exit(EXIT_FAILURE);
52 }
53 }
54
55 memcached_st *memc= memcached_create(NULL);
56
57 memcached_server_st *servers= memcached_servers_parse(opt_servers);
58 memcached_server_push(memc, servers);
59 memcached_server_list_free(servers);
60 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
61 (uint64_t) opt_binary);
62
63 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
64 {
65 memcached_free(memc);
66 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
67 return EXIT_FAILURE;
68 }
69
70 if (opt_username)
71 {
72 memcached_return_t ret;
73 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
74 {
75 std::cerr << memcached_last_error_message(memc) << std::endl;
76 memcached_free(memc);
77 return EXIT_FAILURE;
78 }
79 }
80
81 memcached_return_t rc = memcached_flush(memc, opt_expire);
82 if (rc != MEMCACHED_SUCCESS)
83 {
84 std::cerr << memcached_last_error_message(memc) << std::endl;
85 }
86
87 memcached_free(memc);
88
89 free(opt_servers);
90
91 return EXIT_SUCCESS;
92 }
93
94
95 void options_parse(int argc, char *argv[])
96 {
97 static struct option long_options[]=
98 {
99 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
100 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
101 {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
102 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
103 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
104 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
105 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
106 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
107 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
108 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
109 {0, 0, 0, 0},
110 };
111
112 bool opt_version= false;
113 bool opt_help= false;
114 int option_index= 0;
115 while (1)
116 {
117 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
118 if (option_rv == -1) break;
119 switch (option_rv)
120 {
121 case 0:
122 break;
123
124 case OPT_BINARY:
125 opt_binary= true;
126 break;
127
128 case OPT_VERBOSE: /* --verbose or -v */
129 opt_verbose= OPT_VERBOSE;
130 break;
131
132 case OPT_DEBUG: /* --debug or -d */
133 opt_verbose= OPT_DEBUG;
134 break;
135
136 case OPT_VERSION: /* --version or -V */
137 opt_version= true;
138 break;
139
140 case OPT_HELP: /* --help or -h */
141 opt_help= true;
142 break;
143
144 case OPT_SERVERS: /* --servers or -s */
145 opt_servers= strdup(optarg);
146 break;
147
148 case OPT_EXPIRE: /* --expire */
149 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
150 break;
151
152 case OPT_USERNAME:
153 opt_username= optarg;
154 break;
155
156 case OPT_PASSWD:
157 opt_passwd= optarg;
158 break;
159
160 case OPT_QUIET:
161 close_stdio();
162 break;
163
164 case '?':
165 /* getopt_long already printed an error message. */
166 exit(EXIT_FAILURE);
167
168 default:
169 abort();
170 }
171 }
172
173 if (opt_version)
174 {
175 version_command(PROGRAM_NAME);
176 exit(EXIT_SUCCESS);
177 }
178
179 if (opt_help)
180 {
181 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL);
182 exit(EXIT_SUCCESS);
183 }
184 }