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.
17 #include <libmemcached/memcached.h>
18 #include "client_options.h"
19 #include "utilities.h"
21 static int opt_binary
= 0;
22 static int opt_verbose
= 0;
23 static time_t opt_expire
= 0;
24 static char *opt_servers
= NULL
;
25 static char *opt_username
;
26 static char *opt_passwd
;
28 #define PROGRAM_NAME "memflush"
29 #define PROGRAM_DESCRIPTION "Erase all data in a server of memcached servers."
32 void options_parse(int argc
, char *argv
[]);
34 int main(int argc
, char *argv
[])
37 memcached_return_t rc
;
38 memcached_server_st
*servers
;
40 options_parse(argc
, argv
);
46 if ((temp
= getenv("MEMCACHED_SERVERS")))
47 opt_servers
= strdup(temp
);
50 fprintf(stderr
, "No Servers provided\n");
55 memc
= memcached_create(NULL
);
57 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
);
63 if (!initialize_sasl(memc
, opt_username
, opt_passwd
))
69 rc
= memcached_flush(memc
, opt_expire
);
70 if (rc
!= MEMCACHED_SUCCESS
)
72 fprintf(stderr
, "memflush: memcache error %s",
73 memcached_strerror(memc
, rc
));
74 if (memc
->cached_errno
)
75 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
76 fprintf(stderr
, "\n");
89 void options_parse(int argc
, char *argv
[])
91 memcached_programs_help_st help_options
[]=
96 static struct option long_options
[]=
98 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
99 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
100 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
101 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
102 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
103 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
104 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
105 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
106 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
114 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
115 if (option_rv
== -1) break;
123 case OPT_VERBOSE
: /* --verbose or -v */
124 opt_verbose
= OPT_VERBOSE
;
126 case OPT_DEBUG
: /* --debug or -d */
127 opt_verbose
= OPT_DEBUG
;
129 case OPT_VERSION
: /* --version or -V */
130 version_command(PROGRAM_NAME
);
132 case OPT_HELP
: /* --help or -h */
133 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
135 case OPT_SERVERS
: /* --servers or -s */
136 opt_servers
= strdup(optarg
);
138 case OPT_EXPIRE
: /* --expire */
139 opt_expire
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
142 opt_username
= optarg
;
148 /* getopt_long already printed an error message. */