2 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
13 #include "mem_config.h"
28 #include <sys/types.h>
29 #include <sys/types.h>
30 #include <sys/types.h>
34 #include <libmemcached-1.0/memcached.h>
36 #include "client_options.h"
37 #include "utilities.h"
39 #define PROGRAM_NAME "memcp"
40 #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
43 static void options_parse(int argc
, char *argv
[]);
45 static bool opt_binary
= false;
46 static bool opt_udp
= false;
47 static bool opt_buffer
= false;
48 static int opt_verbose
= 0;
49 static char *opt_servers
= NULL
;
50 static char *opt_hash
= NULL
;
51 static int opt_method
= OPT_SET
;
52 static uint32_t opt_flags
= 0;
53 static time_t opt_expires
= 0;
54 static char *opt_username
;
55 static char *opt_passwd
;
57 static long strtol_wrapper(const char *nptr
, int base
, bool *error
)
62 errno
= 0; /* To distinguish success/failure after call */
63 val
= strtol(nptr
, &endptr
, base
);
65 /* Check for various possible errors */
67 if ((errno
== ERANGE
and (val
== LONG_MAX
or val
== LONG_MIN
))
68 or (errno
!= 0 && val
== 0))
84 int main(int argc
, char *argv
[])
87 options_parse(argc
, argv
);
90 memcached_st
*memc
= memcached_create(NULL
);
96 std::cout
<< "Enabling UDP" << std::endl
;
99 if (memcached_failed(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, opt_udp
)))
101 memcached_free(memc
);
102 std::cerr
<< "Could not enable UDP protocol." << std::endl
;
111 std::cout
<< "Enabling MEMCACHED_BEHAVIOR_BUFFER_REQUESTS" << std::endl
;
114 if (memcached_failed(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, opt_buffer
)))
116 memcached_free(memc
);
117 std::cerr
<< "Could not enable MEMCACHED_BEHAVIOR_BUFFER_REQUESTS." << std::endl
;
122 process_hash_option(memc
, opt_hash
);
124 if (opt_servers
== NULL
)
128 if ((temp
= getenv("MEMCACHED_SERVERS")))
130 opt_servers
= strdup(temp
);
134 std::cerr
<< "No Servers provided" << std::endl
;
139 memcached_server_st
*servers
;
142 servers
= memcached_servers_parse(opt_servers
);
146 servers
= memcached_servers_parse(argv
[--argc
]);
149 memcached_server_push(memc
, servers
);
150 memcached_server_list_free(servers
);
151 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, opt_binary
);
152 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
154 memcached_free(memc
);
155 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
161 memcached_return_t ret
;
162 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
164 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
165 memcached_free(memc
);
170 int exit_code
= EXIT_SUCCESS
;
171 while (optind
< argc
)
173 int fd
= open(argv
[optind
], O_RDONLY
);
178 std::cerr
<< "memcp " << argv
[optind
] << " " << strerror(errno
) << std::endl
;
181 exit_code
= EXIT_FAILURE
;
186 (void)fstat(fd
, &sbuf
);
188 char *ptr
= rindex(argv
[optind
], '/');
200 static const char *opstr
[] = { "set", "add", "replace" };
201 printf("op: %s\nsource file: %s\nlength: %lu\n"
202 "key: %s\nflags: %x\nexpires: %lu\n",
203 opstr
[opt_method
- OPT_SET
], argv
[optind
], (unsigned long)sbuf
.st_size
,
204 ptr
, opt_flags
, (unsigned long)opt_expires
);
207 char *file_buffer_ptr
;
208 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * (size_t)sbuf
.st_size
)) == NULL
)
210 std::cerr
<< "Error allocating file buffer(" << strerror(errno
) << ")" << std::endl
;
216 if ((read_length
= ::read(fd
, file_buffer_ptr
, (size_t)sbuf
.st_size
)) == -1)
218 std::cerr
<< "Error while reading file " << file_buffer_ptr
<< " (" << strerror(errno
) << ")" << std::endl
;
223 if (read_length
!= sbuf
.st_size
)
225 std::cerr
<< "Failure while reading file. Read length was not equal to stat() length" << std::endl
;
230 memcached_return_t rc
;
231 if (opt_method
== OPT_ADD
)
233 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
234 file_buffer_ptr
, (size_t)sbuf
.st_size
,
235 opt_expires
, opt_flags
);
237 else if (opt_method
== OPT_REPLACE
)
239 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
240 file_buffer_ptr
, (size_t)sbuf
.st_size
,
241 opt_expires
, opt_flags
);
245 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
246 file_buffer_ptr
, (size_t)sbuf
.st_size
,
247 opt_expires
, opt_flags
);
250 if (memcached_failed(rc
))
252 std::cerr
<< "Error occrrured during memcached_set(): " << memcached_last_error_message(memc
) << std::endl
;
254 exit_code
= EXIT_FAILURE
;
257 ::free(file_buffer_ptr
);
264 std::cout
<< "Calling memcached_free()" << std::endl
;
267 memcached_free(memc
);
282 static void options_parse(int argc
, char *argv
[])
284 memcached_programs_help_st help_options
[]=
289 static struct option long_options
[]=
291 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
292 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
293 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
294 {(OPTIONSTRING
)"udp", no_argument
, NULL
, OPT_UDP
},
295 {(OPTIONSTRING
)"buffer", no_argument
, NULL
, OPT_BUFFER
},
296 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
297 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
298 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
299 {(OPTIONSTRING
)"flag", required_argument
, NULL
, OPT_FLAG
},
300 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
301 {(OPTIONSTRING
)"set", no_argument
, NULL
, OPT_SET
},
302 {(OPTIONSTRING
)"add", no_argument
, NULL
, OPT_ADD
},
303 {(OPTIONSTRING
)"replace", no_argument
, NULL
, OPT_REPLACE
},
304 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
305 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
306 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
307 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
311 bool opt_version
= false;
312 bool opt_help
= false;
317 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
331 case OPT_VERBOSE
: /* --verbose or -v */
332 opt_verbose
= OPT_VERBOSE
;
335 case OPT_DEBUG
: /* --debug or -d */
336 opt_verbose
= OPT_DEBUG
;
339 case OPT_VERSION
: /* --version or -V */
343 case OPT_HELP
: /* --help or -h */
347 case OPT_SERVERS
: /* --servers or -s */
348 opt_servers
= strdup(optarg
);
351 case OPT_FLAG
: /* --flag */
354 opt_flags
= (uint32_t)strtol_wrapper(optarg
, 16, &strtol_error
);
355 if (strtol_error
== true)
357 fprintf(stderr
, "Bad value passed via --flag\n");
363 case OPT_EXPIRE
: /* --expire */
366 opt_expires
= (time_t)strtol_wrapper(optarg
, 16, &strtol_error
);
367 if (strtol_error
== true)
369 fprintf(stderr
, "Bad value passed via --flag\n");
380 opt_method
= OPT_REPLACE
;
388 opt_hash
= strdup(optarg
);
392 opt_username
= optarg
;
412 /* getopt_long already printed an error message. */
421 version_command(PROGRAM_NAME
);
427 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);