Ignore *.exe
[awesomized/libmemcached] / clients / memflush.c
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 <stdio.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <getopt.h>
17 #include <libmemcached/memcached.h>
18 #include "client_options.h"
19 #include "utilities.h"
20
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;
27
28 #define PROGRAM_NAME "memflush"
29 #define PROGRAM_DESCRIPTION "Erase all data in a server of memcached servers."
30
31 /* Prototypes */
32 void options_parse(int argc, char *argv[]);
33
34 int main(int argc, char *argv[])
35 {
36 memcached_st *memc;
37 memcached_return_t rc;
38 memcached_server_st *servers;
39
40 options_parse(argc, argv);
41
42 if (!opt_servers)
43 {
44 char *temp;
45
46 if ((temp= getenv("MEMCACHED_SERVERS")))
47 opt_servers= strdup(temp);
48 else
49 {
50 fprintf(stderr, "No Servers provided\n");
51 exit(1);
52 }
53 }
54
55 memc= memcached_create(NULL);
56
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);
62
63 if (!initialize_sasl(memc, opt_username, opt_passwd))
64 {
65 memcached_free(memc);
66 return 1;
67 }
68
69 rc = memcached_flush(memc, opt_expire);
70 if (rc != MEMCACHED_SUCCESS)
71 {
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");
77 }
78
79 memcached_free(memc);
80
81 free(opt_servers);
82
83 shutdown_sasl();
84
85 return 0;
86 }
87
88
89 void options_parse(int argc, char *argv[])
90 {
91 memcached_programs_help_st help_options[]=
92 {
93 {0},
94 };
95
96 static struct option long_options[]=
97 {
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},
107 {0, 0, 0, 0},
108 };
109 int option_index= 0;
110 int option_rv;
111
112 while (1)
113 {
114 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
115 if (option_rv == -1) break;
116 switch (option_rv)
117 {
118 case 0:
119 break;
120 case OPT_BINARY:
121 opt_binary = 1;
122 break;
123 case OPT_VERBOSE: /* --verbose or -v */
124 opt_verbose = OPT_VERBOSE;
125 break;
126 case OPT_DEBUG: /* --debug or -d */
127 opt_verbose = OPT_DEBUG;
128 break;
129 case OPT_VERSION: /* --version or -V */
130 version_command(PROGRAM_NAME);
131 break;
132 case OPT_HELP: /* --help or -h */
133 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
134 break;
135 case OPT_SERVERS: /* --servers or -s */
136 opt_servers= strdup(optarg);
137 break;
138 case OPT_EXPIRE: /* --expire */
139 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
140 break;
141 case OPT_USERNAME:
142 opt_username= optarg;
143 break;
144 case OPT_PASSWD:
145 opt_passwd= optarg;
146 break;
147 case '?':
148 /* getopt_long already printed an error message. */
149 exit(1);
150 default:
151 abort();
152 }
153 }
154 }