b19c286067c627801ff853fb6f0368c95e5c5000
[m6w6/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 memcached_st *memc;
39 memcached_return_t rc;
40 memcached_server_st *servers;
41
42 options_parse(argc, argv);
43
44 if (!opt_servers)
45 {
46 char *temp;
47
48 if ((temp= getenv("MEMCACHED_SERVERS")))
49 opt_servers= strdup(temp);
50 else
51 {
52 fprintf(stderr, "No Servers provided\n");
53 exit(1);
54 }
55 }
56
57 memc= memcached_create(NULL);
58
59 servers= memcached_servers_parse(opt_servers);
60 memcached_server_push(memc, servers);
61 memcached_server_list_free(servers);
62 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
63 (uint64_t) opt_binary);
64
65 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
66 {
67 memcached_free(memc);
68 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
69 return EXIT_FAILURE;
70 }
71
72
73 if (!initialize_sasl(memc, opt_username, opt_passwd))
74 {
75 memcached_free(memc);
76 return EXIT_FAILURE;
77 }
78
79 rc = memcached_flush(memc, opt_expire);
80 if (rc != MEMCACHED_SUCCESS)
81 {
82 fprintf(stderr, "memflush: memcache error %s",
83 memcached_strerror(memc, rc));
84 if (memcached_last_error_errno(memc))
85 fprintf(stderr, " system error %s", strerror(memcached_last_error_errno(memc)));
86 fprintf(stderr, "\n");
87 }
88
89 memcached_free(memc);
90
91 free(opt_servers);
92
93 shutdown_sasl();
94
95 return EXIT_SUCCESS;
96 }
97
98
99 void options_parse(int argc, char *argv[])
100 {
101 memcached_programs_help_st help_options[]=
102 {
103 {0},
104 };
105
106 static struct option long_options[]=
107 {
108 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
109 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
110 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
111 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
112 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
113 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
114 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
115 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
116 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
117 {0, 0, 0, 0},
118 };
119 int option_index= 0;
120 int option_rv;
121
122 while (1)
123 {
124 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
125 if (option_rv == -1) break;
126 switch (option_rv)
127 {
128 case 0:
129 break;
130 case OPT_BINARY:
131 opt_binary = 1;
132 break;
133 case OPT_VERBOSE: /* --verbose or -v */
134 opt_verbose = OPT_VERBOSE;
135 break;
136 case OPT_DEBUG: /* --debug or -d */
137 opt_verbose = OPT_DEBUG;
138 break;
139 case OPT_VERSION: /* --version or -V */
140 version_command(PROGRAM_NAME);
141 break;
142 case OPT_HELP: /* --help or -h */
143 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
144 break;
145 case OPT_SERVERS: /* --servers or -s */
146 opt_servers= strdup(optarg);
147 break;
148 case OPT_EXPIRE: /* --expire */
149 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
150 break;
151 case OPT_USERNAME:
152 opt_username= optarg;
153 break;
154 case OPT_PASSWD:
155 opt_passwd= optarg;
156 break;
157 case '?':
158 /* getopt_long already printed an error message. */
159 exit(1);
160 default:
161 abort();
162 }
163 }
164 }