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 +--------------------------------------------------------------------+
16 #include "mem_config.h"
30 #include <sys/types.h>
33 #include "libmemcached-1.0/memcached.h"
35 #include "client_options.h"
36 #include "utilities.h"
38 #define PROGRAM_NAME "memcp"
39 #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
42 static void options_parse(int argc
, char *argv
[]);
44 static bool opt_binary
= false;
45 static bool opt_udp
= false;
46 static bool opt_buffer
= false;
47 static int opt_verbose
= 0;
48 static char *opt_servers
= NULL
;
49 static char *opt_hash
= NULL
;
50 static int opt_method
= OPT_SET
;
51 static uint32_t opt_flags
= 0;
52 static time_t opt_expires
= 0;
53 static char *opt_username
;
54 static char *opt_passwd
;
56 static long strtol_wrapper(const char *nptr
, int base
, bool *error
) {
60 errno
= 0; /* To distinguish success/failure after call */
61 val
= strtol(nptr
, &endptr
, base
);
63 /* Check for various possible errors */
65 if ((errno
== ERANGE
and (val
== LONG_MAX
or val
== LONG_MIN
)) or (errno
!= 0 && val
== 0)) {
79 int main(int argc
, char *argv
[]) {
80 options_parse(argc
, argv
);
83 fprintf(stderr
, "Expected argument after options\n");
89 memcached_st
*memc
= memcached_create(NULL
);
93 std::cout
<< "Enabling UDP" << std::endl
;
96 if (memcached_failed(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, opt_udp
))) {
98 std::cerr
<< "Could not enable UDP protocol." << std::endl
;
105 std::cout
<< "Enabling MEMCACHED_BEHAVIOR_BUFFER_REQUESTS" << std::endl
;
108 if (memcached_failed(
109 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, opt_buffer
))) {
110 memcached_free(memc
);
111 std::cerr
<< "Could not enable MEMCACHED_BEHAVIOR_BUFFER_REQUESTS." << std::endl
;
116 process_hash_option(memc
, opt_hash
);
118 if (opt_servers
== NULL
) {
121 if ((temp
= getenv("MEMCACHED_SERVERS"))) {
122 opt_servers
= strdup(temp
);
125 else if (argc
>= 1 and argv
[--argc
])
127 opt_servers
= strdup(argv
[argc
]);
131 if (opt_servers
== NULL
) {
132 std::cerr
<< "No Servers provided" << std::endl
;
137 memcached_server_st
*servers
= memcached_servers_parse(opt_servers
);
138 if (servers
== NULL
or memcached_server_list_count(servers
) == 0) {
139 std::cerr
<< "Invalid server list provided:" << opt_servers
<< std::endl
;
143 memcached_server_push(memc
, servers
);
144 memcached_server_list_free(servers
);
145 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, opt_binary
);
146 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0) {
147 memcached_free(memc
);
148 std::cerr
<< "--username was supplied, but binary was not built with SASL support."
154 memcached_return_t ret
;
155 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
))) {
156 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
157 memcached_free(memc
);
162 int exit_code
= EXIT_SUCCESS
;
163 while (optind
< argc
) {
164 int fd
= open(argv
[optind
], O_RDONLY
);
166 std::cerr
<< "memcp " << argv
[optind
] << " " << strerror(errno
) << std::endl
;
168 exit_code
= EXIT_FAILURE
;
173 if (fstat(fd
, &sbuf
) == -1) {
174 std::cerr
<< "memcp " << argv
[optind
] << " " << strerror(errno
) << std::endl
;
176 exit_code
= EXIT_FAILURE
;
180 char *ptr
= rindex(argv
[optind
], '/');
188 static const char *opstr
[] = {"set", "add", "replace"};
189 printf("op: %s\nsource file: %s\nlength: %lu\n"
190 "key: %s\nflags: %x\nexpires: %lu\n",
191 opstr
[opt_method
- OPT_SET
], argv
[optind
], (unsigned long) sbuf
.st_size
, ptr
,
192 opt_flags
, (unsigned long) opt_expires
);
195 // The file may be empty
196 char *file_buffer_ptr
= NULL
;
197 if (sbuf
.st_size
> 0) {
198 if ((file_buffer_ptr
= (char *) malloc(sizeof(char) * (size_t) sbuf
.st_size
)) == NULL
) {
199 std::cerr
<< "Error allocating file buffer(" << strerror(errno
) << ")" << std::endl
;
205 if ((read_length
= ::read(fd
, file_buffer_ptr
, (size_t) sbuf
.st_size
)) == -1) {
206 std::cerr
<< "Error while reading file " << file_buffer_ptr
<< " (" << strerror(errno
)
209 free(file_buffer_ptr
);
213 if (read_length
!= sbuf
.st_size
) {
214 std::cerr
<< "Failure while reading file. Read length was not equal to stat() length"
217 free(file_buffer_ptr
);
222 memcached_return_t rc
;
223 if (opt_method
== OPT_ADD
) {
224 rc
= memcached_add(memc
, ptr
, strlen(ptr
), file_buffer_ptr
, (size_t) sbuf
.st_size
,
225 opt_expires
, opt_flags
);
226 } else if (opt_method
== OPT_REPLACE
) {
227 rc
= memcached_replace(memc
, ptr
, strlen(ptr
), file_buffer_ptr
, (size_t) sbuf
.st_size
,
228 opt_expires
, opt_flags
);
230 rc
= memcached_set(memc
, ptr
, strlen(ptr
), file_buffer_ptr
, (size_t) sbuf
.st_size
,
231 opt_expires
, opt_flags
);
234 if (memcached_failed(rc
)) {
235 std::cerr
<< "Error occrrured during memcached_set(): " << memcached_last_error_message(memc
)
237 exit_code
= EXIT_FAILURE
;
240 ::free(file_buffer_ptr
);
246 std::cout
<< "Calling memcached_free()" << std::endl
;
249 memcached_free(memc
);
262 static void options_parse(int argc
, char *argv
[]) {
263 memcached_programs_help_st help_options
[] = {
267 static struct option long_options
[] = {
268 {(OPTIONSTRING
) "version", no_argument
, NULL
, OPT_VERSION
},
269 {(OPTIONSTRING
) "help", no_argument
, NULL
, OPT_HELP
},
270 {(OPTIONSTRING
) "quiet", no_argument
, NULL
, OPT_QUIET
},
271 {(OPTIONSTRING
) "udp", no_argument
, NULL
, OPT_UDP
},
272 {(OPTIONSTRING
) "buffer", no_argument
, NULL
, OPT_BUFFER
},
273 {(OPTIONSTRING
) "verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
274 {(OPTIONSTRING
) "debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
275 {(OPTIONSTRING
) "servers", required_argument
, NULL
, OPT_SERVERS
},
276 {(OPTIONSTRING
) "flag", required_argument
, NULL
, OPT_FLAG
},
277 {(OPTIONSTRING
) "expire", required_argument
, NULL
, OPT_EXPIRE
},
278 {(OPTIONSTRING
) "set", no_argument
, NULL
, OPT_SET
},
279 {(OPTIONSTRING
) "add", no_argument
, NULL
, OPT_ADD
},
280 {(OPTIONSTRING
) "replace", no_argument
, NULL
, OPT_REPLACE
},
281 {(OPTIONSTRING
) "hash", required_argument
, NULL
, OPT_HASH
},
282 {(OPTIONSTRING
) "binary", no_argument
, NULL
, OPT_BINARY
},
283 {(OPTIONSTRING
) "username", required_argument
, NULL
, OPT_USERNAME
},
284 {(OPTIONSTRING
) "password", required_argument
, NULL
, OPT_PASSWD
},
288 bool opt_version
= false;
289 bool opt_help
= false;
290 int option_index
= 0;
293 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
301 case OPT_BINARY
: opt_binary
= true; break;
303 case OPT_VERBOSE
: /* --verbose or -v */ opt_verbose
= OPT_VERBOSE
; break;
305 case OPT_DEBUG
: /* --debug or -d */ opt_verbose
= OPT_DEBUG
; break;
307 case OPT_VERSION
: /* --version or -V */ opt_version
= true; break;
309 case OPT_HELP
: /* --help or -h */ opt_help
= true; break;
311 case OPT_SERVERS
: /* --servers or -s */ opt_servers
= strdup(optarg
); break;
313 case OPT_FLAG
: /* --flag */
316 opt_flags
= (uint32_t) strtol_wrapper(optarg
, 16, &strtol_error
);
317 if (strtol_error
== true) {
318 fprintf(stderr
, "Bad value passed via --flag\n");
323 case OPT_EXPIRE
: /* --expire */
326 opt_expires
= (time_t) strtol_wrapper(optarg
, 10, &strtol_error
);
327 if (strtol_error
== true) {
328 fprintf(stderr
, "Bad value passed via --expire\n");
333 case OPT_SET
: opt_method
= OPT_SET
; break;
335 case OPT_REPLACE
: opt_method
= OPT_REPLACE
; break;
337 case OPT_ADD
: opt_method
= OPT_ADD
; break;
339 case OPT_HASH
: opt_hash
= strdup(optarg
); break;
341 case OPT_USERNAME
: opt_username
= optarg
; break;
343 case OPT_PASSWD
: opt_passwd
= optarg
; break;
345 case OPT_QUIET
: close_stdio(); break;
347 case OPT_UDP
: opt_udp
= true; break;
349 case OPT_BUFFER
: opt_buffer
= true; break;
352 /* getopt_long already printed an error message. */
359 version_command(PROGRAM_NAME
);
364 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);