84de35edcfc38b7ddccf5c92ea07b01335f3db46
[awesomized/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: " << flags << std::endl;
101 }
102 } else {
103 if (opt_verbose) {
104 std::cout << "key: " << argv[optind] << std::endl
105 << "flags: " << flags << std::endl
106 << "length: " << string_length << std::endl
107 << "value: ";
108 }
109
110 if (opt_file) {
111 FILE *fp = fopen(opt_file, "w");
112 if (fp == NULL) {
113 perror("fopen");
114 return_code = EXIT_FAILURE;
115 break;
116 }
117
118 size_t written = fwrite(string, 1, string_length, fp);
119 if (written != string_length) {
120 std::cerr << "error writing file to file " << opt_file << " wrote " << written
121 << ", should have written" << string_length << std::endl;
122 return_code = EXIT_FAILURE;
123 break;
124 }
125
126 if (fclose(fp)) {
127 std::cerr << "error closing " << opt_file << std::endl;
128 return_code = EXIT_FAILURE;
129 break;
130 }
131 } else {
132 std::cout.write(string, string_length);
133 std::cout << std::endl;
134 }
135 free(string);
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 }
155
156 memcached_free(memc);
157
158 if (opt_servers) {
159 free(opt_servers);
160 }
161 if (opt_hash) {
162 free(opt_hash);
163 }
164
165 return return_code;
166 }
167
168 void options_parse(int argc, char *argv[]) {
169 int option_index = 0;
170
171 memcached_programs_help_st help_options[] = {
172 {0},
173 };
174
175 static struct option long_options[] = {
176 {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION},
177 {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP},
178 {(OPTIONSTRING) "quiet", no_argument, NULL, OPT_QUIET},
179 {(OPTIONSTRING) "verbose", no_argument, &opt_verbose, OPT_VERBOSE},
180 {(OPTIONSTRING) "debug", no_argument, &opt_verbose, OPT_DEBUG},
181 {(OPTIONSTRING) "servers", required_argument, NULL, OPT_SERVERS},
182 {(OPTIONSTRING) "flag", no_argument, &opt_displayflag, OPT_FLAG},
183 {(OPTIONSTRING) "hash", required_argument, NULL, OPT_HASH},
184 {(OPTIONSTRING) "binary", no_argument, NULL, OPT_BINARY},
185 {(OPTIONSTRING) "username", required_argument, NULL, OPT_USERNAME},
186 {(OPTIONSTRING) "password", required_argument, NULL, OPT_PASSWD},
187 {(OPTIONSTRING) "file", required_argument, NULL, OPT_FILE},
188 {0, 0, 0, 0},
189 };
190
191 while (1) {
192 int option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
193 if (option_rv == -1)
194 break;
195 switch (option_rv) {
196 case 0:
197 break;
198 case OPT_BINARY:
199 opt_binary = 1;
200 break;
201 case OPT_VERBOSE: /* --verbose or -v */
202 opt_verbose = OPT_VERBOSE;
203 break;
204 case OPT_DEBUG: /* --debug or -d */
205 opt_verbose = OPT_DEBUG;
206 break;
207 case OPT_VERSION: /* --version or -V */
208 version_command(PROGRAM_NAME);
209 break;
210 case OPT_HELP: /* --help or -h */
211 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
212 break;
213 case OPT_SERVERS: /* --servers or -s */
214 opt_servers = strdup(optarg);
215 break;
216 case OPT_HASH:
217 opt_hash = strdup(optarg);
218 break;
219 case OPT_USERNAME:
220 opt_username = optarg;
221 break;
222 case OPT_PASSWD:
223 opt_passwd = optarg;
224 break;
225 case OPT_FILE:
226 opt_file = optarg;
227 break;
228
229 case OPT_QUIET:
230 close_stdio();
231 break;
232
233 case '?':
234 /* getopt_long already printed an error message. */
235 exit(EXIT_FAILURE);
236 default:
237 abort();
238 }
239 }
240 }