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
;
253 exit_code
= EXIT_FAILURE
;
256 ::free(file_buffer_ptr
);
263 std::cout
<< "Calling memcached_free()" << std::endl
;
266 memcached_free(memc
);
281 static void options_parse(int argc
, char *argv
[])
283 memcached_programs_help_st help_options
[]=
288 static struct option long_options
[]=
290 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
291 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
292 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
293 {(OPTIONSTRING
)"udp", no_argument
, NULL
, OPT_UDP
},
294 {(OPTIONSTRING
)"buffer", no_argument
, NULL
, OPT_BUFFER
},
295 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
296 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
297 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
298 {(OPTIONSTRING
)"flag", required_argument
, NULL
, OPT_FLAG
},
299 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
300 {(OPTIONSTRING
)"set", no_argument
, NULL
, OPT_SET
},
301 {(OPTIONSTRING
)"add", no_argument
, NULL
, OPT_ADD
},
302 {(OPTIONSTRING
)"replace", no_argument
, NULL
, OPT_REPLACE
},
303 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
304 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
305 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
306 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
310 bool opt_version
= false;
311 bool opt_help
= false;
316 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
330 case OPT_VERBOSE
: /* --verbose or -v */
331 opt_verbose
= OPT_VERBOSE
;
334 case OPT_DEBUG
: /* --debug or -d */
335 opt_verbose
= OPT_DEBUG
;
338 case OPT_VERSION
: /* --version or -V */
342 case OPT_HELP
: /* --help or -h */
346 case OPT_SERVERS
: /* --servers or -s */
347 opt_servers
= strdup(optarg
);
350 case OPT_FLAG
: /* --flag */
353 opt_flags
= (uint32_t)strtol_wrapper(optarg
, 16, &strtol_error
);
354 if (strtol_error
== true)
356 fprintf(stderr
, "Bad value passed via --flag\n");
362 case OPT_EXPIRE
: /* --expire */
365 opt_expires
= (time_t)strtol_wrapper(optarg
, 16, &strtol_error
);
366 if (strtol_error
== true)
368 fprintf(stderr
, "Bad value passed via --flag\n");
379 opt_method
= OPT_REPLACE
;
387 opt_hash
= strdup(optarg
);
391 opt_username
= optarg
;
411 /* getopt_long already printed an error message. */
420 version_command(PROGRAM_NAME
);
426 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);