d4d93c2e9c2621e80b3bc56613889f715802f231
[awesomized/libmemcached] / clients / memrm.cc
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 <getopt.h>
16 #include <libmemcached/memcached.h>
17 #include <string.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_hash= NULL;
26 static char *opt_username;
27 static char *opt_passwd;
28
29 #define PROGRAM_NAME "memrm"
30 #define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached cluster."
31
32 /* Prototypes */
33 static void options_parse(int argc, char *argv[]);
34
35 int main(int argc, char *argv[])
36 {
37 memcached_st *memc;
38 memcached_return_t rc;
39 memcached_server_st *servers;
40
41 int return_code= 0;
42
43 options_parse(argc, argv);
44 initialize_sockets();
45
46 if (!opt_servers)
47 {
48 char *temp;
49
50 if ((temp= getenv("MEMCACHED_SERVERS")))
51 opt_servers= strdup(temp);
52 else
53 {
54 fprintf(stderr, "No Servers provided\n");
55 exit(1);
56 }
57 }
58
59 memc= memcached_create(NULL);
60 process_hash_option(memc, opt_hash);
61
62 servers= memcached_servers_parse(opt_servers);
63 memcached_server_push(memc, servers);
64 memcached_server_list_free(servers);
65 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
66 (uint64_t) opt_binary);
67
68 if (!initialize_sasl(memc, opt_username, opt_passwd))
69 {
70 memcached_free(memc);
71 return EXIT_FAILURE;
72 }
73
74 while (optind < argc)
75 {
76 if (opt_verbose)
77 printf("key: %s\nexpires: %llu\n", argv[optind], (unsigned long long)opt_expire);
78 rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire);
79
80 if (rc != MEMCACHED_SUCCESS)
81 {
82 fprintf(stderr, "memrm: %s: memcache error %s",
83 argv[optind], 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");
87
88 return_code= -1;
89 }
90
91 optind++;
92 }
93
94 memcached_free(memc);
95
96 if (opt_servers)
97 free(opt_servers);
98
99 if (opt_hash)
100 free(opt_hash);
101
102 shutdown_sasl();
103
104 return return_code;
105 }
106
107
108 static void options_parse(int argc, char *argv[])
109 {
110 memcached_programs_help_st help_options[]=
111 {
112 {0},
113 };
114
115 static struct option long_options[]=
116 {
117 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
118 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
119 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
120 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
121 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
122 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
123 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
124 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
125 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
126 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
127 {0, 0, 0, 0},
128 };
129 int option_index= 0;
130 int option_rv;
131
132 while (1)
133 {
134 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
135 if (option_rv == -1) break;
136 switch (option_rv)
137 {
138 case 0:
139 break;
140 case OPT_BINARY:
141 opt_binary = 1;
142 break;
143 case OPT_VERBOSE: /* --verbose or -v */
144 opt_verbose = OPT_VERBOSE;
145 break;
146 case OPT_DEBUG: /* --debug or -d */
147 opt_verbose = OPT_DEBUG;
148 break;
149 case OPT_VERSION: /* --version or -V */
150 version_command(PROGRAM_NAME);
151 break;
152 case OPT_HELP: /* --help or -h */
153 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
154 break;
155 case OPT_SERVERS: /* --servers or -s */
156 opt_servers= strdup(optarg);
157 break;
158 case OPT_EXPIRE: /* --expire */
159 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
160 break;
161 case OPT_HASH:
162 opt_hash= strdup(optarg);
163 break;
164 case OPT_USERNAME:
165 opt_username= optarg;
166 break;
167 case OPT_PASSWD:
168 opt_passwd= optarg;
169 break;
170 case '?':
171 /* getopt_long already printed an error message. */
172 exit(1);
173 default:
174 abort();
175 }
176 }
177 }