50e213951e6f41029392c69999b2ce6c8ccbac6c
[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 free(string);
137 }
138 } else if (rc != MEMCACHED_NOTFOUND) {
139 std::cerr << "error on " << argv[optind] << "(" << memcached_strerror(memc, rc) << ")";
140 if (memcached_last_error_errno(memc)) {
141 std::cerr << " system error (" << strerror(memcached_last_error_errno(memc)) << ")"
142 << std::endl;
143 }
144 std::cerr << std::endl;
145
146 return_code = EXIT_FAILURE;
147 break;
148 } else // Unknown Issue
149 {
150 std::cerr << "error on " << argv[optind] << "(" << memcached_strerror(NULL, rc) << ")"
151 << std::endl;
152 return_code = EXIT_FAILURE;
153 }
154 optind++;
155 }
156
157 memcached_free(memc);
158
159 if (opt_servers) {
160 free(opt_servers);
161 }
162 if (opt_hash) {
163 free(opt_hash);
164 }
165
166 return return_code;
167 }
168
169 void options_parse(int argc, char *argv[]) {
170 int option_index = 0;
171
172 memcached_programs_help_st help_options[] = {
173 {0},
174 };
175
176 static struct option long_options[] = {
177 {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION},
178 {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP},
179 {(OPTIONSTRING) "quiet", no_argument, NULL, OPT_QUIET},
180 {(OPTIONSTRING) "verbose", no_argument, &opt_verbose, OPT_VERBOSE},
181 {(OPTIONSTRING) "debug", no_argument, &opt_verbose, OPT_DEBUG},
182 {(OPTIONSTRING) "servers", required_argument, NULL, OPT_SERVERS},
183 {(OPTIONSTRING) "flag", no_argument, &opt_displayflag, OPT_FLAG},
184 {(OPTIONSTRING) "hash", required_argument, NULL, OPT_HASH},
185 {(OPTIONSTRING) "binary", no_argument, NULL, OPT_BINARY},
186 {(OPTIONSTRING) "username", required_argument, NULL, OPT_USERNAME},
187 {(OPTIONSTRING) "password", required_argument, NULL, OPT_PASSWD},
188 {(OPTIONSTRING) "file", required_argument, NULL, OPT_FILE},
189 {0, 0, 0, 0},
190 };
191
192 while (1) {
193 int option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
194 if (option_rv == -1)
195 break;
196 switch (option_rv) {
197 case 0:
198 break;
199 case OPT_BINARY:
200 opt_binary = 1;
201 break;
202 case OPT_VERBOSE: /* --verbose or -v */
203 opt_verbose = OPT_VERBOSE;
204 break;
205 case OPT_DEBUG: /* --debug or -d */
206 opt_verbose = OPT_DEBUG;
207 break;
208 case OPT_VERSION: /* --version or -V */
209 version_command(PROGRAM_NAME);
210 break;
211 case OPT_HELP: /* --help or -h */
212 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
213 break;
214 case OPT_SERVERS: /* --servers or -s */
215 opt_servers = strdup(optarg);
216 break;
217 case OPT_HASH:
218 opt_hash = strdup(optarg);
219 break;
220 case OPT_USERNAME:
221 opt_username = optarg;
222 break;
223 case OPT_PASSWD:
224 opt_passwd = optarg;
225 break;
226 case OPT_FILE:
227 opt_file = optarg;
228 break;
229
230 case OPT_QUIET:
231 close_stdio();
232 break;
233
234 case '?':
235 /* getopt_long already printed an error message. */
236 exit(EXIT_FAILURE);
237 default:
238 abort();
239 }
240 }
241 }