2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
19 #include <libmemcached/memcached.h>
20 #include "client_options.h"
21 #include "utilities.h"
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
;
30 #define PROGRAM_NAME "memflush"
31 #define PROGRAM_DESCRIPTION "Erase all data in a server of memcached servers."
34 void options_parse(int argc
, char *argv
[]);
36 int main(int argc
, char *argv
[])
39 memcached_return_t rc
;
40 memcached_server_st
*servers
;
42 options_parse(argc
, argv
);
48 if ((temp
= getenv("MEMCACHED_SERVERS")))
49 opt_servers
= strdup(temp
);
52 fprintf(stderr
, "No Servers provided\n");
57 memc
= memcached_create(NULL
);
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
);
65 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
68 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
73 if (!initialize_sasl(memc
, opt_username
, opt_passwd
))
79 rc
= memcached_flush(memc
, opt_expire
);
80 if (rc
!= MEMCACHED_SUCCESS
)
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");
99 void options_parse(int argc
, char *argv
[])
101 memcached_programs_help_st help_options
[]=
106 static struct option long_options
[]=
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
},
124 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
125 if (option_rv
== -1) break;
133 case OPT_VERBOSE
: /* --verbose or -v */
134 opt_verbose
= OPT_VERBOSE
;
136 case OPT_DEBUG
: /* --debug or -d */
137 opt_verbose
= OPT_DEBUG
;
139 case OPT_VERSION
: /* --version or -V */
140 version_command(PROGRAM_NAME
);
142 case OPT_HELP
: /* --help or -h */
143 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
145 case OPT_SERVERS
: /* --servers or -s */
146 opt_servers
= strdup(optarg
);
148 case OPT_EXPIRE
: /* --expire */
149 opt_expire
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
152 opt_username
= optarg
;
158 /* getopt_long already printed an error message. */