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