ca69d6f9f228528ed628993794769b74876e87fc
[m6w6/libmemcached] / src / bin / memcat.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "mem_config.h"
17
18 #include <cstdio>
19 #include <cstring>
20 #include <getopt.h>
21 #include <iostream>
22 #include <unistd.h>
23 #include "libmemcached-1.0/memcached.h"
24
25 #include "utilities.h"
26
27 #define PROGRAM_NAME "memcat"
28 #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout."
29
30 /* Prototypes */
31 void options_parse(int argc, char *argv[]);
32
33 static int opt_binary = 0;
34 static int opt_verbose = 0;
35 static int opt_displayflag = 0;
36 static char *opt_servers = NULL;
37 static char *opt_hash = NULL;
38 static char *opt_username;
39 static char *opt_passwd;
40 static char *opt_file;
41
42 int main(int argc, char *argv[]) {
43 char *string;
44 size_t string_length;
45 uint32_t flags;
46 memcached_return_t rc;
47
48 int return_code = EXIT_SUCCESS;
49
50 options_parse(argc, argv);
51 initialize_sockets();
52
53 if (opt_servers == NULL) {
54 char *temp;
55
56 if ((temp = getenv("MEMCACHED_SERVERS"))) {
57 opt_servers = strdup(temp);
58 }
59
60 if (opt_servers == NULL) {
61 std::cerr << "No servers provided" << std::endl;
62 exit(EXIT_FAILURE);
63 }
64 }
65
66 memcached_server_st *servers = memcached_servers_parse(opt_servers);
67 if (servers == NULL or memcached_server_list_count(servers) == 0) {
68 std::cerr << "Invalid server list provided:" << opt_servers << std::endl;
69 return EXIT_FAILURE;
70 }
71
72 memcached_st *memc = memcached_create(NULL);
73 process_hash_option(memc, opt_hash);
74
75 memcached_server_push(memc, servers);
76 memcached_server_list_free(servers);
77 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t) opt_binary);
78
79 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) {
80 memcached_free(memc);
81 std::cerr << "--username was supplied, but binary was not built with SASL support."
82 << std::endl;
83 return EXIT_FAILURE;
84 }
85
86 if (opt_username) {
87 memcached_return_t ret;
88 if (memcached_failed(ret = memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) {
89 std::cerr << memcached_last_error_message(memc) << std::endl;
90 memcached_free(memc);
91 return EXIT_FAILURE;
92 }
93 }
94
95 while (optind < argc) {
96 string = memcached_get(memc, argv[optind], strlen(argv[optind]), &string_length, &flags, &rc);
97 if (rc == MEMCACHED_SUCCESS) {
98 if (opt_displayflag) {
99 if (opt_verbose) {
100 std::cout << "key: " << argv[optind] << std::endl << "flags: ";
101 }
102 std::cout << flags << std::endl;
103 } else {
104 if (opt_verbose) {
105 std::cout << "key: " << argv[optind] << std::endl
106 << "flags: " << flags << std::endl
107 << "length: " << string_length << std::endl
108 << "value: ";
109 }
110
111 if (opt_file) {
112 FILE *fp = fopen(opt_file, "w");
113 if (fp == NULL) {
114 perror("fopen");
115 return_code = EXIT_FAILURE;
116 break;
117 }
118
119 size_t written = fwrite(string, 1, string_length, fp);
120 if (written != string_length) {
121 std::cerr << "error writing file to file " << opt_file << " wrote " << written
122 << ", should have written" << string_length << std::endl;
123 return_code = EXIT_FAILURE;
124 break;
125 }
126
127 if (fclose(fp)) {
128 std::cerr << "error closing " << opt_file << std::endl;
129 return_code = EXIT_FAILURE;
130 break;
131 }
132 } else {
133 std::cout.write(string, string_length);
134 std::cout << std::endl;
135 }
136 }
137 } else if (rc != MEMCACHED_NOTFOUND) {
138 std::cerr << "error on " << argv[optind] << "(" << memcached_strerror(memc, rc) << ")";
139 if (memcached_last_error_errno(memc)) {
140 std::cerr << " system error (" << strerror(memcached_last_error_errno(memc)) << ")"
141 << std::endl;
142 }
143 std::cerr << std::endl;
144
145 return_code = EXIT_FAILURE;
146 break;
147 } else // Unknown Issue
148 {
149 std::cerr << "error on " << argv[optind] << "(" << memcached_strerror(NULL, rc) << ")"
150 << std::endl;
151 return_code = EXIT_FAILURE;
152 }
153 optind++;
154 free(string);
155 string = nullptr;
156 }
157 if (string) {
158 free(string);
159 }
160
161 memcached_free(memc);
162
163 if (opt_servers) {
164 free(opt_servers);
165 }
166 if (opt_hash) {
167 free(opt_hash);
168 }
169
170 return return_code;
171 }
172
173 void options_parse(int argc, char *argv[]) {
174 int option_index = 0;
175
176 memcached_programs_help_st help_options[] = {
177 {0},
178 };
179
180 static struct option long_options[] = {
181 {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION},
182 {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP},
183 {(OPTIONSTRING) "quiet", no_argument, NULL, OPT_QUIET},
184 {(OPTIONSTRING) "verbose", no_argument, &opt_verbose, OPT_VERBOSE},
185 {(OPTIONSTRING) "debug", no_argument, &opt_verbose, OPT_DEBUG},
186 {(OPTIONSTRING) "servers", required_argument, NULL, OPT_SERVERS},
187 {(OPTIONSTRING) "flag", no_argument, &opt_displayflag, OPT_FLAG},
188 {(OPTIONSTRING) "hash", required_argument, NULL, OPT_HASH},
189 {(OPTIONSTRING) "binary", no_argument, NULL, OPT_BINARY},
190 {(OPTIONSTRING) "username", required_argument, NULL, OPT_USERNAME},
191 {(OPTIONSTRING) "password", required_argument, NULL, OPT_PASSWD},
192 {(OPTIONSTRING) "file", required_argument, NULL, OPT_FILE},
193 {0, 0, 0, 0},
194 };
195
196 while (1) {
197 int option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
198 if (option_rv == -1)
199 break;
200 switch (option_rv) {
201 case 0:
202 break;
203 case OPT_BINARY:
204 opt_binary = 1;
205 break;
206 case OPT_VERBOSE: /* --verbose or -v */
207 opt_verbose = OPT_VERBOSE;
208 break;
209 case OPT_DEBUG: /* --debug or -d */
210 opt_verbose = OPT_DEBUG;
211 break;
212 case OPT_VERSION: /* --version or -V */
213 version_command(PROGRAM_NAME);
214 break;
215 case OPT_HELP: /* --help or -h */
216 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
217 break;
218 case OPT_SERVERS: /* --servers or -s */
219 opt_servers = strdup(optarg);
220 break;
221 case OPT_HASH:
222 opt_hash = strdup(optarg);
223 break;
224 case OPT_USERNAME:
225 opt_username = optarg;
226 break;
227 case OPT_PASSWD:
228 opt_passwd = optarg;
229 break;
230 case OPT_FILE:
231 opt_file = optarg;
232 break;
233
234 case OPT_QUIET:
235 close_stdio();
236 break;
237
238 case '?':
239 /* getopt_long already printed an error message. */
240 exit(EXIT_FAILURE);
241 default:
242 abort();
243 }
244 }
245 }