2 * Copyright (C) 2011-2013 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"
27 #include <sys/types.h>
31 #include "libmemcached-1.0/memcached.h"
33 #include "client_options.h"
34 #include "utilities.h"
36 #define PROGRAM_NAME "memcp"
37 #define PROGRAM_DESCRIPTION "Copy a set of files to a memcached cluster."
40 static void options_parse(int argc
, char *argv
[]);
42 static bool opt_binary
= false;
43 static bool opt_udp
= false;
44 static bool opt_buffer
= false;
45 static int opt_verbose
= 0;
46 static char *opt_servers
= NULL
;
47 static char *opt_hash
= NULL
;
48 static int opt_method
= OPT_SET
;
49 static uint32_t opt_flags
= 0;
50 static time_t opt_expires
= 0;
51 static char *opt_username
;
52 static char *opt_passwd
;
54 static long strtol_wrapper(const char *nptr
, int base
, bool *error
)
59 errno
= 0; /* To distinguish success/failure after call */
60 val
= strtol(nptr
, &endptr
, base
);
62 /* Check for various possible errors */
64 if ((errno
== ERANGE
and (val
== LONG_MAX
or val
== LONG_MIN
))
65 or (errno
!= 0 && val
== 0))
81 int main(int argc
, char *argv
[])
84 options_parse(argc
, argv
);
88 fprintf(stderr
, "Expected argument after options\n");
94 memcached_st
*memc
= memcached_create(NULL
);
100 std::cout
<< "Enabling UDP" << std::endl
;
103 if (memcached_failed(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, opt_udp
)))
105 memcached_free(memc
);
106 std::cerr
<< "Could not enable UDP protocol." << std::endl
;
115 std::cout
<< "Enabling MEMCACHED_BEHAVIOR_BUFFER_REQUESTS" << std::endl
;
118 if (memcached_failed(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, opt_buffer
)))
120 memcached_free(memc
);
121 std::cerr
<< "Could not enable MEMCACHED_BEHAVIOR_BUFFER_REQUESTS." << std::endl
;
126 process_hash_option(memc
, opt_hash
);
128 if (opt_servers
== NULL
)
132 if ((temp
= getenv("MEMCACHED_SERVERS")))
134 opt_servers
= strdup(temp
);
137 else if (argc
>= 1 and argv
[--argc
])
139 opt_servers
= strdup(argv
[argc
]);
143 if (opt_servers
== NULL
)
145 std::cerr
<< "No Servers provided" << std::endl
;
150 memcached_server_st
* servers
= memcached_servers_parse(opt_servers
);
151 if (servers
== NULL
or memcached_server_list_count(servers
) == 0)
153 std::cerr
<< "Invalid server list provided:" << opt_servers
<< std::endl
;
157 memcached_server_push(memc
, servers
);
158 memcached_server_list_free(servers
);
159 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, opt_binary
);
160 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
162 memcached_free(memc
);
163 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
169 memcached_return_t ret
;
170 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
172 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
173 memcached_free(memc
);
178 int exit_code
= EXIT_SUCCESS
;
179 while (optind
< argc
)
181 int fd
= open(argv
[optind
], O_RDONLY
);
184 std::cerr
<< "memcp " << argv
[optind
] << " " << strerror(errno
) << std::endl
;
186 exit_code
= EXIT_FAILURE
;
191 if (fstat(fd
, &sbuf
) == -1)
193 std::cerr
<< "memcp " << argv
[optind
] << " " << strerror(errno
) << std::endl
;
195 exit_code
= EXIT_FAILURE
;
199 char *ptr
= rindex(argv
[optind
], '/');
211 static const char *opstr
[] = { "set", "add", "replace" };
212 printf("op: %s\nsource file: %s\nlength: %lu\n"
213 "key: %s\nflags: %x\nexpires: %lu\n",
214 opstr
[opt_method
- OPT_SET
], argv
[optind
], (unsigned long)sbuf
.st_size
,
215 ptr
, opt_flags
, (unsigned long)opt_expires
);
218 // The file may be empty
219 char *file_buffer_ptr
= NULL
;
220 if (sbuf
.st_size
> 0)
222 if ((file_buffer_ptr
= (char *)malloc(sizeof(char) * (size_t)sbuf
.st_size
)) == NULL
)
224 std::cerr
<< "Error allocating file buffer(" << strerror(errno
) << ")" << std::endl
;
230 if ((read_length
= ::read(fd
, file_buffer_ptr
, (size_t)sbuf
.st_size
)) == -1)
232 std::cerr
<< "Error while reading file " << file_buffer_ptr
<< " (" << strerror(errno
) << ")" << std::endl
;
234 free(file_buffer_ptr
);
238 if (read_length
!= sbuf
.st_size
)
240 std::cerr
<< "Failure while reading file. Read length was not equal to stat() length" << std::endl
;
242 free(file_buffer_ptr
);
247 memcached_return_t rc
;
248 if (opt_method
== OPT_ADD
)
250 rc
= memcached_add(memc
, ptr
, strlen(ptr
),
251 file_buffer_ptr
, (size_t)sbuf
.st_size
,
252 opt_expires
, opt_flags
);
254 else if (opt_method
== OPT_REPLACE
)
256 rc
= memcached_replace(memc
, ptr
, strlen(ptr
),
257 file_buffer_ptr
, (size_t)sbuf
.st_size
,
258 opt_expires
, opt_flags
);
262 rc
= memcached_set(memc
, ptr
, strlen(ptr
),
263 file_buffer_ptr
, (size_t)sbuf
.st_size
,
264 opt_expires
, opt_flags
);
267 if (memcached_failed(rc
))
269 std::cerr
<< "Error occrrured during memcached_set(): " << memcached_last_error_message(memc
) << std::endl
;
270 exit_code
= EXIT_FAILURE
;
273 ::free(file_buffer_ptr
);
280 std::cout
<< "Calling memcached_free()" << std::endl
;
283 memcached_free(memc
);
298 static void options_parse(int argc
, char *argv
[])
300 memcached_programs_help_st help_options
[]=
305 static struct option long_options
[]=
307 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
308 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
309 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
310 {(OPTIONSTRING
)"udp", no_argument
, NULL
, OPT_UDP
},
311 {(OPTIONSTRING
)"buffer", no_argument
, NULL
, OPT_BUFFER
},
312 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
313 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
314 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
315 {(OPTIONSTRING
)"flag", required_argument
, NULL
, OPT_FLAG
},
316 {(OPTIONSTRING
)"expire", required_argument
, NULL
, OPT_EXPIRE
},
317 {(OPTIONSTRING
)"set", no_argument
, NULL
, OPT_SET
},
318 {(OPTIONSTRING
)"add", no_argument
, NULL
, OPT_ADD
},
319 {(OPTIONSTRING
)"replace", no_argument
, NULL
, OPT_REPLACE
},
320 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
321 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
322 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
323 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
327 bool opt_version
= false;
328 bool opt_help
= false;
333 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
347 case OPT_VERBOSE
: /* --verbose or -v */
348 opt_verbose
= OPT_VERBOSE
;
351 case OPT_DEBUG
: /* --debug or -d */
352 opt_verbose
= OPT_DEBUG
;
355 case OPT_VERSION
: /* --version or -V */
359 case OPT_HELP
: /* --help or -h */
363 case OPT_SERVERS
: /* --servers or -s */
364 opt_servers
= strdup(optarg
);
367 case OPT_FLAG
: /* --flag */
370 opt_flags
= (uint32_t)strtol_wrapper(optarg
, 16, &strtol_error
);
371 if (strtol_error
== true)
373 fprintf(stderr
, "Bad value passed via --flag\n");
379 case OPT_EXPIRE
: /* --expire */
382 opt_expires
= (time_t)strtol_wrapper(optarg
, 10, &strtol_error
);
383 if (strtol_error
== true)
385 fprintf(stderr
, "Bad value passed via --expire\n");
396 opt_method
= OPT_REPLACE
;
404 opt_hash
= strdup(optarg
);
408 opt_username
= optarg
;
428 /* getopt_long already printed an error message. */
437 version_command(PROGRAM_NAME
);
443 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);