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.
12 #include "libmemcached/common.h"
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
;
26 #define PROGRAM_NAME "memflush"
27 #define PROGRAM_DESCRIPTION "Erase all data in a server of memcached servers."
30 void options_parse(int argc
, char *argv
[]);
32 int main(int argc
, char *argv
[])
35 memcached_return_t rc
;
36 memcached_server_st
*servers
;
38 options_parse(argc
, argv
);
44 if ((temp
= getenv("MEMCACHED_SERVERS")))
45 opt_servers
= strdup(temp
);
48 fprintf(stderr
, "No Servers provided\n");
53 memc
= memcached_create(NULL
);
55 servers
= memcached_servers_parse(opt_servers
);
56 memcached_server_push(memc
, servers
);
57 memcached_server_list_free(servers
);
58 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
59 (uint64_t) opt_binary
);
61 rc
= memcached_flush(memc
, opt_expire
);
62 if (rc
!= MEMCACHED_SUCCESS
)
64 fprintf(stderr
, "memflush: memcache error %s",
65 memcached_strerror(memc
, rc
));
66 if (memc
->cached_errno
)
67 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
68 fprintf(stderr
, "\n");
79 void options_parse(int argc
, char *argv
[])
81 memcached_programs_help_st help_options
[]=
86 static struct option long_options
[]=
88 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
89 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
90 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
91 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
92 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
93 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
94 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
102 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
103 if (option_rv
== -1) break;
111 case OPT_VERBOSE
: /* --verbose or -v */
112 opt_verbose
= OPT_VERBOSE
;
114 case OPT_DEBUG
: /* --debug or -d */
115 opt_verbose
= OPT_DEBUG
;
117 case OPT_VERSION
: /* --version or -V */
118 version_command(PROGRAM_NAME
);
120 case OPT_HELP
: /* --help or -h */
121 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
123 case OPT_SERVERS
: /* --servers or -s */
124 opt_servers
= strdup(optarg
);
126 case OPT_EXPIRE
: /* --expire */
127 opt_expire
= (time_t)strtoll(optarg
, (char **)NULL
, 10);
130 /* getopt_long already printed an error message. */