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
;
74 memcached_return_t ret
;
75 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
77 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
83 rc
= memcached_flush(memc
, opt_expire
);
84 if (rc
!= MEMCACHED_SUCCESS
)
86 fprintf(stderr
, "memflush: memcache error %s",
87 memcached_strerror(memc
, rc
));
88 if (memcached_last_error_errno(memc
))
89 fprintf(stderr
, " system error %s", strerror(memcached_last_error_errno(memc
)));
90 fprintf(stderr
, "\n");
101 void options_parse(int argc
, char *argv
[])
103 memcached_programs_help_st help_options
[]=
108 static struct option long_options
[]=
110 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
111 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
112 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
113 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
114 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
115 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
116 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
117 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
118 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
126 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
127 if (option_rv
== -1) break;
135 case OPT_VERBOSE
: /* --verbose or -v */
136 opt_verbose
= OPT_VERBOSE
;
138 case OPT_DEBUG
: /* --debug or -d */
139 opt_verbose
= OPT_DEBUG
;
141 case OPT_VERSION
: /* --version or -V */
142 version_command(PROGRAM_NAME
);
144 case OPT_HELP
: /* --help or -h */
145 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
147 case OPT_SERVERS
: /* --servers or -s */
148 opt_servers
= strdup(optarg
);
150 case OPT_EXPIRE
: /* --expire */
151 opt_expire
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
154 opt_username
= optarg
;
160 /* getopt_long already printed an error message. */